private DNA <double> GetCurrentGene()
        {
            WorldScene   scene = Extensions.GetWorldScene();
            DNA <double> dna   = scene.GenePool.Population[CurrentDNA];

            return(dna);
        }
        private void SetNetworkWeights(DNA <double> dna)
        {
            WorldScene scene = Extensions.GetWorldScene();

            // Get only the network weights
            double[] genes = dna.Genes.Take(scene.NeuralNetworkWeightsCount).ToArray();

            int index = 0;

            foreach (var layer in Network.Layers)
            {
                foreach (var neur in layer.Neurons)
                {
                    for (int i = 0; i < neur.Weights.Length; i++)
                    {
                        neur.Weights[i] = genes[index++];
                    }

                    ((ActivationNeuron)neur).Threshold = genes[index++];
                }
            }

            if (index != scene.NeuralNetworkWeightsCount)
            {
                throw new Exception("An error ocurred while retrieving the network weights!");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get current WorldScene from the simulation engine
        /// </summary>
        /// <returns>Current WorldScene</returns>
        /// <exception cref="Exception">If the current scene is not an WorldScene, and exception will be thrown</exception>
        public static WorldScene GetWorldScene()
        {
            if (GeneticEvolution.Engine.Scene.GetType() != typeof(WorldScene))
            {
                throw new Exception("The current running scene is not WorldScene. Cannot capture the food on the world");
            }

            WorldScene scene = (WorldScene)GeneticEvolution.Engine.Scene;

            return(scene);
        }
Ejemplo n.º 4
0
        private Food GetClosestFood(EvoEngine engine)
        {
            WorldScene scene = Extensions.GetWorldScene();
            Food       food  = null;
            float      dist  = float.MaxValue;

            foreach (IEntity entity in scene.World)
            {
                if (entity.GetType() == typeof(Food))
                {
                    float fooddist = Extensions.GetVectorDistance(snake.Location, entity.Location);
                    if (fooddist < dist && dist > 50.0f)                     //Ensure we do not circle current food
                    {
                        food = (Food)entity;
                        dist = fooddist;
                    }
                }
            }

            return(food);
        }