Battle Simulation With Lanchester Model in C#


This post will show a simple implementation of the famous Lanchester equations to simulate battles “evolution”.

For those that does not know this model, i really recomend this and this PDFs. (especially the first one). For the rest of the post, iam assuming that the reader “WILL read” these recomended texts =P.

Lanchester modeling is pretty beautiful cause it is based in very simple hypothesis and even so, it stills very usefull. (not so good as some stochastic models but good enough for lots of situations ).

In simple ways, we have a system of Differential equations to solve. There are LOOOTS of numeric methods avaliable, i choose one that for me is very intuitive and easy to implement. (using EigenValues and Eigen Vectors -> it is graphically intuitive =P)

The following code shows the Lanchester Model Implementation:
I used plain C# with .net and the alglib for math.
Actually, the alglib was used just to get the eigenvalues and eigenvectors of the lanchester diferential system. It could be easily done by “hand”. (calculate roots of the characteristic equation to find the eigenvalues and then substitute in the formal eigenvector/eigenvalue definition equation to get the eigenvectors =P).

The Squad Class (represents one Army =P)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Settlers.Battle
{
    class Squad
    {
        public float numericalStrenght;
        public float effectiveness;
    }
}

The simulator class:

There are some static methods that can be used to discover the winner before the “Battle begin =P” (without explicitelly using the time variable. See the recomended papers to know the reason why we can do this)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Settlers.Battle
{
    class LanchesterSimulation
    {
        public LanchesterSimulation(Squad a1, Squad a2)
        {
            double[,] vects = new double[2,2];
            vects[0, 1] = -a2.effectiveness;
            vects[1, 0] = -a1.effectiveness;
            double[] real = new double[2];
            double[] ima = new double[2];
            double[,] left = new double[2, 2];
            double[,] right= new double[2, 2];

            alglib.rmatrixevd(vects, 2, 2, out real, out ima, out right, out left);

            ev1 = real[0];
            ev2 = real[1];

            auv1x = right[1, 0];
            auv1y = right[0, 0];

            auv2x = right[1, 1];
            auv2y = right[0, 1];

            c2 = (a2.numericalStrenght - (a1.numericalStrenght * auv1y) / auv1x ) * auv1x / (-auv2x * auv1y + auv2y * auv1x);
            c1 = (a1.numericalStrenght - auv2x * c2) / auv1x;

        }

        double ev1, ev2;
        double c2, c1;
        double auv1x,auv1y,auv2x,auv2y ;

        public float GetArmy1Size(float t)
        {
            double resp = c1 * auv1x * Math.Exp(ev1 * t) + c2 * auv2x * Math.Exp(ev2 * t);
            return (float)resp;
        }

        public float GetArmy2Size(float t)
        {
            double resp = c1 * auv1y * Math.Exp(ev1 * t) + c2 * auv2y * Math.Exp(ev2 * t);
            return (float)resp;
        }

        public static Squad WhoWillWin(Squad a1, Squad a2)
        {
            float val = a1.effectiveness * a1.numericalStrenght * a1.numericalStrenght - a2.effectiveness * a2.numericalStrenght * a2.numericalStrenght;
            return val > 0 ? a1 : a2;
        }

        public static Squad[] WhoWillWin(Squad[] a1, Squad[] a2)
        {
            float apower = 0;
            float atotal = 0;
            for (int i = 0; i < a1.Count(); i++)
            {
                apower += a1[i].effectiveness * a1[i].numericalStrenght;
                atotal += a1[i].numericalStrenght;
            }

            float bpower = 0;
            float btotal = 0;
            for (int i = 0; i < a2.Count(); i++)
            {
                bpower += a2[i].effectiveness * a2[i].numericalStrenght;
                btotal += a2[i].numericalStrenght;
            }

            float val = apower * atotal - bpower * btotal;
            return val > 0 ? a1 : a2;
        }
    }

}

That is all for today =P

  1. #1 by modifikasi motor beat drag on 23 de agosto de 2016 - 9:38 am

    I discovered your blog site on google and examine a few of your early posts. Proceed to keep up the very good operate. I just additional up your RSS feed to my MSN Information Reader. In search of ahead to studying extra from you afterward!…

  2. #2 by http://news.blazersuksale.com/blazersuksale/606.html on 23 de agosto de 2016 - 9:42 am

    Recieved the product before the time given in a really good gifts box . That picture that’s shown for bracelet is actually what you can expect to recieve. This might be quite delicate still your pretty section to precious jewelry which will past due to the fact iof on toggle clasp that is very sturdy so the bracelet eill maybe not come off.

  3. #3 by Anti Wrinkle Cream on 23 de agosto de 2016 - 9:57 am

    hello!,I like your writing very much! share we communicate more approximately your post on AOL? I need a specialist in this house to unravel my problem. May be that’s you! Having a look forward to look you.

  4. #4 by printed plastic packaging on 23 de agosto de 2016 - 10:30 am

    I appreciate you sharing this blog post.Really thank you! Want more.

  5. #5 by modifikasi motor beat hijau putih on 23 de agosto de 2016 - 10:38 am

    As I website possessor I believe the content material here is rattling excellent , appreciate it for your efforts. You should keep it up forever! Good Luck.

  6. #6 by diving and island hopping cebu philippines on 23 de agosto de 2016 - 10:43 am

    In the great scheme of things you’ll secure a B+ with regard to hard work. Exactly where you misplaced everybody was first on your particulars. You know, as the maxim goes, the devil is in the details… And that couldn’t be much more true here. Having said that, let me reveal to you what did deliver the results. The article (parts of it) is pretty convincing and this is most likely the reason why I am making the effort in order to comment. I do not really make it a regular habit of doing that. Secondly, although I can certainly notice a leaps in logic you make, I am not really certain of just how you seem to unite your points which make the actual conclusion. For right now I will, no doubt yield to your position however wish in the near future you actually link your facts better.

  7. #7 by retail packaging on 23 de agosto de 2016 - 11:12 am

    wow, awesome post.Much thanks again. Will read on…

  8. #8 by kdf podatki kalkulator wynagrodzeń anglia on 23 de agosto de 2016 - 2:34 pm

    I really like your writing style, wonderful info , regards for posting : D.

  9. #9 by atlanta airport shuttles sedans that fit on 23 de agosto de 2016 - 3:10 pm

    Hello there. I discovered your web site by the use of Google whilst looking for a related matter, your web site got here up. It seems to be good. I have bookmarked it in my google bookmarks to visit then.

  10. #10 by http://everod.nu/templates/html/144.html on 23 de agosto de 2016 - 4:32 pm

    I got our gift of our mom of Christmas time due she was the precious jewelry freak. The particular something she does not wear a great deal to are, bracelets. I bought the lady your charm bracelet as well as once she opened up information technology yesterday she absolutley liked things! This time the problem is, this girl buying it on top of additionally off simply by herself. Haha… general great system, information technology delivered and also appeared completely early and our mother are experiencing it. Thank shoppers.

  11. #11 by http://oakridgemo.com/media/216.html on 23 de agosto de 2016 - 4:32 pm

    it comes in a beautiful box seems very good still their a bit smaller including towards my personal mothers wrist but it appearance awesome simply really want they was much longer

  12. #12 by modifikasi motor beat cw fi on 23 de agosto de 2016 - 5:28 pm

    Hello there, simply changed into aware of your weblog through Google, and located that it’s truly informative. I am gonna watch out for brussels. I will be grateful if you happen to continue this in future. Numerous people shall be benefited out of your writing. Cheers!

1 96 97 98
(não será publicado)