Ejemplo n.º 1
0
        double FitnessFunction(Player player)
        {
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            int temp = 0;

            Parallel.For(0, m_battleCount, i =>
            {
                SandBox sb = new SandBox(Enemy, player, EnemyArmy, AllyArmy, m_mapSize);
                if (sb.Fight(m_currentGeneration))
                {
                    Interlocked.Add(ref temp, 1);
                }
            }
                         );


            //sw.Stop();
            //System.Windows.MessageBox.Show(sw.Elapsed.TotalMilliseconds.ToString());
            //sw.Reset();
            return((double)temp / (double)m_battleCount);
        }
Ejemplo n.º 2
0
        private void AllCalculations()
        {
            RectangleF[][][] Animations1 = new RectangleF[Enum.GetValues(typeof(Sprite.AnimationAction)).Length][][];
            for (int i = 0; i < Enum.GetValues(typeof(Sprite.AnimationAction)).Length; i++)
            {
                switch ((Sprite.AnimationAction)i)
                {
                case Sprite.AnimationAction.Standing:
                    int lengthS = 23;

                    Animations1[i]    = new RectangleF[1][];
                    Animations1[i][0] = new RectangleF[lengthS];
                    for (int j = 0; j < lengthS; j++)
                    {
                        Animations1[i][0][j] = new RectangleF(72 * j, 224, 72, 88);
                    }
                    break;

                case Sprite.AnimationAction.Moving:
                    int lengthM = 2;

                    Animations1[i]    = new RectangleF[1][];
                    Animations1[i][0] = new RectangleF[] {
                        //new RectangleF(0, 320, 72, 88),
                        new RectangleF(100, 320, 72, 88),
                        new RectangleF(200, 320, 72, 88),
                        new RectangleF(310, 320, 72, 88)
                    };

                    break;

                case Sprite.AnimationAction.Attacking:

                    int lengthA = 28;

                    Animations1[i]    = new RectangleF[1][];
                    Animations1[i][0] = new RectangleF[lengthA];
                    for (int j = 0; j < lengthA; j++)
                    {
                        Animations1[i][0][j] = new RectangleF(72 * j, 96, 72, 120);
                    }

                    break;

                case Sprite.AnimationAction.TakingDamage:
                    int lengthT = 13;

                    Animations1[i]    = new RectangleF[1][];
                    Animations1[i][0] = new RectangleF[lengthT];
                    for (int j = 0; j < lengthT; j++)
                    {
                        Animations1[i][0][j] = new RectangleF(72 * j, 0, 72, 88);
                    }

                    break;

                case Sprite.AnimationAction.Dying:
                    Animations1[i] = Animations1[(int)Sprite.AnimationAction.TakingDamage];
                    break;
                }
            }
            var gSpriteHorse = new Sprite(global::project.Properties.Resources.MyHorseman1, Animations1, false);


            RectangleF[][][] Animations2 = new RectangleF[Enum.GetValues(typeof(Sprite.AnimationAction)).Length][][];
            for (int i = 0; i < Enum.GetValues(typeof(Sprite.AnimationAction)).Length; i++)
            {
                switch ((Sprite.AnimationAction)i)
                {
                case Sprite.AnimationAction.Standing:
                    int lengthS = 23;

                    Animations2[i]    = new RectangleF[1][];
                    Animations2[i][0] = new RectangleF[lengthS];
                    for (int j = 0; j < lengthS; j++)
                    {
                        Animations2[i][0][j] = new RectangleF(72 * j, 88, 72, 80);
                    }
                    break;

                case Sprite.AnimationAction.Moving:
                    int lengthM = 10;

                    Animations2[i]    = new RectangleF[1][];
                    Animations2[i][0] = new RectangleF[lengthM];
                    for (int j = 0; j < lengthM; j++)
                    {
                        Animations2[i][0][j] = new RectangleF(96 * (j + 3), 176, 72, 80);
                    }
                    break;

                case Sprite.AnimationAction.Attacking:

                    int lengthA = 21;

                    Animations2[i]    = new RectangleF[1][];
                    Animations2[i][0] = new RectangleF[lengthA];
                    for (int j = 0; j < lengthA; j++)
                    {
                        Animations2[i][0][j] = new RectangleF(96 * j, 264, 72, 80);
                    }

                    break;

                case Sprite.AnimationAction.TakingDamage:
                    int lengthT = 13;

                    Animations2[i]    = new RectangleF[1][];
                    Animations2[i][0] = new RectangleF[lengthT];
                    for (int j = 0; j < lengthT; j++)
                    {
                        Animations2[i][0][j] = new RectangleF(72 * j, 0, 72, 80);
                    }

                    break;

                case Sprite.AnimationAction.Dying:
                    Animations2[i] = Animations2[(int)Sprite.AnimationAction.TakingDamage];
                    break;
                }
            }



            var gSpriteSoldier = new Sprite(global::project.Properties.Resources.fighter_transparent_2, Animations2, true);

            enemy = new Player(new double[4] {
                0.1, 0.9, 0.9, 0.9
            });
            Unit humanKnights = new Unit(4, 17, 3, 5, 7, 25, 1.5f)
            {
                SideASprite = gSpriteHorse, SideBSprite = gSpriteHorse
            };
            Unit humanSoliders = new Unit(4, 16, 2, 4, 4, 30, 1.5f)
            {
                SideASprite = gSpriteSoldier, SideBSprite = gSpriteSoldier
            };
            Unit humanArcher = new Unit(4, 12, 5, 4, 3, 20, 10f);

            int n = 4;

            army = new Squad[3 * n];
            for (int i = 0; i < army.Length; i += 3)
            {
                army[i]     = new Squad(humanKnights);
                army[i + 1] = new Squad(humanSoliders);
                army[i + 2] = new Squad(humanArcher);
            }
            //for (int i = 0; i < ; i++)
            //{
            //    if (i<army.Length/2)
            //    army[i] = new Squad(humanKnights);
            //    else
            //    army[i] = new Squad(humanSoliders);
            //}

            Stopwatch sw = Stopwatch.StartNew();

            ga = new GA(enemy, army, battleCount: 5, populationSize: 6, mapSize: 16);
            ga.Go();

            sw.Stop();

            // System.Windows.MessageBox.Show("Genetic algorithm has finished in " + sw.Elapsed.TotalSeconds.ToString());

            SandBox sb = new SandBox(enemy, ga.GetBest(), army, army, 16)
            {
                Visualization = true
            };

            v = sb.BattleData.Visualization;
            sb.Fight(1);
            v.SetTime(0);
            Battle.Visualization = v;
            Battle.Frame         = 0;
        }