protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { //note - the inner crossover is called here and the result is converted to a prins representation //some refactoring should be done here in the future - the crossover operation should be called directly if (parent1.Length != parent2.Length) { return(parent1.Clone() as ZhuEncoding); } InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName; IAtomicOperation op = this.ExecutionContext.CreateOperation( InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope); op.Operator.Execute((IExecutionContext)op, CancellationToken); string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName; if (ExecutionContext.Scope.Variables.ContainsKey(childName)) { Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation; ExecutionContext.Scope.Variables.Remove(childName); return(new ZhuEncoding(permutation, ProblemInstance)); } else { return(null); } }
protected override void Manipulate(IRandom random, ZhuEncoding individual) { InnerManipulatorParameter.ActualValue.PermutationParameter.ActualName = VRPToursParameter.ActualName; IAtomicOperation op = this.ExecutionContext.CreateOperation( InnerManipulatorParameter.ActualValue, this.ExecutionContext.Scope); op.Operator.Execute((IExecutionContext)op, CancellationToken); }
private void Swap(ZhuEncoding individual, int city1, int city2) { int index1 = individual.IndexOf(city1); int index2 = individual.IndexOf(city2); int temp = individual[index1]; individual[index1] = individual[index2]; individual[index2] = temp; }
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { List<int> p1 = new List<int>(parent1); List<int> p2 = new List<int>(parent2); ZhuEncoding child = parent2.Clone() as ZhuEncoding; if (parent1.Length != parent2.Length) return child; int breakPoint = random.Next(child.Length); int i = breakPoint; int predecessor = breakPoint - 1; if (predecessor < 0) predecessor = predecessor + child.Length; int parent1Index = i; int parent2Index = i; while (i != predecessor) { if (i == breakPoint) { child[i] = p1[parent1Index]; p1.Remove(child[i]); if (parent1Index >= p1.Count) parent1Index = 0; p2.Remove(child[i]); if (parent2Index >= p2.Count) parent2Index = 0; } if (ProblemInstance.GetDistance( child[i] + 1, p1[parent1Index] + 1, child) < ProblemInstance.GetDistance( child[i] + 1, p2[parent2Index] + 1, child)) { child[(i + 1) % child.Length] = p1[parent1Index]; } else { child[(i + 1) % child.Length] = p2[parent2Index]; } p1.Remove(child[(i + 1) % child.Length]); if (parent1Index >= p1.Count) parent1Index = 0; p2.Remove(child[(i + 1) % child.Length]); if (parent2Index >= p2.Count) parent2Index = 0; i = (i + 1) % child.Length; } return child; }
public override IOperation InstrumentedApply() { IVRPEncoding solution = VRPToursParameter.ActualValue; if (!(solution is ZhuEncoding)) { VRPToursParameter.ActualValue = ZhuEncoding.ConvertFrom(solution, ProblemInstance); } Manipulate(RandomParameter.ActualValue, VRPToursParameter.ActualValue as ZhuEncoding); return(base.InstrumentedApply()); }
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { parent1 = parent1.Clone() as ZhuEncoding; parent2 = parent2.Clone() as ZhuEncoding; ZhuEncoding child = parent2.Clone() as ZhuEncoding; if (parent1.Length != parent2.Length) { return(child); } int breakPoint = random.Next(child.Length); int i = breakPoint; DoubleArray dueTime = null; if (ProblemInstance is ITimeWindowedProblemInstance) { dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime; } do { if (i == breakPoint) { child[i] = parent1[i]; Swap(parent2, parent2[i], parent1[i]); } else { if ( (dueTime != null) && (dueTime[parent1[i] + 1] < dueTime[parent2[i] + 1])) { child[i] = parent1[i]; Swap(parent2, parent2[i], parent1[i]); } else { child[i] = parent2[i]; Swap(parent1, parent1[i], parent2[i]); } } i = (i + 1) % child.Length; } while (i != breakPoint); return(child); }
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { parent1 = parent1.Clone() as ZhuEncoding; parent2 = parent2.Clone() as ZhuEncoding; ZhuEncoding child = parent2.Clone() as ZhuEncoding; if (parent1.Length != parent2.Length) { return(child); } int breakPoint = random.Next(child.Length); int i = breakPoint; int predecessor = breakPoint - 1; if (predecessor < 0) { predecessor = predecessor + child.Length; } while (i != predecessor) { if (i == breakPoint) { child[i] = parent1[i]; Swap(parent2, parent2[i], parent1[i]); } if (ProblemInstance.GetDistance( child[i] + 1, parent1[(i + 1) % child.Length] + 1, child) < ProblemInstance.GetDistance( child[i] + 1, parent2[(i + 1) % child.Length] + 1, child)) { child[(i + 1) % child.Length] = parent1[(i + 1) % child.Length]; Swap(parent2, parent2[(i + 1) % child.Length], parent1[(i + 1) % child.Length]); } else { child[(i + 1) % child.Length] = parent2[(i + 1) % child.Length]; Swap(parent1, parent1[(i + 1) % child.Length], parent2[(i + 1) % child.Length]); } i = (i + 1) % child.Length; } return(child); }
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { List<int> p1 = new List<int>(parent1); List<int> p2 = new List<int>(parent2); ZhuEncoding child = parent2.Clone() as ZhuEncoding; if (parent1.Length != parent2.Length) return child; int breakPoint = random.Next(child.Length); int i = breakPoint; int parent1Index = i; int parent2Index = i; DoubleArray dueTime = null; if (ProblemInstance is ITimeWindowedProblemInstance) { dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime; } do { if (i == breakPoint) { child[i] = p1[parent1Index]; } else { if (dueTime != null && (dueTime[p1[parent1Index] + 1] < dueTime[p2[parent2Index] + 1])) { child[i] = p1[parent1Index]; } else { child[i] = p2[parent2Index]; } } p1.Remove(child[i]); if (parent1Index >= p1.Count) parent1Index = 0; p2.Remove(child[i]); if (parent2Index >= p2.Count) parent2Index = 0; i = (i + 1) % child.Length; } while (i != breakPoint); return child; }
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { //note - the inner crossover is called here and the result is converted to a prins representation //some refactoring should be done here in the future - the crossover operation should be called directly if (parent1.Length != parent2.Length) return parent1.Clone() as ZhuEncoding; InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName; IAtomicOperation op = this.ExecutionContext.CreateOperation( InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope); op.Operator.Execute((IExecutionContext)op, CancellationToken); string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName; if (ExecutionContext.Scope.Variables.ContainsKey(childName)) { Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation; ExecutionContext.Scope.Variables.Remove(childName); return new ZhuEncoding(permutation, ProblemInstance); } else return null; }
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { parent1 = parent1.Clone() as ZhuEncoding; parent2 = parent2.Clone() as ZhuEncoding; ZhuEncoding child = parent2.Clone() as ZhuEncoding; if (parent1.Length != parent2.Length) return child; int breakPoint = random.Next(child.Length); int i = breakPoint; DoubleArray dueTime = null; if (ProblemInstance is ITimeWindowedProblemInstance) { dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime; } do { if (i == breakPoint) { child[i] = parent1[i]; Swap(parent2, parent2[i], parent1[i]); } else { if ( (dueTime != null) && (dueTime[parent1[i] + 1] < dueTime[parent2[i] + 1])) { child[i] = parent1[i]; Swap(parent2, parent2[i], parent1[i]); } else { child[i] = parent2[i]; Swap(parent1, parent1[i], parent2[i]); } } i = (i + 1) % child.Length; } while (i != breakPoint); return child; }
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { parent1 = parent1.Clone() as ZhuEncoding; parent2 = parent2.Clone() as ZhuEncoding; ZhuEncoding child = parent2.Clone() as ZhuEncoding; if (parent1.Length != parent2.Length) return child; int breakPoint = random.Next(child.Length); int i = breakPoint; int predecessor = breakPoint - 1; if (predecessor < 0) predecessor = predecessor + child.Length; while (i != predecessor) { if (i == breakPoint) { child[i] = parent1[i]; Swap(parent2, parent2[i], parent1[i]); } if (ProblemInstance.GetDistance( child[i] + 1, parent1[(i + 1) % child.Length] + 1, child) < ProblemInstance.GetDistance( child[i] + 1, parent2[(i + 1) % child.Length] + 1, child)) { child[(i + 1) % child.Length] = parent1[(i + 1) % child.Length]; Swap(parent2, parent2[(i + 1) % child.Length], parent1[(i + 1) % child.Length]); } else { child[(i + 1) % child.Length] = parent2[(i + 1) % child.Length]; Swap(parent1, parent1[(i + 1) % child.Length], parent2[(i + 1) % child.Length]); } i = (i + 1) % child.Length; } return child; }
public override IOperation InstrumentedApply() { ItemArray <IVRPEncoding> parents = new ItemArray <IVRPEncoding>(ParentsParameter.ActualValue.Length); for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) { IVRPEncoding solution = ParentsParameter.ActualValue[i]; if (!(solution is ZhuEncoding)) { parents[i] = ZhuEncoding.ConvertFrom(solution, ProblemInstance); } else { parents[i] = solution; } } ParentsParameter.ActualValue = parents; ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as ZhuEncoding, parents[1] as ZhuEncoding); return(base.InstrumentedApply()); }
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { List <int> p1 = new List <int>(parent1); List <int> p2 = new List <int>(parent2); ZhuEncoding child = parent2.Clone() as ZhuEncoding; if (parent1.Length != parent2.Length) { return(child); } int breakPoint = random.Next(child.Length); int i = breakPoint; int predecessor = breakPoint - 1; if (predecessor < 0) { predecessor = predecessor + child.Length; } int parent1Index = i; int parent2Index = i; while (i != predecessor) { if (i == breakPoint) { child[i] = p1[parent1Index]; p1.Remove(child[i]); if (parent1Index >= p1.Count) { parent1Index = 0; } p2.Remove(child[i]); if (parent2Index >= p2.Count) { parent2Index = 0; } } if (ProblemInstance.GetDistance( child[i] + 1, p1[parent1Index] + 1, child) < ProblemInstance.GetDistance( child[i] + 1, p2[parent2Index] + 1, child)) { child[(i + 1) % child.Length] = p1[parent1Index]; } else { child[(i + 1) % child.Length] = p2[parent2Index]; } p1.Remove(child[(i + 1) % child.Length]); if (parent1Index >= p1.Count) { parent1Index = 0; } p2.Remove(child[(i + 1) % child.Length]); if (parent2Index >= p2.Count) { parent2Index = 0; } i = (i + 1) % child.Length; } return(child); }
protected ZhuEncoding(ZhuEncoding original, Cloner cloner) : base(original, cloner) { }
protected abstract ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2);
protected abstract void Manipulate(IRandom random, ZhuEncoding individual);
protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { List <int> p1 = new List <int>(parent1); List <int> p2 = new List <int>(parent2); ZhuEncoding child = parent2.Clone() as ZhuEncoding; if (parent1.Length != parent2.Length) { return(child); } int breakPoint = random.Next(child.Length); int i = breakPoint; int parent1Index = i; int parent2Index = i; DoubleArray dueTime = null; if (ProblemInstance is ITimeWindowedProblemInstance) { dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime; } do { if (i == breakPoint) { child[i] = p1[parent1Index]; } else { if (dueTime != null && (dueTime[p1[parent1Index] + 1] < dueTime[p2[parent2Index] + 1])) { child[i] = p1[parent1Index]; } else { child[i] = p2[parent2Index]; } } p1.Remove(child[i]); if (parent1Index >= p1.Count) { parent1Index = 0; } p2.Remove(child[i]); if (parent2Index >= p2.Count) { parent2Index = 0; } i = (i + 1) % child.Length; } while (i != breakPoint); return(child); }