//When a car collides kill the child
    public void OnCollisionEnter(Collision col)
    {
        CalculateFitness();
        ResetCarPosition();

        //display the current child fitness
        Display += "Child " + (currentChild + 1) + ":  " + Fitness[currentChild].ToString("f2") + "\n";
        PopulationText.GetComponent <Text>().text = Display;

        //if all children have died
        if (currentChild == Fitness.Length - 1)
        {
            int[] FitestParents = new int[2];

            //find 2 of the fittest parents
            FitestParents = FindFittestParents(Fitness);

            GeneticNetwork Father = Cars[FitestParents[0]];
            GeneticNetwork Mother = Cars[FitestParents[1]];

            //create a new generation of children based on 2 fittest parents
            for (int i = 0; i < Population; i++)
            {
                Fitness[i] = 0;
                Cars[i]    = new GeneticNetwork(Father, Mother, Probability);
            }
            //increment generation, reset current child and fitness display
            Generation++;
            currentChild = -1;
            Display      = "";
        }
        currentChild++;
    }
 //Initialise objects
 void Start()
 {
     Rigidbody  = GetComponent <Rigidbody>();
     Outputs    = new List <double>();
     Car        = new GeneticNetwork();
     saveLoad   = new GeneticWeights();
     controller = new CarController();
     Rigidbody.GetComponent <CarPhysics>().driver = Driver.AI;
     position = new ResetPosition();
     LoadChild();
 }
Пример #3
0
    public void Random2()
    {
        // ARRANGE
        GeneticNetwork genetic = new GeneticNetwork();

        // ACT
        double random = genetic.GetRandomWeight();

        // ASSERT
        Assert.That(random, Is.InRange(-1f, 1f));
    }
Пример #4
0
    public void Sigmoid1()
    {
        // ARRANGE
        GeneticNetwork genetic = new GeneticNetwork();
        List <double>  sums    = new List <double> {
            1.0,
            6.3,
            2.2,
            0.00005
        };

        // ACT
        List <double> outputs = genetic.Sigmoid(sums);

        // ASSERT
        foreach (var output in outputs)
        {
            Assert.That(output, Is.InRange(0f, 1f));
        }
    }
Пример #5
0
    public void FeedForward2()
    {
        // ARRANGE
        GeneticNetwork genetic = new GeneticNetwork();

        double[] inputs = { 0.6, 0.4, 0.2, 0, 0 };

        int expectedlength = 2;

        // ACT
        List <double> outputs = genetic.FeedForward(inputs);

        // ASSERT
        Assert.That(outputs.Count, Is.EqualTo(expectedlength));

        foreach (var output in outputs)
        {
            Assert.That(output, Is.InRange(0f, 1f));
        }
    }
    //Crossover & mutation of weights based on selected probability at end of generation
    public GeneticNetwork(GeneticNetwork Father, GeneticNetwork Mother, float probability)
    {
        System.Random randomBool = new System.Random();
        this.weights = new List <double[][]>();

        //For each layer create new matrix
        for (int i = 0; i < lengthLayers - 1; i++)
        {
            double[][] layerWeights = new double[layers[i]][];

            //for each row
            for (int j = 0; j < layers[i]; j++)
            {
                layerWeights[j] = new double[layers[i + 1]];

                //and for each column
                for (int k = 0; k < layers[i + 1]; k++)
                {
                    //randomly crossover either mother or father
                    if (randomBool.Next(2) == 0)
                    {
                        layerWeights[j][k] = Father.weights[i][j][k];
                    }
                    else
                    {
                        layerWeights[j][k] = Mother.weights[i][j][k];
                    }

                    //mutate that layer to a random weight if it falls under the probability
                    if (Random.Range(0f, 1f) < probability)
                    {
                        layerWeights[j][k] = GetRandomWeight();
                    }
                }
            }
            //Add that new weight to the new list of weights (children)
            weights.Add(layerWeights);
        }
    }
Пример #7
0
    public void Init1()
    {
        // ARRANGE
        int population = 10;

        GeneticNetwork[] genetic = new GeneticNetwork[population];
        int expectedlength       = 10;

        // ACT
        for (int i = 0; i < population; i++)
        {
            genetic[i] = new GeneticNetwork();
        }

        // ASSERT
        Assert.That(genetic.Length, Is.EqualTo(expectedlength));

        for (int i = 0; i < population; i++)
        {
            Assert.That(genetic[i].GetWeights().GetType() == typeof(List <double[][]>));
        }
    }
    //Initialise various methods and arrays
    void Start()
    {
        fitnessMeasure = LearningModeScript.fitnessMeasure;
        Population     = LearningModeScript.Population;
        Probability    = LearningModeScript.Probability;
        nextChild.onClick.AddListener(NextChild);
        Rigidbody  = GetComponent <Rigidbody>();
        controller = new CarController();
        saveLoad   = new GeneticWeights();
        Rigidbody.GetComponent <CarPhysics>().driver = Driver.AI;
        Fitness  = new double[Population];
        Outputs  = new List <double>();
        Position = transform.position;

        //Create cars of size population
        Cars = new GeneticNetwork[Population];

        //Initiate list of weights of size pupulation
        for (int i = 0; i < Population; i++)
        {
            Cars[i] = new GeneticNetwork();
        }
    }