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 | return solScope.Fitness;
|
---|
324 | }
|
---|
325 |
|
---|
326 | public virtual void Evaluate(ISingleObjectiveSolutionScope<TSolution> solScope, CancellationToken token) {
|
---|
327 | var pdef = Problem as ISingleObjectiveProblemDefinition;
|
---|
328 | if (pdef != null) {
|
---|
329 | var ind = new SingleEncodingIndividual(pdef.Encoding, solScope);
|
---|
330 | solScope.Fitness = pdef.Evaluate(ind, Random);
|
---|
331 | } else {
|
---|
332 | RunOperator(Problem.Evaluator, solScope, token);
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution);
|
---|
337 |
|
---|
338 | public void IncrementEvaluatedSolutions(int byEvaluations) {
|
---|
339 | if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
|
---|
340 | EvaluatedSolutions += byEvaluations;
|
---|
341 | }
|
---|
342 |
|
---|
343 | #region Breeding Performance
|
---|
344 | public void AddBreedingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
345 | if (IsBetter(a, b))
|
---|
346 | breedingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, child.Fitness));
|
---|
347 | else breedingStat.Add(Tuple.Create(b.Fitness, a.Fitness, parentDist, child.Fitness));
|
---|
348 | }
|
---|
349 | public bool BreedingSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
350 | return true;
|
---|
351 | }
|
---|
352 | #endregion
|
---|
353 |
|
---|
354 | #region Relinking Performance
|
---|
355 | public void AddRelinkingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
356 | if (IsBetter(a, b))
|
---|
357 | relinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - a.Fitness : a.Fitness - child.Fitness));
|
---|
358 | else relinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - b.Fitness : b.Fitness - child.Fitness));
|
---|
359 | }
|
---|
360 | public bool RelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
361 | return true;
|
---|
362 | }
|
---|
363 | #endregion
|
---|
364 |
|
---|
365 | #region Delinking Performance
|
---|
366 | public void AddDelinkingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
367 | if (IsBetter(a, b))
|
---|
368 | delinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - a.Fitness : a.Fitness - child.Fitness));
|
---|
369 | else delinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - b.Fitness : b.Fitness - child.Fitness));
|
---|
370 | }
|
---|
371 | public bool DelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
372 | return true;
|
---|
373 | }
|
---|
374 | #endregion
|
---|
375 |
|
---|
376 | #region Sampling Performance
|
---|
377 | public void AddSamplingResult(ISingleObjectiveSolutionScope<TSolution> sample, double avgDist) {
|
---|
378 | samplingStat.Add(Tuple.Create(avgDist, sample.Fitness));
|
---|
379 | }
|
---|
380 | public bool SamplingSuited(double avgDist) {
|
---|
381 | return true;
|
---|
382 | }
|
---|
383 | #endregion
|
---|
384 |
|
---|
385 | #region Hillclimbing Performance
|
---|
386 | public void AddHillclimbingResult(ISingleObjectiveSolutionScope<TSolution> input, ISingleObjectiveSolutionScope<TSolution> outcome) {
|
---|
387 | hillclimbingStat.Add(Tuple.Create(input.Fitness, Maximization ? outcome.Fitness - input.Fitness : input.Fitness - outcome.Fitness));
|
---|
388 | }
|
---|
389 | public bool HillclimbingSuited(double startingFitness) {
|
---|
390 | return true;
|
---|
391 | }
|
---|
392 | #endregion
|
---|
393 |
|
---|
394 | #region Adaptivewalking Performance
|
---|
395 | public void AddAdaptivewalkingResult(ISingleObjectiveSolutionScope<TSolution> input, ISingleObjectiveSolutionScope<TSolution> outcome) {
|
---|
396 | adaptivewalkingStat.Add(Tuple.Create(input.Fitness, Maximization ? outcome.Fitness - input.Fitness : input.Fitness - outcome.Fitness));
|
---|
397 | }
|
---|
398 | public bool AdaptivewalkingSuited(double startingFitness) {
|
---|
399 | return true;
|
---|
400 | }
|
---|
401 | #endregion
|
---|
402 |
|
---|
403 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
---|
404 | public bool IsBetter(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b) {
|
---|
405 | return IsBetter(a.Fitness, b.Fitness);
|
---|
406 | }
|
---|
407 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
---|
408 | public bool IsBetter(double a, double b) {
|
---|
409 | return double.IsNaN(b) && !double.IsNaN(a)
|
---|
410 | || Maximization && a > b
|
---|
411 | || !Maximization && a < b;
|
---|
412 | }
|
---|
413 |
|
---|
414 | #region IExecutionContext members
|
---|
415 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
416 | return new ExecutionContext(this, op, Scope);
|
---|
417 | }
|
---|
418 |
|
---|
419 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
420 | return new ExecutionContext(this, op, s);
|
---|
421 | }
|
---|
422 |
|
---|
423 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
424 | return new ExecutionContext(this, op, Scope);
|
---|
425 | }
|
---|
426 |
|
---|
427 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
428 | return new ExecutionContext(this, op, s);
|
---|
429 | }
|
---|
430 | #endregion
|
---|
431 |
|
---|
432 | #region Engine Helper
|
---|
433 | public void RunOperator(IOperator op, IScope scope, CancellationToken cancellationToken) {
|
---|
434 | var stack = new Stack<IOperation>();
|
---|
435 | stack.Push(CreateChildOperation(op, scope));
|
---|
436 |
|
---|
437 | while (stack.Count > 0) {
|
---|
438 | cancellationToken.ThrowIfCancellationRequested();
|
---|
439 |
|
---|
440 | var next = stack.Pop();
|
---|
441 | if (next is OperationCollection) {
|
---|
442 | var coll = (OperationCollection)next;
|
---|
443 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
444 | if (coll[i] != null) stack.Push(coll[i]);
|
---|
445 | } else if (next is IAtomicOperation) {
|
---|
446 | var operation = (IAtomicOperation)next;
|
---|
447 | try {
|
---|
448 | next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
|
---|
449 | } catch (Exception ex) {
|
---|
450 | stack.Push(operation);
|
---|
451 | if (ex is OperationCanceledException) throw ex;
|
---|
452 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
453 | }
|
---|
454 | if (next != null) stack.Push(next);
|
---|
455 | }
|
---|
456 | }
|
---|
457 | }
|
---|
458 | #endregion
|
---|
459 | }
|
---|
460 |
|
---|
461 | [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")]
|
---|
462 | [StorableClass]
|
---|
463 | public abstract class MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
464 | ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>, IEvaluationServiceContext<TSolution>
|
---|
465 | where TProblem : class, IItem, ISingleObjectiveHeuristicOptimizationProblem
|
---|
466 | where TSolution : class, IItem
|
---|
467 | where TContext : MemPRPopulationContext<TProblem, TSolution, TContext, TSolutionContext>
|
---|
468 | where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> {
|
---|
469 |
|
---|
470 | private TContext parent;
|
---|
471 | public IExecutionContext Parent {
|
---|
472 | get { return parent; }
|
---|
473 | set { throw new InvalidOperationException("Cannot set the parent of a single-solution context."); }
|
---|
474 | }
|
---|
475 |
|
---|
476 | [Storable]
|
---|
477 | private ISingleObjectiveSolutionScope<TSolution> scope;
|
---|
478 | public IScope Scope {
|
---|
479 | get { return scope; }
|
---|
480 | }
|
---|
481 |
|
---|
482 | IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
|
---|
483 | get { return Parameters; }
|
---|
484 | }
|
---|
485 |
|
---|
486 | public TProblem Problem {
|
---|
487 | get { return parent.Problem; }
|
---|
488 | }
|
---|
489 | public bool Maximization {
|
---|
490 | get { return parent.Maximization; }
|
---|
491 | }
|
---|
492 |
|
---|
493 | public double BestQuality {
|
---|
494 | get { return parent.BestQuality; }
|
---|
495 | set { parent.BestQuality = value; }
|
---|
496 | }
|
---|
497 |
|
---|
498 | public TSolution BestSolution {
|
---|
499 | get { return parent.BestSolution; }
|
---|
500 | set { parent.BestSolution = value; }
|
---|
501 | }
|
---|
502 |
|
---|
503 | public IRandom Random {
|
---|
504 | get { return parent.Random; }
|
---|
505 | }
|
---|
506 |
|
---|
507 | [Storable]
|
---|
508 | private IValueParameter<IntValue> evaluatedSolutions;
|
---|
509 | public int EvaluatedSolutions {
|
---|
510 | get { return evaluatedSolutions.Value.Value; }
|
---|
511 | set { evaluatedSolutions.Value.Value = value; }
|
---|
512 | }
|
---|
513 |
|
---|
514 | [Storable]
|
---|
515 | private IValueParameter<IntValue> iterations;
|
---|
516 | public int Iterations {
|
---|
517 | get { return iterations.Value.Value; }
|
---|
518 | set { iterations.Value.Value = value; }
|
---|
519 | }
|
---|
520 |
|
---|
521 | ISingleObjectiveSolutionScope<TSolution> ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>.Solution {
|
---|
522 | get { return scope; }
|
---|
523 | }
|
---|
524 |
|
---|
525 | [StorableConstructor]
|
---|
526 | protected MemPRSolutionContext(bool deserializing) : base(deserializing) { }
|
---|
527 | protected MemPRSolutionContext(MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> original, Cloner cloner)
|
---|
528 | : base(original, cloner) {
|
---|
529 | scope = cloner.Clone(original.scope);
|
---|
530 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
531 | iterations = cloner.Clone(original.iterations);
|
---|
532 | }
|
---|
533 | public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
|
---|
534 | parent = baseContext;
|
---|
535 | scope = solution;
|
---|
536 |
|
---|
537 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
538 | Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
539 | }
|
---|
540 |
|
---|
541 | public void IncrementEvaluatedSolutions(int byEvaluations) {
|
---|
542 | if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
|
---|
543 | EvaluatedSolutions += byEvaluations;
|
---|
544 | }
|
---|
545 | public virtual double Evaluate(TSolution solution, CancellationToken token) {
|
---|
546 | return parent.Evaluate(solution, token);
|
---|
547 | }
|
---|
548 |
|
---|
549 | public virtual void Evaluate(ISingleObjectiveSolutionScope<TSolution> solScope, CancellationToken token) {
|
---|
550 | parent.Evaluate(solScope, token);
|
---|
551 | }
|
---|
552 |
|
---|
553 | #region IExecutionContext members
|
---|
554 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
555 | return new ExecutionContext(this, op, Scope);
|
---|
556 | }
|
---|
557 |
|
---|
558 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
559 | return new ExecutionContext(this, op, s);
|
---|
560 | }
|
---|
561 |
|
---|
562 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
563 | return new ExecutionContext(this, op, Scope);
|
---|
564 | }
|
---|
565 |
|
---|
566 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
567 | return new ExecutionContext(this, op, s);
|
---|
568 | }
|
---|
569 | #endregion
|
---|
570 | }
|
---|
571 | }
|
---|