Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Encodings.ScheduleEncoding/3.3/PermutationWithRepetition/Crossovers/PWRPPXCrossover.cs @ 6406

Last change on this file since 6406 was 6406, checked in by jhelm, 14 years ago

#1329: Applied suggestions from codereview. Added unit-tests. Renamed encoding-project.

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.ScheduleEncoding.PermutationWithRepetition {
30  [Item("PWROrderCrossover", "Represents a crossover operation swapping sequences of the parents to generate offspring.")]
31  [StorableClass]
32  public class PWRPPXCrossover : PWRCrossover {
33    [StorableConstructor]
34    protected PWRPPXCrossover(bool deserializing) : base(deserializing) { }
35    protected PWRPPXCrossover(PWRPPXCrossover original, Cloner cloner)
36      : base(original, cloner) {
37    }
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new PWRPPXCrossover(this, cloner);
40    }
41    public PWRPPXCrossover() : base() { }
42
43    public static PWREncoding Apply(IRandom random, PWREncoding parent1, PWREncoding parent2) {
44      PWREncoding result = new PWREncoding();
45      List<int> p1 = ((IntegerVector)(parent1.PermutationWithRepetition.Clone())).ToList<int>();
46      List<int> p2 = ((IntegerVector)(parent2.PermutationWithRepetition.Clone())).ToList<int>();
47      List<int> child = new List<int>();
48
49      bool[] lookUpTable = new bool[parent1.PermutationWithRepetition.Length];
50      for (int i = 0; i < lookUpTable.Length; i++) {
51        lookUpTable[i] = random.Next(2) == 1;
52      }
53
54      foreach (bool b in lookUpTable) {
55        if (b) {
56          child.Add(p1[0]);
57          p2.Remove(p1[0]);
58          p1.RemoveAt(0);
59        } else {
60          child.Add(p2[0]);
61          p1.Remove(p2[0]);
62          p2.RemoveAt(0);
63        }
64      }
65
66      result.PermutationWithRepetition = new IntegerVector(child.ToArray());
67
68      return result;
69    }
70
71    public override PWREncoding Cross(IRandom random, PWREncoding parent1, PWREncoding parent2) {
72      return Apply(random, parent1, parent2);
73    }
74
75
76  }
77}
Note: See TracBrowser for help on using the repository browser.