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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
27 | using HeuristicLab.Algorithms.MemPR.Util;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.MemPR.LinearLinkage {
|
---|
37 | [Item("MemPR (linear linkage)", "MemPR implementation for linear linkage vectors.")]
|
---|
38 | [StorableClass]
|
---|
39 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)]
|
---|
40 | public class LinearLinkageMemPR : MemPRAlgorithm<SingleObjectiveBasicProblem<LinearLinkageEncoding>, Encodings.LinearLinkageEncoding.LinearLinkage, LinearLinkageMemPRPopulationContext, LinearLinkageMemPRSolutionContext> {
|
---|
41 | private const double UncommonBitSubsetMutationProbabilityMagicConst = 0.05;
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | protected LinearLinkageMemPR(bool deserializing) : base(deserializing) { }
|
---|
45 | protected LinearLinkageMemPR(LinearLinkageMemPR original, Cloner cloner) : base(original, cloner) { }
|
---|
46 | public LinearLinkageMemPR() {
|
---|
47 | foreach (var trainer in ApplicationManager.Manager.GetInstances<ISolutionModelTrainer<LinearLinkageMemPRPopulationContext>>())
|
---|
48 | SolutionModelTrainerParameter.ValidValues.Add(trainer);
|
---|
49 |
|
---|
50 | foreach (var localSearch in ApplicationManager.Manager.GetInstances<ILocalSearch<LinearLinkageMemPRSolutionContext>>()) {
|
---|
51 | LocalSearchParameter.ValidValues.Add(localSearch);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new LinearLinkageMemPR(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected override bool Eq(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) {
|
---|
60 | return a.Solution.SequenceEqual(b.Solution);
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected override double Dist(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) {
|
---|
64 | if (a.Solution.Length != b.Solution.Length) throw new ArgumentException("Comparing encodings of unequal length");
|
---|
65 | var dist = 0;
|
---|
66 | for (var i = 0; i < a.Solution.Length; i++) {
|
---|
67 | if (a.Solution[i] != b.Solution[i]) dist++;
|
---|
68 | }
|
---|
69 | return dist / (double)a.Solution.Length;
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> ToScope(Encodings.LinearLinkageEncoding.LinearLinkage code, double fitness = double.NaN) {
|
---|
73 | var creator = Problem.SolutionCreator as ILinearLinkageCreator;
|
---|
74 | if (creator == null) throw new InvalidOperationException("Can only solve linear linkage encoded problems with MemPR (linear linkage)");
|
---|
75 | return new SingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>(code, creator.LLEParameter.ActualName, fitness, Problem.Evaluator.QualityParameter.ActualName) {
|
---|
76 | Parent = Context.Scope
|
---|
77 | };
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected override ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> CalculateSubspace(IEnumerable<Encodings.LinearLinkageEncoding.LinearLinkage> solutions, bool inverse = false) {
|
---|
81 | var pop = solutions.ToList();
|
---|
82 | var N = pop[0].Length;
|
---|
83 | var subspace = new bool[N];
|
---|
84 | for (var i = 0; i < N; i++) {
|
---|
85 | var val = pop[0][i];
|
---|
86 | if (inverse) subspace[i] = true;
|
---|
87 | for (var p = 1; p < pop.Count; p++) {
|
---|
88 | if (pop[p][i] != val) subspace[i] = !inverse;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return new LinearLinkageSolutionSubspace(subspace);
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected override int TabuWalk(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> scope, int maxEvals, CancellationToken token, ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> subspace = null) {
|
---|
95 | return 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Cross(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p1Scope, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p2Scope, CancellationToken token) {
|
---|
99 | var p1 = p1Scope.Solution;
|
---|
100 | var p2 = p2Scope.Solution;
|
---|
101 | var transfered = new bool[p1.Length];
|
---|
102 | var subspace = new bool[p1.Length];
|
---|
103 | var lleeChild = new int[p1.Length];
|
---|
104 | var lleep1 = p1.ToLLEe();
|
---|
105 | var lleep2 = p2.ToLLEe();
|
---|
106 | for (var i = p1.Length - 1; i >= 0; i--) {
|
---|
107 | // Step 1
|
---|
108 | subspace[i] = p1[i] != p2[i];
|
---|
109 | var p1IsEnd = p1[i] == i;
|
---|
110 | var p2IsEnd = p2[i] == i;
|
---|
111 | if (p1IsEnd & p2IsEnd) {
|
---|
112 | transfered[i] = true;
|
---|
113 | } else if (p1IsEnd | p2IsEnd) {
|
---|
114 | transfered[i] = Context.Random.NextDouble() < 0.5;
|
---|
115 | }
|
---|
116 | lleeChild[i] = i;
|
---|
117 |
|
---|
118 | // Step 2
|
---|
119 | if (transfered[i]) continue;
|
---|
120 | var end1 = lleep1[i];
|
---|
121 | var end2 = lleep2[i];
|
---|
122 | var containsEnd1 = transfered[end1];
|
---|
123 | var containsEnd2 = transfered[end2];
|
---|
124 | if (containsEnd1 & containsEnd2) {
|
---|
125 | if (Context.Random.NextDouble() < 0.5) {
|
---|
126 | lleeChild[i] = end1;
|
---|
127 | } else {
|
---|
128 | lleeChild[i] = end2;
|
---|
129 | }
|
---|
130 | } else if (containsEnd1) {
|
---|
131 | lleeChild[i] = end1;
|
---|
132 | } else if (containsEnd2) {
|
---|
133 | lleeChild[i] = end2;
|
---|
134 | } else {
|
---|
135 | if (Context.Random.NextDouble() < 0.5) {
|
---|
136 | lleeChild[i] = lleeChild[p1[i]];
|
---|
137 | } else {
|
---|
138 | lleeChild[i] = lleeChild[p2[i]];
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | var child = new Encodings.LinearLinkageEncoding.LinearLinkage(lleeChild.Length);
|
---|
143 | child.FromLLEe(lleeChild);
|
---|
144 |
|
---|
145 | return ToScope(child);
|
---|
146 | }
|
---|
147 |
|
---|
148 | protected override void Mutate(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> offspring, CancellationToken token, ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> subspace = null) {
|
---|
149 | var lle = offspring.Solution;
|
---|
150 | var subset = subspace is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)subspace).Subspace : null;
|
---|
151 | for (var i = 0; i < lle.Length - 1; i++) {
|
---|
152 | if (subset == null || subset[i]) continue; // mutation works against crossover so aims to mutate noTouch points
|
---|
153 | if (Context.Random.NextDouble() < UncommonBitSubsetMutationProbabilityMagicConst) {
|
---|
154 | subset[i] = true;
|
---|
155 | var index = Context.Random.Next(i, lle.Length);
|
---|
156 | for (var j = index - 1; j >= i; j--) {
|
---|
157 | if (lle[j] == index) index = j;
|
---|
158 | }
|
---|
159 | lle[i] = index;
|
---|
160 | index = i;
|
---|
161 | var idx2 = i;
|
---|
162 | for (var j = i - 1; j >= 0; j--) {
|
---|
163 | if (lle[j] == lle[index]) {
|
---|
164 | lle[j] = idx2;
|
---|
165 | index = idx2 = j;
|
---|
166 | } else if (lle[j] == idx2) idx2 = j;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Relink(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b, CancellationToken token) {
|
---|
173 | var maximization = Context.Problem.Maximization;
|
---|
174 | if (double.IsNaN(a.Fitness)) {
|
---|
175 | Evaluate(a, token);
|
---|
176 | Context.IncrementEvaluatedSolutions(1);
|
---|
177 | }
|
---|
178 | if (double.IsNaN(b.Fitness)) {
|
---|
179 | Evaluate(b, token);
|
---|
180 | Context.IncrementEvaluatedSolutions(1);
|
---|
181 | }
|
---|
182 | var child = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)a.Clone();
|
---|
183 | var cgroups = child.Solution.GetGroups().Select(x => new HashSet<int>(x)).ToList();
|
---|
184 | var g2 = b.Solution.GetGroups().ToList();
|
---|
185 | var order = Enumerable.Range(0, g2.Count).Shuffle(Context.Random).ToList();
|
---|
186 | ISingleObjectiveSolutionScope <Encodings.LinearLinkageEncoding.LinearLinkage> bestChild = null;
|
---|
187 | for (var j = 0; j < g2.Count; j++) {
|
---|
188 | var g = g2[order[j]];
|
---|
189 | var changed = false;
|
---|
190 | for (var k = j; k < cgroups.Count; k++) {
|
---|
191 | foreach (var f in g) if (cgroups[k].Remove(f)) changed = true;
|
---|
192 | if (cgroups[k].Count == 0) {
|
---|
193 | cgroups.RemoveAt(k);
|
---|
194 | k--;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | cgroups.Insert(0, new HashSet<int>(g));
|
---|
198 | child.Solution.SetGroups(cgroups);
|
---|
199 | if (changed) {
|
---|
200 | Evaluate(child, token);
|
---|
201 | if (bestChild == null || FitnessComparer.IsBetter(maximization, child.Fitness, bestChild.Fitness)) {
|
---|
202 | bestChild = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)child.Clone();
|
---|
203 | }
|
---|
204 | }
|
---|
205 | };
|
---|
206 | return bestChild;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|