Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.VariableVector/3.3/Crossover/SinglePointCrossover.cs @ 11813

Last change on this file since 11813 was 9392, checked in by sforsten, 12 years ago

#1980:

  • several small bug fixes
  • added windowing technique ILAS to GAssist
  • GAssist and XCS work now with real-valued features
  • severely improved the performance of XCS
File size: 4.3 KB
RevLine 
[9194]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
[9226]29namespace HeuristicLab.Encodings.VariableVector {
[9194]30  [Item("SinglePointCrossover", "Description missing")]
31  [StorableClass]
32  public class SinglePointCrossover : VariableVectorCrossover {
33
34    #region Parameter Properties
35    #endregion
36
37    [StorableConstructor]
38    protected SinglePointCrossover(bool deserializing) : base(deserializing) { }
39    protected SinglePointCrossover(SinglePointCrossover original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public SinglePointCrossover() : base() { }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new SinglePointCrossover(this, cloner);
46    }
47
48    public static VariableVector Apply(IRandom random, VariableVector parent1, VariableVector parent2) {
49      var parent1Condition = parent1.Condition;
50      var parent2Condition = parent2.Condition;
51
52      var parent1Action = parent1.Action;
53      var parent2Action = parent2.Action;
54
55      if (parent1.Count != parent2.Count && parent1.VirtualLength != parent2.VirtualLength
[9242]56        && parent1Condition.VariableDictionary.Count != parent2Condition.VariableDictionary.Count && parent1Condition.VirtualLength != parent2Condition.VirtualLength
57        && parent1Action.VariableDictionary.Count != parent2Action.VariableDictionary.Count && parent1Action.VirtualLength != parent2Action.VirtualLength)
[9194]58        throw new ArgumentException("SinglePointCrossover: The parents are of different length.");
59
60      int conditionLength = parent1Condition.VirtualLength;
61      int breakPoint = random.Next(1, conditionLength);
[9242]62      IList<IVariable> newCondition = new List<IVariable>(parent1Condition.VariableDictionary.Count);
[9194]63
[9392]64      var keyEnumerator = parent1Condition.VariableDictionary.Keys.GetEnumerator();
[9194]65
66      int index = 0;
[9242]67      IVariable curVariable = null;
[9194]68      while (index < breakPoint) {
[9242]69        if (!keyEnumerator.MoveNext()) { throw new IndexOutOfRangeException("Enumerator has no next element!"); }
70        curVariable = parent1Condition.VariableDictionary[keyEnumerator.Current];
71        if (index + curVariable.VirtualLength <= breakPoint) {
[9194]72          newCondition.Add(curVariable.GetSetCopy());
73        }
[9242]74        index += curVariable.VirtualLength;
[9194]75      }
76
77      // breakpoint is in between a variable
78      if (index != breakPoint) {
[9242]79        newCondition.Add(parent1Condition.VariableDictionary[keyEnumerator.Current].CrossParentsAtPosition(parent2Condition.VariableDictionary[keyEnumerator.Current], breakPoint - (index - curVariable.VirtualLength)));
[9194]80      }
81
82      while (index < conditionLength) {
83        keyEnumerator.MoveNext();
[9242]84        curVariable = parent2Condition.VariableDictionary[keyEnumerator.Current];
[9194]85        newCondition.Add(curVariable.GetSetCopy());
86        index += curVariable.VirtualLength;
87      }
88
[9242]89      var action = random.Next(2) == 1 ? parent1Action.VariableDictionary.Values.Select(x => x.GetSetCopy()) : parent2Action.VariableDictionary.Values.Select(x => x.GetSetCopy());
[9194]90
91      return new VariableVector(newCondition, action);
92    }
93
94    protected override VariableVector Cross(IRandom random, ItemArray<VariableVector> parents) {
95      if (parents.Length != 2) throw new ArgumentException("ERROR in SinglePointCrossover: The number of parents is not equal to 2");
96      return Apply(random, parents[0], parents[1]);
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.