public NerualNetwork InitialiseCopy(int hiddenLayerCount, int hiddenNeuronCount)
    {
        NerualNetwork network = new NerualNetwork();

        List <Matrix <float> > newWeights = new List <Matrix <float> >();

        for (int i = 0; i < this.weights.Count; i++)
        {
            Matrix <float> currentWeight = Matrix <float> .Build.Dense(weights[i].RowCount, weights[i].ColumnCount);

            for (int x = 0; x < currentWeight.RowCount; x++)
            {
                for (int y = 0; y < currentWeight.ColumnCount; y++)
                {
                    currentWeight[x, y] = weights[i][x, y];
                }
            }

            newWeights.Add(currentWeight);
        }

        List <float> newBiases = new List <float>();

        newBiases.AddRange(biases);

        network.weights = newWeights;
        network.biases  = newBiases;

        network.InitialiseHidden(hiddenLayerCount, hiddenNeuronCount);

        return(network);
    }
Пример #2
0
    private NerualNetwork[] PickBestPopulation()
    {
        NerualNetwork[] newPopulation = new NerualNetwork[population];

        for (int i = 0; i < numOfBestAgentSelctions; i++)
        {
            newPopulation[naturalSelected]         = neuralNetworkPopulation[i].InitialiseCopy(carController.neuralLayers, carController.neurons);
            newPopulation[naturalSelected].fitness = 0;
            naturalSelected++;

            int f = Mathf.RoundToInt(neuralNetworkPopulation[i].fitness * 10);

            // fitness times 10 to increase the change
            // that the neuralnetwork be selected from the gene pool
            for (int c = 0; c < f; c++)
            {
                genePool.Add(i);
            }
        }

        for (int i = 0; i < numOfWorstAgentSelections; i++)
        {
            int last = neuralNetworkPopulation.Length - (i + 1);
            // last -= i;

            int f = Mathf.RoundToInt(neuralNetworkPopulation[last].fitness * 10);

            for (int c = 0; c < f; c++)
            {
                genePool.Add(last);
            }
        }

        return(newPopulation);
    }
    void Awake()
    {
        // Car accelaration and steering data initialization
        accelerationRate = 1.5f;
        steeringRate     = 3f;

        car = this.gameObject;
        rb  = car.GetComponent <Rigidbody2D>();

        hitBorder = false;

        distanceTravelled = 0;

        // Record start postion and rotation
        startPosition = transform.position;
        startRotation = transform.eulerAngles;
        lastPosition  = startPosition;

        // ignore car object's own collider when raycast
        Physics2D.queriesStartInColliders = false;

        neuralNetwork = GetComponent <NerualNetwork>();
        neuralLayers  = 3;
        neurons       = 10;

        neuralNetwork.Initialize(neuralLayers, neurons);
    }
Пример #4
0
 private void RandomizeChildren(NerualNetwork[] networkPopulation, int startIndex)
 {
     while (startIndex < population)
     {
         networkPopulation[startIndex] = new NerualNetwork();
         networkPopulation[startIndex].Initialize(carController.neuralLayers, carController.neurons);
         startIndex++;
     }
 }
Пример #5
0
 public void Death(float fitness, NerualNetwork network)
 {
     if (currentGenome < neuralNetworkPopulation.Length - 1)
     {
         neuralNetworkPopulation[currentGenome].fitness = fitness;
         currentGenome++;
         ResetToCurrentGenome();
     }
     else
     {
         RePopulate();
     }
 }
Пример #6
0
 private void SortPopulation(NerualNetwork[] networks)
 {
     for (int i = 0; i < networks.Length; i++)
     {
         for (int j = i; j < networks.Length; j++)
         {
             if (networks[i].fitness < networks[j].fitness)
             {
                 NerualNetwork temp = networks[i];
                 networks[i] = networks[j];
                 networks[j] = temp;
             }
         }
     }
 }
