public void Operate_OneMutationDesired_ReturnsMutatedDv() { var mutator = new MutationAddRandomNumber( 0.5, 1, 1); var newDv = mutator.Operate(testDv); Assert.NotEqual(testDv, newDv); Assert.Equal(testDv.Count, newDv.Count); Assert.Equal(testDv.Count - 1, newDv.Where((v, i) => v == testDv.ElementAt(i)).Count()); }
public void Operate_OneMutationDesired_ReturnsMutatedDv() { var mutator = new MutationReplaceWithRandomNumber( 1, 1); var newDv = mutator.Operate(testDv); // TODO: This can fail if it generates the same value it already had! Assert.NotEqual(testDv, newDv); Assert.Equal(testDv.Count, newDv.Count); Assert.Equal(testDv.Count - 1, newDv.Where((v, i) => v == testDv.ElementAt(i)).Count()); }
public void Operate_EqualLengthVectors_CreatesOnlyExpectedCrossovers() { var cx = new CrossoverUniform(); for (var i = 0; i < 10; i++) { // Try this a few times to try to cause a mistake. var child = cx.Operate(parent1, parent2); for (var d = 0; d < child.Count; d++) { Assert.True(child.ElementAt(d) == parent1.ElementAt(d) || child.ElementAt(d) == parent2.ElementAt(d)); } } }
/// <summary> /// Gets a new Decision Vector where elements have potentially been mutated. /// Uses <see cref="IVariable"/> to implement the random number generation, so only valid individuals can be created. /// </summary> /// <param name="decisionVector">The existing Decision Vector.</param> /// <returns>A new <see cref="DecisionVector"/>.</returns> /// <exception cref="ArgumentException">Thrown when Decision Vector is zero length.</exception> public DecisionVector Operate(DecisionVector decisionVector) { if (decisionVector.Count == 0) { throw new ArgumentException("Decision Vector must not be empty.", nameof(decisionVector)); } var locationsToMutate = rngManager.GetLocations( decisionVector.Count, maximumNumberOfMutations, true, mutationProbability); var newDv = new object[decisionVector.Count]; var newDs = decisionVector.GetDecisionSpace(); for (var i = 0; i < decisionVector.Count; i++) { // Variable may be mutated multiple times. var numTimesToMutate = locationsToMutate.Count(l => l == i); if (numTimesToMutate == 0) { newDv[i] = decisionVector.ElementAt(i); } else { for (var j = 0; j < numTimesToMutate; j++) { newDv[i] = newDs.ElementAt(i).GetNextRandom(rngManager.Rng); } } } return(DecisionVector.CreateFromArray(newDs, newDv)); }
public void Operate_OneMutationDesired_ReturnsMutatedDv() { var mutator = new MutationAddRandomNumberFromSet( new[] { 2.0 }, 1, 1); var newDv = mutator.Operate(testDv); Assert.NotEqual(testDv, newDv); Assert.Equal(testDv.Count, newDv.Count); var changedValue = newDv.Where((v, i) => (double)v != (double)testDv.ElementAt(i)).Select(d => (double)d); Assert.True(changedValue.Count() == 1); var oldValue = testDv.Where((v, i) => (double)v != (double)newDv.ElementAt(i)).Select(d => (double)d); Assert.True(Math.Abs(oldValue.First() - changedValue.First()) - 2.0 < 1e-6); }
public void Operate_OneMutationDesired_ReturnsMutatedDv() { var mutator = new MutationAddRandomInteger( -1, 1, false, 1, 1); var newDv = mutator.Operate(testDv); Assert.NotEqual(testDv, newDv); Assert.Equal(testDv.Count, newDv.Count); var changedValue = newDv.Where((v, i) => (double)v != (double)testDv.ElementAt(i)).Select(d => (double)d); Assert.True(changedValue.Count() == 1); var oldValue = testDv.Where((v, i) => (double)v != (double)newDv.ElementAt(i)).Select(d => (double)d); Assert.Equal(1, Math.Abs(oldValue.First() - changedValue.First())); }
public void Operate_AllConditionsPassed_OneMutationDesired_ReturnsMutatedDv() { var mutator = new MutationConditional( insideMutation, o => Convert.ToDouble(o) >= double.MinValue); var newDv = mutator.Operate(testDv); Assert.NotEqual(testDv, newDv); Assert.Equal(testDv.Count, newDv.Count); var changedValue = newDv.Where((v, i) => (double)v != (double)testDv.ElementAt(i)).Select(d => (double)d); Assert.True(changedValue.Count() == 1); var oldValue = testDv.Where((v, i) => (double)v != (double)newDv.ElementAt(i)).Select(d => (double)d); Assert.True(Math.Abs(Math.Abs(oldValue.First() - changedValue.First()) - mutation) < 1e-8); }
public void Operate_SinglePoint_UnEqualLengthVectors_CreatesOnlyExpectedCrossovers() { var cx = new CrossoverMultiPoint(1); for (var i = 0; i < 10; i++) { // Try this a few times to try to cause a mistake. var child = cx.Operate(parent1, parent3Longer); Assert.True(child.Count == parent3Longer.Count || child.Count == parent1.Count); for (var d = 0; d < parent1.Count; d++) { Assert.True(child.ElementAt(d) == parent1.ElementAt(d) || child.ElementAt(d) == parent3Longer.ElementAt(d)); } } }
public void Operate_CertainMutation_ReturnsMutatedDv() { var mutator = new MutationRandomSwap(1); var newDv = mutator.Operate(testDv); Assert.NotEqual(testDv, newDv); Assert.Equal(testDv.Count, newDv.Count); Assert.Equal(testDv.Count - 2, newDv.Where((v, i) => v == testDv.ElementAt(i)).Count()); }
public void Operate_EqualLengthVectors_NoBias_ReturnsAverage() { var cx = new CrossoverArithmeticWeighted(); var child = cx.Operate(parent1, parent2); for (var i = 0; i < child.Count; i++) { var avg = 0.5 * ((double)parent1.ElementAt(i) + (double)parent2.ElementAt(i)); Assert.True(Math.Abs((double)child.ElementAt(i) - avg) < 1e-6); } }
public override IEnumerable <double> Evaluate(DecisionVector definition) { // Zitzler, Deb, Thiele, "Comparison of Multiobjective Evolutionary Algorithms: Empirical Results", 2000 var numDims = definition.Count; var f1 = (double)definition.ElementAt(0); var g = 0.0; for (var i = 1; i < numDims; i++) { g += (double)definition.ElementAt(i) / (numDims - 1); } g *= 9; g += 1; var h = 1 - Math.Sqrt(f1 / g) - (f1 / g) * Math.Sin(10 * Math.PI * f1); var f2 = g * h; return(new[] { f1, f2 }); }
public void Operate_VeryHighEta_ReturnsOneOrOtherParent() { var cx = new CrossoverSimulatedBinary(int.MaxValue - 10); var child = cx.Operate(parent1, parent2) .Select(d => (double)d).ToArray(); // Since we've set eta so high, the child should always be very close to one or other parent. Assert.True( child .Select((d, i) => Math.Abs(d - (double)parent1.ElementAt(i))) .All(d => d < 1e-6) || child .Select((d, i) => Math.Abs(d - (double)parent2.ElementAt(i))) .All(d => d < 1e-6)); }
/// <summary> /// Gets a new Decision Vector where continuous elements have potentially been mutated. /// Uses <see cref="IVariable"/> to wrap the added number, ensuring a valid Decision Vector is always created. /// </summary> /// <param name="decisionVector">The existing Decision Vector.</param> /// <returns>A new Decision Vector.</returns> /// <exception cref="ArgumentException">Thrown when Decision Vector is zero length or has no <seealso cref="VariableContinuous"/> elements.</exception> public DecisionVector Operate(DecisionVector decisionVector) { var oldVectorContinuousElements = decisionVector.GetContinuousElements(); if (oldVectorContinuousElements.Count == 0) { throw new ArgumentException("Decision Vector must have continuous elements", nameof(decisionVector)); } var locationsToMutate = rngManager.GetLocations( oldVectorContinuousElements.Count, maximumNumberOfMutations, true, mutationProbability); var newDv = new object[decisionVector.Count]; var newDs = decisionVector.GetDecisionSpace(); var offset = 0; for (var i = 0; i < decisionVector.Count; i++) { newDv[i] = decisionVector.ElementAt(i); // If variable is not continuous, just copy if (newDs.ElementAt(i).GetType() != typeof(VariableContinuous)) { offset++; continue; } // Variable is continuous - it may be mutated multiple times. var numTimesToMutate = locationsToMutate.Count(l => l == (i - offset)); for (var j = 0; j < numTimesToMutate; j++) { var mutationLocation = rngManager.Rng.Next(0, numberSet.Length); newDv[i] = newDs.ElementAt(i).AddOrWrap(newDv[i], numberSet[mutationLocation]); } } return(DecisionVector.CreateFromArray(newDs, newDv)); }