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 Womens Fashion on 9 de dezembro de 2016 - 9:49 pm

    Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! By the way, how could we communicate?

  2. #2 by http://www.cite-pere.qc.ca/outletca.asp on 9 de dezembro de 2016 - 11:02 pm

    Bought our being a gifts to our mom furthermore she liked things. sweet good pleasant pricing furthermore our mother enjoyed that it. Furthermore delivered extremely quick. So if you require a gift quick and you like this versus this is the a person you will want to purchase!

  3. #3 by http://www.hcate.com/ on 9 de dezembro de 2016 - 11:02 pm

    Purchased this particular being a gift of the mama to she liked information technology. good top quality kind cost furthermore my mother loved things. Always delivered super quick. So if you require a gifts fast and you like this compared to here is the one it is best to go for!

  4. #4 by kredit ohne schufaeintrag ohne arbeitsnachweis englisch on 9 de dezembro de 2016 - 11:53 pm

    Calling all cars, calling all cars, we’re ready to make a deal.

  5. #5 by kredit trotz erledigungsvermerk on 10 de dezembro de 2016 - 12:59 am

    Ooh! Great insight, Dayle! My brother brought this up the other day. He asked something like: "Am I am the way I am because I read about psychology theories or because I really am that way?" I guess that's why they say that ignorance is bliss. =P

  6. #6 by 20000 euro kredit trotz on 10 de dezembro de 2016 - 2:54 am

    If my problem was a Death Star, this article is a photon torpedo.

  7. #8 by louis vuitton josephine portemonnaie on 10 de dezembro de 2016 - 3:25 am

  8. #9 by kredit vorvertragliche informationen on 10 de dezembro de 2016 - 3:30 am

    That’s a genuinely impressive answer.

  9. #10 by billig kredit geld sofort on 10 de dezembro de 2016 - 3:49 am

    Appearance > Mantra Settings, Presentation Page section… scroll down and you find “Extra – Extra Text”… click on it and it opens up a couple more fields.

  10. #11 by chanel handbags outlet online on 10 de dezembro de 2016 - 4:21 am

    I have been surfing online more than three hours today, yet I never found any interesting article like yours. Its pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the internet will be much more useful than ever before.
    chanel handbags outlet online http://www.tmearegion26.com/chanel/

  11. #12 by north face outlet ohio on 10 de dezembro de 2016 - 4:33 am

    She said she thinks Erving recognized her during that first meeting. I stood in line for an autographed ball, and I didnt really want to. And I got there. I just stood in line to see what he looked like. And I got up there, he asked me if I wanted a ball, and I said `no, and I walked away.
    north face outlet ohio http://www.joessmogtestonly.com/north-face/

  12. #13 by zinsspiegel hypothekenkredit 15 jahr hochzeitstag on 10 de dezembro de 2016 - 4:43 am

    Even non-inspiring days are learning experiences! I’m glad you took the time to get it right and learn more about correct exposure and using a reflector.

  13. #14 by Hai Chafetz on 10 de dezembro de 2016 - 5:05 am

    The processor or CPU may be the brains with the private computer – it does most of the calculations your video game titles want to need to run (with the movie card undertaking most of the graphics function). With it acquiring such an critical part, you would instinctually go for the fastest – and most pricey – processor you could uncover, but there is a improved alternate to blowing hard cash on 1 thing that will probably be obsolete in a year: receiving a processor that will play tomorrow’s video game titles at a low cost is the sweet spot for any gaming computer.

  14. #15 by north face sale on 10 de dezembro de 2016 - 5:32 am

    Resources such as the one you mentioned right here will be extremely helpful to myself! Ill publish a hyperlink to this web page on my individual blog. Im positive my site guests will locate that quite beneficial.
    north face sale http://www.lticonstruction.com/north-face/

  15. #16 by michael kors coupon 2016 on 10 de dezembro de 2016 - 5:46 am

    tsumura disinter spear ziggy meri meniscus herkulesfurdoi usherette thessalonians d8cd98f0
    michael kors coupon 2016 http://www.factorymichaelkorsoutletonline.com

  16. #17 by ugg sale boots clearance on 10 de dezembro de 2016 - 6:46 am

    Smart stuff, I expect reading more.
    ugg sale boots clearance http://www.unilorites.com/ugg/

  17. #18 by moncler online store on 10 de dezembro de 2016 - 7:58 am

    Football betting professional
    moncler online store http://www.moncleroutletstoreonline.com

1 204 205 206
(não será publicado)