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 Frank Ocean Boys Don't Cry full album download on 29 de julho de 2016 - 6:05 am

    I cannot thank you enough for the article post. Really Great.

  2. #2 by Wood Planks on 29 de julho de 2016 - 6:29 am

    I think this is one of the most vital information for me. And i am glad reading your article. But want to remark on some general things, The web site style is ideal, the articles is really excellent : D. Good job, cheers

  3. #3 by month to month smartphone plans on 29 de julho de 2016 - 6:29 am

    Excellent weblog right here! Also your web site so much up fast! What host are you the usage of? Can I am getting your affiliate link for your host? I want my website loaded up as quickly as yours lol

  4. #4 by recent business events on 29 de julho de 2016 - 6:40 am

    Wonderful work! That is the kind of information that are meant to be shared around the net. Disgrace on the seek engines for no longer positioning this submit higher! Come on over and consult with my website . Thanks =)

  5. #5 by Buy adsense account on 29 de julho de 2016 - 6:50 am

    this wonderful read!! I definitely really liked every little

  6. #6 by 3d glass pictures on 29 de julho de 2016 - 6:56 am

    I loved your blog.Much thanks again. Awesome.

  7. #7 by Modern Sofa on 29 de julho de 2016 - 7:12 am

    You could certainly see your enthusiasm within the paintings you write. The sector hopes for more passionate writers like you who aren’t afraid to mention how they believe. Always go after your heart.

  8. #8 by on 29 de julho de 2016 - 7:25 am

    I got this particular headphonesfor the our mom for parents evening, plus she absolutely enjoyed that it! Their quite cute headphonesas well as the saying on the card in which will come within the box is really emotional!! And grade of beads was awesome!

  9. #9 by on 29 de julho de 2016 - 7:26 am

    I have your headphonesfor the my mother to parents time, additionally she absolutely enjoyed things! The actually attractive headphonesand the stating in the card it will come in box is really sentimental!! And the top quality of the beads is ideal!

  10. #10 by 3d pictures in glass on 29 de julho de 2016 - 7:34 am

    Thanks for sharing, this is a fantastic post. Really Great.

  11. #11 by on 29 de julho de 2016 - 8:28 am

    Recieved it item prior to the date given in a very sweet present container . Your image that’s displayed for the bracelet is what you will definitely recieve. This is certainly really fine but your breathtaking part of precious jewelry that will past because iof that the toggle clasp that is very sturdy so that the bracelet eill maybe not come off.

  12. #12 by on 29 de julho de 2016 - 8:28 am

    Recieved the product prior to the date offered in a really terrific gift container . The particular visualize that is presented for bracelet was what you certainly will recieve. It is quite delicate yet one striking part of precious jewelry that will final simply because iof that the toggle clasp which is very durable so the bracelet eill never be removed.

  13. #13 by on 29 de julho de 2016 - 8:40 am

    please let me expand on the final policy. when you’ve got a getting day, one of these in a hurry to toss your stomach in trash can also known as cancel the order your membership towards your favourite tipster! almost everyone makes dropping off days, sure enough never go to getting rid of weeks on top of that flawed months. around the, After a decent stretch of time you will certainly be kind of precise if or not one origin of bets must be coming back again successful sales and profits,

  14. #14 by fournisseur dentaire on 29 de julho de 2016 - 9:02 am

    Really enjoyed this article post.Thanks Again. Awesome.

  15. #15 by on 29 de julho de 2016 - 9:18 am

    we’ll start with something straight forward, also most of us do different things. i am certain body who is ecstatic whom 2007 is along with. explain to resented 2007. some kind of educational instituations stored engine limiters on scenario to keep unbelievably extremely high transfers, sadly persons stage in which it intelligence through the motor coach buses since college. some kind of ideas from time to time exercise clapboard lessons wthout using stride race car (charge car just that “constraints” Drivers’ schedule once there is also a up race car the actual right track, Passing undoubtedly forbidden). usually, selection, you’ll have definitely a whole lot of opportunity for multi digit velocities in an interesting or beginner training program.

  16. #16 by on 29 de julho de 2016 - 9:32 am

    the moment pertaining to 1:04.27 in their state 100 breaststroke was very theraputic for second and all us rank. The three the time every single one of realized scuba diver completely outclassed to be able toros straightaway third year, placing a live up to driving report utilizing a health involving 585.10. past summer season, He ready in the 15 at life senior going championships in kuala lumpur, Malaysia.

  17. #17 by handmade katana on 29 de julho de 2016 - 9:35 am

    Arrived just fine; very happy, thanks!

  18. #18 by house cleaning services on 29 de julho de 2016 - 9:52 am

    I appreciate you sharing this article. Fantastic.

  19. #19 by Cheap Micro Niche Sites on 29 de julho de 2016 - 10:12 am

    This is just what I ave been sharp on behalf of all day. Achieve not bring to a halt updating your blog.

  20. #20 by Cleaning Services Buena Park on 29 de julho de 2016 - 10:29 am

    Enjoyed every bit of your article post.Really thank you! Really Cool.

  21. #21 by Cheap Micro Niche Sites on 29 de julho de 2016 - 10:57 am

    Link exchange is nothing else but it is simply placing the other person as web site link on your page at suitable place and other person will also do same for you.

1 66 67 68
(não será publicado)