Changeset 15564 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary
- Timestamp:
- 01/01/18 23:52:39 (7 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary
- Files:
-
- 1 added
- 2 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
Note: See TracChangeset
for help on using the changeset viewer.