protected override void PerformMutation(IGraphChromosome graph, float probability) { if (RandomizationProvider.Current.GetDouble() >= probability) { return; } // No edges to add if (graph.EdgeCount == graph.VertexCount * graph.VertexCount) { return; } // Shuffle var allIDs = graph.Vertices.Select(v => v.ID); uint[] fromVerts = MutationService.Shuffle(allIDs, RandomizationProvider.Current).ToArray(); uint[] toVerts = MutationService.Shuffle(allIDs, RandomizationProvider.Current).ToArray(); // Find the first that does not exist foreach (var v1 in fromVerts) { foreach (var v2 in toVerts) { if (!graph.ContainsEdge(v1, v2)) { graph.AddEdge(v1, v2, graph.CreateNewEdge()); return; } } } }
public void Start_UsingAllConfigurationCombinationsAvailable_AllRun() { var selections = SelectionService.GetSelectionNames(); var crossovers = CrossoverService.GetCrossoverNames(); var mutations = MutationService.GetMutationNames().Where(m => !m.Equals("Flip Bit")); var reinsertions = ReinsertionService.GetReinsertionNames(); var chromosome = new OrderedChromosomeStub(); foreach (var s in selections) { foreach (var c in crossovers) { foreach (var m in mutations) { foreach (var r in reinsertions) { var selection = SelectionService.CreateSelectionByName(s); var crossover = CrossoverService.CreateCrossoverByName(c); var mutation = MutationService.CreateMutationByName(m); var reinsertion = ReinsertionService.CreateReinsertionByName(r); if (crossover.IsOrdered ^ mutation.IsOrdered) { continue; } if (crossover.ParentsNumber > crossover.ChildrenNumber && !reinsertion.CanExpand) { continue; } if (mutation is UniformMutation) { mutation = new UniformMutation(1); } var target = new GeneticAlgorithm( new Population(50, 50, chromosome.Clone()) { GenerationStrategy = new TrackingGenerationStrategy() }, new FitnessStub() { SupportsParallel = false }, selection, crossover, mutation); target.Reinsertion = reinsertion; target.Termination = new GenerationNumberTermination(25); target.CrossoverProbability = reinsertion.CanExpand ? 0.75f : 1f; target.Start(); Assert.AreEqual(25, target.Population.Generations.Count); } } } } }
public void GetMutationTypeByName_InvalidName_Exception() { Assert.Catch <ArgumentException>(() => { MutationService.GetMutationTypeByName("Test"); }, "There is no IMutation implementation with name 'Test'."); }
public void CreateMutationByName_ValidNameButInvalidConstructorArgs_Exception() { Assert.Catch <ArgumentException>(() => { MutationService.CreateMutationByName("Uniform", 1f); }, "A IMutation's implementation with name 'Uniform' was found, but seems the constructor args were invalid."); }
public void GetMutationTypeByName_InvalidName_Exception() { ExceptionAssert.IsThrowing(new ArgumentException("There is no IMutation implementation with name 'Test'.", "name"), () => { MutationService.GetMutationTypeByName("Test"); }); }
public void CreateMutationByName_ValidNameButInvalidConstructorArgs_Exception() { ExceptionAssert.IsThrowing(new ArgumentException("A IMutation's implementation with name 'Uniform' was found, but seems the constructor args were invalid.", "constructorArgs"), () => { MutationService.CreateMutationByName("Uniform", 1f); }); }
public void GetMutationNames_NoArgs_AllAvailableMutationsNames() { var actual = MutationService.GetMutationNames(); Assert.AreEqual(3, actual.Count); Assert.AreEqual("Reverse Sequence (RSM)", actual [0]); Assert.AreEqual("Twors", actual [1]); Assert.AreEqual("Uniform", actual [2]); }
public void GetMutationTypes_NoArgs_AllAvailableMutations() { var actual = MutationService.GetMutationTypes(); Assert.AreEqual(3, actual.Count); Assert.AreEqual(typeof(ReverseSequenceMutation), actual [0]); Assert.AreEqual(typeof(TworsMutation), actual [1]); Assert.AreEqual(typeof(UniformMutation), actual[2]); }
public void GetMutationTypeByName_ValidName_CrossoverTpe() { var actual = MutationService.GetMutationTypeByName("Reverse Sequence (RSM)"); Assert.AreEqual(typeof(ReverseSequenceMutation), actual); actual = MutationService.GetMutationTypeByName("Twors"); Assert.AreEqual(typeof(TworsMutation), actual); actual = MutationService.GetMutationTypeByName("Uniform"); Assert.AreEqual(typeof(UniformMutation), actual); }
public void CreateMutationByName_ValidName_MutationCreated() { IMutation actual = MutationService.CreateMutationByName("Reverse Sequence (RSM)") as ReverseSequenceMutation; Assert.IsNotNull(actual); actual = MutationService.CreateMutationByName("Twors") as TworsMutation; Assert.IsNotNull(actual); actual = MutationService.CreateMutationByName("Uniform", true) as UniformMutation; Assert.IsNotNull(actual); }
public void GetMutationNames_NoArgs_AllAvailableMutationsNames() { var actual = MutationService.GetMutationNames(); Assert.AreEqual(7, actual.Count); Assert.AreEqual("Displacement", actual[0]); Assert.AreEqual("Flip Bit", actual[1]); Assert.AreEqual("Insertion", actual[2]); Assert.AreEqual("Partial Shuffle (PSM)", actual[3]); Assert.AreEqual("Reverse Sequence (RSM)", actual[4]); Assert.AreEqual("Twors", actual[5]); Assert.AreEqual("Uniform", actual[6]); }
public void GetMutationTypes_NoArgs_AllAvailableMutations() { var actual = MutationService.GetMutationTypes(); Assert.AreEqual(7, actual.Count); Assert.AreEqual(typeof(DisplacementMutation), actual[0]); Assert.AreEqual(typeof(FlipBitMutation), actual[1]); Assert.AreEqual(typeof(InsertionMutation), actual[2]); Assert.AreEqual(typeof(PartialShuffleMutation), actual[3]); Assert.AreEqual(typeof(ReverseSequenceMutation), actual[4]); Assert.AreEqual(typeof(TworsMutation), actual[5]); Assert.AreEqual(typeof(UniformMutation), actual[6]); }
public override double Evaluate(IBlackBox net) { if (net.InputCount != 2 || net.OutputCount != 1) { throw new IndexOutOfRangeException(); } var inputs = MutationService.Shuffle(Inputs, RandomizationProvider.Current); double error = 0; foreach (var ins in inputs) { net.Reset(); net.SetInputs(ins); net.Activate(3); double output = net.GetOutput(0); double expected = (ins[0] == ins[1]) ? 0 : 1; double diff = output - expected; error += (diff >= 0) ? diff : -diff; } return(4 - error); }