Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2981_MRCPSP/HeuristicLab.Problem.Scheduling.MRCPSP/3.3/Crossovers/MRCPSPTwoPointCrossover.cs @ 16598

Last change on this file since 16598 was 16598, checked in by ddorfmei, 5 years ago

#2981:

  • added problem definition
  • added improvers and crossover
  • added file importer
File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Problems.Scheduling.MRCPSP {
35
36  [Item("MRCPSP TwoPoint Crossover", "Applies two-point crossovers to cross a multi-encoding.")]
37  [StorableClass]
38  public sealed class MrcpspTwoPointCrossover : SingleSuccessorOperator, ICrossover, IStochasticOperator {
39
40    public MrcpspTwoPointCrossover() {
41      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
42      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RandomKeyParents", ""));
43      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("ModeListParents", ""));
44      Parameters.Add(new LookupParameter<RealVector>("RandomKeyChild", ""));
45      Parameters.Add(new LookupParameter<IntegerVector>("ModeListChild", ""));
46    }
47
48    [StorableConstructor]
49    private MrcpspTwoPointCrossover(bool deserializing) : base(deserializing) { }
50
51    private MrcpspTwoPointCrossover(MrcpspTwoPointCrossover original, Cloner cloner) : base(original, cloner) {
52    }
53
54    public ILookupParameter<IntegerVector> ModeListChild => (ILookupParameter<IntegerVector>)Parameters["ModeListChild"];
55
56    public IScopeTreeLookupParameter<IntegerVector> ModeListParents => (IScopeTreeLookupParameter<IntegerVector>)Parameters["ModeListParents"];
57
58    public ILookupParameter<RealVector> RandomKeyChild => (ILookupParameter<RealVector>)Parameters["RandomKeyChild"];
59
60    public IScopeTreeLookupParameter<RealVector> RandomKeyParents =>
61      (IScopeTreeLookupParameter<RealVector>)Parameters["RandomKeyParents"];
62
63    public ILookupParameter<IRandom> RandomParameter => (ILookupParameter<IRandom>)Parameters["Random"];
64
65    public override IOperation Apply() {
66      var random = RandomParameter.ActualValue;
67      var length = RandomKeyParents.ActualValue[0].Length;
68      var breakpoint1 = random.Next(0, length);
69      var breakpoint2 = (breakpoint1 + random.Next(1, length)) % length;
70      var breakpoints = new[] { breakpoint1, breakpoint2 };
71      Array.Sort(breakpoints);
72
73      RandomKeyChild.ActualValue = Cross(RandomKeyParents.ActualValue, breakpoints);
74      ModeListChild.ActualValue = Cross(ModeListParents.ActualValue, breakpoints);
75
76      return base.Apply();
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new MrcpspTwoPointCrossover(this, cloner);
81    }
82
83    private static IntegerVector Cross(ItemArray<IntegerVector> parents, IReadOnlyList<int> breakpoints) {
84      var parent1 = parents.First();
85      var parent2 = parents.Last();
86      var length = parent1.Length;
87      var result = new int[length];
88
89      //perform crossover
90      var arrayIndex = 0;
91      var breakPointIndex = 0;
92      var firstParent = true;
93
94      while (arrayIndex < length) {
95        if (breakPointIndex < breakpoints.Count &&
96            arrayIndex == breakpoints[breakPointIndex]) {
97          breakPointIndex++;
98          firstParent = !firstParent;
99        }
100
101        result[arrayIndex] = firstParent ? parent1[arrayIndex] : parent2[arrayIndex];
102
103        arrayIndex++;
104      }
105
106      return new IntegerVector(result);
107    }
108
109    private static double Scale(double value, double min, double max, double newMin = 0.0, double newMax = 1.0) =>
110      (newMax - newMin) * (value - min) / (max - min) + newMin;
111
112    private static RealVector Scale(RealVector vector) {
113      var min = vector.Min();
114      var max = vector.Max();
115
116      var scaledVector = new RealVector(vector.Length);
117      for (var i = 0; i < vector.Length; i++) {
118        scaledVector[i] = Scale(vector[i], min, max);
119      }
120
121      return scaledVector;
122    }
123
124    private static RealVector Cross(ItemArray<RealVector> parents, IReadOnlyList<int> breakpoints) {
125      var parent1 = Scale(parents.First());
126      var parent2 = Scale(parents.Last());
127      var length = parent1.Length;
128      var result = new double[length];
129
130      //perform crossover
131      var arrayIndex = 0;
132      var breakPointIndex = 0;
133      var firstParent = true;
134
135      while (arrayIndex < length) {
136        if (breakPointIndex < breakpoints.Count &&
137            arrayIndex == breakpoints[breakPointIndex]) {
138          breakPointIndex++;
139          firstParent = !firstParent;
140        }
141
142        result[arrayIndex] = firstParent ? parent1[arrayIndex] : parent2[arrayIndex];
143
144        arrayIndex++;
145      }
146
147      return new RealVector(result);
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.