Changeset 15563 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary
- Timestamp:
- 12/30/17 23:10:29 (7 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary
- Files:
-
- 3 edited
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 }
Note: See TracChangeset
for help on using the changeset viewer.