Changeset 15563
- Timestamp:
- 12/30/17 23:10:29 (7 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3
- Files:
-
- 1 added
- 8 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/ESContext.cs
r15562 r15563 45 45 } 46 46 47 [Storable] 48 private IValueParameter<IRandom> normalRand; 49 public IRandom NormalRand { 50 get { return normalRand.Value; } 51 set { normalRand.Value = value; } 52 } 53 47 54 public void ReplacePopulation(IEnumerable<ISingleObjectiveSolutionScope<ESGQAPSolution>> replacement) { 48 55 Scope.SubScopes.Replace(replacement); … … 55 62 problem = cloner.Clone(original.problem); 56 63 bestSolution = cloner.Clone(original.bestSolution); 64 normalRand = cloner.Clone(original.normalRand); 57 65 } 58 66 public ESContext() : this("Evolution Strategy (GQAP) Context") { } … … 60 68 Parameters.Add(problem = new ValueParameter<GQAP>("Problem")); 61 69 Parameters.Add(bestSolution = new ValueParameter<ESGQAPSolution>("BestFoundSolution")); 70 Parameters.Add(normalRand = new ValueParameter<IRandom>("NormalRand")); 62 71 } 63 72 … … 74 83 scope.Variables.Add(new Variable(name, code.Assignment)); 75 84 scope.Variables.Add(new Variable("Evaluation", code.Evaluation)); 85 scope.Variables.Add(new Variable("StrategyParameter", code.sParam)); 76 86 return scope; 77 87 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/ESGQAPSolution.cs
r15562 r15563 21 21 22 22 using HeuristicLab.Common; 23 using HeuristicLab.Data; 23 24 using HeuristicLab.Encodings.IntegerVectorEncoding; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 28 29 29 30 [Storable] 30 private double sParam;31 internal DoubleValue sParam; 31 32 public double SParam { 32 get { return sParam ; }33 get { return sParam.Value; } 33 34 set { 34 if (sParam == value) return;35 sParam = value;35 if (sParam.Value == value) return; 36 sParam.Value = value; 36 37 OnPropertyChanged(nameof(SParam)); 37 38 } … … 42 43 protected ESGQAPSolution(ESGQAPSolution original, Cloner cloner) 43 44 : base(original, cloner) { 44 sParam = original.sParam;45 sParam = cloner.Clone(original.sParam); 45 46 } 46 47 public ESGQAPSolution(IntegerVector assignment, Evaluation eval, double sParam) 47 48 : base(assignment, eval) { 48 this.sParam = sParam;49 this.sParam = new DoubleValue(sParam); 49 50 } 50 51 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/EvolutionStrategy.cs
r15562 r15563 106 106 base.Initialize(cancellationToken); 107 107 108 Context.NormalRand = new NormalDistributedRandom(Context.Random, 0, 1); 108 109 Context.Problem = Problem; 109 110 Context.BestQuality = double.NaN; 110 111 Context.BestSolution = null; 111 112 112 113 for (var m = 0; m < Mu; m++) { 113 114 var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length); … … 115 116 Context.EvaluatedSolutions++; 116 117 117 var ind = new ESGQAPSolution(assign, eval, 1.0 / assign.Length);118 var ind = new ESGQAPSolution(assign, eval, Context.Random.NextDouble() * 2 - 1); 118 119 var fit = Problem.ProblemInstance.ToSingleObjective(eval); 119 120 Context.AddToPopulation(Context.ToScope(ind, fit)); … … 133 134 134 135 protected override void Run(CancellationToken cancellationToken) { 136 var lastUpdate = ExecutionTime; 137 135 138 while (!StoppingCriterion()) { 136 139 var nextGen = new List<ISingleObjectiveSolutionScope<ESGQAPSolution>>(Lambda); … … 141 144 var offspring = (ESGQAPSolution)m.Solution.Clone(); 142 145 var count = Mutate(m, offspring); 143 offspring.SParam += ((1.0 / count) - offspring.SParam) / 10.0;146 offspring.SParam += 0.7071 * Context.NormalRand.NextDouble(); //((1.0 / count) - offspring.SParam) / 10.0; 144 147 145 148 offspring.Evaluation = Problem.ProblemInstance.Evaluate(offspring.Assignment); … … 162 165 163 166 IResult result; 164 if (Results.TryGetValue("Iterations", out result)) 165 ((IntValue)result.Value).Value = Context.Iterations; 166 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 167 if (Results.TryGetValue("EvaluatedSolutions", out result)) 168 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 169 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 167 if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) { 168 if (Results.TryGetValue("Iterations", out result)) 169 ((IntValue)result.Value).Value = Context.Iterations; 170 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 171 if (Results.TryGetValue("EvaluatedSolutions", out result)) 172 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 173 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 174 lastUpdate = ExecutionTime; 175 } 170 176 if (Results.TryGetValue("BestQuality", out result)) 171 177 ((DoubleValue)result.Value).Value = Context.BestQuality; … … 180 186 if (cancellationToken.IsCancellationRequested) break; 181 187 } 188 IResult result2; 189 if (Results.TryGetValue("Iterations", out result2)) 190 ((IntValue)result2.Value).Value = Context.Iterations; 191 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 192 if (Results.TryGetValue("EvaluatedSolutions", out result2)) 193 ((IntValue)result2.Value).Value = Context.EvaluatedSolutions; 194 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 182 195 } 183 196 184 197 private int Mutate(ISingleObjectiveSolutionScope<ESGQAPSolution> m, ESGQAPSolution offspring) { 198 var stopProb = (Math.Tanh(m.Solution.SParam) + 1) / 2.0; // squash strategy parameter to ]0;1[ 185 199 var offspringFeasible = offspring.Evaluation.IsFeasible; 186 200 double[] slack = null; … … 210 224 offspring.Assignment[equip] = newLoc; 211 225 } 212 if (Context.Random.NextDouble() < m.Solution.SParam) break;226 if (Context.Random.NextDouble() < stopProb) break; 213 227 count++; 214 228 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASP/GRASP.cs
r15562 r15563 164 164 protected override void Run(CancellationToken cancellationToken) { 165 165 var eq = new IntegerVectorEqualityComparer(); 166 var lastUpdate = ExecutionTime; 166 167 while (!StoppingCriterion()) { // line 2 in Algorithm 1 167 168 // next: line 3 in Algorithm 1 … … 217 218 218 219 IResult result; 219 if (Results.TryGetValue("Iterations", out result)) 220 ((IntValue)result.Value).Value = Context.Iterations; 221 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 222 if (Results.TryGetValue("EvaluatedSolutions", out result)) 223 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 224 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 220 if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) { 221 if (Results.TryGetValue("Iterations", out result)) 222 ((IntValue)result.Value).Value = Context.Iterations; 223 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 224 if (Results.TryGetValue("EvaluatedSolutions", out result)) 225 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 226 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 227 lastUpdate = ExecutionTime; 228 } 225 229 if (Results.TryGetValue("BestQuality", out result)) 226 230 ((DoubleValue)result.Value).Value = Context.BestQuality; … … 235 239 if (cancellationToken.IsCancellationRequested) break; 236 240 } 241 IResult result2; 242 if (Results.TryGetValue("Iterations", out result2)) 243 ((IntValue)result2.Value).Value = Context.Iterations; 244 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 245 if (Results.TryGetValue("EvaluatedSolutions", out result2)) 246 ((IntValue)result2.Value).Value = Context.EvaluatedSolutions; 247 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 237 248 } 238 249 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms-3.3.csproj
r15562 r15563 110 110 <Compile Include="GRASP\GRASPContext.cs" /> 111 111 <Compile Include="GRASP\GRASP.cs" /> 112 <Compile Include="LAHC\pLAHCContext.cs" /> 113 <Compile Include="LAHC\pLAHC.cs" /> 114 <Compile Include="LAHC\LAHC.cs" /> 115 <Compile Include="LAHC\LAHCContext.cs" /> 112 116 <Compile Include="LocalSearch\LocalSearchContext.cs" /> 113 117 <Compile Include="Infrastructure\Interfaces.cs" /> … … 140 144 <None Include="HeuristicLab.snk" /> 141 145 </ItemGroup> 146 <ItemGroup /> 142 147 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 143 148 <PropertyGroup> -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Contexts/StochasticContext.cs
r15562 r15563 24 24 using HeuristicLab.Parameters; 25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Random;27 26 28 27 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms { … … 43 42 } 44 43 protected StochasticContext() : base() { 45 Parameters.Add(random = new ValueParameter<IRandom>("Random" , new MersenneTwister()));44 Parameters.Add(random = new ValueParameter<IRandom>("Random")); 46 45 } 47 46 protected StochasticContext(string name) : base(name) { 48 Parameters.Add(random = new ValueParameter<IRandom>("Random" , new MersenneTwister()));47 Parameters.Add(random = new ValueParameter<IRandom>("Random")); 49 48 } 50 49 protected StochasticContext(string name, ParameterCollection parameters) : base(name, parameters) { 51 Parameters.Add(random = new ValueParameter<IRandom>("Random" , new MersenneTwister()));50 Parameters.Add(random = new ValueParameter<IRandom>("Random")); 52 51 } 53 52 protected StochasticContext(string name, string description) : base(name, description) { 54 Parameters.Add(random = new ValueParameter<IRandom>("Random" , new MersenneTwister()));53 Parameters.Add(random = new ValueParameter<IRandom>("Random")); 55 54 } 56 55 protected StochasticContext(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { 57 Parameters.Add(random = new ValueParameter<IRandom>("Random" , new MersenneTwister()));56 Parameters.Add(random = new ValueParameter<IRandom>("Random")); 58 57 } 59 58 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/LAHC.cs
r15562 r15563 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Threading; 24 25 using HeuristicLab.Common; … … 27 28 using HeuristicLab.Encodings.IntegerVectorEncoding; 28 29 using HeuristicLab.Optimization; 30 using HeuristicLab.Parameters; 29 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 32 31 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.L ocalSearch{32 [Item(" Iterated Local Search (GQAP)", "Iterated local searchfor the GQAP.")]33 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LAHC { 34 [Item("LAHC (GQAP)", "Late-acceptance hill climber for the GQAP.")] 33 35 [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)] 34 36 [StorableClass] 35 public sealed class IteratedLS : ContextAlgorithm<LocalSearchContext> {37 public sealed class LAHC : StochasticAlgorithm<LAHCContext> { 36 38 37 39 public override bool SupportsPause { … … 48 50 } 49 51 52 [Storable] 53 private FixedValueParameter<IntValue> memorySizeParameter; 54 public IFixedValueParameter<IntValue> MemorySizeParameter { 55 get { return memorySizeParameter; } 56 } 57 58 public int MemorySize { 59 get { return memorySizeParameter.Value.Value; } 60 set { memorySizeParameter.Value.Value = value; } 61 } 62 50 63 [StorableConstructor] 51 private IteratedLS(bool deserializing) : base(deserializing) { }52 private IteratedLS(IteratedLSoriginal, Cloner cloner)64 private LAHC(bool deserializing) : base(deserializing) { } 65 private LAHC(LAHC original, Cloner cloner) 53 66 : base(original, cloner) { 67 memorySizeParameter = cloner.Clone(original.memorySizeParameter); 54 68 } 55 public IteratedLS() { 69 public LAHC() { 70 Parameters.Add(memorySizeParameter = new FixedValueParameter<IntValue>("MemorySize", "The size of the memory, the shorter the more greedy LAHC performs.", new IntValue(100))); 56 71 57 72 Problem = new GQAP(); … … 59 74 60 75 public override IDeepCloneable Clone(Cloner cloner) { 61 return new IteratedLS(this, cloner);76 return new LAHC(this, cloner); 62 77 } 63 78 … … 66 81 67 82 Context.Problem = Problem; 68 Context.BestQuality = double.NaN; 69 Context.BestSolution = null; 70 71 var assign = GreedyRandomizedSolutionCreator.CreateSolution(Context.Random, Problem.ProblemInstance, 10, true, token); 83 Context.LastSuccess = 0; 84 85 var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length); 72 86 var eval = Problem.ProblemInstance.Evaluate(assign); 73 87 var fit = Problem.ProblemInstance.ToSingleObjective(eval); … … 75 89 76 90 var candidate = new GQAPSolution(assign, eval); 77 var lsevaluations = 0; 78 OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations); 79 Context.EvaluatedSolutions += lsevaluations; 80 91 81 92 Context.ReplaceIncumbent(Context.ToScope(candidate, fit)); 82 93 Context.BestQuality = fit; 83 94 Context.BestSolution = (GQAPSolution)candidate.Clone(); 95 96 Context.Memory = new DoubleArray(Enumerable.Repeat(Context.BestQuality, MemorySize).ToArray()); 84 97 85 98 Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); … … 92 105 93 106 protected override void Run(CancellationToken cancellationToken) { 107 var lastUpdate = ExecutionTime; 94 108 while (!StoppingCriterion()) { 95 var lsevaluations = 0; 96 var candidate = (GQAPSolution)Context.Incumbent.Solution.Clone(); 97 RandomWalk(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, candidate.Assignment.Length); 98 candidate.Evaluation = Problem.ProblemInstance.Evaluate(candidate.Assignment); 99 Context.EvaluatedSolutions++; 100 OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations); 101 Context.EvaluatedSolutions += lsevaluations; 109 var move = StochasticNMoveSingleMoveGenerator.GenerateOneMove(Context.Random, 110 Context.Incumbent.Solution.Assignment, Problem.ProblemInstance.Capacities); 111 var moveEval = GQAPNMoveEvaluator.Evaluate(move, 112 Context.Incumbent.Solution.Assignment, 113 Context.Incumbent.Solution.Evaluation, Problem.ProblemInstance); 114 if (Context.Iterations % Problem.ProblemInstance.Demands.Length == 0) 115 Context.EvaluatedSolutions++; 116 var nextFit = Problem.ProblemInstance.ToSingleObjective(moveEval); 117 var nextVec = new IntegerVector(Context.Incumbent.Solution.Assignment); 118 NMoveMaker.Apply(nextVec, move); 119 120 var v = Context.Iterations % Context.Memory.Length; 121 Context.Iterations++; 122 var prevFit = Context.Memory[v]; 102 123 103 var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation); 104 if (candidateFit < Context.Incumbent.Fitness) { 105 Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit)); 106 Context.BestQuality = candidateFit; 107 Context.BestSolution = (GQAPSolution)candidate.Clone(); 124 var accept = nextFit <= Context.Incumbent.Fitness 125 || nextFit <= prevFit; 126 127 if (accept && nextFit < Context.Incumbent.Fitness) 128 Context.LastSuccess = Context.Iterations; 129 130 if (accept) { 131 Context.ReplaceIncumbent(Context.ToScope(new GQAPSolution(nextVec, moveEval), nextFit)); 132 if (nextFit < Context.BestQuality) { 133 Context.BestSolution = (GQAPSolution)Context.Incumbent.Solution.Clone(); 134 Context.BestQuality = nextFit; 135 } 108 136 } 109 137 138 if (Context.Incumbent.Fitness < prevFit) 139 Context.Memory[v] = Context.Incumbent.Fitness; 140 110 141 IResult result; 111 if (Results.TryGetValue("Iterations", out result)) 112 ((IntValue)result.Value).Value = Context.Iterations; 113 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 114 if (Results.TryGetValue("EvaluatedSolutions", out result)) 115 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 116 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 142 if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) { 143 if (Results.TryGetValue("Iterations", out result)) 144 ((IntValue)result.Value).Value = Context.Iterations; 145 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 146 if (Results.TryGetValue("EvaluatedSolutions", out result)) 147 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 148 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 149 lastUpdate = ExecutionTime; 150 } 117 151 if (Results.TryGetValue("BestQuality", out result)) 118 152 ((DoubleValue)result.Value).Value = Context.BestQuality; … … 123 157 124 158 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 125 126 Context.Iterations++; 159 127 160 if (cancellationToken.IsCancellationRequested) break; 128 161 } 129 } 130 131 private static void RandomWalk(IRandom random, IntegerVector assignment, int locations, int walkLength) { 132 for (int i = 0; i < walkLength; i++) { 133 var equipment = random.Next(assignment.Length); 134 assignment[equipment] = random.Next(locations); 135 if (random.NextDouble() < 1.0 / walkLength) break; 136 } 162 IResult result2; 163 if (Results.TryGetValue("Iterations", out result2)) 164 ((IntValue)result2.Value).Value = Context.Iterations; 165 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 166 if (Results.TryGetValue("EvaluatedSolutions", out result2)) 167 ((IntValue)result2.Value).Value = Context.EvaluatedSolutions; 168 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 137 169 } 138 170 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/LAHCContext.cs
r15562 r15563 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data; 24 25 using HeuristicLab.Parameters; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 27 27 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.L ocalSearch{28 public sealed class L ocalSearchContext : SingleObjectiveSingleSolutionContext<ISingleObjectiveSolutionScope<GQAPSolution>> {28 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LAHC { 29 public sealed class LAHCContext : SingleObjectiveSingleSolutionContext<ISingleObjectiveSolutionScope<GQAPSolution>> { 29 30 [Storable] 30 31 private IValueParameter<GQAP> problem; … … 40 41 set { bestSolution.Value = value; } 41 42 } 43 44 [Storable] 45 private IValueParameter<DoubleArray> memory; 46 public DoubleArray Memory { 47 get { return memory.Value; } 48 set { memory.Value = value; } 49 } 50 51 [Storable] 52 private IValueParameter<IntValue> lastSuccess; 53 public int LastSuccess { 54 get { return lastSuccess.Value.Value; } 55 set { lastSuccess.Value.Value = value; } 56 } 42 57 43 58 [StorableConstructor] 44 private L ocalSearchContext(bool deserializing) : base(deserializing) { }45 private L ocalSearchContext(LocalSearchContext original, Cloner cloner)59 private LAHCContext(bool deserializing) : base(deserializing) { } 60 private LAHCContext(LAHCContext original, Cloner cloner) 46 61 : base(original, cloner) { 47 62 problem = cloner.Clone(original.problem); 48 63 bestSolution = cloner.Clone(original.bestSolution); 64 memory = cloner.Clone(original.memory); 65 lastSuccess = cloner.Clone(original.lastSuccess); 49 66 } 50 public L ocalSearchContext() {67 public LAHCContext() { 51 68 Parameters.Add(problem = new ValueParameter<GQAP>("Problem")); 52 69 Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution")); 70 Parameters.Add(memory = new ValueParameter<DoubleArray>("Memory")); 71 Parameters.Add(lastSuccess = new ValueParameter<IntValue>("LastSuccess")); 53 72 } 54 73 55 74 public override IDeepCloneable Clone(Cloner cloner) { 56 return new L ocalSearchContext(this, cloner);75 return new LAHCContext(this, cloner); 57 76 } 58 77 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/pLAHC.cs
r15562 r15563 27 27 using HeuristicLab.Encodings.IntegerVectorEncoding; 28 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Parameters; 29 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 31 31 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.L ocalSearch{32 [Item(" Iterated Local Search (GQAP)", "Iterated local searchfor the GQAP.")]32 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LAHC { 33 [Item("pLAHC-s (GQAP)", "Parameterless Late-acceptance hill climber for the GQAP.")] 33 34 [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)] 34 35 [StorableClass] 35 public sealed class IteratedLS : ContextAlgorithm<LocalSearchContext> {36 public sealed class PLAHCS : StochasticAlgorithm<PLAHCContext> { 36 37 37 38 public override bool SupportsPause { … … 48 49 } 49 50 51 [Storable] 52 private FixedValueParameter<IntValue> maximumExponentParameter; 53 public IFixedValueParameter<IntValue> MaximumExponentParameter { 54 get { return maximumExponentParameter; } 55 } 56 57 [Storable] 58 private FixedValueParameter<IntValue> minimumSprintIterationsParameter; 59 public IFixedValueParameter<IntValue> MinimumSprintIterationsParameter { 60 get { return minimumSprintIterationsParameter; } 61 } 62 63 public int MaximumExponent { 64 get { return maximumExponentParameter.Value.Value; } 65 set { maximumExponentParameter.Value.Value = value; } 66 } 67 68 public int MinimumSprintIterations { 69 get { return minimumSprintIterationsParameter.Value.Value; } 70 set { minimumSprintIterationsParameter.Value.Value = value; } 71 } 72 50 73 [StorableConstructor] 51 private IteratedLS(bool deserializing) : base(deserializing) { }52 private IteratedLS(IteratedLS original, Cloner cloner)74 private PLAHCS(bool deserializing) : base(deserializing) { } 75 private PLAHCS(PLAHCS original, Cloner cloner) 53 76 : base(original, cloner) { 54 } 55 public IteratedLS() { 77 maximumExponentParameter = cloner.Clone(original.maximumExponentParameter); 78 minimumSprintIterationsParameter = cloner.Clone(original.minimumSprintIterationsParameter); 79 } 80 public PLAHCS() { 81 Parameters.Add(maximumExponentParameter = new FixedValueParameter<IntValue>("MaximumExponent", "The maximum power to which memory sizes should be tried (2^x). Do not set higher than 31", new IntValue(24))); 82 Parameters.Add(minimumSprintIterationsParameter = new FixedValueParameter<IntValue>("MinimumSprintIterations", "The minimum amount of iterations per sprint.", new IntValue(100000))); 56 83 57 84 Problem = new GQAP(); … … 59 86 60 87 public override IDeepCloneable Clone(Cloner cloner) { 61 return new IteratedLS(this, cloner);88 return new PLAHCS(this, cloner); 62 89 } 63 90 … … 66 93 67 94 Context.Problem = Problem; 68 Context.BestQuality = double.NaN; 69 Context.BestSolution = null; 70 71 var assign = GreedyRandomizedSolutionCreator.CreateSolution(Context.Random, Problem.ProblemInstance, 10, true, token); 95 Context.LastSuccess = 0; 96 97 var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length); 72 98 var eval = Problem.ProblemInstance.Evaluate(assign); 73 99 var fit = Problem.ProblemInstance.ToSingleObjective(eval); … … 75 101 76 102 var candidate = new GQAPSolution(assign, eval); 77 var lsevaluations = 0;78 OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);79 Context.EvaluatedSolutions += lsevaluations;80 103 81 104 Context.ReplaceIncumbent(Context.ToScope(candidate, fit)); … … 83 106 Context.BestSolution = (GQAPSolution)candidate.Clone(); 84 107 108 Context.BestList = new ItemList<DoubleValue>(new[] { new DoubleValue(Context.BestQuality) }); 109 110 Results.Add(new Result("Sprint Iterations", new IntValue(Context.SprintIterations))); 85 111 Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 86 112 Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 87 Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality))); 88 Results.Add(new Result("BestSolution", Context.BestSolution)); 89 90 Context.RunOperator(Analyzer, Context.Scope, token); 113 Results.Add(new Result("CurrentMemorySize", new IntValue(0))); 91 114 } 92 115 93 116 protected override void Run(CancellationToken cancellationToken) { 94 while (!StoppingCriterion()) { 95 var lsevaluations = 0; 96 var candidate = (GQAPSolution)Context.Incumbent.Solution.Clone(); 97 RandomWalk(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, candidate.Assignment.Length); 98 candidate.Evaluation = Problem.ProblemInstance.Evaluate(candidate.Assignment); 99 Context.EvaluatedSolutions++; 100 OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations); 101 Context.EvaluatedSolutions += lsevaluations; 102 103 var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation); 104 if (candidateFit < Context.Incumbent.Fitness) { 105 Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit)); 106 Context.BestQuality = candidateFit; 107 Context.BestSolution = (GQAPSolution)candidate.Clone(); 117 var lastUpdate = ExecutionTime; 118 for (var i = 0; i <= MaximumExponent; i++) { 119 var l = (int)Math.Pow(2, i); 120 var memory = new double[l]; 121 for (var vv = 0; vv < l; vv++) 122 memory[vv] = Context.BestList[Context.BestList.Count - 1 - (vv % Context.BestList.Count)].Value; 123 Array.Sort(memory); 124 Context.Memory = new DoubleArray(memory); 125 if (i > 0) Context.ReplaceIncumbent(Context.ToScope((GQAPSolution)Context.BestSolution.Clone(), Context.BestQuality)); 126 IResult memorySizeResult; 127 if (Results.TryGetValue("CurrentMemorySize", out memorySizeResult)) 128 ((IntValue)memorySizeResult.Value).Value = Context.Memory.Length; 129 else Results.Add(new Result("CurrentMemorySize", new IntValue(Context.Memory.Length))); 130 131 Context.SprintIterations = 0; 132 while (!StoppingCriterion() 133 && (Context.SprintIterations < MinimumSprintIterations 134 || (Context.SprintIterations - Context.LastSuccess) < Context.SprintIterations * 0.02)) { 135 136 var move = StochasticNMoveSingleMoveGenerator.GenerateOneMove(Context.Random, 137 Context.Incumbent.Solution.Assignment, Problem.ProblemInstance.Capacities); 138 var moveEval = GQAPNMoveEvaluator.Evaluate(move, 139 Context.Incumbent.Solution.Assignment, 140 Context.Incumbent.Solution.Evaluation, Problem.ProblemInstance); 141 if (Context.SprintIterations % Problem.ProblemInstance.Demands.Length == 0) 142 Context.EvaluatedSolutions++; 143 var nextFit = Problem.ProblemInstance.ToSingleObjective(moveEval); 144 var nextVec = new IntegerVector(Context.Incumbent.Solution.Assignment); 145 NMoveMaker.Apply(nextVec, move); 146 147 var v = Context.Iterations % Context.Memory.Length; 148 Context.Iterations++; 149 Context.SprintIterations++; 150 var prevFit = Context.Memory[v]; 151 152 var accept = nextFit <= Context.Incumbent.Fitness 153 || nextFit <= prevFit; 154 155 if (accept && nextFit < Context.Incumbent.Fitness) 156 Context.LastSuccess = Context.SprintIterations; 157 158 if (accept) { 159 Context.ReplaceIncumbent(Context.ToScope(new GQAPSolution(nextVec, moveEval), nextFit)); 160 if (nextFit < Context.BestQuality) { 161 Context.BestSolution = (GQAPSolution)Context.Incumbent.Solution.Clone(); 162 Context.BestQuality = nextFit; 163 Context.BestList.Add(new DoubleValue(Context.BestQuality)); 164 } 165 } 166 167 if (Context.Incumbent.Fitness < prevFit) 168 Context.Memory[v] = Context.Incumbent.Fitness; 169 170 IResult result; 171 if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) { 172 if (Results.TryGetValue("Iterations", out result)) 173 ((IntValue)result.Value).Value = Context.Iterations; 174 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 175 if (Results.TryGetValue("Sprint Iterations", out result)) 176 ((IntValue)result.Value).Value = Context.SprintIterations; 177 else Results.Add(new Result("Total Iterations", new IntValue(Context.SprintIterations))); 178 if (Results.TryGetValue("EvaluatedSolutions", out result)) 179 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 180 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 181 lastUpdate = ExecutionTime; 182 } 183 if (Results.TryGetValue("BestQuality", out result)) 184 ((DoubleValue)result.Value).Value = Context.BestQuality; 185 else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality))); 186 if (Results.TryGetValue("BestSolution", out result)) 187 result.Value = Context.BestSolution; 188 else Results.Add(new Result("BestSolution", Context.BestSolution)); 189 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 190 191 if (cancellationToken.IsCancellationRequested) break; 108 192 } 109 110 IResult result; 111 if (Results.TryGetValue("Iterations", out result)) 112 ((IntValue)result.Value).Value = Context.Iterations; 113 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 114 if (Results.TryGetValue("EvaluatedSolutions", out result)) 115 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 116 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 117 if (Results.TryGetValue("BestQuality", out result)) 118 ((DoubleValue)result.Value).Value = Context.BestQuality; 119 else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality))); 120 if (Results.TryGetValue("BestSolution", out result)) 121 result.Value = Context.BestSolution; 122 else Results.Add(new Result("BestSolution", Context.BestSolution)); 123 124 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 125 126 Context.Iterations++; 127 if (cancellationToken.IsCancellationRequested) break; 193 if (StoppingCriterion() || cancellationToken.IsCancellationRequested) break; 128 194 } 129 } 130 131 private static void RandomWalk(IRandom random, IntegerVector assignment, int locations, int walkLength) { 132 for (int i = 0; i < walkLength; i++) { 133 var equipment = random.Next(assignment.Length); 134 assignment[equipment] = random.Next(locations); 135 if (random.NextDouble() < 1.0 / walkLength) break; 136 } 195 196 IResult result2; 197 if (Results.TryGetValue("Iterations", out result2)) 198 ((IntValue)result2.Value).Value = Context.Iterations; 199 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 200 if (Results.TryGetValue("Sprint Iterations", out result2)) 201 ((IntValue)result2.Value).Value = Context.SprintIterations; 202 else Results.Add(new Result("Sprint Iterations", new IntValue(Context.SprintIterations))); 203 if (Results.TryGetValue("EvaluatedSolutions", out result2)) 204 ((IntValue)result2.Value).Value = Context.EvaluatedSolutions; 205 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 137 206 } 138 207 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/pLAHCContext.cs
r15562 r15563 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data; 24 25 using HeuristicLab.Parameters; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 27 27 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.L ocalSearch{28 public sealed class LocalSearchContext : SingleObjectiveSingleSolutionContext<ISingleObjectiveSolutionScope<GQAPSolution>> {28 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LAHC { 29 public sealed class PLAHCContext : SingleObjectiveSingleSolutionContext<ISingleObjectiveSolutionScope<GQAPSolution>> { 29 30 [Storable] 30 31 private IValueParameter<GQAP> problem; … … 40 41 set { bestSolution.Value = value; } 41 42 } 43 44 [Storable] 45 private IValueParameter<IntValue> sprintIterations; 46 public int SprintIterations { 47 get { return sprintIterations.Value.Value; } 48 set { sprintIterations.Value.Value = value; } 49 } 50 51 [Storable] 52 private IValueParameter<DoubleArray> memory; 53 public DoubleArray Memory { 54 get { return memory.Value; } 55 set { memory.Value = value; } 56 } 57 58 [Storable] 59 private IValueParameter<IntValue> lastSuccess; 60 public int LastSuccess { 61 get { return lastSuccess.Value.Value; } 62 set { lastSuccess.Value.Value = value; } 63 } 64 65 [Storable] 66 private IValueParameter<ItemList<DoubleValue>> bestList; 67 public ItemList<DoubleValue> BestList { 68 get { return bestList.Value; } 69 set { bestList.Value = value; } 70 } 42 71 43 72 [StorableConstructor] 44 private LocalSearchContext(bool deserializing) : base(deserializing) { }45 private LocalSearchContext(LocalSearchContext original, Cloner cloner)73 private PLAHCContext(bool deserializing) : base(deserializing) { } 74 private PLAHCContext(PLAHCContext original, Cloner cloner) 46 75 : base(original, cloner) { 47 76 problem = cloner.Clone(original.problem); 48 77 bestSolution = cloner.Clone(original.bestSolution); 78 sprintIterations = cloner.Clone(original.sprintIterations); 79 memory = cloner.Clone(original.memory); 80 lastSuccess = cloner.Clone(original.lastSuccess); 81 bestList = cloner.Clone(original.bestList); 49 82 } 50 public LocalSearchContext() {83 public PLAHCContext() { 51 84 Parameters.Add(problem = new ValueParameter<GQAP>("Problem")); 52 85 Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution")); 86 Parameters.Add(sprintIterations = new ValueParameter<IntValue>("SprintIterations")); 87 Parameters.Add(memory = new ValueParameter<DoubleArray>("Memory")); 88 Parameters.Add(lastSuccess = new ValueParameter<IntValue>("LastSuccess")); 89 Parameters.Add(bestList = new ValueParameter<ItemList<DoubleValue>>("BestList")); 53 90 } 54 91 55 92 public override IDeepCloneable Clone(Cloner cloner) { 56 return new LocalSearchContext(this, cloner);93 return new PLAHCContext(this, cloner); 57 94 } 58 95 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/IteratedLS.cs
r15562 r15563 33 33 [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)] 34 34 [StorableClass] 35 public sealed class IteratedLS : ContextAlgorithm<LocalSearchContext> {35 public sealed class IteratedLS : StochasticAlgorithm<LocalSearchContext> { 36 36 37 37 public override bool SupportsPause { … … 92 92 93 93 protected override void Run(CancellationToken cancellationToken) { 94 var lastUpdate = ExecutionTime; 95 94 96 while (!StoppingCriterion()) { 95 97 var lsevaluations = 0; … … 109 111 110 112 IResult result; 111 if (Results.TryGetValue("Iterations", out result)) 112 ((IntValue)result.Value).Value = Context.Iterations; 113 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 114 if (Results.TryGetValue("EvaluatedSolutions", out result)) 115 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 116 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 113 if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) { 114 if (Results.TryGetValue("Iterations", out result)) 115 ((IntValue)result.Value).Value = Context.Iterations; 116 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 117 if (Results.TryGetValue("EvaluatedSolutions", out result)) 118 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 119 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 120 lastUpdate = ExecutionTime; 121 } 117 122 if (Results.TryGetValue("BestQuality", out result)) 118 123 ((DoubleValue)result.Value).Value = Context.BestQuality; … … 127 132 if (cancellationToken.IsCancellationRequested) break; 128 133 } 134 IResult result2; 135 if (Results.TryGetValue("Iterations", out result2)) 136 ((IntValue)result2.Value).Value = Context.Iterations; 137 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 138 if (Results.TryGetValue("EvaluatedSolutions", out result2)) 139 ((IntValue)result2.Value).Value = Context.EvaluatedSolutions; 140 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 129 141 } 130 142 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/MultistartLS.cs
r15562 r15563 33 33 [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)] 34 34 [StorableClass] 35 public sealed class MultistartLS : ContextAlgorithm<LocalSearchContext> {35 public sealed class MultistartLS : StochasticAlgorithm<LocalSearchContext> { 36 36 37 37 public override bool SupportsPause { … … 71 71 72 72 protected override void Run(CancellationToken cancellationToken) { 73 var lastUpdate = ExecutionTime; 74 73 75 while (!StoppingCriterion()) { 74 76 var lsevaluations = 0; … … 89 91 90 92 IResult result; 91 if (Results.TryGetValue("Iterations", out result)) 92 ((IntValue)result.Value).Value = Context.Iterations; 93 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 94 if (Results.TryGetValue("EvaluatedSolutions", out result)) 95 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 96 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 93 if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) { 94 if (Results.TryGetValue("Iterations", out result)) 95 ((IntValue)result.Value).Value = Context.Iterations; 96 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 97 if (Results.TryGetValue("EvaluatedSolutions", out result)) 98 ((IntValue)result.Value).Value = Context.EvaluatedSolutions; 99 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 100 lastUpdate = ExecutionTime; 101 } 97 102 if (Results.TryGetValue("BestQuality", out result)) 98 103 ((DoubleValue)result.Value).Value = Context.BestQuality; … … 107 112 if (cancellationToken.IsCancellationRequested) break; 108 113 } 114 115 IResult result2; 116 if (Results.TryGetValue("Iterations", out result2)) 117 ((IntValue)result2.Value).Value = Context.Iterations; 118 else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); 119 if (Results.TryGetValue("EvaluatedSolutions", out result2)) 120 ((IntValue)result2.Value).Value = Context.EvaluatedSolutions; 121 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 109 122 } 110 123 }
Note: See TracChangeset
for help on using the changeset viewer.