1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Runtime.CompilerServices;
|
---|
26 | using System.Threading;
|
---|
27 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
28 | using HeuristicLab.Analysis.FitnessLandscape;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
35 | using HeuristicLab.Random;
|
---|
36 | using ExecutionContext = HeuristicLab.Core.ExecutionContext;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Algorithms.MemPR {
|
---|
39 | [Item("MemPRContext", "Abstract base class for MemPR contexts.")]
|
---|
40 | [StorableClass]
|
---|
41 | public abstract class MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
42 | IPopulationBasedHeuristicAlgorithmContext<TProblem, TSolution>, ISolutionModelContext<TSolution>, IEvaluationServiceContext<TSolution>,
|
---|
43 | ILocalSearchPathContext<TSolution>
|
---|
44 | where TProblem : class, IItem, ISingleObjectiveHeuristicOptimizationProblem
|
---|
45 | where TSolution : class, IItem
|
---|
46 | where TPopulationContext : MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext>
|
---|
47 | where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TPopulationContext, TSolutionContext> {
|
---|
48 |
|
---|
49 | private IExecutionContext parent;
|
---|
50 | public IExecutionContext Parent {
|
---|
51 | get { return parent; }
|
---|
52 | set { parent = value; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | [Storable]
|
---|
56 | private IScope scope;
|
---|
57 | public IScope Scope {
|
---|
58 | get { return scope; }
|
---|
59 | private set { scope = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
|
---|
63 | get { return Parameters; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | [Storable]
|
---|
67 | private IValueParameter<TProblem> problem;
|
---|
68 | public TProblem Problem {
|
---|
69 | get { return problem.Value; }
|
---|
70 | set { problem.Value = value; }
|
---|
71 | }
|
---|
72 | public bool Maximization {
|
---|
73 | get { return ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | [Storable]
|
---|
77 | private IValueParameter<BoolValue> initialized;
|
---|
78 | public bool Initialized {
|
---|
79 | get { return initialized.Value.Value; }
|
---|
80 | set { initialized.Value.Value = value; }
|
---|
81 | }
|
---|
82 |
|
---|
83 | [Storable]
|
---|
84 | private IValueParameter<IntValue> iterations;
|
---|
85 | public int Iterations {
|
---|
86 | get { return iterations.Value.Value; }
|
---|
87 | set { iterations.Value.Value = value; }
|
---|
88 | }
|
---|
89 |
|
---|
90 | [Storable]
|
---|
91 | private IValueParameter<IntValue> evaluatedSolutions;
|
---|
92 | public int EvaluatedSolutions {
|
---|
93 | get { return evaluatedSolutions.Value.Value; }
|
---|
94 | set { evaluatedSolutions.Value.Value = value; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | [Storable]
|
---|
98 | private IValueParameter<DoubleValue> bestQuality;
|
---|
99 | public double BestQuality {
|
---|
100 | get { return bestQuality.Value.Value; }
|
---|
101 | set { bestQuality.Value.Value = value; }
|
---|
102 | }
|
---|
103 |
|
---|
104 | [Storable]
|
---|
105 | private IValueParameter<TSolution> bestSolution;
|
---|
106 | public TSolution BestSolution {
|
---|
107 | get { return bestSolution.Value; }
|
---|
108 | set { bestSolution.Value = value; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | [Storable]
|
---|
112 | private IValueParameter<IntValue> localSearchEvaluations;
|
---|
113 | public int LocalSearchEvaluations {
|
---|
114 | get { return localSearchEvaluations.Value.Value; }
|
---|
115 | set { localSearchEvaluations.Value.Value = value; }
|
---|
116 | }
|
---|
117 |
|
---|
118 | [Storable]
|
---|
119 | private IValueParameter<DoubleValue> localOptimaLevel;
|
---|
120 | public double LocalOptimaLevel {
|
---|
121 | get { return localOptimaLevel.Value.Value; }
|
---|
122 | set { localOptimaLevel.Value.Value = value; }
|
---|
123 | }
|
---|
124 |
|
---|
125 | [Storable]
|
---|
126 | private IValueParameter<IntValue> byBreeding;
|
---|
127 | public int ByBreeding {
|
---|
128 | get { return byBreeding.Value.Value; }
|
---|
129 | set { byBreeding.Value.Value = value; }
|
---|
130 | }
|
---|
131 |
|
---|
132 | [Storable]
|
---|
133 | private IValueParameter<IntValue> byRelinking;
|
---|
134 | public int ByRelinking {
|
---|
135 | get { return byRelinking.Value.Value; }
|
---|
136 | set { byRelinking.Value.Value = value; }
|
---|
137 | }
|
---|
138 |
|
---|
139 | [Storable]
|
---|
140 | private IValueParameter<IntValue> byDelinking;
|
---|
141 | public int ByDelinking {
|
---|
142 | get { return byDelinking.Value.Value; }
|
---|
143 | set { byDelinking.Value.Value = value; }
|
---|
144 | }
|
---|
145 |
|
---|
146 | [Storable]
|
---|
147 | private IValueParameter<IntValue> bySampling;
|
---|
148 | public int BySampling {
|
---|
149 | get { return bySampling.Value.Value; }
|
---|
150 | set { bySampling.Value.Value = value; }
|
---|
151 | }
|
---|
152 |
|
---|
153 | [Storable]
|
---|
154 | private IValueParameter<IntValue> byHillclimbing;
|
---|
155 | public int ByHillclimbing {
|
---|
156 | get { return byHillclimbing.Value.Value; }
|
---|
157 | set { byHillclimbing.Value.Value = value; }
|
---|
158 | }
|
---|
159 |
|
---|
160 | [Storable]
|
---|
161 | private IValueParameter<IntValue> byAdaptivewalking;
|
---|
162 | public int ByAdaptivewalking {
|
---|
163 | get { return byAdaptivewalking.Value.Value; }
|
---|
164 | set { byAdaptivewalking.Value.Value = value; }
|
---|
165 | }
|
---|
166 |
|
---|
167 | [Storable]
|
---|
168 | private IValueParameter<DirectedPath<TSolution>> relinkedPaths;
|
---|
169 | public DirectedPath<TSolution> RelinkedPaths {
|
---|
170 | get { return relinkedPaths.Value; }
|
---|
171 | set { relinkedPaths.Value = value; }
|
---|
172 | }
|
---|
173 |
|
---|
174 | [Storable]
|
---|
175 | private IValueParameter<DirectedPath<TSolution>> localSearchPaths;
|
---|
176 | public DirectedPath<TSolution> LocalSearchPaths {
|
---|
177 | get { return localSearchPaths.Value; }
|
---|
178 | set { localSearchPaths.Value = value; }
|
---|
179 | }
|
---|
180 |
|
---|
181 | [Storable]
|
---|
182 | private IValueParameter<IRandom> random;
|
---|
183 | public IRandom Random {
|
---|
184 | get { return random.Value; }
|
---|
185 | set { random.Value = value; }
|
---|
186 | }
|
---|
187 |
|
---|
188 | public IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Population {
|
---|
189 | get { return scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>(); }
|
---|
190 | }
|
---|
191 | public void AddToPopulation(ISingleObjectiveSolutionScope<TSolution> solScope) {
|
---|
192 | scope.SubScopes.Add(solScope);
|
---|
193 | }
|
---|
194 | public void ReplaceAtPopulation(int index, ISingleObjectiveSolutionScope<TSolution> solScope) {
|
---|
195 | scope.SubScopes[index] = solScope;
|
---|
196 | }
|
---|
197 | public ISingleObjectiveSolutionScope<TSolution> AtPopulation(int index) {
|
---|
198 | return scope.SubScopes[index] as ISingleObjectiveSolutionScope<TSolution>;
|
---|
199 | }
|
---|
200 | public void SortPopulation() {
|
---|
201 | scope.SubScopes.Replace(scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>().OrderBy(x => Maximization ? -x.Fitness : x.Fitness).ToList());
|
---|
202 | }
|
---|
203 | public int PopulationCount {
|
---|
204 | get { return scope.SubScopes.Count; }
|
---|
205 | }
|
---|
206 |
|
---|
207 | [Storable]
|
---|
208 | private List<Tuple<double, double, double, double>> breedingStat;
|
---|
209 | public IEnumerable<Tuple<double, double, double, double>> BreedingStat {
|
---|
210 | get { return breedingStat; }
|
---|
211 | }
|
---|
212 | [Storable]
|
---|
213 | private List<Tuple<double, double, double, double>> relinkingStat;
|
---|
214 | public IEnumerable<Tuple<double, double, double, double>> RelinkingStat {
|
---|
215 | get { return relinkingStat; }
|
---|
216 | }
|
---|
217 | [Storable]
|
---|
218 | private List<Tuple<double, double, double, double>> delinkingStat;
|
---|
219 | public IEnumerable<Tuple<double, double, double, double>> DelinkingStat {
|
---|
220 | get { return delinkingStat; }
|
---|
221 | }
|
---|
222 | [Storable]
|
---|
223 | private List<Tuple<double, double>> samplingStat;
|
---|
224 | public IEnumerable<Tuple<double, double>> SamplingStat {
|
---|
225 | get { return samplingStat; }
|
---|
226 | }
|
---|
227 | [Storable]
|
---|
228 | private List<Tuple<double, double>> hillclimbingStat;
|
---|
229 | public IEnumerable<Tuple<double, double>> HillclimbingStat {
|
---|
230 | get { return hillclimbingStat; }
|
---|
231 | }
|
---|
232 | [Storable]
|
---|
233 | private List<Tuple<double, double>> adaptivewalkingStat;
|
---|
234 | public IEnumerable<Tuple<double, double>> AdaptivewalkingStat {
|
---|
235 | get { return adaptivewalkingStat; }
|
---|
236 | }
|
---|
237 |
|
---|
238 | public double AverageQuality {
|
---|
239 | get {
|
---|
240 | return Problem.Parameters.ContainsKey("AverageQuality")
|
---|
241 | ? ((IValueParameter<DoubleValue>)Problem.Parameters["AverageQuality"]).Value.Value
|
---|
242 | : double.NaN;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | public double LowerBound {
|
---|
247 | get {
|
---|
248 | return Problem.Parameters.ContainsKey("LowerBound")
|
---|
249 | ? ((IValueParameter<DoubleValue>)Problem.Parameters["LowerBound"]).Value.Value
|
---|
250 | : double.NaN;
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | [Storable]
|
---|
255 | public ISolutionModel<TSolution> Model { get; set; }
|
---|
256 |
|
---|
257 | [StorableConstructor]
|
---|
258 | protected MemPRPopulationContext(bool deserializing) : base(deserializing) { }
|
---|
259 | protected MemPRPopulationContext(MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> original, Cloner cloner)
|
---|
260 | : base(original, cloner) {
|
---|
261 | scope = cloner.Clone(original.scope);
|
---|
262 | problem = cloner.Clone(original.problem);
|
---|
263 | initialized = cloner.Clone(original.initialized);
|
---|
264 | iterations = cloner.Clone(original.iterations);
|
---|
265 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
266 | bestQuality = cloner.Clone(original.bestQuality);
|
---|
267 | bestSolution = cloner.Clone(original.bestSolution);
|
---|
268 | localSearchEvaluations = cloner.Clone(original.localSearchEvaluations);
|
---|
269 | localOptimaLevel = cloner.Clone(original.localOptimaLevel);
|
---|
270 | byBreeding = cloner.Clone(original.byBreeding);
|
---|
271 | byRelinking = cloner.Clone(original.byRelinking);
|
---|
272 | byDelinking = cloner.Clone(original.byDelinking);
|
---|
273 | bySampling = cloner.Clone(original.bySampling);
|
---|
274 | byHillclimbing = cloner.Clone(original.byHillclimbing);
|
---|
275 | byAdaptivewalking = cloner.Clone(original.byAdaptivewalking);
|
---|
276 | relinkedPaths = cloner.Clone(original.relinkedPaths);
|
---|
277 | localSearchPaths = cloner.Clone(original.localSearchPaths);
|
---|
278 | random = cloner.Clone(original.random);
|
---|
279 | breedingStat = original.breedingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3, x.Item4)).ToList();
|
---|
280 | relinkingStat = original.relinkingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3, x.Item4)).ToList();
|
---|
281 | delinkingStat = original.delinkingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3, x.Item4)).ToList();
|
---|
282 | samplingStat = original.samplingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
283 | hillclimbingStat = original.hillclimbingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
284 | adaptivewalkingStat = original.adaptivewalkingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
285 |
|
---|
286 | Model = cloner.Clone(original.Model);
|
---|
287 | }
|
---|
288 | public MemPRPopulationContext() : this("MemPRContext") { }
|
---|
289 | public MemPRPopulationContext(string name) : base(name) {
|
---|
290 | scope = new Scope("Global");
|
---|
291 |
|
---|
292 | Parameters.Add(problem = new ValueParameter<TProblem>("Problem"));
|
---|
293 | Parameters.Add(initialized = new ValueParameter<BoolValue>("Initialized", new BoolValue(false)));
|
---|
294 | Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
295 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
296 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
|
---|
297 | Parameters.Add(bestSolution = new ValueParameter<TSolution>("BestFoundSolution"));
|
---|
298 | Parameters.Add(localSearchEvaluations = new ValueParameter<IntValue>("LocalSearchEvaluations", new IntValue(0)));
|
---|
299 | Parameters.Add(localOptimaLevel = new ValueParameter<DoubleValue>("LocalOptimaLevel", new DoubleValue(0)));
|
---|
300 | Parameters.Add(byBreeding = new ValueParameter<IntValue>("ByBreeding", new IntValue(0)));
|
---|
301 | Parameters.Add(byRelinking = new ValueParameter<IntValue>("ByRelinking", new IntValue(0)));
|
---|
302 | Parameters.Add(byDelinking = new ValueParameter<IntValue>("ByDelinking", new IntValue(0)));
|
---|
303 | Parameters.Add(bySampling = new ValueParameter<IntValue>("BySampling", new IntValue(0)));
|
---|
304 | Parameters.Add(byHillclimbing = new ValueParameter<IntValue>("ByHillclimbing", new IntValue(0)));
|
---|
305 | Parameters.Add(byAdaptivewalking = new ValueParameter<IntValue>("ByAdaptivewalking", new IntValue(0)));
|
---|
306 | Parameters.Add(relinkedPaths = new ValueParameter<DirectedPath<TSolution>>("RelinkedPaths", new DirectedPath<TSolution>()));
|
---|
307 | Parameters.Add(localSearchPaths = new ValueParameter<DirectedPath<TSolution>>("LocalSearchPaths", new DirectedPath<TSolution>()));
|
---|
308 | Parameters.Add(random = new ValueParameter<IRandom>("Random", new MersenneTwister()));
|
---|
309 |
|
---|
310 | breedingStat = new List<Tuple<double, double, double, double>>();
|
---|
311 | relinkingStat = new List<Tuple<double, double, double, double>>();
|
---|
312 | delinkingStat = new List<Tuple<double, double, double, double>>();
|
---|
313 | samplingStat = new List<Tuple<double, double>>();
|
---|
314 | hillclimbingStat = new List<Tuple<double, double>>();
|
---|
315 | adaptivewalkingStat = new List<Tuple<double, double>>();
|
---|
316 | }
|
---|
317 |
|
---|
318 | public abstract ISingleObjectiveSolutionScope<TSolution> ToScope(TSolution code, double fitness = double.NaN);
|
---|
319 |
|
---|
320 | public virtual double Evaluate(TSolution solution, CancellationToken token) {
|
---|
321 | var solScope = ToScope(solution);
|
---|
322 | Evaluate(solScope, token);
|
---|
323 | solScope.Solution = null;
|
---|
324 | return solScope.Fitness;
|
---|
325 | }
|
---|
326 |
|
---|
327 | public virtual void Evaluate(ISingleObjectiveSolutionScope<TSolution> solScope, CancellationToken token) {
|
---|
328 | var pdef = Problem as ISingleObjectiveProblemDefinition;
|
---|
329 | if (pdef != null) {
|
---|
330 | var ind = new SingleEncodingIndividual(pdef.Encoding, solScope);
|
---|
331 | solScope.Fitness = pdef.Evaluate(ind, Random);
|
---|
332 | } else {
|
---|
333 | RunOperator(Problem.Evaluator, solScope, token);
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution);
|
---|
338 |
|
---|
339 | public void IncrementEvaluatedSolutions(int byEvaluations) {
|
---|
340 | if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
|
---|
341 | EvaluatedSolutions += byEvaluations;
|
---|
342 | }
|
---|
343 |
|
---|
344 | #region Breeding Performance
|
---|
345 | public void AddBreedingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
346 | if (IsBetter(a, b))
|
---|
347 | breedingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, child.Fitness));
|
---|
348 | else breedingStat.Add(Tuple.Create(b.Fitness, a.Fitness, parentDist, child.Fitness));
|
---|
349 | }
|
---|
350 | public bool BreedingSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
351 | return true;
|
---|
352 | }
|
---|
353 | #endregion
|
---|
354 |
|
---|
355 | #region Relinking Performance
|
---|
356 | public void AddRelinkingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
357 | if (IsBetter(a, b))
|
---|
358 | relinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - a.Fitness : a.Fitness - child.Fitness));
|
---|
359 | else relinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - b.Fitness : b.Fitness - child.Fitness));
|
---|
360 | }
|
---|
361 | public bool RelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
362 | return true;
|
---|
363 | }
|
---|
364 | #endregion
|
---|
365 |
|
---|
366 | #region Delinking Performance
|
---|
367 | public void AddDelinkingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
368 | if (IsBetter(a, b))
|
---|
369 | delinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - a.Fitness : a.Fitness - child.Fitness));
|
---|
370 | else delinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - b.Fitness : b.Fitness - child.Fitness));
|
---|
371 | }
|
---|
372 | public bool DelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
373 | return true;
|
---|
374 | }
|
---|
375 | #endregion
|
---|
376 |
|
---|
377 | #region Sampling Performance
|
---|
378 | public void AddSamplingResult(ISingleObjectiveSolutionScope<TSolution> sample, double avgDist) {
|
---|
379 | samplingStat.Add(Tuple.Create(avgDist, sample.Fitness));
|
---|
380 | }
|
---|
381 | public bool SamplingSuited(double avgDist) {
|
---|
382 | return true;
|
---|
383 | }
|
---|
384 | #endregion
|
---|
385 |
|
---|
386 | #region Hillclimbing Performance
|
---|
387 | public void AddHillclimbingResult(ISingleObjectiveSolutionScope<TSolution> input, ISingleObjectiveSolutionScope<TSolution> outcome) {
|
---|
388 | hillclimbingStat.Add(Tuple.Create(input.Fitness, Maximization ? outcome.Fitness - input.Fitness : input.Fitness - outcome.Fitness));
|
---|
389 | }
|
---|
390 | public bool HillclimbingSuited(double startingFitness) {
|
---|
391 | return true;
|
---|
392 | }
|
---|
393 | #endregion
|
---|
394 |
|
---|
395 | #region Adaptivewalking Performance
|
---|
396 | public void AddAdaptivewalkingResult(ISingleObjectiveSolutionScope<TSolution> input, ISingleObjectiveSolutionScope<TSolution> outcome) {
|
---|
397 | adaptivewalkingStat.Add(Tuple.Create(input.Fitness, Maximization ? outcome.Fitness - input.Fitness : input.Fitness - outcome.Fitness));
|
---|
398 | }
|
---|
399 | public bool AdaptivewalkingSuited(double startingFitness) {
|
---|
400 | return true;
|
---|
401 | }
|
---|
402 | #endregion
|
---|
403 |
|
---|
404 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
---|
405 | public bool IsBetter(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b) {
|
---|
406 | return IsBetter(a.Fitness, b.Fitness);
|
---|
407 | }
|
---|
408 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
---|
409 | public bool IsBetter(double a, double b) {
|
---|
410 | return double.IsNaN(b) && !double.IsNaN(a)
|
---|
411 | || Maximization && a > b
|
---|
412 | || !Maximization && a < b;
|
---|
413 | }
|
---|
414 |
|
---|
415 | #region IExecutionContext members
|
---|
416 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
417 | return new ExecutionContext(this, op, Scope);
|
---|
418 | }
|
---|
419 |
|
---|
420 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
421 | return new ExecutionContext(this, op, s);
|
---|
422 | }
|
---|
423 |
|
---|
424 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
425 | return new ExecutionContext(this, op, Scope);
|
---|
426 | }
|
---|
427 |
|
---|
428 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
429 | return new ExecutionContext(this, op, s);
|
---|
430 | }
|
---|
431 | #endregion
|
---|
432 |
|
---|
433 | #region Engine Helper
|
---|
434 | public void RunOperator(IOperator op, IScope scope, CancellationToken cancellationToken) {
|
---|
435 | var stack = new Stack<IOperation>();
|
---|
436 | stack.Push(CreateChildOperation(op, scope));
|
---|
437 |
|
---|
438 | while (stack.Count > 0) {
|
---|
439 | cancellationToken.ThrowIfCancellationRequested();
|
---|
440 |
|
---|
441 | var next = stack.Pop();
|
---|
442 | if (next is OperationCollection) {
|
---|
443 | var coll = (OperationCollection)next;
|
---|
444 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
445 | if (coll[i] != null) stack.Push(coll[i]);
|
---|
446 | } else if (next is IAtomicOperation) {
|
---|
447 | var operation = (IAtomicOperation)next;
|
---|
448 | try {
|
---|
449 | next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
|
---|
450 | } catch (Exception ex) {
|
---|
451 | stack.Push(operation);
|
---|
452 | if (ex is OperationCanceledException) throw ex;
|
---|
453 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
454 | }
|
---|
455 | if (next != null) stack.Push(next);
|
---|
456 | }
|
---|
457 | }
|
---|
458 | }
|
---|
459 | #endregion
|
---|
460 | }
|
---|
461 |
|
---|
462 | [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")]
|
---|
463 | [StorableClass]
|
---|
464 | public abstract class MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
465 | ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>, IEvaluationServiceContext<TSolution>
|
---|
466 | where TProblem : class, IItem, ISingleObjectiveHeuristicOptimizationProblem
|
---|
467 | where TSolution : class, IItem
|
---|
468 | where TContext : MemPRPopulationContext<TProblem, TSolution, TContext, TSolutionContext>
|
---|
469 | where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> {
|
---|
470 |
|
---|
471 | private TContext parent;
|
---|
472 | protected TContext BaseContext {
|
---|
473 | get { return parent;}
|
---|
474 | }
|
---|
475 | public IExecutionContext Parent {
|
---|
476 | get { return parent; }
|
---|
477 | set { throw new InvalidOperationException("Cannot set the parent of a single-solution context."); }
|
---|
478 | }
|
---|
479 |
|
---|
480 | [Storable]
|
---|
481 | private ISingleObjectiveSolutionScope<TSolution> scope;
|
---|
482 | public IScope Scope {
|
---|
483 | get { return scope; }
|
---|
484 | }
|
---|
485 |
|
---|
486 | IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
|
---|
487 | get { return Parameters; }
|
---|
488 | }
|
---|
489 |
|
---|
490 | public TProblem Problem {
|
---|
491 | get { return parent.Problem; }
|
---|
492 | }
|
---|
493 | public bool Maximization {
|
---|
494 | get { return parent.Maximization; }
|
---|
495 | }
|
---|
496 |
|
---|
497 | public double BestQuality {
|
---|
498 | get { return parent.BestQuality; }
|
---|
499 | set { parent.BestQuality = value; }
|
---|
500 | }
|
---|
501 |
|
---|
502 | public TSolution BestSolution {
|
---|
503 | get { return parent.BestSolution; }
|
---|
504 | set { parent.BestSolution = value; }
|
---|
505 | }
|
---|
506 |
|
---|
507 | public IRandom Random {
|
---|
508 | get { return parent.Random; }
|
---|
509 | }
|
---|
510 |
|
---|
511 | [Storable]
|
---|
512 | private IValueParameter<IntValue> evaluatedSolutions;
|
---|
513 | public int EvaluatedSolutions {
|
---|
514 | get { return evaluatedSolutions.Value.Value; }
|
---|
515 | set { evaluatedSolutions.Value.Value = value; }
|
---|
516 | }
|
---|
517 |
|
---|
518 | [Storable]
|
---|
519 | private IValueParameter<IntValue> iterations;
|
---|
520 | public int Iterations {
|
---|
521 | get { return iterations.Value.Value; }
|
---|
522 | set { iterations.Value.Value = value; }
|
---|
523 | }
|
---|
524 |
|
---|
525 | ISingleObjectiveSolutionScope<TSolution> ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>.Solution {
|
---|
526 | get { return scope; }
|
---|
527 | }
|
---|
528 |
|
---|
529 | [StorableConstructor]
|
---|
530 | protected MemPRSolutionContext(bool deserializing) : base(deserializing) { }
|
---|
531 | protected MemPRSolutionContext(MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> original, Cloner cloner)
|
---|
532 | : base(original, cloner) {
|
---|
533 | scope = cloner.Clone(original.scope);
|
---|
534 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
535 | iterations = cloner.Clone(original.iterations);
|
---|
536 | }
|
---|
537 | public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
|
---|
538 | parent = baseContext;
|
---|
539 | scope = solution;
|
---|
540 |
|
---|
541 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
542 | Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
543 | }
|
---|
544 |
|
---|
545 | public void IncrementEvaluatedSolutions(int byEvaluations) {
|
---|
546 | if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
|
---|
547 | EvaluatedSolutions += byEvaluations;
|
---|
548 | }
|
---|
549 | public virtual double Evaluate(TSolution solution, CancellationToken token) {
|
---|
550 | return parent.Evaluate(solution, token);
|
---|
551 | }
|
---|
552 |
|
---|
553 | public virtual void Evaluate(ISingleObjectiveSolutionScope<TSolution> solScope, CancellationToken token) {
|
---|
554 | parent.Evaluate(solScope, token);
|
---|
555 | }
|
---|
556 |
|
---|
557 | #region IExecutionContext members
|
---|
558 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
559 | return new ExecutionContext(this, op, Scope);
|
---|
560 | }
|
---|
561 |
|
---|
562 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
563 | return new ExecutionContext(this, op, s);
|
---|
564 | }
|
---|
565 |
|
---|
566 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
567 | return new ExecutionContext(this, op, Scope);
|
---|
568 | }
|
---|
569 |
|
---|
570 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
571 | return new ExecutionContext(this, op, s);
|
---|
572 | }
|
---|
573 | #endregion
|
---|
574 | }
|
---|
575 | }
|
---|