Changeset 14552 for branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LocalSearch
- Timestamp:
- 01/09/17 00:36:20 (8 years ago)
- Location:
- branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LocalSearch
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LocalSearch/ExhaustiveSubspace.cs
r14544 r14552 22 22 using System.Threading; 23 23 using HeuristicLab.Algorithms.MemPR.Interfaces; 24 using HeuristicLab.Algorithms.MemPR.Util;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 33 32 [StorableClass] 34 33 public class ExhaustiveSubspace<TContext> : NamedItem, ILocalSearch<TContext> 35 where TContext : ISingleSolutionHeuristicAlgorithmContext<SingleObjectiveBasicProblem<LinearLinkageEncoding>, LinearLinkage>, ILinearLinkageSubspaceContext { 34 where TContext : ISingleSolutionHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, LinearLinkage>, 35 ILinearLinkageSubspaceContext, IEvaluationServiceContext<LinearLinkage> { 36 36 37 37 [StorableConstructor] … … 48 48 49 49 public void Optimize(TContext context) { 50 var evalWrapper = new EvaluationWrapper<LinearLinkage>(context.Problem, context.Solution);51 50 var quality = context.Solution.Fitness; 52 51 try { 53 52 var result = ExhaustiveLocalSearch.Optimize(context.Random, context.Solution.Solution, ref quality, 54 context. Problem.Maximization, evalWrapper.Evaluate, CancellationToken.None, context.Subspace.Subspace);53 context.Maximization, context.Evaluate, CancellationToken.None, context.Subspace.Subspace); 55 54 context.IncrementEvaluatedSolutions(result.Item1); 56 55 context.Iterations = result.Item2; -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LocalSearch/StaticAPI/ExhaustiveLocalSearch.cs
r14544 r14552 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using System.Threading; 25 26 using HeuristicLab.Algorithms.MemPR.Util; 26 using HeuristicLab.Collections;27 27 using HeuristicLab.Core; 28 28 using HeuristicLab.Encodings.LinearLinkageEncoding; … … 30 30 namespace HeuristicLab.Algorithms.MemPR.Grouping.LocalSearch { 31 31 public static class ExhaustiveLocalSearch { 32 public static Tuple<int, int> Optimize(IRandom random, LinearLinkage solution, ref double quality, bool maximization, Func<LinearLinkage, IRandom, double> eval, CancellationToken token, bool[] subspace = null) {32 public static Tuple<int, int> Optimize(IRandom random, LinearLinkage solution, ref double quality, bool maximization, Func<LinearLinkage, CancellationToken, double> eval, CancellationToken token, bool[] subspace = null) { 33 33 var evaluations = 0; 34 var current = solution;35 34 if (double.IsNaN(quality)) { 36 quality = eval( current, random);35 quality = eval(solution, token); 37 36 evaluations++; 38 37 } 39 38 var steps = 0; 40 // this dictionary holds the last relevant links 41 var links = new BidirectionalDictionary<int, int>(); 39 var lleb = solution.ToBackLinks(); 42 40 for (var iter = 0; iter < int.MaxValue; iter++) { 43 41 var change = false; 44 // clear the dictionary before a new pass through the array is made 45 links.Clear(); 46 for (var i = 0; i < current.Length; i++) { 47 if (subspace != null && !subspace[i]) { 48 links.RemoveBySecond(i); 49 links.Add(i, current[i]); 50 continue; 51 } 52 var pred = -1; 53 var isFirst = !links.TryGetBySecond(i, out pred); 54 var keepLink = false; 55 if (!isFirst) { 56 keepLink = subspace != null && !subspace[pred]; 57 } 58 var next = current[i]; 59 var isLast = next == i; 60 61 if (!keepLink) { 62 // try to insert current into each previous group 63 // first remove i from its group 64 var linksList = links.Where(x => x.Value != i).ToList(); 65 if (linksList.Count > 0 && !isFirst) current[pred] = isLast ? pred : next; 66 for (var k = 0; k < linksList.Count; k++) { 67 var l = linksList[k]; 68 current[l.Key] = i; 69 current[i] = Math.Max(i, l.Value); 70 var moveF = eval(current, random); 71 evaluations++; 72 if (FitnessComparer.IsBetter(maximization, moveF, quality)) { 73 steps++; 74 quality = moveF; 75 change = true; 76 links.RemoveBySecond(i); 77 links.SetByFirst(l.Key, i); // otherwise the link won't be removed 78 if (!isFirst) links.SetByFirst(pred, isLast ? pred : next); 79 next = current[i]; 80 if (next == i) { isLast = true; } 81 pred = l.Key; 82 isFirst = false; 83 break; 84 } else { // undo 85 current[l.Key] = l.Value; 86 if (k == linksList.Count - 1) { 87 // all attempts unsuccessful 88 if (!isFirst) current[pred] = i; // undo - readd i to its group 89 current[i] = next; 90 } 91 } 92 } 93 } 94 95 if (!isLast) { 96 // try to split group at this point 97 // this is safe even if keepLink was true 98 current[i] = i; 99 var moveF = eval(current, random); 42 var groupItems = new List<int>(); 43 for (var i = 0; i < solution.Length; i++) { 44 foreach (var move in MoveGenerator.GenerateForItem(i, groupItems, solution, lleb).ToList()) { 45 move.Apply(solution); 46 var moveF = eval(solution, token); 100 47 evaluations++; 101 48 if (FitnessComparer.IsBetter(maximization, moveF, quality)) { 49 move.ApplyToLLEb(lleb); 102 50 steps++; 103 51 quality = moveF; 104 52 change = true; 105 isLast = true;106 next = i;107 links.SetBySecond(i, i);108 continue;109 } else current[i] = next; // undo53 break; 54 } else { 55 move.Undo(solution); 56 } 57 if (token.IsCancellationRequested) break; 110 58 } 111 112 if (isFirst && !isLast) { 113 // try merge with all terminated groups 114 foreach (var l in links.Where(x => x.Key == x.Value && (subspace == null || subspace[x.Key]))) { 115 current[l.Key] = i; 116 var moveF = eval(current, random); 117 evaluations++; 118 if (FitnessComparer.IsBetter(maximization, moveF, quality)) { 119 steps++; 120 quality = moveF; 121 change = true; 122 isFirst = false; 123 pred = l.Key; 124 links.SetByFirst(l.Key, i); 125 break; 126 } else { 127 current[l.Key] = l.Value; 128 } 129 } 130 } else if (!isFirst && !keepLink) { 131 // try to extract current into own group 132 current[pred] = isLast ? pred : next; 133 current[i] = i; 134 var moveF = eval(current, random); 135 evaluations++; 136 if (FitnessComparer.IsBetter(maximization, moveF, quality)) { 137 steps++; 138 links.SetByFirst(pred, current[pred]); 139 quality = moveF; 140 change = true; 141 } else { // undo 142 current[pred] = i; 143 current[i] = next; 144 } 145 } 146 links.RemoveBySecond(i); 147 links.Add(i, current[i]); 59 if (lleb[i] != i) 60 groupItems.Remove(lleb[i]); 61 groupItems.Add(i); 148 62 if (token.IsCancellationRequested) break; 149 63 } … … 154 68 } 155 69 156 public static Tuple<int, int> OptimizeSwap Only(IRandom random, LinearLinkage solution, ref double quality, bool maximization, Func<LinearLinkage, IRandom, double> eval, CancellationToken token) {70 public static Tuple<int, int> OptimizeSwap(IRandom random, LinearLinkage solution, ref double quality, bool maximization, Func<LinearLinkage, IRandom, double> eval, CancellationToken token) { 157 71 var evaluations = 0; 158 72 var current = solution;
Note: See TracChangeset
for help on using the changeset viewer.