void RunExperiment(string XMLFile, string filename) { _filename = filename; _experiment = new SocialExperiment(); // Write the header for the results file in CSV format. using (TextWriter writer = new StreamWriter(_filename)) writer.WriteLine("Generation,Average,Best,Updates"); using (TextWriter writer = new StreamWriter(_filename.Replace(".csv", "_diversity_after.csv"))) writer.WriteLine("Generation,Orientation Variance,Velocity Variance"); using (TextWriter writer = new StreamWriter(_filename.Replace(".csv", "_diversity_before.csv"))) writer.WriteLine("Generation,Orientation Variance,Velocity Variance"); // Load the XML configuration file XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load(XMLFile); _experiment.Initialize("EgalitarianSocialLearning", xmlConfig.DocumentElement); _experiment.TrialId = _trialNum; // Create the evolution algorithm and attach the update event. _ea = _experiment.CreateEvolutionAlgorithm(); _ea.UpdateScheme = new SharpNeat.Core.UpdateScheme(1); _ea.UpdateEvent += new EventHandler(_ea_UpdateEvent); _experiment.Evaluator.TrialId = _trialNum; _experiment.Evaluator.DiversityFile = _filename.Replace(".csv", "_diversity.csv"); // Start algorithm (it will run on a background thread). _ea.StartContinue(); }
private void GenomesToAcceptability(IList <TGenome> genomeList) { string TEMP_NETWORK_FILE = string.Format("____temp{0}____network.xml", TrialId); var neatGenomeParams = new NeatGenomeParameters() { ActivationFn = PlainSigmoid.__DefaultInstance, InitialInterconnectionsProportion = 1 }; int inputs = _world.PlantTypes.Count() * World.SENSORS_PER_OBJECT_TYPE + _world.Predators.Count() * World.SENSORS_PER_OBJECT_TYPE + 1; int outputs = 2; var factory = new NeatGenomeFactory(inputs, outputs, neatGenomeParams); for (int i = 0; i < _agents.Length; i++) { // Decode the genome. IBlackBox phenome = _genomeDecoder.Decode(genomeList[i]); IAcceptabilityFunction accept = new RecurrentNeuralAcceptability(phenome); // Check that the genome is valid. if (phenome == null) { Console.WriteLine("Couldn't decode genome {0}!", i); _agents[i] = new SpinningAgent(i); continue; } // Create a feed forward network with 10 hidden nodes and random weights SocialExperiment.CreateNetwork(TEMP_NETWORK_FILE, inputs, outputs); using (var xr = XmlReader.Create(TEMP_NETWORK_FILE)) { var controllerGenome = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, factory)[0]; var controllerPhenome = _genomeDecoder.Decode((TGenome)controllerGenome); _agents[i] = new SocialAgent(i, _genomeList[i].SpecieIdx, controllerPhenome, _agentsNavigate, _agentsHide, accept) { MemorySize = CurrentMemorySize }; var network = (FastCyclicNetwork)controllerPhenome; network.Momentum = ((SocialAgent)_agents[i]).Momentum; network.BackpropLearningRate = ((SocialAgent)_agents[i]).LearningRate; } } File.Delete(TEMP_NETWORK_FILE); }
static void RunTrial(int offset) { RESULTS_FILE = EXPERIMENTS_DIR + RESULTS_FILE_BASE + offset + ".csv"; _random = new FastRandom(); _experiment = new SocialExperiment(); XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load(CONFIG_FILE); _experiment.Initialize("SimpleEvolution", xmlConfig.DocumentElement); _experiment.NeatGenomeParameters.AddConnectionMutationProbability = 0; _experiment.NeatGenomeParameters.AddNodeMutationProbability = 0; _experiment.NeatGenomeParameters.DeleteConnectionMutationProbability = 0; SocialExperiment.CreateNetwork(FEED_FORWARD_NETWORK_FILE, _experiment.InputCount, _experiment.OutputCount); // Record the changes at each step _experiment.World.Stepped += new social_learning.World.StepEventHandler(World_Stepped); // Read in the seed genome from file. This is the prototype for our other population of networks. var seed = _experiment.LoadPopulation(XmlReader.Create(FEED_FORWARD_NETWORK_FILE))[0]; // Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes. IGenomeFactory<NeatGenome> genomeFactory = _experiment.CreateGenomeFactory(); // Create an initial population of randomly generated genomes. List<NeatGenome> genomeList = genomeFactory.CreateGenomeList(_experiment.DefaultPopulationSize, 0, seed); // Randomize the genomes RandomizeGenomes(genomeList); // Create genome decoder. IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder = _experiment.CreateGenomeDecoder(); // Create the evaluator that will handle the simulation _evaluator = new ForagingEvaluator<NeatGenome>(genomeDecoder, _experiment.World, AgentTypes.Social) { MaxTimeSteps = 200000UL, BackpropEpochsPerExample = 1 }; using (TextWriter writer = new StreamWriter(RESULTS_FILE)) writer.WriteLine("Step,Best,Average"); // Start the simulation _evaluator.Evaluate(genomeList); }
static void Main(string[] args) { _random = new FastRandom(); _experiment = new SocialExperiment(); XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load(CONFIG_FILE); _experiment.Initialize("SimpleEvolution", xmlConfig.DocumentElement); _experiment.NeatGenomeParameters.AddConnectionMutationProbability = 0; _experiment.NeatGenomeParameters.AddNodeMutationProbability = 0; _experiment.NeatGenomeParameters.DeleteConnectionMutationProbability = 0; SocialExperiment.CreateNetwork(FEED_FORWARD_NETWORK_FILE, _experiment.InputCount, 20, _experiment.OutputCount); // Record the changes at each step _experiment.World.Stepped += new social_learning.World.StepEventHandler(World_Stepped); // Read in the teacher genome from file. var agentGenome = _experiment.LoadPopulation(XmlReader.Create(FEED_FORWARD_NETWORK_FILE)); // Create genome decoder. IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder = _experiment.CreateGenomeDecoder(); // Create the evaluator that will handle the simulation _evaluator = new ForagingEvaluator<NeatGenome>(genomeDecoder, _experiment.World, AgentTypes.QLearning) { MaxTimeSteps = 50000000UL, BackpropEpochsPerExample = 1 }; using (TextWriter writer = new StreamWriter(RESULTS_FILE)) writer.WriteLine("Step,Score"); // Start the simulation _evaluator.Evaluate(agentGenome); }
private void evolve_Click(object sender, EventArgs e) { if (btnEvolve.Text == "Stop!") { stopEvolution(); return; } running = true; _experiment = new SocialExperiment(); // Load config XML. XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load(_configFile); _experiment.Initialize("SimpleEvolution", xmlConfig.DocumentElement); _experiment.PlantLayout = _plantLayout; _experiment.World.Changed += new World.ChangedEventHandler(world_Changed); _experiment.World.AgentEaten += new World.AgentEatenHandler(World_AgentEaten); btnEvolve.Text = "Stop!"; // Start the evolution if (_configFile == QLEARNING_CONFIG_FILE) { qLearningThread = new Thread(new ThreadStart(startQLearning)); qLearningThread.Start(); } else { qLearningThread = new Thread(new ThreadStart(startEvolution)); qLearningThread.Start(); } }