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 improvements on 19 de agosto de 2017 - 3:07 pm

    Hi there, just became alert to your blog through Google, and found that it is really informative. I am going to watch out for brussels. I’ll appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  2. #2 by hunter boots sale on 19 de agosto de 2017 - 5:07 pm

    I learned a lot thank you
    hunter boots sale

  3. #3 by Sung Madarang on 19 de agosto de 2017 - 5:24 pm

    Will fake or real no one give shit .second she dosent look fake . I like what I see brunette bombshell.

  4. #4 by Augusto de Arruda Botelho on 19 de agosto de 2017 - 5:54 pm

    I like it when individuals come together and share thoughts.
    Great website, keep it up!

  5. #5 by watch tv on 19 de agosto de 2017 - 6:42 pm

    I was reading some of your blog posts on this website and I believe this internet site is very informative! Keep putting up.

  6. #6 by diese website on 19 de agosto de 2017 - 6:55 pm

    Es gab auch sogenannte Arbeits- oder Dienstkleidung, wodurch die TrägerIn ihre Funktion oder ihren Beruf optisch preisgab.

  7. #7 by Jordan 13 Bred 2017 on 19 de agosto de 2017 - 6:57 pm

  8. #8 by free snap milfs on 19 de agosto de 2017 - 7:36 pm

    snap milfs

  9. #9 by on 19 de agosto de 2017 - 7:49 pm

    I have your headphonesof my personal mom of parents time, as well as she completely loved that it! It really is actually adorable headphonesand the stating regarding the card you already know will come inside container is very sentimental!! And the grade of the beads was very good!

  10. #10 by hermes for sale on 19 de agosto de 2017 - 7:52 pm

    Terrific job here. I seriously enjoyed what you had to say. Keep going because you absolutely bring a new voice to this subject. Not many people would say what youve said and still make it interesting. Well, at least Im interested. Cant wait to see more of this from you.
    hermes for sale

  11. #11 by garage door on 19 de agosto de 2017 - 7:52 pm

    very handful of internet sites that come about to be comprehensive below, from our point of view are undoubtedly effectively worth checking out

  12. #12 by repairing garage doors on 19 de agosto de 2017 - 8:06 pm

    very handful of sites that take place to be detailed beneath, from our point of view are undoubtedly properly worth checking out

  13. #13 by instacart promo code 2017 on 19 de agosto de 2017 - 8:08 pm

    Hello there! I could have sworn I’ve visited this website before but after looking at
    a few of the articles I realized it’s new to me. Regardless, I’m definitely
    happy I found it and I’ll be book-marking it and
    checking back frequently!

  14. #14 by louis vuitton sac rouge on 19 de agosto de 2017 - 8:18 pm

  15. #15 by cartier love bracelet replica on 19 de agosto de 2017 - 8:26 pm

    I got this headphonesto the mom to moms evening, then she completely adored they! It really is quite pretty headphonesas well as the saying regarding the card in which goes in container is very sentimental!! And good of beads is very good!

  16. #16 by Zenaida on 19 de agosto de 2017 - 8:30 pm

    Hello, I enjoy reading through your post. I like to write a
    little comment to support you.

1 453 454 455
(não será publicado)