Esempio n. 1
0
    private ANNTrainer haveChild(
        ANNTrainer parent1,
        ANNTrainer parent2,
        double mutateChance,
        double mutateMaxFactor
        )
    {
        ANNTrainer child = new ANNTrainer(
            new ANN(
                parent1.nn.getLayers()
                )
            );

        for (int i = 0; i < parent1.nn._biases.Count(); ++i)
        {
            child.nn._biases[i] = Rand.Chance(.5) ? parent1.nn._biases[i] : parent2.nn._biases[i];
        }

        for (int i = 0; i < parent1.nn._weights.Count(); ++i)
        {
            child.nn._weights[i] = Rand.Chance(.5) ? parent1.nn._weights[i] : parent2.nn._weights[i];
        }

        return(mutate(child, mutateChance, mutateMaxFactor));
    }
Esempio n. 2
0
    public void Start()
    {
        if (gameOverPanel != null)
        {
            gameOverPanel.SetActive(false);
        }

        _zones = GameObject.FindGameObjectsWithTag(
            "ScoreZone"
            ).ToList <GameObject>(
            ).ConvertAll <SafeZone>(
            (go) => go.GetComponent <SafeZone>()
            );

        _buildingMask = 1 << LayerMask.NameToLayer("Building");

        _bart          = GameObject.Find("Bart");
        _bartZone      = _bart.GetComponent <Enemy>().zone;
        _marge         = GameObject.Find("Marge");
        _margeZone     = _marge.GetComponent <Enemy>().zone;
        _moe           = GameObject.Find("Moe");
        _moeZone       = _moe.GetComponent <Enemy>().zone;
        _moeMovement   = _moe.GetComponent <Movement>();
        _burns         = GameObject.Find("Burns");
        _burnsZone     = _burns.GetComponent <Enemy>().zone;
        _burnsMovement = _burns.GetComponent <Movement>();

        _homerFactory = GameObject.Find("HomerFactory").GetComponent <HomerFactory>();

        const int  populationCount = 8;
        List <int> layers          = new List <int>();

        layers.Add(8);         // Input layer
        layers.Add(18);        // Hidden layer
        layers.Add(2);         // Output layer
        const double mutateChance                   = .4;
        const double mutateMaxFactor                = .4;
        const double migrationPercentage            = 0;
        const double childrenPercentage             = .19;
        const int    generations                    = 1000;
        Action <ANNTrainer, Action <double> > train = (ANNTrainer t, Action <double> callback) => {
            _nn       = t;
            _callback = callback;
            _startGame();
        };

        new EO(
            //populationCount,
            //layers,
            @"C:\Users\Public\EO\mc40mmf40mp0cp19g1000p8generation8.txt",
            mutateChance,
            mutateMaxFactor,
            migrationPercentage,
            childrenPercentage,
            generations,
            train
            );
    }
Esempio n. 3
0
    public void Start()
    {
        if (gameOverPanel != null) {

            gameOverPanel.SetActive(false);
        }

        _zones = GameObject.FindGameObjectsWithTag(
            "ScoreZone"
        ).ToList<GameObject>(
        ).ConvertAll<SafeZone>(
            (go) => go.GetComponent<SafeZone>()
        );

        _buildingMask = 1 << LayerMask.NameToLayer("Building");

        _bart = GameObject.Find("Bart");
        _bartZone = _bart.GetComponent<Enemy>().zone;
        _marge = GameObject.Find("Marge");
        _margeZone = _marge.GetComponent<Enemy>().zone;
        _moe = GameObject.Find("Moe");
        _moeZone = _moe.GetComponent<Enemy>().zone;
        _moeMovement = _moe.GetComponent<Movement>();
        _burns = GameObject.Find("Burns");
        _burnsZone = _burns.GetComponent<Enemy>().zone;
        _burnsMovement = _burns.GetComponent<Movement>();

        _homerFactory = GameObject.Find("HomerFactory").GetComponent<HomerFactory>();

        const int populationCount = 8;
        List<int> layers = new List<int>();
        layers.Add(8); // Input layer
        layers.Add(18); // Hidden layer
        layers.Add(2); // Output layer
        const double mutateChance = .4;
        const double mutateMaxFactor = .4;
        const double migrationPercentage = 0;
        const double childrenPercentage = .19;
        const int generations = 1000;
        Action<ANNTrainer, Action<double>> train = (ANNTrainer t, Action<double> callback) => {

            _nn = t;
            _callback = callback;
            _startGame();
        };

        new EO(
            //populationCount,
            //layers,
            @"C:\Users\Public\EO\mc40mmf40mp0cp19g1000p8generation8.txt",
            mutateChance,
            mutateMaxFactor,
            migrationPercentage,
            childrenPercentage,
            generations,
            train
        );
    }
