Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/MemPRContext.cs @ 14667

Last change on this file since 14667 was 14666, checked in by abeham, 8 years ago

#2457: copied MemPR algorithm from its branch to this branch

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