private NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeListEvaluator <NeatGenome> evaluator, int populationSize) { var genomeFactory = new NeatGenomeFactory(_inputCount.Value, _outputCount.Value, _neatGenomeParams); var genomeList = genomeFactory.CreateGenomeList(populationSize, 0); return(CreateEvolutionAlgorithm(evaluator, genomeList)); }
/// <summary> /// Loads a list of genomes from the save file fitting the experiment name and the ExperimentFileType. /// </summary> private static List <NeatGenome> ReadGenomes(INeatExperiment experiment, ExperimentFileType fileType, bool createNewGenesIfNotLoadable = true) { List <NeatGenome> genomeList = null; NeatGenomeFactory genomeFactory = (NeatGenomeFactory)experiment.CreateGenomeFactory(); string filePath = GetSaveFilePath(experiment.Name, fileType); try { using (XmlReader xr = XmlReader.Create(filePath)) { genomeList = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory); if (genomeList != null && genomeList.Count > 0) { Utility.Log("Successfully loaded the genomes of the '" + fileType.ToString() + "' for the experiment '" + experiment.Name + "' from the location:\n" + filePath); } } } catch (Exception e1) { Utility.Log("Error loading genome from file, could not find the file at: " + filePath + "\n" + e1.Message); if (createNewGenesIfNotLoadable) { genomeList = genomeFactory.CreateGenomeList(experiment.DefaultPopulationSize, 0); } } return(genomeList); }
/// <summary> /// Loads a list of NeatGenome(s) from XML that has a containing 'Root' element. The root element /// also contains the activation function library that the network definitions are associated with. /// </summary> /// <param name="xmlNode">The XmlNode to read from. This can be an XmlDocument or XmlElement.</param> /// <param name="nodeFnIds">Indicates if node activation function IDs should be read. If false then /// all node activation function IDs default to 0.</param> /// <param name="genomeFactory">A NeatGenomeFactory object to construct genomes against.</param> public static List<NeatGenome> LoadCompleteGenomeList(XmlNode xmlNode, bool nodeFnIds, NeatGenomeFactory genomeFactory) { using(XmlNodeReader xr = new XmlNodeReader(xmlNode)) { return ReadCompleteGenomeList(xr, nodeFnIds, genomeFactory); } }
/// <summary> /// Load a population of genomes from an XmlReader and returns the genomes in a new list. /// The genome factory for the genomes can be obtained from any one of the genomes. /// </summary> public List <NeatGenome> LoadPopulation(XmlReader xr) { NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(); return(NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory)); // return NeatGenomeUtils.LoadPopulation(xr, false, this.InputCount, this.OutputCount); }
public List <NeatGenome> LoadPopulation(string filename, int amountToLoad) { if (File.Exists(Application.dataPath + HelperConstants.saveDirectory + filename)) { List <NeatGenome> genomes; using (XmlReader xr = XmlReader.Create(Application.dataPath + HelperConstants.saveDirectory + filename)) { NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(); genomes = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory); } var newGenomes = new List <NeatGenome>(amountToLoad); while (newGenomes.Count < amountToLoad) { for (int i = 0; i < genomes.Count; i++) { if (newGenomes.Count >= amountToLoad) { break; } newGenomes.Add(genomes[i]); } } Debug.Assert(newGenomes.Count == amountToLoad); return(newGenomes); } else { Debug.Log("Could not load pop from " + Application.dataPath + HelperConstants.saveDirectory + filename); return(null); } }
private static void Play() { var neatGenomeFactory = new NeatGenomeFactory(NeatConsts.ViewX * NeatConsts.ViewY * NeatConsts.typeIds.Count, 1); var activationScheme = NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(1); var genomeDecoder = new NeatGenomeDecoder(activationScheme); XmlReader xr; while (true) { try { xr = XmlReader.Create($"{NeatConsts.experimentName}/best.xml"); break; } catch (Exception) { } } var genome = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, neatGenomeFactory)[0]; var phenome = genomeDecoder.Decode(genome); using var game = new Game(true); var brain = new BlackBoxBrain(phenome, game); while (!game.hasEnded) { brain.Step(); Thread.Sleep(200); } }
/// <summary> /// The map evaluator constructor. /// </summary> /// <param name="experimentParameters">The experiment definition and control parameters.</param> /// <param name="agentInputNeuronCount">The number of input neurons in the agent neural controller.</param> /// <param name="agentOutputNeuronCount">The number of output neurons in the agent neural controller.</param> public MapEvaluator(ExperimentParameters experimentParameters, int agentInputNeuronCount, int agentOutputNeuronCount) { // Create the NEAT genome (agent) decoder - acyclic activation is always used _agentDecoder = new NeatGenomeDecoder(CreateActivationScheme(experimentParameters.ActivationScheme, experimentParameters.ActivationIters, experimentParameters.ActivationDeltaThreshold)); // Create the maze decoder _mazeDecoder = new MazeDecoder(experimentParameters.MazeScaleMultiplier); // Initialize evaluation units EvaluationUnits = new List <MazeNavigatorEvaluationUnit>(); // Create maze factory with default dimensions (NEAT factory will be set later based on structure of first // genome encountered) _mazeGenomeFactory = new MazeGenomeFactory(experimentParameters.MazeHeight, experimentParameters.MazeWidth, experimentParameters.MazeQuadrantHeight, experimentParameters.MazeQuadrantWidth); _neatGenomeFactory = new NeatGenomeFactory(agentInputNeuronCount, agentOutputNeuronCount); // Set experiment parameters _experimentParameters = experimentParameters; // Create new agent ID list and maze ID/structure map _agentGenomeIds = new List <int>(); _mazeIdStructureMap = new Dictionary <int, MazeStructure>(); }
NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(bool load) { // Create a genome2 factory with our neat genome2 parameters object and the appropriate number of input and output neuron genes. var genomeFactory = new NeatGenomeFactory(TetrisEvaluator.NumInputs, TetrisEvaluator.NumOutputs); // Create an initial population of randomly generated genomes. List <NeatGenome> genomeList = null; if (load) { try { using (var reader = XmlReader.Create("SavedProgress.xml")) genomeList = NeatGenomeXmlIO.ReadCompleteGenomeList(reader, true, genomeFactory); Console.WriteLine("Loaded network!"); } catch { load = false; } } if (!load) { genomeList = genomeFactory.CreateGenomeList(150, 0); } var parallelOpts = new ParallelOptions() { MaxDegreeOfParallelism = -1 }; // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference. var distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0); var speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, parallelOpts); // Create the evolution algorithm. var ea = new NeatEvolutionAlgorithm <NeatGenome>(new NeatEvolutionAlgorithmParameters { SpecieCount = 10 }, speciationStrategy, new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, 50)); // Create genome2 decoder. var genomeDecoder = new NeatGenomeDecoder(NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(2)); // Create a genome2 list evaluator. This packages up the genome2 decoder with the genome2 evaluator. IGenomeListEvaluator <NeatGenome> genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, tetrisEvaluator, parallelOpts); // Wrap the list evaluator in a 'selective' evaulator that will only evaluate new genomes. That is, we skip re-evaluating any genomes // that were in the population in previous generations (elite genomes). This is determiend by examining each genome2's evaluation info object. //if (!EvaluateParents) //genomeListEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(genomeListEvaluator, SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly()); ea.UpdateEvent += Ea_UpdateEvent; // Initialize the evolution algorithm. ea.Initialize(genomeListEvaluator, genomeFactory, genomeList); // Finished. Return the evolution algorithm return(ea); }
public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(BattleEvaluator <NeatGenome> evaluator, int populationSize) { Debug.Assert(populationSize > 5); _eaParams.SpecieCount = populationSize / 5; var genomeFactory = new NeatGenomeFactory(InputCount, OutputCount, _neatGenomeParams); var genomeList = genomeFactory.CreateGenomeList(populationSize, 0); return(CreateEvolutionAlgorithm(evaluator, genomeList)); }
private static IBlackBox BrainFromFile(string brainFileName, int inputCount, int outputCount) { // TODO: Refactor var genomeDecoder = new NeatGenomeDecoder(NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(1)); var genomeFactory = new NeatGenomeFactory(inputCount, outputCount, new NeatGenomeParameters()); NeatGenome genome; using (var xr = XmlReader.Create(brainFileName)) { genome = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory)[0]; } return genomeDecoder.Decode(genome); }
private NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeListEvaluator <NeatGenome> evaluator, List <NeatGenome> list) { IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0); ISpeciationStrategy <NeatGenome> speciationStrategy = new KMeansClusteringStrategy <NeatGenome>(distanceMetric); IComplexityRegulationStrategy complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, _complexityThreshold); NeatEvolutionAlgorithm <NeatGenome> neatEvolutionAlgorithm = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy); var genomeFactory = new NeatGenomeFactory(_inputCount.Value, _outputCount.Value, _neatGenomeParams); neatEvolutionAlgorithm.Initialize(evaluator, genomeFactory, list); return(neatEvolutionAlgorithm); }
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); }
public void ConectionBufferTest() { NeatGenomeFactory genomeFactory = new NeatGenomeFactory(3, 1); for (uint i = 0; i < 200000; i++) { ConnectionEndpointsStruct endpointsStruct = new ConnectionEndpointsStruct(i, i+1); AddedNeuronGeneStruct addedNeuronGeneStruct = new AddedNeuronGeneStruct(genomeFactory.InnovationIdGenerator); genomeFactory.AddedConnectionBuffer.Enqueue(endpointsStruct, i); genomeFactory.AddedNeuronBuffer.Enqueue(i, addedNeuronGeneStruct); } //Assert.Fail(); }
private void Start() { int populationSize = 100; NetworkActivationScheme activationScheme = NetworkActivationScheme.CreateAcyclicScheme(); NeatGenomeParameters neatParams = new NeatGenomeParameters(); neatParams.ActivationFn = TanH.__DefaultInstance; neatParams.FeedforwardOnly = activationScheme.AcyclicNetwork; IGenomeDecoder <NeatGenome, IBlackBox> neatDecoder = new NeatGenomeDecoder(activationScheme); IGenomeFactory <NeatGenome> neatFactory = new NeatGenomeFactory(3, 3, neatParams); List <NeatGenome> genomeList = neatFactory.CreateGenomeList(populationSize, 0); ArenaEvaluator evaluator = GetComponent <ArenaEvaluator>(); evaluator.Initialize(neatDecoder); IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0); // Evolution parameters NeatEvolutionAlgorithmParameters neatEvolutionParams = new NeatEvolutionAlgorithmParameters(); neatEvolutionParams.SpecieCount = 10; ISpeciationStrategy <NeatGenome> speciationStrategy = new KMeansClusteringStrategy <NeatGenome>(distanceMetric); IComplexityRegulationStrategy complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, 10); NeatEvolutionAlgorithm <NeatGenome> ea = GetComponent <UnityEvolutionAlgorithm>(); ea.Construct(neatEvolutionParams, speciationStrategy, complexityRegulationStrategy, new NullGenomeListEvaluator <NeatGenome, IBlackBox>()); ea.Initialize(evaluator, neatFactory, genomeList); ea.UpdateScheme = new UpdateScheme(1); // This needs to be set AFTER Initialize is called ea.PausedEvent += (sender, e) => { //ea.StartContinue(); }; ea.GenerationEvent += (sender, gen) => { Debug.Log($"Generation {gen}"); Debug.Log($"Highest fitness: {ea.CurrentChampGenome.EvaluationInfo.Fitness}"); nnMesh.GenerateMesh(ea.CurrentChampGenome); ea.RequestPause(); StartCoroutine(PauseRoutine(ea)); }; ea.StartContinue(); }
private void InitTeam(double teamDisc, NeatEvolutionAlgorithm <NeatGenome> team, NeatEvolutionAlgorithm <NeatGenome> opponent) { NeatGenomeParameters genomeParams = new NeatGenomeParameters(); genomeParams.FeedforwardOnly = true; //genomeParams.InitialInterconnectionsProportion = 1.0; IGenomeFactory <NeatGenome> genomeFactory = new NeatGenomeFactory(7 * 6, 7, genomeParams); IGenomeDecoder <NeatGenome, IBlackBox> decoder = new NeatGenomeDecoder(SharpNeat.Decoders.NetworkActivationScheme.CreateAcyclicScheme()); IGenomeListEvaluator <NeatGenome> evaluator = new CacheFirstParallelGenomeListEvaluator <NeatGenome, IBlackBox>( decoder, new ConnectFourEvaluator(teamDisc, opponent, decoder)); team.UpdateEvent += ((sender, eventArgs) => OnUpdate()); team.Initialize(evaluator, genomeFactory, populationSize); }
/// <summary> /// Reads in seed NEAT genomes used to bootstrap MCC experiments. /// </summary> /// <param name="seedNeatPath"> /// The path of the single NEAT genome or a directory containing multiple XML genome definitions. /// </param> /// <param name="neatGenomeFactory">The NEAT genome factory to assign to each genome.</param> /// <returns>The list of seed NEAT genomes.</returns> public static List <NeatGenome> ReadSeedNeatGenomes(string seedNeatPath, NeatGenomeFactory neatGenomeFactory) { var neatGenomes = new List <NeatGenome>(); // Get the NEAT genome files in the given path var neatGenomeFiles = GetGenomeFiles(seedNeatPath); // Read in all NEAT genomes and add them to the list foreach (var neatGenomeFile in neatGenomeFiles) { using (var xr = XmlReader.Create(neatGenomeFile)) { // Read in the NEAT genomes var curNeatGenomes = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, neatGenomeFactory); // Add the genomes to the overall genome list neatGenomes.AddRange(curNeatGenomes); } } return(neatGenomes); }
public IBlackBox BestPhenome() { var genomeFactory = new NeatGenomeFactory(this.InputCount, this.OutputCount, new NeatGenomeParameters()); //var genomeFactory = new SharpNeat.Genomes.HyperNeat.CppnGenomeFactory( // this.InputCount, // this.OutputCount, // SharpNeat.Network.DefaultActivationFunctionLibrary.CreateLibraryCppn(), // new NeatGenomeParameters()); List <NeatGenome> genomeList; using (XmlReader xr = XmlReader.Create(this.xmlPopulationFile)) { genomeList = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory); } //var decoder = new NeatGenomeDecoder(NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(1)); var decoder = new NeatGenomeDecoder(NetworkActivationScheme.CreateAcyclicScheme()); return(decoder.Decode(genomeList[0])); }
/// <inheritdoc /> /// <summary> /// Creates the MCC algorithm container using the given agent and maze population sizes. /// </summary> /// <param name="populationSize1">The agent population size.</param> /// <param name="populationSize2">The maze population size.</param> /// <returns>The instantiated MCC algorithm container.</returns> public override IMCCAlgorithmContainer <NeatGenome, MazeGenome> CreateMCCAlgorithmContainer( int populationSize1, int populationSize2) { // Create a genome factory for the NEAT genomes IGenomeFactory <NeatGenome> neatGenomeFactory = new NeatGenomeFactory(AnnInputCount, AnnOutputCount, NeatGenomeParameters); // Create a genome factory for the maze genomes IGenomeFactory <MazeGenome> mazeGenomeFactory = new MazeGenomeFactory(MazeGenomeParameters, MazeHeight, MazeWidth, MazeQuadrantHeight, MazeQuadrantWidth); // Create an initial population of maze navigators var neatGenomeList = neatGenomeFactory.CreateGenomeList(populationSize1, 0); // Create an initial population of mazes // NOTE: the population is set to 1 here because we're just starting with a single, completely open maze space var mazeGenomeList = mazeGenomeFactory.CreateGenomeList(populationSize2, 0); // Create the evolution algorithm container return(CreateMCCAlgorithmContainer(neatGenomeFactory, mazeGenomeFactory, neatGenomeList, mazeGenomeList, false)); }
private static void Train() { File.WriteAllText($"{NeatConsts.experimentName}/fitness.csv", "generation,firness\n"); var neatGenomeFactory = new NeatGenomeFactory(NeatConsts.ViewX * NeatConsts.ViewY * NeatConsts.typeIds.Count, 1); var genomeList = neatGenomeFactory.CreateGenomeList(NeatConsts.SpecCount, 0); var eaParams = new NeatEvolutionAlgorithmParameters { SpecieCount = NeatConsts.SpecCount }; //var distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0); var distanceMetric = new ManhattanDistanceMetric(); var parallelOptions = new ParallelOptions(); var speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, parallelOptions); //var speciationStrategy = new KMeansClusteringStrategy<NeatGenome>(distanceMetric); //var speciationStrategy = new RandomClusteringStrategy<NeatGenome>(); var complexityRegulationStrategy = new NullComplexityRegulationStrategy(); //var complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Relative, 0.50); var ea = new NeatEvolutionAlgorithm <NeatGenome>(eaParams, speciationStrategy, complexityRegulationStrategy); var activationScheme = NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(1); var genomeDecoder = new NeatGenomeDecoder(activationScheme); var phenomeEvaluator = new GameEvaluator(); var genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, phenomeEvaluator, parallelOptions); ea.Initialize(genomeListEvaluator, neatGenomeFactory, genomeList); ea.UpdateScheme = new UpdateScheme(NeatConsts.LogRate); ea.StartContinue(); ea.UpdateEvent += Ea_UpdateEvent; while (ea.RunState != RunState.Paused) { } ea.Stop(); }
private static List <NeatGenome> LoadPopulation_static(XmlReader xr, ExperimentInitArgs_Activation activation, HyperNEAT_Args hyperneatArgs) { NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(hyperneatArgs, activation); return(NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory)); }
/// <summary> /// Creates the coevolution algorithm container using the given agent and maze population sizes. /// </summary> /// <param name="populationSize1">The agent population size.</param> /// <param name="populationSize2">The maze population size.</param> /// <returns>The instantiated coevolution algorithm container.</returns> public override ICoevolutionAlgorithmContainer<NeatGenome, MazeGenome> CreateCoevolutionAlgorithmContainer( int populationSize1, int populationSize2) { // Create a genome factory for the NEAT genomes IGenomeFactory<NeatGenome> neatGenomeFactory = new NeatGenomeFactory(AnnInputCount, AnnOutputCount, NeatGenomeParameters); // Create a genome factory for the maze genomes IGenomeFactory<MazeGenome> mazeGenomeFactory = new MazeGenomeFactory(MazeGenomeParameters, _mazeHeight, _mazeWidth); // Create an initial population of maze navigators List<NeatGenome> neatGenomeList = neatGenomeFactory.CreateGenomeList(populationSize1, 0); // Create an initial population of mazes // NOTE: the population is set to 1 here because we're just starting with a single, completely open maze space List<MazeGenome> mazeGenomeList = mazeGenomeFactory.CreateGenomeList(populationSize2, 0); // Create the evolution algorithm container return CreateCoevolutionAlgorithmContainer(neatGenomeFactory, mazeGenomeFactory, neatGenomeList, mazeGenomeList); }
public void VerifyBootstrappedStateTest() { const string parentDirectory = "F:/User Data/Jonathan/Documents/school/Jonathan/Graduate/PhD/Development/C# NEAT/SharpNoveltyNeat/SharpNeatConsole/bin/Debug/"; const string agentGenomeFile = "ViableSeedGenomes.xml"; const string baseBitmapFilename = "AgentTrajectory"; const int mazeHeight = 20; const int mazeWidth = 20; const int scaleMultiplier = 16; const int maxTimesteps = 400; const int minSuccessDistance = 5; // Setup stuff for the navigators List<NeatGenome> agentGenomes; NeatGenomeDecoder agentGenomeDecoder = new NeatGenomeDecoder(NetworkActivationScheme.CreateAcyclicScheme()); NeatGenomeFactory agentGenomeFactory = new NeatGenomeFactory(10, 2); // Create new minimal maze (no barriers) MazeStructure mazeStructure = new MazeDecoder(mazeHeight, mazeWidth, scaleMultiplier).Decode( new MazeGenomeFactory(null, null, null).CreateGenome(0)); // Create behavior characterization factory IBehaviorCharacterizationFactory behaviorCharacterizationFactory = new TrajectoryBehaviorCharacterizationFactory(null); // Create evaluator MazeNavigatorMCSEvaluator mazeNavigatorEvaluator = new MazeNavigatorMCSEvaluator(maxTimesteps, minSuccessDistance, behaviorCharacterizationFactory, 1); // Set maze within evaluator mazeNavigatorEvaluator.UpdateEvaluatorPhenotypes(new List<MazeStructure> {mazeStructure}); // Read in agents using (XmlReader xr = XmlReader.Create(parentDirectory + agentGenomeFile)) { agentGenomes = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, agentGenomeFactory); } // Decode agent genomes to phenotype and run simulation for (int i = 0; i < agentGenomes.Count; i++) { // Decode navigator genome IBlackBox agentPhenome = agentGenomeDecoder.Decode(agentGenomes[i]); // Run simulation BehaviorInfo behaviorInfo = mazeNavigatorEvaluator.Evaluate(agentPhenome, 0, false, null, null); // Print the navigator trajectory through the maze DomainTestUtils.PrintMazeAndTrajectory(mazeStructure, behaviorInfo.Behaviors, string.Format("{0}_{1}.bmp", baseBitmapFilename, i)); } }
private void Reset2_Click(object sender, RoutedEventArgs e) { try { // If not hyperneat, just replace with a new net // If hyperneat, compare other args and retain genomes if the only change is input/output resolution string prevGenomeXML = null; if (chkHyperNEAT.IsChecked.Value && _experiment != null && _ea != null) { //_ea.Stop(); // currently, Stop just calls RequestPause, so don't use it. There needs to be a dispose that removes the underlying thread _ea.RequestPauseAndWait(); prevGenomeXML = ExperimentNEATBase.SavePopulation(_ea.GenomeList); } RemoveExistingExperiment(); // My stuff #region harness args _harnessArgs = new HarnessArgs( trkMapSize.Value, trkVisionSize.Value, trkOutputSize.Value, trkInputPixels.Value.ToInt_Round(), trkOutputPixels.Value.ToInt_Round(), trkDelayBetweenInstances.Value); #endregion #region eval args if (chkRandomStartingConditions.IsChecked.Value) { _evalArgs = new EvaluatorArgs( trkEvalIterations.Value.ToInt_Round(), trkDelay.Value, trkEvalElapsedTime.Value, trkMaxSpeed.Value, chkBounceOffWalls.IsChecked.Value, new[] { (TrackedItemType)cboTrackedItemType.SelectedValue }, trkNewItemDuration.Value, trkNewItemErrorMultiplier.Value, (ScoreLeftRightBias)cboErrorBias.SelectedValue); } else { Point position = Math3D.GetRandomVector(_harnessArgs.MapSize / 2).ToPoint2D(); Vector velocity = Math3D.GetRandomVector_Circular(trkMaxSpeed.Value).ToVector2D(); // Don't let the velocity be in the same quadrant as the position (otherwise, you could have something spawn next to a wall, heading // toward the wall). These if statements force it to cross the x,y axiis if (Math.Sign(position.X) == Math.Sign(velocity.X)) { velocity = new Vector(-velocity.X, velocity.Y); } if (Math.Sign(position.Y) == Math.Sign(velocity.Y)) { velocity = new Vector(velocity.X, -velocity.Y); } _evalArgs = new EvaluatorArgs( trkEvalIterations.Value.ToInt_Round(), trkDelay.Value, trkEvalElapsedTime.Value, new[] { Tuple.Create((TrackedItemType)cboTrackedItemType.SelectedValue, position, velocity, chkBounceOffWalls.IsChecked.Value) }, trkNewItemDuration.Value, trkNewItemErrorMultiplier.Value, (ScoreLeftRightBias)cboErrorBias.SelectedValue); } #endregion // SharpNEAT #region experiment args _experimentArgs = new ExperimentInitArgs() { Description = "Input is a pixel array. Output is a pixel array. The NN needs to watch the object and anticipate where it will be at some fixed time in the future", InputCount = _harnessArgs.InputSizeXY * _harnessArgs.InputSizeXY, OutputCount = _harnessArgs.OutputSizeXY * _harnessArgs.OutputSizeXY, IsHyperNEAT = chkHyperNEAT.IsChecked.Value, PopulationSize = trkPopulationSize.Value.ToInt_Round(), SpeciesCount = trkSpeciesCount.Value.ToInt_Round(), Activation = new ExperimentInitArgs_Activation_CyclicFixedTimesteps() { TimestepsPerActivation = trkTimestepsPerActivation.Value.ToInt_Round(), FastFlag = true }, Complexity_RegulationStrategy = ComplexityCeilingType.Absolute, Complexity_Threshold = trkComplexityThreshold.Value.ToInt_Round(), }; #endregion #region hyperneat args _hyperneatArgs = null; if (chkHyperNEAT.IsChecked.Value) { // Use two square sheets var hyperPoints = HyperNEAT_Args.GetSquareSheets(trkVisionSize.Value, trkOutputSize.Value, _harnessArgs.InputSizeXY, _harnessArgs.OutputSizeXY); _hyperneatArgs = new HyperNEAT_Args() { InputPositions = hyperPoints.inputs, OutputPositions = hyperPoints.outputs, }; } #endregion #region create harness _harness = new TrackedItemHarness(_harnessArgs); _harness.ItemRemoved += (s1, e1) => { _harness.SetItem(AntPos_Evaluator.GetNewItem(_harness, _evalArgs)); }; _harness.SetItem(AntPos_Evaluator.GetNewItem(_harness, _evalArgs)); #endregion #region create evaluator AntPos_Evaluator evaluator = new AntPos_Evaluator(_harnessArgs, _evalArgs); //FitnessInfo score = evaluator.Evaluate(new RandomBlackBoxNetwork(_harness.InputSizeXY * _harness.InputSizeXY, _harness.OutputSizeXY * _harness.OutputSizeXY, true)); // this is a good place to unit test the evaluator #endregion #region create experiment _experiment = new ExperimentNEATBase(); _experiment.Initialize("anticipate position", _experimentArgs, evaluator); #endregion #region create evolution algorithm if (prevGenomeXML == null) { if (chkHyperNEAT.IsChecked.Value) { _ea = _experiment.CreateEvolutionAlgorithm(_hyperneatArgs); } else { _ea = _experiment.CreateEvolutionAlgorithm(); } } else { List <NeatGenome> genomeList; if (_hyperneatArgs == null) { genomeList = ExperimentNEATBase.LoadPopulation(prevGenomeXML, _experimentArgs.Activation, _experimentArgs.InputCount, _experimentArgs.OutputCount); } else { genomeList = ExperimentNEATBase.LoadPopulation(prevGenomeXML, _experimentArgs.Activation, _hyperneatArgs); } // The factory is the same for all items, so just grab the first one NeatGenomeFactory genomeFactory = genomeList[0].GenomeFactory; _ea = _experiment.CreateEvolutionAlgorithm(genomeFactory, genomeList, _hyperneatArgs); } _ea.UpdateEvent += EA_UpdateEvent; _ea.PausedEvent += EA_PausedEvent; #endregion ShowBestGenome(); // this ensures the neural viewer is created _winningBrainTime = DateTime.UtcNow - TimeSpan.FromDays(1); // put it way in the past so the first tick will request a new winner _winningBrain = null; //_winningBrain = new RandomBlackBoxNetwork(_harness.InputSizeXY * _harness.InputSizeXY, _harness.OutputSizeXY * _harness.OutputSizeXY, true); _tickCounter = _evalArgs.TotalNumberEvaluations * 2; // force the timer to get the winning NN right away (otherwise it will do a round before refreshing) _ea.StartContinue(); // this needs to be done last } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
void UpdateGenomeAndFactory() { genome = evolutionAlgorithm.GenomeList[0]; factory = genome.GenomeFactory; }
private void GetGenomeFactory() { _GenomeFactory = new NeatGenomeFactory(_InputCount, _OutputCount, _GenomeParameters); }
public static NeatGenome ReadNeatGenome(string serializedGenomePath, int inputCount, int outputCount, IActivationFunctionLibrary actFuncLib) { // Create a new genome factory NeatGenomeFactory genomeFactory = new NeatGenomeFactory(inputCount, outputCount, actFuncLib); // Create a reader for the serialized genome XmlReader reader = XmlReader.Create(serializedGenomePath); // Create XML document and give it the reader reference XmlDocument document = new XmlDocument(); document.Load(reader); // Traverse down to the network definition XmlNodeList nodeList = document.GetElementsByTagName("Root"); // Read in the genome NeatGenome genome = NeatGenomeXmlIO.LoadCompleteGenomeList(nodeList[0], true, genomeFactory)[0]; return genome; }
private static List <NeatGenome> LoadPopulation_static(XmlReader xr, ExperimentInitArgs_Activation activation, int inputCount, int outputCount) { NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(inputCount, outputCount, activation); return(NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory)); }
/// <summary> /// The map evaluator constructor. /// </summary> /// <param name="experimentParameters">The experiment definition and control parameters.</param> /// <param name="agentInputNeuronCount">The number of input neurons in the agent neural controller.</param> /// <param name="agentOutputNeuronCount">The number of output neurons in the agent neural controller.</param> public MapEvaluator(ExperimentParameters experimentParameters, int agentInputNeuronCount, int agentOutputNeuronCount) { // Create the NEAT genome (agent) decoder - acyclic activation is always used _agentDecoder = new NeatGenomeDecoder(NetworkActivationScheme.CreateAcyclicScheme()); // Create the maze decoder _mazeDecoder = new MazeDecoder(experimentParameters.MazeHeight, experimentParameters.MazeWidth, experimentParameters.MazeScaleMultiplier); // Initialize evaluation units EvaluationUnits = new List<MazeNavigatorEvaluationUnit>(); // Create default maze factory (NEAT factory will be set later based on structure of first genome encountered) _mazeGenomeFactory = new MazeGenomeFactory(); _neatGenomeFactory = new NeatGenomeFactory(agentInputNeuronCount, agentOutputNeuronCount); // Set experiment parameters _experimentParameters = experimentParameters; // Create new agent ID list and maze ID/structure map _agentGenomeIds = new List<int>(); _mazeIdStructureMap = new Dictionary<int, MazeStructure>(); }
public List <NeatGenome> LoadPopulation(XmlReader xr) { NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(); return(NeatGenomeXmlIO.ReadCompleteGenomeList(xr, true, genomeFactory));//true for hyper neat false for reg }
/// <summary> /// Reads a single genome from a population from the given XML file. This is typically used in cases where a /// population file is being read in, but it only contains one genome. /// </summary> /// <param name="xr"></param> /// Reference to the XmlReader. /// <param name="nodeFnIds"> /// Indicates if node activation function IDs should be read. They are required for HyperNEAT /// genomes but not for NEAT. /// </param> /// <param name="genomeFactory">A NeatGenomeFactory object to construct genomes against.</param> /// <returns>Instantiated NEAT genome.</returns> public static NeatGenome ReadSingleGenomeFromRoot(XmlReader xr, bool nodeFnIds, NeatGenomeFactory genomeFactory) { return ReadCompleteGenomeList(xr, nodeFnIds, genomeFactory)[0]; }
public List <NeatGenome> LoadPopulation(XmlReader xr) { NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(); return(NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory)); }
/// <summary> /// Reads a list of NeatGenome(s) from XML that has a containing 'Root' element. The root /// element also contains the activation function library that the genomes are associated with. /// </summary> /// <param name="xr">The XmlReader to read from.</param> /// <param name="nodeFnIds">Indicates if node activation function IDs should be read. If false then /// all node activation function IDs default to 0.</param> /// <param name="genomeFactory">A NeatGenomeFactory object to construct genomes against.</param> public static List<NeatGenome> ReadCompleteGenomeList(XmlReader xr, bool nodeFnIds, NeatGenomeFactory genomeFactory) { // Find <Root>. XmlIoUtils.MoveToElement(xr, false, __ElemRoot); // Read IActivationFunctionLibrary. This library is not used, it is compared against the one already present in the // genome factory to confirm that the loaded genomes are compatible with the genome factory. XmlIoUtils.MoveToElement(xr, true, __ElemActivationFunctions); IActivationFunctionLibrary activationFnLib = NetworkXmlIO.ReadActivationFunctionLibrary(xr); XmlIoUtils.MoveToElement(xr, false, __ElemNetworks); // Read genomes. List<NeatGenome> genomeList = new List<NeatGenome>(); using(XmlReader xrSubtree = xr.ReadSubtree()) { // Re-scan for the root <Networks> element. XmlIoUtils.MoveToElement(xrSubtree, false); // Move to first Network elem. XmlIoUtils.MoveToElement(xrSubtree, true, __ElemNetwork); // Read Network elements. do { NeatGenome genome = ReadGenome(xrSubtree, nodeFnIds); genomeList.Add(genome); } while(xrSubtree.ReadToNextSibling(__ElemNetwork)); } // Check for empty list. if(genomeList.Count == 0) { return genomeList; } // Get the number of inputs and outputs expected by the genome factory. int inputCount = genomeFactory.InputNeuronCount; int outputCount = genomeFactory.OutputNeuronCount; // Check all genomes have the same number of inputs & outputs. // Also track the highest genomeID and innovation ID values; we need these to construct a new genome factory. uint maxGenomeId = 0; uint maxInnovationId = 0; foreach(NeatGenome genome in genomeList) { // Check number of inputs/outputs. if(genome.InputNeuronCount != inputCount || genome.OutputNeuronCount != outputCount) { throw new SharpNeatException(string.Format("Genome with wrong number of inputs and/or outputs, expected [{0}][{1}] got [{2}][{3}]", inputCount, outputCount, genome.InputNeuronCount, genome.OutputNeuronCount)); } // Track max IDs. maxGenomeId = Math.Max(maxGenomeId, genome.Id); // Node and connection innovation IDs are in the same ID space. foreach(NeuronGene nGene in genome.NeuronGeneList) { maxInnovationId = Math.Max(maxInnovationId, nGene.InnovationId); } // Register connection IDs. foreach(ConnectionGene cGene in genome.ConnectionGeneList) { maxInnovationId = Math.Max(maxInnovationId, cGene.InnovationId); } } // Check that activation functions in XML match that in the genome factory. IList<ActivationFunctionInfo> loadedActivationFnList = activationFnLib.GetFunctionList(); IList<ActivationFunctionInfo> factoryActivationFnList = genomeFactory.ActivationFnLibrary.GetFunctionList(); if(loadedActivationFnList.Count != factoryActivationFnList.Count) { throw new SharpNeatException("The activation function library loaded from XML does not match the genome factory's activation function library."); } for(int i=0; i<factoryActivationFnList.Count; i++) { if( (loadedActivationFnList[i].Id != factoryActivationFnList[i].Id) || (loadedActivationFnList[i].ActivationFunction.FunctionId != factoryActivationFnList[i].ActivationFunction.FunctionId)) { throw new SharpNeatException("The activation function library loaded from XML does not match the genome factory's activation function library."); } } // Initialise the genome factory's genome and innovation ID generators. genomeFactory.GenomeIdGenerator.Reset(Math.Max(genomeFactory.GenomeIdGenerator.Peek, maxGenomeId+1)); genomeFactory.InnovationIdGenerator.Reset(Math.Max(genomeFactory.InnovationIdGenerator.Peek, maxInnovationId+1)); // Retrospecitively assign the genome factory to the genomes. This is how we overcome the genome/genomeFactory // chicken and egg problem. foreach(NeatGenome genome in genomeList) { genome.GenomeFactory = genomeFactory; } return genomeList; }