Esempio n. 1
0
    /* TODO:
     *  1) написатьь бы класс для хранения инфы о поколении (популяции), а то так фигово выглядит.
     */
    public Controller(World world)
    {
        this.world = world;

        random = new Random();

        allGenomsScore = new List <float>();
        allAvgScores   = new List <float>();
        genoms         = new List <Genom.Genom>();
        genomScore     = new List <float>();

        currentCar = null;

        for (int i = 0; i < populationSize; i++)
        {
            Genom.Genom newGenom = new Genom.Genom(random);
            newGenom.mutate();
            genoms.Add(newGenom);
            genomScore.Add(0);
        }
    }
Esempio n. 2
0
    public void Update(GameTime time, Matrix view)
    {
        if (currentCar == null)
        {
            if (currentGenom < populationSize)
            {
                currentCar = new PhysicalWorld.Car(world, genoms[currentGenom]);
            }
            else
            {
                int   maxIndex = 0;
                float maxScore = 0;
                for (int i = 0; i < populationSize; i++)
                {
                    if (genomScore[i] > maxScore)
                    {
                        maxScore = genomScore[i];
                        maxIndex = i;
                    }
                }

                if (maxScore > absoluteRecord)
                {
                    absoluteRecord = maxScore;
                    bestGenom      = genoms[maxIndex].Clone();
                }

                allGenomsScore.Add(maxScore);

                /**********************/
                float avg = 0;
                for (int i = 0; i < populationSize; i++)
                {
                    avg += genomScore[i];
                }
                avg /= populationSize;
                allAvgScores.Add(avg);
                /**********************/

                for (int i = 0; i < populationSize; i++)
                {
                    genomScore[i] = 0;
                }

                genoms[0] = bestGenom.Clone();
                for (int i = 1; i < populationSize; i++)
                {
                    Genom.Genom mutatedGenom = bestGenom.Clone();
                    mutatedGenom.mutate();
                    genoms[i] = mutatedGenom;
                }

                currentGenom = 0;
                ++generation;
            }
        }
        else
        {
            if (!currentCar.isMove(time))
            {
                genomScore[currentGenom++] = currentCar.getScore();
                currentCar.die(world);
                currentCar = null;
            }
        }
    }