コード例 #1
0
 public void SpawnBestCar(GameObject prefab, NeatNeuralNetwork network)
 {
     Car                 = (Instantiate(prefab, new Vector3(0, 1.6f, -16), new Quaternion(0, 0, 1, 0))).GetComponent <Bot>();//create botes
     Car.network         = network;
     Car.gameObject.name = "SimplyTheBest";
     Car.GetComponent <MeshRenderer>().material.color = Color.green;
 }
コード例 #2
0
 public NeuralGeneConnection(NeuralGeneNode _inputNeuron, NeuralGeneNode _outputNeuron, bool _connectionIsEnabled, int _innovation)
 {
     inputNeuron         = _inputNeuron;
     outputNeuron        = _outputNeuron;
     weight              = NeatNeuralNetwork.GetRandom();
     connectionIsEnabled = _connectionIsEnabled;
     innovation          = _innovation;
 }
コード例 #3
0
 public NeuralGeneNode(NeuralActivationFunction _neuralActivationFunction, int _nodeNumber, NeuralNodeType _nodeType)
 {
     inputSynapses  = new List <NeuralGeneConnection>();
     outputSynapses = new List <NeuralGeneConnection>();
     SetActivationFunction(_neuralActivationFunction);
     nodeNumber = _nodeNumber;
     nodeType   = _nodeType;
     bias       = NeatNeuralNetwork.GetRandom();
 }
コード例 #4
0
    private NeatNeuralNetwork MixedChooseParent(NeatNeuralNetwork parent)
    {
        int randomNumber = random.Next(0, MatingPool.Count);

        if (parent != null)
        {
            while (MatingPool[randomNumber] == parent)
            {
                randomNumber = random.Next(0, MatingPool.Count);
            }
        }
        return(MatingPool[randomNumber]);
    }
コード例 #5
0
    //public void SaveGeneration(string filePath, bool Encript)
    //{
    //    SaveData.GeneticSaveData<T> save = new SaveData.GeneticSaveData<T>
    //    {
    //        Generation = Generation,
    //        PopulationGenes = new List<T[]>(Population.Count),
    //    };

    //    for (int i = 0; i < Population.Count; i++)
    //    {
    //        save.PopulationGenes.Add(new T[dnaSize]);
    //        Array.Copy(Population[i].Genes, save.PopulationGenes[i], dnaSize);
    //    }

    //    if (Encript)
    //        Utils.FileReadWrite.WriteEncryptedToBinaryFile(filePath, save);
    //    else
    //        Utils.FileReadWrite.WriteToBinaryFile(filePath, save);
    //}

    //public bool LoadGeneration(string filePath, bool Encript)
    //{
    //    if (!System.IO.File.Exists(filePath))
    //        return false;
    //    SaveData.GeneticSaveData<T> save = null;
    //    if (Encript)
    //        save = Utils.FileReadWrite.ReadEncryptedFromBinaryFile<SaveData.GeneticSaveData<T>>(filePath);
    //    else
    //        save = Utils.FileReadWrite.ReadFromBinaryFile<SaveData.GeneticSaveData<T>>(filePath);
    //    Generation = save.Generation;
    //    for (int i = 0; i < save.PopulationGenes.Count; i++)
    //    {
    //        if (i >= Population.Count)
    //        {
    //            Population.Add(new DNA<T>(dnaSize, random, getRandomGene, fitnessFunction, false));
    //        }
    //        Array.Copy(save.PopulationGenes[i], Population[i].Genes, dnaSize);
    //    }
    //    return true;
    //}

    public int CompareDNA(NeatNeuralNetwork a, NeatNeuralNetwork b)
    {
        if (a.genome.fitness > b.genome.fitness)
        {
            return(-1);
        }
        else if (a.genome.fitness < b.genome.fitness)
        {
            return(1);
        }
        else
        {
            return(0);
        }
    }
コード例 #6
0
    public void CalculateFitness()
    {
        fitnessSum = 0;
        NeatNeuralNetwork best = Population[0];

        for (int i = 0; i < Population.Count; i++)
        {
            fitnessSum += Population[i].genome.CalculateFitness(i);

            if (Population[i].genome.fitness > best.genome.fitness)
            {
                best = Population[i];
            }
        }

        BestFitness = (float)best.genome.fitness;
        BestGenes   = best.genome;
    }
