1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Operators;
|
---|
7 | using HeuristicLab.Random;
|
---|
8 | using HeuristicLab.Evolutionary;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.SimOpt {
|
---|
11 | public class SimOptSinglePointCrossover : CrossoverBase {
|
---|
12 |
|
---|
13 | public override string Description {
|
---|
14 | get { return @"This operator applies a single point crossover on the variables defined"; }
|
---|
15 | }
|
---|
16 |
|
---|
17 | public SimOptSinglePointCrossover()
|
---|
18 | : base() {
|
---|
19 | AddVariableInfo(new VariableInfo("Item", "The item list to be crossed", typeof(ConstrainedItemList), VariableKind.In));
|
---|
20 | }
|
---|
21 |
|
---|
22 | protected override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
|
---|
23 | ICollection<IConstraint> violated;
|
---|
24 | ConstrainedItemList p1 = parent1.GetVariableValue<ConstrainedItemList>("Item", false);
|
---|
25 | ConstrainedItemList p2 = parent2.GetVariableValue<ConstrainedItemList>("Item", false);
|
---|
26 |
|
---|
27 | if (p1.Count != p2.Count) throw new InvalidOperationException("ERROR: the lists do not contain the same number of items");
|
---|
28 |
|
---|
29 | ConstrainedItemList childList = (ConstrainedItemList)p1.Clone();
|
---|
30 | if (childList.Count > 1) {
|
---|
31 | int iter = 0;
|
---|
32 | do {
|
---|
33 | childList.BeginCombinedOperation();
|
---|
34 | int crossPoint = random.Next(1, childList.Count);
|
---|
35 | for (int i = crossPoint; i < childList.Count; i++) {
|
---|
36 | childList.TrySetAt(i, (IItem)p2[i].Clone(), out violated);
|
---|
37 | }
|
---|
38 | } while (!childList.EndCombinedOperation(out violated) && ++iter < 100);
|
---|
39 | if (violated.Count == 0) {
|
---|
40 | child.AddVariable(new Variable(parent1.TranslateName("Item"), childList));
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | } |
---|