[14420] | 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;
|
---|
[14450] | 26 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
[14466] | 27 | using HeuristicLab.Algorithms.MemPR.Util;
|
---|
[14477] | 28 | using HeuristicLab.Collections;
|
---|
[14420] | 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
[14466] | 31 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
[14420] | 32 | using HeuristicLab.Optimization;
|
---|
| 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 34 | using HeuristicLab.PluginInfrastructure;
|
---|
| 35 | using HeuristicLab.Random;
|
---|
| 36 |
|
---|
[14466] | 37 | namespace HeuristicLab.Algorithms.MemPR.LinearLinkage {
|
---|
| 38 | [Item("MemPR (linear linkage)", "MemPR implementation for linear linkage vectors.")]
|
---|
[14420] | 39 | [StorableClass]
|
---|
| 40 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)]
|
---|
[14466] | 41 | public class LinearLinkageMemPR : MemPRAlgorithm<SingleObjectiveBasicProblem<LinearLinkageEncoding>, Encodings.LinearLinkageEncoding.LinearLinkage, LinearLinkageMemPRPopulationContext, LinearLinkageMemPRSolutionContext> {
|
---|
[14420] | 42 | private const double UncommonBitSubsetMutationProbabilityMagicConst = 0.05;
|
---|
| 43 |
|
---|
| 44 | [StorableConstructor]
|
---|
[14466] | 45 | protected LinearLinkageMemPR(bool deserializing) : base(deserializing) { }
|
---|
| 46 | protected LinearLinkageMemPR(LinearLinkageMemPR original, Cloner cloner) : base(original, cloner) { }
|
---|
| 47 | public LinearLinkageMemPR() {
|
---|
| 48 | foreach (var trainer in ApplicationManager.Manager.GetInstances<ISolutionModelTrainer<LinearLinkageMemPRPopulationContext>>())
|
---|
[14420] | 49 | SolutionModelTrainerParameter.ValidValues.Add(trainer);
|
---|
| 50 |
|
---|
[14466] | 51 | foreach (var localSearch in ApplicationManager.Manager.GetInstances<ILocalSearch<LinearLinkageMemPRSolutionContext>>()) {
|
---|
[14450] | 52 | LocalSearchParameter.ValidValues.Add(localSearch);
|
---|
[14420] | 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[14466] | 57 | return new LinearLinkageMemPR(this, cloner);
|
---|
[14420] | 58 | }
|
---|
| 59 |
|
---|
[14466] | 60 | protected override bool Eq(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) {
|
---|
[14471] | 61 | var s1 = a.Solution;
|
---|
| 62 | var s2 = b.Solution;
|
---|
| 63 | if (s1.Length != s2.Length) return false;
|
---|
| 64 | for (var i = 0; i < s1.Length; i++)
|
---|
| 65 | if (s1[i] != s2[i]) return false;
|
---|
| 66 | return true;
|
---|
[14420] | 67 | }
|
---|
| 68 |
|
---|
[14466] | 69 | protected override double Dist(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) {
|
---|
[14471] | 70 | return HammingSimilarityCalculator.CalculateSimilarity(a.Solution, b.Solution);
|
---|
[14420] | 71 | }
|
---|
| 72 |
|
---|
[14466] | 73 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> ToScope(Encodings.LinearLinkageEncoding.LinearLinkage code, double fitness = double.NaN) {
|
---|
| 74 | var creator = Problem.SolutionCreator as ILinearLinkageCreator;
|
---|
| 75 | if (creator == null) throw new InvalidOperationException("Can only solve linear linkage encoded problems with MemPR (linear linkage)");
|
---|
| 76 | return new SingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>(code, creator.LLEParameter.ActualName, fitness, Problem.Evaluator.QualityParameter.ActualName) {
|
---|
[14420] | 77 | Parent = Context.Scope
|
---|
| 78 | };
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[14466] | 81 | protected override ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> CalculateSubspace(IEnumerable<Encodings.LinearLinkageEncoding.LinearLinkage> solutions, bool inverse = false) {
|
---|
[14420] | 82 | var pop = solutions.ToList();
|
---|
| 83 | var N = pop[0].Length;
|
---|
| 84 | var subspace = new bool[N];
|
---|
| 85 | for (var i = 0; i < N; i++) {
|
---|
| 86 | var val = pop[0][i];
|
---|
| 87 | if (inverse) subspace[i] = true;
|
---|
| 88 | for (var p = 1; p < pop.Count; p++) {
|
---|
| 89 | if (pop[p][i] != val) subspace[i] = !inverse;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
[14466] | 92 | return new LinearLinkageSolutionSubspace(subspace);
|
---|
[14420] | 93 | }
|
---|
| 94 |
|
---|
[14477] | 95 | protected override int TabuWalk(
|
---|
| 96 | ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> scope,
|
---|
| 97 | int maxEvals, CancellationToken token,
|
---|
| 98 | ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> sub_space = null) {
|
---|
| 99 | var maximization = Context.Problem.Maximization;
|
---|
| 100 | var subspace = sub_space is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)sub_space).Subspace : null;
|
---|
| 101 | var evaluations = 0;
|
---|
[14471] | 102 | var quality = scope.Fitness;
|
---|
[14477] | 103 | var bestQuality = quality;
|
---|
[14471] | 104 | if (double.IsNaN(quality)) {
|
---|
[14477] | 105 | Evaluate(scope, token);
|
---|
| 106 | quality = scope.Fitness;
|
---|
[14471] | 107 | evaluations++;
|
---|
[14477] | 108 | if (evaluations >= maxEvals) return evaluations;
|
---|
[14471] | 109 | }
|
---|
[14477] | 110 | var currentScope = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)scope.Clone();
|
---|
| 111 | var current = currentScope.Solution;
|
---|
| 112 |
|
---|
[14471] | 113 | var tabu = new double[current.Length, current.Length];
|
---|
| 114 | for (var i = 0; i < current.Length; i++) {
|
---|
| 115 | for (var j = i; j < current.Length; j++) {
|
---|
[14477] | 116 | tabu[i, j] = tabu[j, i] = maximization ? double.MinValue : double.MaxValue;
|
---|
[14471] | 117 | }
|
---|
[14477] | 118 | tabu[i, current[i]] = quality;
|
---|
[14471] | 119 | }
|
---|
[14477] | 120 |
|
---|
| 121 | // this dictionary holds the last relevant links
|
---|
| 122 | var links = new BidirectionalDictionary<int, int>();
|
---|
| 123 | bool allMoveTabu;
|
---|
[14471] | 124 | for (var iter = 0; iter < int.MaxValue; iter++) {
|
---|
[14477] | 125 | allMoveTabu = true;
|
---|
| 126 | // clear the dictionary before a new pass through the array is made
|
---|
| 127 | links.Clear();
|
---|
| 128 | for (var i = 0; i < current.Length; i++) {
|
---|
| 129 | if (subspace != null && !subspace[i]) {
|
---|
| 130 | links.RemoveBySecond(i);
|
---|
| 131 | links.Add(i, current[i]);
|
---|
[14471] | 132 | continue;
|
---|
[14477] | 133 | }
|
---|
[14471] | 134 |
|
---|
[14477] | 135 | var next = current[i];
|
---|
| 136 | foreach (var move in MoveGenerator.GenerateForItem(i, next, links)) {
|
---|
| 137 | // we intend to break link i -> next
|
---|
| 138 | var qualityToBreak = tabu[i, next];
|
---|
| 139 | move.Apply(current);
|
---|
| 140 | var qualityToRestore = tabu[i, current[i]]; // current[i] is new next
|
---|
| 141 | Evaluate(currentScope, token);
|
---|
| 142 | evaluations++;
|
---|
| 143 | var moveF = currentScope.Fitness;
|
---|
| 144 | var isNotTabu = FitnessComparer.IsBetter(maximization, moveF, qualityToBreak)
|
---|
| 145 | && FitnessComparer.IsBetter(maximization, moveF, qualityToRestore);
|
---|
| 146 | if (isNotTabu) allMoveTabu = false;
|
---|
| 147 | var isImprovement = FitnessComparer.IsBetter(maximization, moveF, bestQuality);
|
---|
| 148 | if (isNotTabu || isImprovement) {
|
---|
| 149 | if (maximization) {
|
---|
| 150 | tabu[i, next] = Math.Max(tabu[i, next], moveF);
|
---|
| 151 | tabu[i, current[i]] = Math.Max(tabu[i, current[i]], moveF);
|
---|
| 152 | } else {
|
---|
| 153 | tabu[i, next] = Math.Min(tabu[i, next], moveF);
|
---|
| 154 | tabu[i, current[i]] = Math.Min(tabu[i, current[i]], moveF);
|
---|
| 155 | }
|
---|
| 156 | quality = moveF;
|
---|
| 157 | if (isImprovement) bestQuality = quality;
|
---|
[14471] | 158 |
|
---|
[14477] | 159 | move.UpdateLinks(links);
|
---|
| 160 | break;
|
---|
| 161 | } else move.Undo(current);
|
---|
| 162 | if (evaluations >= maxEvals) break;
|
---|
[14471] | 163 | }
|
---|
[14477] | 164 | links.RemoveBySecond(i);
|
---|
| 165 | links.Add(i, current[i]);
|
---|
[14471] | 166 | if (evaluations >= maxEvals) break;
|
---|
[14477] | 167 | if (token.IsCancellationRequested) break;
|
---|
[14471] | 168 | }
|
---|
[14477] | 169 | if (allMoveTabu) break;
|
---|
[14471] | 170 | if (evaluations >= maxEvals) break;
|
---|
[14477] | 171 | if (token.IsCancellationRequested) break;
|
---|
[14471] | 172 | }
|
---|
[14477] | 173 | return evaluations;
|
---|
[14466] | 174 | }
|
---|
[14420] | 175 |
|
---|
[14466] | 176 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Cross(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p1Scope, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p2Scope, CancellationToken token) {
|
---|
| 177 | var p1 = p1Scope.Solution;
|
---|
| 178 | var p2 = p2Scope.Solution;
|
---|
| 179 | var transfered = new bool[p1.Length];
|
---|
| 180 | var subspace = new bool[p1.Length];
|
---|
| 181 | var lleeChild = new int[p1.Length];
|
---|
| 182 | var lleep1 = p1.ToLLEe();
|
---|
| 183 | var lleep2 = p2.ToLLEe();
|
---|
| 184 | for (var i = p1.Length - 1; i >= 0; i--) {
|
---|
| 185 | // Step 1
|
---|
| 186 | subspace[i] = p1[i] != p2[i];
|
---|
| 187 | var p1IsEnd = p1[i] == i;
|
---|
| 188 | var p2IsEnd = p2[i] == i;
|
---|
| 189 | if (p1IsEnd & p2IsEnd) {
|
---|
| 190 | transfered[i] = true;
|
---|
| 191 | } else if (p1IsEnd | p2IsEnd) {
|
---|
| 192 | transfered[i] = Context.Random.NextDouble() < 0.5;
|
---|
| 193 | }
|
---|
| 194 | lleeChild[i] = i;
|
---|
[14420] | 195 |
|
---|
[14466] | 196 | // Step 2
|
---|
| 197 | if (transfered[i]) continue;
|
---|
| 198 | var end1 = lleep1[i];
|
---|
| 199 | var end2 = lleep2[i];
|
---|
| 200 | var containsEnd1 = transfered[end1];
|
---|
| 201 | var containsEnd2 = transfered[end2];
|
---|
| 202 | if (containsEnd1 & containsEnd2) {
|
---|
| 203 | if (Context.Random.NextDouble() < 0.5) {
|
---|
| 204 | lleeChild[i] = end1;
|
---|
| 205 | } else {
|
---|
| 206 | lleeChild[i] = end2;
|
---|
[14420] | 207 | }
|
---|
[14466] | 208 | } else if (containsEnd1) {
|
---|
| 209 | lleeChild[i] = end1;
|
---|
| 210 | } else if (containsEnd2) {
|
---|
| 211 | lleeChild[i] = end2;
|
---|
| 212 | } else {
|
---|
| 213 | if (Context.Random.NextDouble() < 0.5) {
|
---|
| 214 | lleeChild[i] = lleeChild[p1[i]];
|
---|
| 215 | } else {
|
---|
| 216 | lleeChild[i] = lleeChild[p2[i]];
|
---|
[14420] | 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
[14466] | 220 | var child = new Encodings.LinearLinkageEncoding.LinearLinkage(lleeChild.Length);
|
---|
| 221 | child.FromLLEe(lleeChild);
|
---|
| 222 |
|
---|
| 223 | return ToScope(child);
|
---|
[14420] | 224 | }
|
---|
| 225 |
|
---|
[14466] | 226 | protected override void Mutate(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> offspring, CancellationToken token, ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> subspace = null) {
|
---|
| 227 | var lle = offspring.Solution;
|
---|
| 228 | var subset = subspace is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)subspace).Subspace : null;
|
---|
| 229 | for (var i = 0; i < lle.Length - 1; i++) {
|
---|
| 230 | if (subset == null || subset[i]) continue; // mutation works against crossover so aims to mutate noTouch points
|
---|
[14420] | 231 | if (Context.Random.NextDouble() < UncommonBitSubsetMutationProbabilityMagicConst) {
|
---|
[14466] | 232 | subset[i] = true;
|
---|
| 233 | var index = Context.Random.Next(i, lle.Length);
|
---|
| 234 | for (var j = index - 1; j >= i; j--) {
|
---|
| 235 | if (lle[j] == index) index = j;
|
---|
| 236 | }
|
---|
| 237 | lle[i] = index;
|
---|
| 238 | index = i;
|
---|
| 239 | var idx2 = i;
|
---|
| 240 | for (var j = i - 1; j >= 0; j--) {
|
---|
| 241 | if (lle[j] == lle[index]) {
|
---|
| 242 | lle[j] = idx2;
|
---|
| 243 | index = idx2 = j;
|
---|
| 244 | } else if (lle[j] == idx2) idx2 = j;
|
---|
| 245 | }
|
---|
[14420] | 246 | }
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[14466] | 250 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Relink(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b, CancellationToken token) {
|
---|
| 251 | var maximization = Context.Problem.Maximization;
|
---|
[14453] | 252 | if (double.IsNaN(a.Fitness)) {
|
---|
| 253 | Evaluate(a, token);
|
---|
| 254 | Context.IncrementEvaluatedSolutions(1);
|
---|
| 255 | }
|
---|
| 256 | if (double.IsNaN(b.Fitness)) {
|
---|
| 257 | Evaluate(b, token);
|
---|
| 258 | Context.IncrementEvaluatedSolutions(1);
|
---|
| 259 | }
|
---|
[14466] | 260 | var child = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)a.Clone();
|
---|
| 261 | var cgroups = child.Solution.GetGroups().Select(x => new HashSet<int>(x)).ToList();
|
---|
| 262 | var g2 = b.Solution.GetGroups().ToList();
|
---|
| 263 | var order = Enumerable.Range(0, g2.Count).Shuffle(Context.Random).ToList();
|
---|
| 264 | ISingleObjectiveSolutionScope <Encodings.LinearLinkageEncoding.LinearLinkage> bestChild = null;
|
---|
| 265 | for (var j = 0; j < g2.Count; j++) {
|
---|
| 266 | var g = g2[order[j]];
|
---|
| 267 | var changed = false;
|
---|
| 268 | for (var k = j; k < cgroups.Count; k++) {
|
---|
| 269 | foreach (var f in g) if (cgroups[k].Remove(f)) changed = true;
|
---|
| 270 | if (cgroups[k].Count == 0) {
|
---|
| 271 | cgroups.RemoveAt(k);
|
---|
| 272 | k--;
|
---|
[14420] | 273 | }
|
---|
[14466] | 274 | }
|
---|
| 275 | cgroups.Insert(0, new HashSet<int>(g));
|
---|
| 276 | child.Solution.SetGroups(cgroups);
|
---|
| 277 | if (changed) {
|
---|
| 278 | Evaluate(child, token);
|
---|
| 279 | if (bestChild == null || FitnessComparer.IsBetter(maximization, child.Fitness, bestChild.Fitness)) {
|
---|
| 280 | bestChild = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)child.Clone();
|
---|
[14420] | 281 | }
|
---|
| 282 | }
|
---|
[14466] | 283 | };
|
---|
| 284 | return bestChild;
|
---|
[14420] | 285 | }
|
---|
| 286 | }
|
---|
| 287 | }
|
---|