[583] | 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 SimOptDiscreteMultiCrossover : MultiCrossoverBase {
|
---|
| 12 |
|
---|
| 13 | public override string Description {
|
---|
| 14 | get { return @"This operator applies a discrete recombination on the variables defined"; }
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | public SimOptDiscreteMultiCrossover()
|
---|
| 18 | : base() {
|
---|
| 19 | AddVariableInfo(new VariableInfo("Item", "The item list to be recombined", typeof(ConstrainedItemList), VariableKind.In));
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | protected override void Cross(IScope scope, IRandom random, IScope[] parents, IScope child) {
|
---|
| 23 | ICollection<IConstraint> violated;
|
---|
| 24 |
|
---|
| 25 | ConstrainedItemList[] p = new ConstrainedItemList[parents.Length];
|
---|
| 26 | for (int i = 0; i < p.Length; i++) {
|
---|
| 27 | p[i] = parents[i].GetVariableValue<ConstrainedItemList>("Item", false);
|
---|
| 28 | if (i > 0 && p[i].Count != p[i-1].Count) throw new InvalidOperationException("ERROR: the lists do not contain the same number of items");
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | ConstrainedItemList childList = (ConstrainedItemList)p[0].Clone();
|
---|
| 32 | if (childList.Count > 1) {
|
---|
| 33 | int iter = 0;
|
---|
| 34 | do {
|
---|
| 35 | childList.BeginCombinedOperation();
|
---|
| 36 | for (int i = 0; i < childList.Count; i++) {
|
---|
| 37 | int nextParent = random.Next(0, parents.Length);
|
---|
| 38 | if (nextParent > 0) childList.TrySetAt(i, (IItem)p[nextParent].Clone(), out violated);
|
---|
| 39 | }
|
---|
| 40 | } while (!childList.EndCombinedOperation(out violated) && ++iter < 100);
|
---|
| 41 | if (violated.Count == 0) {
|
---|
| 42 | child.AddVariable(new Variable(parents[0].TranslateName("Item"), childList));
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | } |
---|