예제 #1
0
        private static void initializeGeneticAlgorithm(
            int populationSize,
            int parentalChromosomesSurviveCount,
            OptimizableNeuralNetwork baseNeuralNetwork)
        {
            Population <OptimizableNeuralNetwork> brains = new Population <OptimizableNeuralNetwork>();

            for (int i = 0; i < (populationSize - 1); i++)
            {
                if (baseNeuralNetwork == null)
                {
                    brains.Add(NeuralNetworkDrivenAgent.randomNeuralNetworkBrain());
                }
                else
                {
                    brains.Add(baseNeuralNetwork.Mutate());
                }
            }
            if (baseNeuralNetwork != null)
            {
                brains.Add(baseNeuralNetwork);
            }
            else
            {
                brains.Add(NeuralNetworkDrivenAgent.randomNeuralNetworkBrain());
            }

            ga = new GeneticAlgorithm <OptimizableNeuralNetwork, double>(brains, TournamentEnvironmentFitness.Calculate);

            ga.ParentChromosomesSurviveCount = parentalChromosomesSurviveCount;
        }
예제 #2
0
        public static OptimizableNeuralNetwork randomNeuralNetworkBrain()
        {
            OptimizableNeuralNetwork nn = new OptimizableNeuralNetwork(15);

            for (int i = 0; i < 15; i++)
            {
                var f = ThresholdFunction.Random();
                nn.SetNeuronFunction(i, f, ThresholdFunction.GetDefaultParams(f));
            }
            for (int i = 0; i < 6; i++)
            {
                nn.SetNeuronFunction(i, ThresholdFunction.Function.Linear, ThresholdFunction.GetDefaultParams(ThresholdFunction.Function.Linear));
            }
            for (int i = 0; i < 6; i++)
            {
                for (int j = 6; j < 15; j++)
                {
                    nn.AddLink(i, j, random.NextDouble());
                }
            }
            for (int i = 6; i < 15; i++)
            {
                for (int j = 6; j < 15; j++)
                {
                    if (i < j)
                    {
                        nn.AddLink(i, j, random.NextDouble());
                    }
                }
            }
            return(nn);
        }
예제 #3
0
        public static double Calculate(OptimizableNeuralNetwork chromosome)
        {
            // TODO maybe, its better to initialize these parameters in constructor
            const int width                 = 200;
            const int height                = 200;
            int       agentsCount           = 10;
            int       foodCount             = 5;
            int       environmentIterations = 50;

            AgentsEnvironment env = new AgentsEnvironment(width, height);

            for (int i = 0; i < agentsCount; i++)
            {
                int    x         = random.Next(width);
                int    y         = random.Next(height);
                double direction = 2 * Math.PI * random.NextDouble();

                NeuralNetworkDrivenAgent agent = new NeuralNetworkDrivenAgent(x, y, direction);
                agent.setBrain(chromosome.Clone() as NeuralNetwork);

                env.Add(agent);
            }

            for (int i = 0; i < foodCount; i++)
            {
                Food food = newPieceOfFood(width, height);
                env.Add(food);
            }

            EatenFoodObserver tournamentListener = new FitnessObserver(width, height);

            env.AgentEvent += tournamentListener.notify;

            for (int i = 0; i < environmentIterations; i++)
            {
                env.timeStep();
            }

            double score = tournamentListener.getScore();

            return(1.0 / score);
        }