コード例 #7
0
    public void NewGeneration(int numNewDNA = 0, int elitism = 0, int mercy = 0, bool Elitism = true, bool bestWorst = true, bool crossoverNewDNA = true)
    {
        int finalCount = Population.Count + numNewDNA;

        if (finalCount <= 0)
        {
            return;
        }

        elitist    = elitism;
        this.mercy = mercy;

        if (Population.Count > 0)
        {
            CalculateFitness();
            Population.Sort(CompareDNA);

            if (elitist > 0)
            {
                GetBestAndWorstParents();
            }
            else
            {
                bestWorst = false;
            }
        }
        newPopulation = new List <NeatNeuralNetwork>();

        for (int i = 0; i < finalCount; i++)
        {
            if ((i < elitist && Elitism) && i < Population.Count)
            {
                newPopulation.Add(Population[i]);
            }
            else if (i < Population.Count || crossoverNewDNA)
            {
                NeatNeuralNetwork parent1 = null;
                NeatNeuralNetwork parent2 = null;
                if (bestWorst)
                {
                    parent1 = MixedChooseParent(null);
                    parent2 = MixedChooseParent(parent1);
                }
                else
                {
                    parent1 = ChooseParent();
                    parent2 = ChooseParent();
                }

                NeatNeuralNetwork child = parent1.Crossover(parent2);

                if (mutateGene == null)
                {
                    child.genome.Mutate(MutationRate);
                }
                else
                {
                    mutateGene(child, MutationRate);
                }

                newPopulation.Add(child);
            }
            else
            {
                newPopulation.Add(new NeatNeuralNetwork(neuralActivationFunctions, inputSize, outputSize, globalInnovation, false, fitnessFunction, mutationRates, IncreaseGlobalInnovation, learnRate, momentum));
            }
        }

        // List<NeatNeuralNetwork> tmpList = Population;
        Population    = newPopulation;
        newPopulation = new List <NeatNeuralNetwork>();
        Generation++;
    }
コード例 #8
0
    public NeatNeuralNetwork Crossover(NeatNeuralNetwork otherParent)
    {
        NeuralGenome childGenome = new NeuralGenome();

        childGenome.learnRate       = genome.learnRate;
        childGenome.momentum        = genome.momentum;
        childGenome.random          = Random;
        childGenome.InputLayer      = new List <NeuralGeneNode>();
        childGenome.HiddenLayers    = new List <NeuralGeneNode>();
        childGenome.OutputLayer     = new List <NeuralGeneNode>();
        childGenome.nodes           = new List <NeuralGeneNode>();
        childGenome.connections     = new List <NeuralGeneConnection>();
        childGenome.mutationRates   = genome.mutationRates;
        childGenome.fitnessFunction = genome.fitnessFunction;
        childGenome.InitNewGenome(genome.currInnovation, neuralActivationFunctions, genome.IncreaseGlobalInnovation, false);
        // no best parent
        int    bestGenome     = 0;
        bool   takeNotOptimum = false;
        double randRate       = Random.NextDouble();

        if (randRate < 0.01f)
        {
            takeNotOptimum = true;
        }
        // find parent with best fitness
        if (otherParent.genome.fitness > genome.fitness)
        {
            // best parent is other parent
            bestGenome = 1;
        }
        else if (otherParent.genome.fitness < genome.fitness)
        {
            // best parent is thisparent
            bestGenome = 2;
        }
        int index = 0;

        if (this.genome.connections.Count > otherParent.genome.connections.Count)
        {
            foreach (var thisParentConnection in this.genome.connections)
            {
                //foreach (var otherParentConnection in otherParent.genome.connections)
                //{
                if (index > otherParent.genome.connections.Count - 1)
                {
                    HandleExtraNodes(thisParentConnection, null, childGenome, bestGenome, takeNotOptimum);
                }
                else
                {
                    CrossOverParents(thisParentConnection, otherParent.genome.connections[index], childGenome, bestGenome);
                }
                //}
                index++;
            }
        }
        else
        {
            foreach (var otherParentConnection in otherParent.genome.connections)
            {
                //foreach (var thisParentConnection in this.genome.connections)
                //{
                if (index > this.genome.connections.Count - 1)
                {
                    HandleExtraNodes(null, otherParentConnection, childGenome, bestGenome, takeNotOptimum);
                }
                else
                {
                    CrossOverParents(this.genome.connections[index], otherParentConnection, childGenome, bestGenome);
                }
                //}
                index++;
            }
        }

        NeatNeuralNetwork child = new NeatNeuralNetwork(childGenome);

        return(child);
    }
コード例 #9
0
 public void AddConnection(NeuralGeneNode _inputNeuron, NeuralGeneConnection _synapse, bool _connectionIsEnabled, int _innovation)
 {
     bias = NeatNeuralNetwork.GetRandom();
     _inputNeuron.outputSynapses.Add(_synapse);
     inputSynapses.Add(_synapse);
 }