Changeset 15564 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms
- Timestamp:
- 01/01/18 23:52:39 (7 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3
- Files:
-
- 1 added
- 9 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/ESContext.cs
r15563 r15564 20 20 #endregion 21 21 22 using System.Collections.Generic;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 50 49 get { return normalRand.Value; } 51 50 set { normalRand.Value = value; } 52 }53 54 public void ReplacePopulation(IEnumerable<ISingleObjectiveSolutionScope<ESGQAPSolution>> replacement) {55 Scope.SubScopes.Replace(replacement);56 51 } 57 52 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/EvolutionStrategy.cs
r15563 r15564 69 69 get { return selectionParameter; } 70 70 } 71 [Storable] 72 private FixedValueParameter<BoolValue> useRecombinationParameter; 73 public IFixedValueParameter<BoolValue> UseRecombinationParameter { 74 get { return useRecombinationParameter; } 75 } 71 76 72 77 public int Lambda { … … 81 86 get { return selectionParameter.Value.Value; } 82 87 set { selectionParameter.Value.Value = value; } 88 } 89 public bool UseRecombination { 90 get { return useRecombinationParameter.Value.Value; } 91 set { useRecombinationParameter.Value.Value = value; } 83 92 } 84 93 … … 90 99 muParameter = cloner.Clone(original.muParameter); 91 100 selectionParameter = cloner.Clone(original.selectionParameter); 101 useRecombinationParameter = cloner.Clone(original.useRecombinationParameter); 92 102 } 93 103 public EvolutionStrategy() { … … 95 105 Parameters.Add(muParameter = new FixedValueParameter<IntValue>("Mu", "(μ) The population size.", new IntValue(1))); 96 106 Parameters.Add(selectionParameter= new FixedValueParameter<EnumValue<ESSelection>>("Selection", "The selection strategy: elitist (plus) or generational (comma).", new EnumValue<ESSelection>(ESSelection.Plus))); 97 107 Parameters.Add(useRecombinationParameter = new FixedValueParameter<BoolValue>("Use Recombination", "Whether to create an \"intermediate\" solution to perform the mutation from.", new BoolValue(false))); 108 98 109 Problem = new GQAP(); 99 110 } … … 116 127 Context.EvaluatedSolutions++; 117 128 118 var ind = new ESGQAPSolution(assign, eval, Context.Random.NextDouble() * 2 - 1); 129 var min = (1.0 / assign.Length) * 2 - 1; // desired min prob 130 var max = (4.0 / assign.Length) * 2 - 1; // desired max prob 131 min = 0.5 * (Math.Log(1 + min) - Math.Log(1 - min)); // arctanh 132 max = 0.5 * (Math.Log(1 + max) - Math.Log(1 - max)); 133 var ind = new ESGQAPSolution(assign, eval, min + Context.Random.NextDouble() * (max - min)); 119 134 var fit = Problem.ProblemInstance.ToSingleObjective(eval); 120 135 Context.AddToPopulation(Context.ToScope(ind, fit)); … … 135 150 protected override void Run(CancellationToken cancellationToken) { 136 151 var lastUpdate = ExecutionTime; 152 var eq = new IntegerVectorEqualityComparer(); 137 153 138 154 while (!StoppingCriterion()) { … … 140 156 141 157 for (var l = 0; l < Lambda; l++) { 142 var m = Context.AtRandomPopulation(); 143 144 var offspring = (ESGQAPSolution)m.Solution.Clone(); 145 var count = Mutate(m, offspring); 146 offspring.SParam += 0.7071 * Context.NormalRand.NextDouble(); //((1.0 / count) - offspring.SParam) / 10.0; 147 148 offspring.Evaluation = Problem.ProblemInstance.Evaluate(offspring.Assignment); 158 IntegerVector child = null; 159 var sParam = 0.0; 160 if (UseRecombination) { 161 child = DiscreteLocationCrossover.Apply(Context.Random, new ItemArray<IntegerVector>(Context.Population.Select(x => x.Solution.Assignment)), Problem.ProblemInstance.Demands, Problem.ProblemInstance.Capacities); 162 sParam = Context.Population.Select(x => x.Solution.SParam).Average(); 163 } else { 164 var m = Context.AtRandomPopulation(); 165 child = (IntegerVector)m.Solution.Assignment.Clone(); 166 sParam = m.Solution.SParam; 167 } 168 sParam += 0.7071 * Context.NormalRand.NextDouble(); 169 RelocateEquipmentManipluator.Apply(Context.Random, child, 170 Problem.ProblemInstance.Capacities.Length, (Math.Tanh(sParam) + 1) / 2.0); 171 var eval = Problem.ProblemInstance.Evaluate(child); 149 172 Context.EvaluatedSolutions++; 173 174 var offspring = new ESGQAPSolution(child, eval, sParam); 175 150 176 var fit = Problem.ProblemInstance.ToSingleObjective(offspring.Evaluation); 151 nextGen.Add(Context.ToScope(offspring, fit)); 177 if (Selection == ESSelection.Comma || Context.Population.Select(x => x.Solution.Assignment).All(x => !eq.Equals(child, x))) 178 nextGen.Add(Context.ToScope(offspring, fit)); 152 179 153 180 if (fit < Context.BestQuality) { … … 160 187 Context.ReplacePopulation(nextGen.OrderBy(x => x.Fitness).Take(Mu)); 161 188 } else if (Selection == ESSelection.Plus) { 162 var best = nextGen.Concat(Context.Population).OrderBy(x => x.Fitness).Take(Mu).ToList();189 var best = Context.Population.Concat(nextGen).OrderBy(x => x.Fitness).Take(Mu).ToList(); 163 190 Context.ReplacePopulation(best); 164 191 } else throw new InvalidOperationException("Unknown Selection strategy: " + Selection); … … 181 208 else Results.Add(new Result("BestSolution", Context.BestSolution)); 182 209 183 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 210 try { 211 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 212 } catch (OperationCanceledException) { } 184 213 185 214 Context.Iterations++; … … 194 223 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 195 224 } 196 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[199 var offspringFeasible = offspring.Evaluation.IsFeasible;200 double[] slack = null;201 if (offspringFeasible) slack = offspring.Evaluation.Slack.ToArray();202 var count = 1;203 foreach (var equip in Enumerable.Range(0, Problem.ProblemInstance.Demands.Length).Shuffle(Context.Random)) {204 var currentLoc = offspring.Assignment[equip];205 if (offspringFeasible) {206 var demand = Problem.ProblemInstance.Demands[equip];207 var feasibleLoc = slack.Select((v, i) => new { Index = i, Value = v })208 .Where(x => x.Index != currentLoc209 && x.Value >= demand).ToList();210 int newLoc = -1;211 if (feasibleLoc.Count == 0) {212 newLoc = Context.Random.Next(Problem.ProblemInstance.Capacities.Length - 1);213 if (newLoc >= currentLoc) newLoc++; // don't reassign to current loc214 offspringFeasible = false;215 } else {216 newLoc = feasibleLoc.SampleRandom(Context.Random).Index;217 }218 offspring.Assignment[equip] = newLoc;219 slack[currentLoc] += demand;220 slack[newLoc] -= demand;221 } else {222 var newLoc = Context.Random.Next(Problem.ProblemInstance.Capacities.Length - 1);223 if (newLoc >= currentLoc) newLoc++; // don't reassign to current loc224 offspring.Assignment[equip] = newLoc;225 }226 if (Context.Random.NextDouble() < stopProb) break;227 count++;228 }229 230 return count;231 }232 225 } 233 226 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/OSGAContext.cs
r15562 r15564 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 25 26 using HeuristicLab.Parameters; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 28 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms. GRASP{29 [Item(" GRASP+PR (GQAP) Context", "Context for GRASP+PR(GQAP).")]29 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.Evolutionary { 30 [Item("OSGA (GQAP) Context", "Context for OSGA (GQAP).")] 30 31 [StorableClass] 31 public sealed class GRASPContext : SingleObjectivePopulationContext<ISingleObjectiveSolutionScope<GQAPSolution>> {32 public sealed class OSGAContext : SingleObjectivePopulationContext<ISingleObjectiveSolutionScope<GQAPSolution>> { 32 33 33 34 [Storable] … … 44 45 set { bestSolution.Value = value; } 45 46 } 46 47 48 [Storable] 49 private IValueParameter<ItemList<ISingleObjectiveSolutionScope<GQAPSolution>>> nextGeneration; 50 public ItemList<ISingleObjectiveSolutionScope<GQAPSolution>> NextGeneration { 51 get { return nextGeneration.Value; } 52 set { nextGeneration.Value = value; } 53 } 54 55 [Storable] 56 private IValueParameter<DoubleValue> selectionPressure; 57 public double SelectionPressure { 58 get { return selectionPressure.Value.Value; } 59 set { selectionPressure.Value.Value = value; } 60 } 61 62 [Storable] 63 private IValueParameter<IntValue> attempts; 64 public int Attempts { 65 get { return attempts.Value.Value; } 66 set { attempts.Value.Value = value; } 67 } 68 47 69 public void SortPopulation() { 48 70 Scope.SubScopes.Replace(Scope.SubScopes.OfType<ISingleObjectiveSolutionScope<GQAPSolution>>().OrderBy(x => Problem.Maximization ? -x.Fitness : x.Fitness).ToList()); … … 50 72 51 73 [StorableConstructor] 52 private GRASPContext(bool deserializing) : base(deserializing) { }53 private GRASPContext(GRASPContext original, Cloner cloner)74 private OSGAContext(bool deserializing) : base(deserializing) { } 75 private OSGAContext(OSGAContext original, Cloner cloner) 54 76 : base(original, cloner) { 55 77 problem = cloner.Clone(original.problem); 56 78 bestSolution = cloner.Clone(original.bestSolution); 79 nextGeneration = cloner.Clone(original.nextGeneration); 80 selectionPressure = cloner.Clone(original.selectionPressure); 81 attempts = cloner.Clone(original.attempts); 57 82 } 58 public GRASPContext() : this("GRASP+PR(GQAP) Context") { }59 public GRASPContext(string name) : base(name) {83 public OSGAContext() : this("OSGA (GQAP) Context") { } 84 public OSGAContext(string name) : base(name) { 60 85 Parameters.Add(problem = new ValueParameter<GQAP>("Problem")); 61 86 Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution")); 87 Parameters.Add(nextGeneration = new ValueParameter<ItemList<ISingleObjectiveSolutionScope<GQAPSolution>>>("NextGeneration")); 88 Parameters.Add(selectionPressure = new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0))); 89 Parameters.Add(attempts = new ValueParameter<IntValue>("Attempts", new IntValue(0))); 62 90 } 63 91 64 92 public override IDeepCloneable Clone(Cloner cloner) { 65 return new GRASPContext(this, cloner);93 return new OSGAContext(this, cloner); 66 94 } 67 95 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASP/GRASP.cs
r15563 r15564 167 167 while (!StoppingCriterion()) { // line 2 in Algorithm 1 168 168 // next: line 3 in Algorithm 1 169 var pi_prime_vec = GreedyRandomizedSolutionCreator.CreateSolution(Context.Random, Problem.ProblemInstance, 1000, false, cancellationToken); 169 IntegerVector pi_prime_vec = null; 170 try { 171 pi_prime_vec = GreedyRandomizedSolutionCreator.CreateSolution(Context.Random, Problem.ProblemInstance, 1000, false, cancellationToken); 172 } catch (OperationCanceledException) { break; } 173 170 174 if (Context.PopulationCount >= MinimumEliteSetSize) { // line 4 in Algorithm 1 171 175 GQAPSolution pi_prime; … … 234 238 else Results.Add(new Result("BestSolution", Context.BestSolution)); 235 239 236 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 240 try { 241 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 242 } catch (OperationCanceledException) { } 237 243 238 244 Context.Iterations++; -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms-3.3.csproj
r15563 r15564 102 102 <Compile Include="Evolutionary\ESGQAPSolution.cs" /> 103 103 <Compile Include="Evolutionary\EvolutionStrategy.cs" /> 104 <Compile Include="Evolutionary\OSGAContext.cs" /> 105 <Compile Include="Evolutionary\OSGA.cs" /> 104 106 <Compile Include="Infrastructure\Algorithms\StochasticAlgorithm.cs" /> 105 107 <Compile Include="Infrastructure\Contexts\SingleObjectiveSingleSolutionContext.cs" /> -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Contexts/PopulationContext.cs
r15562 r15564 40 40 Scope.SubScopes[index] = solScope; 41 41 } 42 public void ReplacePopulation(IEnumerable<TSolutionScope> replacement) { 43 Scope.SubScopes.Replace(replacement); 44 } 42 45 public TSolutionScope AtPopulation(int index) { 43 46 return Scope.SubScopes[index] as TSolutionScope; -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/LAHC.cs
r15563 r15564 156 156 else Results.Add(new Result("BestSolution", Context.BestSolution)); 157 157 158 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 159 158 try { 159 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 160 } catch (OperationCanceledException) { } 161 160 162 if (cancellationToken.IsCancellationRequested) break; 161 163 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/pLAHC.cs
r15563 r15564 187 187 result.Value = Context.BestSolution; 188 188 else Results.Add(new Result("BestSolution", Context.BestSolution)); 189 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 189 190 try { 191 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 192 } catch (OperationCanceledException) { } 190 193 191 194 if (cancellationToken.IsCancellationRequested) break; -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/IteratedLS.cs
r15563 r15564 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Encodings.IntegerVectorEncoding;28 27 using HeuristicLab.Optimization; 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 30 … … 48 48 } 49 49 50 [Storable] 51 private IFixedValueParameter<PercentValue> perturbationStrengthParameter; 52 public IFixedValueParameter<PercentValue> PerturbationStrengthParameter { 53 get { return perturbationStrengthParameter; } 54 } 55 56 public double PerturbationStrength { 57 get { return perturbationStrengthParameter.Value.Value; } 58 set { perturbationStrengthParameter.Value.Value = value; } 59 } 60 50 61 [StorableConstructor] 51 62 private IteratedLS(bool deserializing) : base(deserializing) { } … … 54 65 } 55 66 public IteratedLS() { 67 Parameters.Add(perturbationStrengthParameter = new FixedValueParameter<PercentValue>("PerturbationStrength", "The expected length of the random walk relative to the size of the solution vector.", new PercentValue(0.5))); 56 68 57 69 Problem = new GQAP(); … … 97 109 var lsevaluations = 0; 98 110 var candidate = (GQAPSolution)Context.Incumbent.Solution.Clone(); 99 R andomWalk(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, candidate.Assignment.Length);111 RelocateEquipmentManipluator.Apply(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, 1.0 / (PerturbationStrength * candidate.Assignment.Length)); 100 112 candidate.Evaluation = Problem.ProblemInstance.Evaluate(candidate.Assignment); 101 113 Context.EvaluatedSolutions++; … … 127 139 else Results.Add(new Result("BestSolution", Context.BestSolution)); 128 140 129 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 141 try { 142 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 143 } catch (OperationCanceledException) { } 130 144 131 145 Context.Iterations++; … … 140 154 else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); 141 155 } 142 143 private static void RandomWalk(IRandom random, IntegerVector assignment, int locations, int walkLength) {144 for (int i = 0; i < walkLength; i++) {145 var equipment = random.Next(assignment.Length);146 assignment[equipment] = random.Next(locations);147 if (random.NextDouble() < 1.0 / walkLength) break;148 }149 }150 156 } 151 157 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/MultistartLS.cs
r15563 r15564 107 107 else Results.Add(new Result("BestSolution", Context.BestSolution)); 108 108 109 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 109 try { 110 Context.RunOperator(Analyzer, Context.Scope, cancellationToken); 111 } catch (OperationCanceledException) { } 110 112 111 113 Context.Iterations++;
Note: See TracChangeset
for help on using the changeset viewer.