Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Encodings.ScheduleEncoding/3.3/PermutationWithRepetition/Crossovers/PWRPPXCrossover.cs @ 15866

Last change on this file since 15866 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
28
29namespace HeuristicLab.Encodings.ScheduleEncoding.PermutationWithRepetition {
30  [Item("PWRPPXCrossover", "Represents a crossover operation swapping sequences of the parents to generate offspring.")]
31  [StorableType("03fff36b-d5de-4982-a4ad-dff2febe3ca5")]
32  public class PWRPPXCrossover : PWRCrossover {
33
34    [StorableConstructor]
35    protected PWRPPXCrossover(StorableConstructorFlag deserializing) : base(deserializing) { }
36    protected PWRPPXCrossover(PWRPPXCrossover original, Cloner cloner) : base(original, cloner) { }
37    public PWRPPXCrossover() : base() { }
38
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new PWRPPXCrossover(this, cloner);
41    }
42
43    public static PWREncoding Apply(IRandom random, PWREncoding parent1, PWREncoding parent2) {
44      var result = new PWREncoding();
45      var p1 = ((IntegerVector)(parent1.PermutationWithRepetition.Clone())).ToList();
46      var p2 = ((IntegerVector)(parent2.PermutationWithRepetition.Clone())).ToList();
47      var child = new List<int>();
48
49      var 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}
Note: See TracBrowser for help on using the repository browser.