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 katanas on 27 de setembro de 2016 - 10:53 am

    very nice and quick delivery A+++++++++++

  2. #2 by Heath on 27 de setembro de 2016 - 12:03 pm

    There are some attention-grabbing closing dates in this article however I don’t know if I see all of them middle to heart. There is some validity but I’ll take hold opinion until I look into it further. Good article , thanks and we wish more! Added to FeedBurner as well

  3. #3 by Verlene on 27 de setembro de 2016 - 12:16 pm

    Hi there would you mind stating which blog platform you’re using? I’m looking to start my own blog soon but I’m having a hard time selecting to go with BlogEngine.

  4. #4 by short Sword on 27 de setembro de 2016 - 12:22 pm

    perfect transaction from a five star seller, thanks

  5. #5 by erhalten weitere Infos on 27 de setembro de 2016 - 12:48 pm

    Für draußen muss doch der Zwiebellook her.

  6. #6 by battle ready samurai swords on 27 de setembro de 2016 - 2:49 pm

    Received today – thank you.

  7. #7 by need on 27 de setembro de 2016 - 4:22 pm

    Really enjoyed this article post.Thanks Again. Cool.

  8. #8 by to read more on 27 de setembro de 2016 - 4:30 pm

    Really enjoyed this article post.Really thank you! Will read on…

  9. #9 by Smart Balance Wheel on 27 de setembro de 2016 - 4:42 pm

    it’s my job to implement Smart Balance Wheel http://adf.ly/6249830/banner/www.fashionhoverboard.com when i have to go in particular climatic conditions the item continues people type! Their incredibly adorable and stylish and then you should retain that designer way up!!:)It has the rather popluar it would make intrinsic and extrinsic stains show up definitely very easily!

  10. #10 by http://www.moresmerteklinikk.no/userfiles/files/283.html on 27 de setembro de 2016 - 5:27 pm

    Recieved this particular item prior to the day given in a really nice gifts box . Your picture that’s revealed for the bracelet is exactly what you’ll recieve. That is extremely fine yet your pretty section regarding precious jewelry which will continue considering iof ones toggle clasp and that is really sturdy so the bracelet eill maybe not be removed.

  11. #11 by http://tropikhalsan.se/python/288.html on 27 de setembro de 2016 - 5:28 pm

    Recieved your product prior to the date offered in a really great gifts package . The photo that’s revealed for bracelet is actually what you certainly will recieve. This might be quite delicate still letter awesome part of precious jewelry which will continue mainly because iof that the toggle clasp and that is most durable so that the bracelet eill never come off.

  12. #12 by Tsuka on 27 de setembro de 2016 - 6:10 pm

    Very smooth transaction. Nice product. Thank you.

  13. #13 by samurai katana on 27 de setembro de 2016 - 6:11 pm

    Great shoe and fast shipment

  14. #14 by Best Realtor in South Pasadena on 27 de setembro de 2016 - 6:13 pm

    Wow, superb blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is magnificent, let alone the content!. Thanks For Your article about sex.

  15. #15 by permanent resident on 27 de setembro de 2016 - 6:14 pm

    Great post!

  16. #16 by more info on 27 de setembro de 2016 - 6:28 pm

    Very informative blog post. Will read on…

  17. #17 by Wakizashi Sword for sale on 27 de setembro de 2016 - 7:18 pm

    Perfect. Thank you very much. Fast shipping.

  18. #18 by http://www.actulite.com/h-jewelry/h-bracelets on 27 de setembro de 2016 - 7:18 pm

    The headset was since stunning as in the picture. Things came immediately. I’d encourage getting a towel more than every single level as you click they off w / the vapor iron. It doesn’t steam out through only a steamer. The iron ended up being necessary. It is very fine, so if you cannot trust yourself because of the towel furthermore vapor iron, after that just take they up to a expert. Breathtaking sound.

  19. #19 by additional hints on 27 de setembro de 2016 - 9:59 pm

    marriage counseling greensburg pa

1 121 122 123
(não será publicado)