Esempio n. 4
0
    private ANNTrainer mutate(ANNTrainer trainer, double mutateChance, double mutateMaxFactor)
    {
        for (int i = 0; i < trainer.nn._biases.Count(); ++i)
        {
            trainer.nn._biases[i] = trainer.nn._biases[i].map(
                (double b) => {
                return(Rand.Chance(mutateChance) ? b + Rand.Range(mutateMaxFactor) : b);
            }
                );
        }

        for (int i = 0; i < trainer.nn._weights.Count(); ++i)
        {
            trainer.nn._weights[i] = trainer.nn._weights[i].map(
                (double w) => {
                return(Rand.Chance(mutateChance) ? w + Rand.Range(mutateMaxFactor) : w);
            }
                );
        }

        return(trainer);
    }
Esempio n. 5
0
    private ANNTrainer haveChild(
		ANNTrainer parent1,
		ANNTrainer parent2, 
		double mutateChance,
		double mutateMaxFactor
	)
    {
        ANNTrainer child = new ANNTrainer(
            new ANN(
                parent1.nn.getLayers()
            )
        );

        for (int i = 0; i < parent1.nn._biases.Count(); ++i) {

            child.nn._biases[i] = Rand.Chance(.5) ? parent1.nn._biases[i] : parent2.nn._biases[i];
        }

        for (int i = 0; i < parent1.nn._weights.Count(); ++i) {

            child.nn._weights[i] = Rand.Chance(.5) ? parent1.nn._weights[i] : parent2.nn._weights[i];
        }

        return mutate(child, mutateChance, mutateMaxFactor);
    }
Esempio n. 6
0
    private void _initialize(
		double mutateChance,
		double mutateMaxFactor,
		double migrationPercentage,
		double childrenPercentage,
		int generations,
		Action<ANNTrainer, Action<double>> train,
		List<ANN> population
	)
    {
        string uniqueId = "mc" + ((int) (mutateChance * 100)) +
            "mmf" + ((int) (mutateMaxFactor * 100)) +
                "mp"  + ((int) (migrationPercentage * 100)) +
                "cp"  + ((int) (childrenPercentage * 100)) +
                "g"  + generations + "p" + population.Count();

        int numMigrate = Mathf.RoundToInt((float) migrationPercentage * population.Count());
        int numChildren = Mathf.RoundToInt((float) childrenPercentage * population.Count());

        System.IO.File.WriteAllText(
            @"C:\Users\Public\EO\" + uniqueId + "generation0.txt",
            String.Join(
            ",\n",
            population.Select(
            nn => nn.ToString()
            ).ToArray()
            )
            );

        // This lambda soup is to allow the training to be asynchronous,
        // I tried using C#'s await, yeild and Task, but was unable to get
        // the desired behvaiour;
        Action<int> trainGeneration;

        trainGeneration = (int gen) => {

            List<ANNTrainer> trained = new List<ANNTrainer>();

            Action populationFinished = () => {

                // Biggest to smalest
                trained.Sort((a, b) => b.score.CompareTo(a.score));

                ANNTrainer best = trained[0];
                ANNTrainer second = trained[1];

                // Add migrants
                for (int i = 1; i <= numMigrate; ++i) {
                    Debug.Log("Adding migrant at " + (trained.Count() - i));
                    trained[trained.Count() - i] = new ANNTrainer(new ANN(best.nn.getLayers()));
                }
                for (int i = 1; i <= numChildren; ++i) {
                    Debug.Log("Adding child at " + (trained.Count() - i - numMigrate));
                    trained[trained.Count() - i - numMigrate] = this.haveChild(
                        best,
                        second,
                        mutateChance,
                        mutateMaxFactor
                    );
                }

                population = trained.ConvertAll<ANN>(trainer => trainer.nn);

                System.IO.File.WriteAllText(
                    @"C:\Users\Public\EO\" + uniqueId + "generation" + gen + ".txt",
                    String.Join(
                        ",\n",
                        population.Select(
                            nn => nn.ToString()
                        ).ToArray()
                    )
                );

                if (gen + 1 < generations) {

                    trainGeneration(gen + 1);
                }
            };

            Action<int> trainPopulation;

            trainPopulation = (int pop) => {
                Debug.Log("Training Generation " + gen + " Pop " + pop);
                ANNTrainer trainer = new ANNTrainer(population[pop]);

                train(
                    trainer,
                    (double score) => {
                    Debug.Log ("Pop " + pop + " scored " + score);
                    trainer.score = score;
                    trained.Add(trainer);

                    if (pop + 1 < population.Count()) {

                        trainPopulation(pop + 1);
                    } else {

                        populationFinished();
                    }
                }
                );
            };

            trainPopulation(0);
        };

        trainGeneration(0);
    }
