public void CrossoverWeights(NeuralNetworkController nncA, NeuralNetworkController nncB)
    {
        for (int r = 0; r < inputWeights.RowCount; r++)
        {
            for (int c = 0; c < inputWeights.ColumnCount; c++)
            {
                if (Random.Range(0, 2) == 1)
                {
                    inputWeights[r, c] = nncA.inputWeights[r, c];
                }
                else
                {
                    inputWeights[r, c] = nncB.inputWeights[r, c];
                }
            }
        }

        for (int r = 0; r < hiddenWeights.RowCount; r++)
        {
            for (int c = 0; c < hiddenWeights.ColumnCount; c++)
            {
                if (Random.Range(0, 2) == 1)
                {
                    hiddenWeights[r, c] = nncA.hiddenWeights[r, c];
                }
                else
                {
                    hiddenWeights[r, c] = nncB.hiddenWeights[r, c];
                }
            }
        }
    }
Exemple #2
0
    // -------- FUNCTIONS --------

    // Start is called before the first frame update
    void Start()
    {
        countTimeStop = 0;

        // Initializing sensor parameters
        sensorLength        = 10f;
        sensorAngle         = 35f;
        frontSensorPosition = 0.1f;
        layer = 1 << LayerMask.NameToLayer("Circuit");
        lap   = 1;

        // Initializing car parameters
        rb             = GetComponent <Rigidbody2D>();
        rb.mass        = 3f;
        rb.drag        = 1f;
        rb.angularDrag = 1f;

        //Checkpoints
        currentCheckpoint = 1;
        previewCheckpoint = 0;

        // Have you finish the lap?

        dataSizeTh = 1;
        dataSizeSt = 1;

        referee       = GameObject.FindGameObjectWithTag("NNController").GetComponent <NeuralNetworkController>();
        weightsThrust = referee.GetDecisionThrust();
        weightsSteer  = referee.GetDecisionSteer();
        thetaThrust   = referee.GetDecisionThrustTheta();
        thetaSteer    = referee.GetDecisionSteerTheta();
    }
 private void Crossover(NeuralNetworkController[] selectedGenomes)
 {
     for (int i = 0; i < selectedGenomes.Length - 1; i++)
     {
         NeuralNetworkController parentA = selectedGenomes[i];
         NeuralNetworkController parentB = selectedGenomes[Random.Range(0, populationQuantity)];
         population[i].CrossoverWeights(parentA, parentB);
         population[i].CrossoverBiases(parentA, parentB);
     }
 }
 public void CrossoverBiases(NeuralNetworkController nncA, NeuralNetworkController nncB)
 {
     if (Random.Range(0, 2) == 1)
     {
         inputBias   = nncA.inputBias;
         hiddentBias = nncB.hiddentBias;
     }
     else
     {
         inputBias   = nncB.inputBias;
         hiddentBias = nncA.hiddentBias;
     }
 }
    private NeuralNetworkController[] RouletteWheelSelection()
    {
        float allFitness = 0f;

        for (int i = 0; i < population.Length; i++)
        {
            allFitness += population[i].fitness;
        }

        float[] normalizedFitness            = new float[populationQuantity];
        float[] accumulatedFitness           = new float[populationQuantity];
        float   allPreviousNormalizedFitness = 0f;

        for (int i = population.Length - 1; i >= 0; i--)
        {
            normalizedFitness[i]          = population[i].fitness / allFitness;
            accumulatedFitness[i]         = normalizedFitness[i] + allPreviousNormalizedFitness;
            allPreviousNormalizedFitness += normalizedFitness[i];
        }

        NeuralNetworkController[] selectedGenomes = new NeuralNetworkController[populationQuantity];
        for (int i = 0; i < topGenomes; i++)
        {
            selectedGenomes[i] = population[i];
        }

        int   selectedIndex = topGenomes;
        float selectionTreshold;

        while (selectedIndex < populationQuantity)
        {
            selectionTreshold = Random.Range(0f, 1f);
            for (int i = accumulatedFitness.Length - 1; i >= 0; i--)
            {
                if (accumulatedFitness[i] >= selectionTreshold)
                {
                    selectedGenomes[selectedIndex] = population[i];
                    selectedIndex++;
                    break;
                }
                else if (i == 0)
                {
                    Debug.LogError("Genome not selected with threshold: " + selectionTreshold + ", rounding problem?");
                }
            }
        }

        Array.Sort(selectedGenomes, neuralNetworkComparer);

        return(selectedGenomes);
    }
Exemple #6
0
    public void StartWithNetwork(NeuralNetworkController currentNetwork)
    {
        network = currentNetwork;

        transform.position    = initPosition;
        transform.eulerAngles = initRotation;

        fitness      = 0f;
        time         = 0f;
        distance     = 0f;
        avgSpeed     = 0f;
        lastPosition = initPosition;
        started      = true;
    }
Exemple #7
0
 public void SetUp()
 {
     mockery                     = new Mockery();
     mockNeuralNetwork           = mockery.NewMock <IFeedForwardNeuralNetwork>();
     testNeuralNetworkController = new NeuralNetworkController(mockNeuralNetwork);
 }