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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.SolutionModel;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Random;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.MemPR {
|
---|
35 | [Item("MemPRContext", "Abstract base class for MemPR contexts.")]
|
---|
36 | [StorableClass]
|
---|
37 | public abstract class MemPRContext<TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
38 | ISingleObjectivePopulationContext<TSolution>, ISolutionModelContext<TSolution>, IStochasticContext,
|
---|
39 | IMaximizationContext, IEvaluatedSolutionsContext
|
---|
40 | where TSolution : class, IItem
|
---|
41 | where TContext : MemPRContext<TSolution, TContext, TSolutionContext>
|
---|
42 | where TSolutionContext : SingleSolutionMemPRContext<TSolution, TContext, 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<BoolValue> maximization;
|
---|
63 | public bool Maximization {
|
---|
64 | get { return maximization.Value.Value; }
|
---|
65 | set { maximization.Value.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<IntValue> hcSteps;
|
---|
98 | public int HcSteps {
|
---|
99 | get { return hcSteps.Value.Value; }
|
---|
100 | set { hcSteps.Value.Value = value; }
|
---|
101 | }
|
---|
102 |
|
---|
103 | [Storable]
|
---|
104 | private IValueParameter<IntValue> byBreeding;
|
---|
105 | public int ByBreeding {
|
---|
106 | get { return byBreeding.Value.Value; }
|
---|
107 | set { byBreeding.Value.Value = value; }
|
---|
108 | }
|
---|
109 |
|
---|
110 | [Storable]
|
---|
111 | private IValueParameter<IntValue> byRelinking;
|
---|
112 | public int ByRelinking {
|
---|
113 | get { return byRelinking.Value.Value; }
|
---|
114 | set { byRelinking.Value.Value = value; }
|
---|
115 | }
|
---|
116 |
|
---|
117 | [Storable]
|
---|
118 | private IValueParameter<IntValue> bySampling;
|
---|
119 | public int BySampling {
|
---|
120 | get { return bySampling.Value.Value; }
|
---|
121 | set { bySampling.Value.Value = value; }
|
---|
122 | }
|
---|
123 |
|
---|
124 | [Storable]
|
---|
125 | private IValueParameter<IntValue> byHillclimbing;
|
---|
126 | public int ByHillclimbing {
|
---|
127 | get { return byHillclimbing.Value.Value; }
|
---|
128 | set { byHillclimbing.Value.Value = value; }
|
---|
129 | }
|
---|
130 |
|
---|
131 | [Storable]
|
---|
132 | private IValueParameter<IntValue> byTabuwalking;
|
---|
133 | public int ByTabuwalking {
|
---|
134 | get { return byTabuwalking.Value.Value; }
|
---|
135 | set { byTabuwalking.Value.Value = value; }
|
---|
136 | }
|
---|
137 |
|
---|
138 | [Storable]
|
---|
139 | private IValueParameter<IRandom> random;
|
---|
140 | public IRandom Random {
|
---|
141 | get { return random.Value; }
|
---|
142 | set { random.Value = value; }
|
---|
143 | }
|
---|
144 |
|
---|
145 | IEnumerable<IScope> IPopulationContext.Population {
|
---|
146 | get { return scope.SubScopes; }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Population {
|
---|
150 | get { return scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>(); }
|
---|
151 | }
|
---|
152 | public void AddToPopulation(ISingleObjectiveSolutionScope<TSolution> solScope) {
|
---|
153 | scope.SubScopes.Add(solScope);
|
---|
154 | }
|
---|
155 | public void ReplaceAtPopulation(int index, ISingleObjectiveSolutionScope<TSolution> solScope) {
|
---|
156 | scope.SubScopes[index] = solScope;
|
---|
157 | }
|
---|
158 | public ISingleObjectiveSolutionScope<TSolution> AtPopulation(int index) {
|
---|
159 | return scope.SubScopes[index] as ISingleObjectiveSolutionScope<TSolution>;
|
---|
160 | }
|
---|
161 | public int PopulationCount {
|
---|
162 | get { return scope.SubScopes.Count; }
|
---|
163 | }
|
---|
164 |
|
---|
165 | [Storable]
|
---|
166 | private List<Tuple<double, double, double>> breedingStat;
|
---|
167 | public List<Tuple<double, double, double>> BreedingStat {
|
---|
168 | get { return breedingStat; }
|
---|
169 | }
|
---|
170 | [Storable]
|
---|
171 | private List<Tuple<double, double>> hillclimbingStat;
|
---|
172 | public List<Tuple<double, double>> HillclimbingStat {
|
---|
173 | get { return hillclimbingStat; }
|
---|
174 | }
|
---|
175 | [Storable]
|
---|
176 | private List<Tuple<double, double>> tabuwalkingStat;
|
---|
177 | public List<Tuple<double, double>> TabuwalkingStat {
|
---|
178 | get { return tabuwalkingStat; }
|
---|
179 | }
|
---|
180 |
|
---|
181 | [Storable]
|
---|
182 | public ISolutionModel<TSolution> Model { get; set; }
|
---|
183 |
|
---|
184 | [StorableConstructor]
|
---|
185 | protected MemPRContext(bool deserializing) : base(deserializing) { }
|
---|
186 | protected MemPRContext(MemPRContext<TSolution, TContext, TSolutionContext> original, Cloner cloner)
|
---|
187 | : base(original, cloner) {
|
---|
188 | scope = cloner.Clone(original.scope);
|
---|
189 | maximization = cloner.Clone(original.maximization);
|
---|
190 | initialized = cloner.Clone(original.initialized);
|
---|
191 | iterations = cloner.Clone(original.iterations);
|
---|
192 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
193 | bestQuality = cloner.Clone(original.bestQuality);
|
---|
194 | hcSteps = cloner.Clone(original.hcSteps);
|
---|
195 | byBreeding = cloner.Clone(original.byBreeding);
|
---|
196 | byRelinking = cloner.Clone(original.byRelinking);
|
---|
197 | bySampling = cloner.Clone(original.bySampling);
|
---|
198 | byHillclimbing = cloner.Clone(original.byHillclimbing);
|
---|
199 | byTabuwalking = cloner.Clone(original.byTabuwalking);
|
---|
200 | random = cloner.Clone(original.random);
|
---|
201 | breedingStat = original.breedingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3)).ToList();
|
---|
202 | hillclimbingStat = original.hillclimbingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
203 | tabuwalkingStat = original.tabuwalkingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
204 |
|
---|
205 | Model = cloner.Clone(original.Model);
|
---|
206 | }
|
---|
207 | public MemPRContext() : this("MemPRContext") { }
|
---|
208 | public MemPRContext(string name) : base(name) {
|
---|
209 | scope = new Scope("Global");
|
---|
210 |
|
---|
211 | Parameters.Add(maximization = new ValueParameter<BoolValue>("Maximization", new BoolValue(false)));
|
---|
212 | Parameters.Add(initialized = new ValueParameter<BoolValue>("Initialized", new BoolValue(false)));
|
---|
213 | Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
214 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
215 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
|
---|
216 | Parameters.Add(hcSteps = new ValueParameter<IntValue>("HcSteps", new IntValue(0)));
|
---|
217 | Parameters.Add(byBreeding = new ValueParameter<IntValue>("ByBreeding", new IntValue(0)));
|
---|
218 | Parameters.Add(byRelinking = new ValueParameter<IntValue>("ByRelinking", new IntValue(0)));
|
---|
219 | Parameters.Add(bySampling = new ValueParameter<IntValue>("BySampling", new IntValue(0)));
|
---|
220 | Parameters.Add(byHillclimbing = new ValueParameter<IntValue>("ByHillclimbing", new IntValue(0)));
|
---|
221 | Parameters.Add(byTabuwalking = new ValueParameter<IntValue>("ByTabuwalking", new IntValue(0)));
|
---|
222 | Parameters.Add(random = new ValueParameter<IRandom>("Random", new MersenneTwister()));
|
---|
223 |
|
---|
224 | breedingStat = new List<Tuple<double, double, double>>();
|
---|
225 | hillclimbingStat = new List<Tuple<double, double>>();
|
---|
226 | tabuwalkingStat = new List<Tuple<double, double>>();
|
---|
227 | }
|
---|
228 |
|
---|
229 | public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution);
|
---|
230 |
|
---|
231 | #region IExecutionContext members
|
---|
232 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
233 | return new ExecutionContext(this, op, Scope);
|
---|
234 | }
|
---|
235 |
|
---|
236 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
237 | return new ExecutionContext(this, op, s);
|
---|
238 | }
|
---|
239 |
|
---|
240 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
241 | return new ExecutionContext(this, op, Scope);
|
---|
242 | }
|
---|
243 |
|
---|
244 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
245 | return new ExecutionContext(this, op, s);
|
---|
246 | }
|
---|
247 | #endregion
|
---|
248 |
|
---|
249 | IEnumerable<ISolutionScope<TSolution>> IPopulationContext<TSolution>.Population {
|
---|
250 | get { return Population; }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")]
|
---|
255 | [StorableClass]
|
---|
256 | public abstract class SingleSolutionMemPRContext<TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
257 | ISingleObjectiveSolutionContext<TSolution>, IEvaluatedSolutionsContext,
|
---|
258 | IIterationsManipulationContext, IStochasticContext, IMaximizationContext
|
---|
259 | where TSolution : class, IItem
|
---|
260 | where TContext : MemPRContext<TSolution, TContext, TSolutionContext>
|
---|
261 | where TSolutionContext : SingleSolutionMemPRContext<TSolution, TContext, TSolutionContext> {
|
---|
262 |
|
---|
263 | private TContext parent;
|
---|
264 | public IExecutionContext Parent {
|
---|
265 | get { return parent; }
|
---|
266 | set { throw new InvalidOperationException("Cannot set the parent of a single-solution context."); }
|
---|
267 | }
|
---|
268 |
|
---|
269 | [Storable]
|
---|
270 | private ISingleObjectiveSolutionScope<TSolution> scope;
|
---|
271 | public IScope Scope {
|
---|
272 | get { return scope; }
|
---|
273 | }
|
---|
274 |
|
---|
275 | IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
|
---|
276 | get { return Parameters; }
|
---|
277 | }
|
---|
278 |
|
---|
279 | public bool Maximization {
|
---|
280 | get { return parent.Maximization; }
|
---|
281 | }
|
---|
282 |
|
---|
283 | public IRandom Random {
|
---|
284 | get { return parent.Random; }
|
---|
285 | }
|
---|
286 |
|
---|
287 | [Storable]
|
---|
288 | private IValueParameter<IntValue> evaluatedSolutions;
|
---|
289 | public int EvaluatedSolutions {
|
---|
290 | get { return evaluatedSolutions.Value.Value; }
|
---|
291 | set { evaluatedSolutions.Value.Value = value; }
|
---|
292 | }
|
---|
293 |
|
---|
294 | [Storable]
|
---|
295 | private IValueParameter<IntValue> iterations;
|
---|
296 | public int Iterations {
|
---|
297 | get { return iterations.Value.Value; }
|
---|
298 | set { iterations.Value.Value = value; }
|
---|
299 | }
|
---|
300 |
|
---|
301 | IScope ISolutionContext.Solution {
|
---|
302 | get { return scope; }
|
---|
303 | }
|
---|
304 |
|
---|
305 | ISolutionScope<TSolution> ISolutionContext<TSolution>.Solution {
|
---|
306 | get { return scope; }
|
---|
307 | }
|
---|
308 |
|
---|
309 | ISingleObjectiveSolutionScope<TSolution> ISingleObjectiveSolutionContext<TSolution>.Solution {
|
---|
310 | get { return scope; }
|
---|
311 | }
|
---|
312 |
|
---|
313 | [StorableConstructor]
|
---|
314 | protected SingleSolutionMemPRContext(bool deserializing) : base(deserializing) { }
|
---|
315 | protected SingleSolutionMemPRContext(SingleSolutionMemPRContext<TSolution, TContext, TSolutionContext> original, Cloner cloner)
|
---|
316 | : base(original, cloner) {
|
---|
317 | scope = cloner.Clone(original.scope);
|
---|
318 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
319 | iterations = cloner.Clone(original.iterations);
|
---|
320 | }
|
---|
321 | public SingleSolutionMemPRContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
|
---|
322 | parent = baseContext;
|
---|
323 | scope = solution;
|
---|
324 |
|
---|
325 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
326 | Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
327 | }
|
---|
328 |
|
---|
329 | #region IExecutionContext members
|
---|
330 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
331 | return new ExecutionContext(this, op, Scope);
|
---|
332 | }
|
---|
333 |
|
---|
334 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
335 | return new ExecutionContext(this, op, s);
|
---|
336 | }
|
---|
337 |
|
---|
338 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
339 | return new ExecutionContext(this, op, Scope);
|
---|
340 | }
|
---|
341 |
|
---|
342 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
343 | return new ExecutionContext(this, op, s);
|
---|
344 | }
|
---|
345 | #endregion
|
---|
346 | }
|
---|
347 | }
|
---|