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_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_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); }
/// <summary> /// Gets a new Decision Vector where elements which pass the conditions have potentially been mutated. /// </summary> /// <param name="decisionVector"><see cref="DecisionVector"/> to mutate.</param> /// <returns>A mutated Decision Vector.</returns> public DecisionVector Operate(DecisionVector decisionVector) { var elementsToMutate = decisionVector.Select(e => conditions.All(c => c(e))); var tempDs = new DecisionSpace(decisionVector.GetDecisionSpace().Where((d, i) => elementsToMutate.ElementAt(i))); var tempDv = DecisionVector.CreateFromArray(tempDs, decisionVector.Where((e, i) => elementsToMutate.ElementAt(i))); var newElements = mutation.Operate(tempDv); var newVector = decisionVector.ToArray(); var item = 0; for (var i = 0; i < newVector.Length; i++) { if (elementsToMutate.ElementAt(i)) { newVector[i] = newElements.ElementAt(item); item++; } } return(DecisionVector.CreateFromArray(decisionVector.GetDecisionSpace(), newVector)); }