Пример #7
0
    // private NerualNetwork[] CrossoverPopulation(){
    //     List<NerualNetwork> test = new List<NerualNetwork>(5);
    // }

    private void Crossover(NerualNetwork[] newPopulation)
    {
        // for (int i = 0; i < numberOfChildrenCrossovered; i+= 2){
        for (int i = naturalSelected; i < population - 20; i++)
        {
            int AIndex = i;
            int BIndex = i;

            if (genePool.Count >= 1)
            {
                for (int l = 0; l < 100; l++)
                // while(true)
                {
                    AIndex = genePool[Random.Range(0, genePool.Count)];
                    BIndex = genePool[Random.Range(0, genePool.Count)];

                    if (AIndex != BIndex)
                    {
                        break;
                    }
                }
            }

            NerualNetwork Child1 = new NerualNetwork();
            NerualNetwork Child2 = new NerualNetwork();

            Child1.Initialize(carController.neuralLayers, carController.neurons);
            // Child2.Initialize(carController.neuralLayers, carController.neurons);

            Child1.fitness = 0;
            // Child2.fitness = 0;


            for (int w = 0; w < Child1.weights.Count; w++)
            {
                if (Random.Range(0.0f, 1.0f) < 0.5f)
                {
                    Child1.weights[w] = neuralNetworkPopulation[AIndex].weights[w];
                    // Child2.weights[w] = neuralNetworkPopulation[BIndex].weights[w];
                }
                else
                {
                    // Child2.weights[w] = neuralNetworkPopulation[AIndex].weights[w];
                    Child1.weights[w] = neuralNetworkPopulation[BIndex].weights[w];
                }
            }


            for (int w = 0; w < Child1.biases.Count; w++)
            {
                if (Random.Range(0.0f, 1.0f) < 0.5f)
                {
                    Child1.biases[w] = neuralNetworkPopulation[AIndex].biases[w];
                    // Child2.biases[w] = neuralNetworkPopulation[BIndex].biases[w];
                }
                else
                {
                    // Child2.biases[w] = neuralNetworkPopulation[AIndex].biases[w];
                    Child1.biases[w] = neuralNetworkPopulation[BIndex].biases[w];
                }
            }

            newPopulation[naturalSelected] = Child1;
            naturalSelected++;

            // newPopulation[naturalSelected] = Child2;
            // naturalSelected++;
        }
    }
Пример #8
0
        static void Main(string[] args)
        {
            // объявления макета сети

            int[] netMaket = new int[3];

            netMaket[0] = 2;
            netMaket[1] = 4;
            netMaket[2] = 2;

            // объявления потока для записи ошибок в эпохе
            StreamWriter errorLog = new StreamWriter("log1.txt");


            // Должно быть на выходе
            double[][] needOut = new double[8][];

            // Данные на вход
            double[][] input = new double[8][];

            #region dataInit

            needOut[0] = new double[] { 0, 1 };
            needOut[1] = new double[] { 1, 0 };
            needOut[2] = new double[] { 0, 1 };
            needOut[3] = new double[] { 1, 0 };
            needOut[4] = new double[] { 1, 0 };
            needOut[5] = new double[] { 0, 1 };
            needOut[6] = new double[] { 1, 0 };
            needOut[7] = new double[] { 1, 0 };

            input[0] = new double[] { 1, 0 };
            input[1] = new double[] { 6, 20 };
            input[2] = new double[] { 2, 0 };
            input[3] = new double[] { 4, 44 };
            input[4] = new double[] { 6, 44 };
            input[5] = new double[] { 8, 1 };
            input[6] = new double[] { 10, 43 };
            input[7] = new double[] { 11, 24 };

            #endregion

            int epohs;

            // Создание конструктора сети
            NetworkConstructor maket = new NetworkConstructor(netMaket);

            // Объявление сети
            NerualNetwork net = maket.GetNet(0.2);

            //net.LoadData();

            // Ввод кол-ва эпох
            Console.Write("\nКоличество эпох (Больше 2000 нет смысла): ");

            epohs = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < epohs; i++)
            {
                for (int j = 0; j < input.Length; j++)
                {
                    net.SetData(input[j]);
                    net.UpdateData();

                    net.Learn(needOut[j]);
                }

                errorLog.WriteLine(net.GetError());
            }

            net.SaveData();

            errorLog.Close();
            Console.Write("Нажмите любую кнопку . . . ");
            Console.ReadKey();
        }
 public void ResetWithNeuralNetwork(NerualNetwork network)
 {
     neuralNetwork = network;
     Reset();
 }