Exemplo n.º 1
0
 /// <summary>
 /// Uses the <see cref="IAgent"/>s produced by an <see cref="IAgentEnvironment"/> to teach a <see cref="NeuralNetwork"/> with the goal of increasing the <see cref="IAgent.GetFitness"/> of resulting <see cref="IAgent"/>s.
 /// </summary>
 /// <param name="algorithm">The given <see cref="IAgentLearningAlgorithm"/>.</param>
 /// <param name="environment">The <see cref="IAgentEnvironment"/> containing the <see cref="IAgent"/>s to train.</param>
 /// <param name="generations">The number of generations of <see cref="IAgent"/>s to go through.</param>
 public static double[] Teach(this IAgentLearningAlgorithm algorithm, IAgentEnvironment environment, int generations)
 {
     double[] output = new double[generations];
     for (int i = 0; i < generations; i++)
     {
         output[i] = algorithm.Teach(environment);
     }
     return(output);
 }
Exemplo n.º 2
0
 private void RunEnvironment(IAgentEnvironment environment)
 {
     environment.Setup();
     for (int i = 0; i < NumberOfExecutes; i++)
     {
         foreach (var agent in environment.Agents)
         {
             agent.Execute();
         }
     }
 }
Exemplo n.º 3
0
 private void Mutate(IAgentEnvironment environment)
 {
     foreach (var neuron in environment.Agents.SelectMany(
                  agent => agent.Network.Layers.SelectMany(layer => layer.Neurons)))
     {
         neuron.Bias += Rand.Current.NextDouble(-MutationLevel, MutationLevel);
         foreach (var synapse in neuron.Synapses)
         {
             synapse.Weight += Rand.Current.NextDouble(-MutationLevel, MutationLevel);
         }
     }
 }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public double Teach(IAgentEnvironment environment)
        {
            //// Create the specified number of environments.
            IAgentEnvironment[] eList = Enumerable.Range(0, GenerationSize).Select(i => environment.Copy()).ToArray();
            Parallel.ForEach(eList, e =>
            {
                Mutate(e);
                RunEnvironment(e);
            });

            var fitnesses = eList.Select(e => e.Agents.Select(a => a.GetFitness())).Transpose()
                            .Select(t => t.ToList()).ToList();

            //// Find the best network for each type of agent in the environment, and swap networks.
            for (int i = 0; i < fitnesses.Count; i++)
            {
                int iMaxIndex = fitnesses[i].IndexOf(fitnesses[i].Max());
                environment.Agents.ElementAt(i).Network =
                    eList[iMaxIndex].Agents.ElementAt(i).Network;
            }

            return(fitnesses.Average(f => f.Max()));
        }
Exemplo n.º 5
0
 public void SetAgentEnvironment(IAgentEnvironment agentEnviroment)
 {
     _agentEnvironment = agentEnviroment;
 }