Пример #1
0
        public void SpawnNewCreature(Vector2 position)
        {
            Creature newCreature = new Creature(position, Rand.Choice(nouns), this);

            creatures.Add(newCreature);
            AddComponent(newCreature);
        }
Пример #2
0
        public NeuralNet(NeuralNet net1, NeuralNet net2, float mutationRate)
        {
            // Creates a network from crossing over two parent networks, with a chance of mutation

            NeuralNet[] parents = { net1, net2 };
            layers = new int[net1.layers.Length];
            List <float[]>   neuronList = new List <float[]>();
            List <float[]>   biasList   = new List <float[]>();
            List <float[][]> weightList = new List <float[][]>();

            for (int i = 0; i < layers.Length; i++)
            {
                layers[i] = net1.layers[i];
                neuronList.Add(new float[layers[i]]);
                float[]        bias            = new float[layers[i]];
                List <float[]> layerWeightList = new List <float[]>();

                for (int j = 0; j < layers[i]; j++)
                {
                    // Choose a random parent's bias
                    bias[j] = Rand.Choice(parents).biases[i][j];
                    // A small chance of slightly mutating the bias
                    if (Rand.Range(1f) < mutationRate)
                    {
                        bias[j] = Math.Max(-0.5f, Math.Min(0.5f, bias[j] + Rand.Range(-0.1f, 0.1f)));
                    }

                    if (i > 0)
                    {
                        float[] neuronWeights = new float[layers[i - 1]];
                        for (int k = 0; k < layers[i - 1]; k++)
                        {
                            // Choose a random parent's weight
                            neuronWeights[k] = Rand.Choice(parents).weights[i - 1][j][k];
                            // A small chance of slightly mutating the weight
                            if (Rand.Range(1f) < mutationRate)
                            {
                                neuronWeights[k] = Math.Max(-0.5f, Math.Min(0.5f, neuronWeights[k] + Rand.Range(-0.1f, 0.1f)));
                            }
                        }
                        layerWeightList.Add(neuronWeights);
                    }
                }

                biasList.Add(bias);
                if (i > 0)
                {
                    weightList.Add(layerWeightList.ToArray());
                }
            }

            neurons = neuronList.ToArray();
            biases  = biasList.ToArray();
            weights = weightList.ToArray();
        }