Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Encodings.SchedulingEncoding/3.3/PermutationWithRepetition/Crossovers/PWRPPXCrossover.cs @ 6294

Last change on this file since 6294 was 6294, checked in by jhelm, 13 years ago

#1329: Did some heavy refactoring after "code-review meeting". Added Encodings.SchedulingEncoding plugin to store encodings used in scheduling problems.

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