Esempio n. 7
0
    private ANNTrainer mutate(ANNTrainer trainer, double mutateChance, double mutateMaxFactor)
    {
        for (int i = 0; i < trainer.nn._biases.Count(); ++i) {

            trainer.nn._biases[i] = trainer.nn._biases[i].map(
                (double b) => {

                    return Rand.Chance(mutateChance) ? b + Rand.Range(mutateMaxFactor) : b;
                }
            );
        }

        for (int i = 0; i < trainer.nn._weights.Count(); ++i) {

            trainer.nn._weights[i] = trainer.nn._weights[i].map(
                (double w) => {

                    return Rand.Chance(mutateChance) ? w + Rand.Range(mutateMaxFactor) : w;
                }
            );
        }

        return trainer;
    }
Esempio n. 8
0
    private void _initialize(
        double mutateChance,
        double mutateMaxFactor,
        double migrationPercentage,
        double childrenPercentage,
        int generations,
        Action <ANNTrainer, Action <double> > train,
        List <ANN> population
        )
    {
        string uniqueId = "mc" + ((int)(mutateChance * 100)) +
                          "mmf" + ((int)(mutateMaxFactor * 100)) +
                          "mp" + ((int)(migrationPercentage * 100)) +
                          "cp" + ((int)(childrenPercentage * 100)) +
                          "g" + generations + "p" + population.Count();

        int numMigrate  = Mathf.RoundToInt((float)migrationPercentage * population.Count());
        int numChildren = Mathf.RoundToInt((float)childrenPercentage * population.Count());

        System.IO.File.WriteAllText(
            @"C:\Users\Public\EO\" + uniqueId + "generation0.txt",
            String.Join(
                ",\n",
                population.Select(
                    nn => nn.ToString()
                    ).ToArray()
                )
            );

        // This lambda soup is to allow the training to be asynchronous,
        // I tried using C#'s await, yeild and Task, but was unable to get
        // the desired behvaiour;
        Action <int> trainGeneration;

        trainGeneration = (int gen) => {
            List <ANNTrainer> trained = new List <ANNTrainer>();

            Action populationFinished = () => {
                // Biggest to smalest
                trained.Sort((a, b) => b.score.CompareTo(a.score));

                ANNTrainer best   = trained[0];
                ANNTrainer second = trained[1];

                // Add migrants
                for (int i = 1; i <= numMigrate; ++i)
                {
                    Debug.Log("Adding migrant at " + (trained.Count() - i));
                    trained[trained.Count() - i] = new ANNTrainer(new ANN(best.nn.getLayers()));
                }
                for (int i = 1; i <= numChildren; ++i)
                {
                    Debug.Log("Adding child at " + (trained.Count() - i - numMigrate));
                    trained[trained.Count() - i - numMigrate] = this.haveChild(
                        best,
                        second,
                        mutateChance,
                        mutateMaxFactor
                        );
                }

                population = trained.ConvertAll <ANN>(trainer => trainer.nn);

                System.IO.File.WriteAllText(
                    @"C:\Users\Public\EO\" + uniqueId + "generation" + gen + ".txt",
                    String.Join(
                        ",\n",
                        population.Select(
                            nn => nn.ToString()
                            ).ToArray()
                        )
                    );

                if (gen + 1 < generations)
                {
                    trainGeneration(gen + 1);
                }
            };

            Action <int> trainPopulation;

            trainPopulation = (int pop) => {
                Debug.Log("Training Generation " + gen + " Pop " + pop);
                ANNTrainer trainer = new ANNTrainer(population[pop]);

                train(
                    trainer,
                    (double score) => {
                    Debug.Log("Pop " + pop + " scored " + score);
                    trainer.score = score;
                    trained.Add(trainer);

                    if (pop + 1 < population.Count())
                    {
                        trainPopulation(pop + 1);
                    }
                    else
                    {
                        populationFinished();
                    }
                }
                    );
            };

            trainPopulation(0);
        };

        trainGeneration(0);
    }