Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRContext.cs @ 14517

Last change on this file since 14517 was 14496, checked in by abeham, 8 years ago

#2701:

  • Reusing similiarty calculator in BinaryMemPR
  • Fixing distance calculation for linear linkage and LinearLinkageMemPR
  • Small changes to base algorithm
  • Added biased model trainer for permutation (rank and fitness)
  • Fixing best known quality calculation for GCP
File size: 13.8 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 HeuristicLab.Algorithms.MemPR.Interfaces;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Random;
33
34namespace HeuristicLab.Algorithms.MemPR {
35  [Item("MemPRContext", "Abstract base class for MemPR contexts.")]
36  [StorableClass]
37  public abstract class MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> : ParameterizedNamedItem,
38    IPopulationBasedHeuristicAlgorithmContext<TProblem, TSolution>, ISolutionModelContext<TSolution>
39      where TProblem : class, IItem, ISingleObjectiveProblemDefinition
40      where TSolution : class, IItem
41      where TPopulationContext : MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext>
42      where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TPopulationContext, TSolutionContext> {
43
44    private IExecutionContext parent;
45    public IExecutionContext Parent {
46      get { return parent; }
47      set { parent = value; }
48    }
49
50    [Storable]
51    private IScope scope;
52    public IScope Scope {
53      get { return scope; }
54      private set { scope = value; }
55    }
56
57    IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
58      get { return Parameters; }
59    }
60
61    [Storable]
62    private IValueParameter<TProblem> problem;
63    public TProblem Problem {
64      get { return problem.Value; }
65      set { problem.Value = value; }
66    }
67
68    [Storable]
69    private IValueParameter<BoolValue> initialized;
70    public bool Initialized {
71      get { return initialized.Value.Value; }
72      set { initialized.Value.Value = value; }
73    }
74
75    [Storable]
76    private IValueParameter<IntValue> iterations;
77    public int Iterations {
78      get { return iterations.Value.Value; }
79      set { iterations.Value.Value = value; }
80    }
81
82    [Storable]
83    private IValueParameter<IntValue> evaluatedSolutions;
84    public int EvaluatedSolutions {
85      get { return evaluatedSolutions.Value.Value; }
86      set { evaluatedSolutions.Value.Value = value; }
87    }
88
89    [Storable]
90    private IValueParameter<DoubleValue> bestQuality;
91    public double BestQuality {
92      get { return bestQuality.Value.Value; }
93      set { bestQuality.Value.Value = value; }
94    }
95
96    [Storable]
97    private IValueParameter<TSolution> bestSolution;
98    public TSolution BestSolution {
99      get { return bestSolution.Value; }
100      set { bestSolution.Value = value; }
101    }
102
103    [Storable]
104    private IValueParameter<IntValue> localSearchEvaluations;
105    public int LocalSearchEvaluations {
106      get { return localSearchEvaluations.Value.Value; }
107      set { localSearchEvaluations.Value.Value = value; }
108    }
109
110    [Storable]
111    private IValueParameter<IntValue> byBreeding;
112    public int ByBreeding {
113      get { return byBreeding.Value.Value; }
114      set { byBreeding.Value.Value = value; }
115    }
116
117    [Storable]
118    private IValueParameter<IntValue> byRelinking;
119    public int ByRelinking {
120      get { return byRelinking.Value.Value; }
121      set { byRelinking.Value.Value = value; }
122    }
123
124    [Storable]
125    private IValueParameter<IntValue> bySampling;
126    public int BySampling {
127      get { return bySampling.Value.Value; }
128      set { bySampling.Value.Value = value; }
129    }
130
131    [Storable]
132    private IValueParameter<IntValue> byHillclimbing;
133    public int ByHillclimbing {
134      get { return byHillclimbing.Value.Value; }
135      set { byHillclimbing.Value.Value = value; }
136    }
137
138    [Storable]
139    private IValueParameter<IntValue> byTabuwalking;
140    public int ByTabuwalking {
141      get { return byTabuwalking.Value.Value; }
142      set { byTabuwalking.Value.Value = value; }
143    }
144
145    [Storable]
146    private IValueParameter<IRandom> random;
147    public IRandom Random {
148      get { return random.Value; }
149      set { random.Value = value; }
150    }
151   
152    public IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Population {
153      get { return scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>(); }
154    }
155    public void AddToPopulation(ISingleObjectiveSolutionScope<TSolution> solScope) {
156      scope.SubScopes.Add(solScope);
157    }
158    public void ReplaceAtPopulation(int index, ISingleObjectiveSolutionScope<TSolution> solScope) {
159      scope.SubScopes[index] = solScope;
160    }
161    public ISingleObjectiveSolutionScope<TSolution> AtPopulation(int index) {
162      return scope.SubScopes[index] as ISingleObjectiveSolutionScope<TSolution>;
163    }
164    public int PopulationCount {
165      get { return scope.SubScopes.Count; }
166    }
167
168    [Storable]
169    private List<Tuple<double, double, double>> breedingStat;
170    public List<Tuple<double, double, double>> BreedingStat {
171      get { return breedingStat; }
172    }
173    [Storable]
174    private List<Tuple<double, double>> hillclimbingStat;
175    public List<Tuple<double, double>> HillclimbingStat {
176      get { return hillclimbingStat; }
177    }
178    [Storable]
179    private List<Tuple<double, double>> tabuwalkingStat;
180    public List<Tuple<double, double>> TabuwalkingStat {
181      get { return tabuwalkingStat; }
182    }
183
184    [Storable]
185    public ISolutionModel<TSolution> Model { get; set; }
186
187    [StorableConstructor]
188    protected MemPRPopulationContext(bool deserializing) : base(deserializing) { }
189    protected MemPRPopulationContext(MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> original, Cloner cloner)
190      : base(original, cloner) {
191      scope = cloner.Clone(original.scope);
192      problem = cloner.Clone(original.problem);
193      initialized = cloner.Clone(original.initialized);
194      iterations = cloner.Clone(original.iterations);
195      evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
196      bestQuality = cloner.Clone(original.bestQuality);
197      bestSolution = cloner.Clone(original.bestSolution);
198      localSearchEvaluations = cloner.Clone(original.localSearchEvaluations);
199      byBreeding = cloner.Clone(original.byBreeding);
200      byRelinking = cloner.Clone(original.byRelinking);
201      bySampling = cloner.Clone(original.bySampling);
202      byHillclimbing = cloner.Clone(original.byHillclimbing);
203      byTabuwalking = cloner.Clone(original.byTabuwalking);
204      random = cloner.Clone(original.random);
205      breedingStat = original.breedingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3)).ToList();
206      hillclimbingStat = original.hillclimbingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
207      tabuwalkingStat = original.tabuwalkingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
208
209      Model = cloner.Clone(original.Model);
210    }
211    public MemPRPopulationContext() : this("MemPRContext") { }
212    public MemPRPopulationContext(string name) : base(name) {
213      scope = new Scope("Global");
214
215      Parameters.Add(problem = new ValueParameter<TProblem>("Problem"));
216      Parameters.Add(initialized = new ValueParameter<BoolValue>("Initialized", new BoolValue(false)));
217      Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
218      Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
219      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
220      Parameters.Add(bestSolution = new ValueParameter<TSolution>("BestSolution"));
221      Parameters.Add(localSearchEvaluations = new ValueParameter<IntValue>("LocalSearchEvaluations", new IntValue(0)));
222      Parameters.Add(byBreeding = new ValueParameter<IntValue>("ByBreeding", new IntValue(0)));
223      Parameters.Add(byRelinking = new ValueParameter<IntValue>("ByRelinking", new IntValue(0)));
224      Parameters.Add(bySampling = new ValueParameter<IntValue>("BySampling", new IntValue(0)));
225      Parameters.Add(byHillclimbing = new ValueParameter<IntValue>("ByHillclimbing", new IntValue(0)));
226      Parameters.Add(byTabuwalking = new ValueParameter<IntValue>("ByTabuwalking", new IntValue(0)));
227      Parameters.Add(random = new ValueParameter<IRandom>("Random", new MersenneTwister()));
228
229      breedingStat = new List<Tuple<double, double, double>>();
230      hillclimbingStat = new List<Tuple<double, double>>();
231      tabuwalkingStat = new List<Tuple<double, double>>();
232    }
233
234    public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution);
235
236    public void IncrementEvaluatedSolutions(int byEvaluations) {
237      if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
238      EvaluatedSolutions += byEvaluations;
239    }
240
241    #region IExecutionContext members
242    public IAtomicOperation CreateOperation(IOperator op) {
243      return new ExecutionContext(this, op, Scope);
244    }
245
246    public IAtomicOperation CreateOperation(IOperator op, IScope s) {
247      return new ExecutionContext(this, op, s);
248    }
249
250    public IAtomicOperation CreateChildOperation(IOperator op) {
251      return new ExecutionContext(this, op, Scope);
252    }
253
254    public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
255      return new ExecutionContext(this, op, s);
256    }
257    #endregion
258  }
259
260  [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")]
261  [StorableClass]
262  public abstract class MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
263    ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>
264      where TProblem : class, IItem, ISingleObjectiveProblemDefinition
265      where TSolution : class, IItem
266      where TContext : MemPRPopulationContext<TProblem, TSolution, TContext, TSolutionContext>
267      where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> {
268
269    private TContext parent;
270    public IExecutionContext Parent {
271      get { return parent; }
272      set { throw new InvalidOperationException("Cannot set the parent of a single-solution context."); }
273    }
274
275    [Storable]
276    private ISingleObjectiveSolutionScope<TSolution> scope;
277    public IScope Scope {
278      get { return scope; }
279    }
280
281    IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
282      get { return Parameters; }
283    }
284
285    public TProblem Problem {
286      get { return parent.Problem; }
287    }
288
289    public double BestQuality {
290      get { return parent.BestQuality; }
291      set { parent.BestQuality = value; }
292    }
293
294    public TSolution BestSolution {
295      get { return parent.BestSolution; }
296      set { parent.BestSolution = value; }
297    }
298
299    public IRandom Random {
300      get { return parent.Random; }
301    }
302
303    [Storable]
304    private IValueParameter<IntValue> evaluatedSolutions;
305    public int EvaluatedSolutions {
306      get { return evaluatedSolutions.Value.Value; }
307      set { evaluatedSolutions.Value.Value = value; }
308    }
309
310    [Storable]
311    private IValueParameter<IntValue> iterations;
312    public int Iterations {
313      get { return iterations.Value.Value; }
314      set { iterations.Value.Value = value; }
315    }
316
317    ISingleObjectiveSolutionScope<TSolution> ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>.Solution {
318      get { return scope; }
319    }
320
321    [StorableConstructor]
322    protected MemPRSolutionContext(bool deserializing) : base(deserializing) { }
323    protected MemPRSolutionContext(MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> original, Cloner cloner)
324      : base(original, cloner) {
325      scope = cloner.Clone(original.scope);
326      evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
327      iterations = cloner.Clone(original.iterations);
328    }
329    public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
330      parent = baseContext;
331      scope = solution;
332     
333      Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
334      Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
335    }
336
337    public void IncrementEvaluatedSolutions(int byEvaluations) {
338      if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
339      EvaluatedSolutions += byEvaluations;
340    }
341
342    #region IExecutionContext members
343    public IAtomicOperation CreateOperation(IOperator op) {
344      return new ExecutionContext(this, op, Scope);
345    }
346
347    public IAtomicOperation CreateOperation(IOperator op, IScope s) {
348      return new ExecutionContext(this, op, s);
349    }
350
351    public IAtomicOperation CreateChildOperation(IOperator op) {
352      return new ExecutionContext(this, op, Scope);
353    }
354
355    public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
356      return new ExecutionContext(this, op, s);
357    }
358    #endregion
359  }
360}
Note: See TracBrowser for help on using the repository browser.