Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/SimOptDiscreteMultiCrossover.cs @ 583

Last change on this file since 583 was 583, checked in by abeham, 16 years ago

Adding communication framework to branch 3.1 (ticket #278)

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Operators;
7using HeuristicLab.Random;
8using HeuristicLab.Evolutionary;
9
10namespace 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}
Note: See TracBrowser for help on using the repository browser.