[14420] | 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;
|
---|
[14544] | 25 | using System.Runtime.CompilerServices;
|
---|
| 26 | using System.Threading;
|
---|
| 27 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
[14450] | 28 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
[14573] | 29 | using HeuristicLab.Analysis;
|
---|
[14420] | 30 | using HeuristicLab.Common;
|
---|
| 31 | using HeuristicLab.Core;
|
---|
| 32 | using HeuristicLab.Data;
|
---|
| 33 | using HeuristicLab.Optimization;
|
---|
| 34 | using HeuristicLab.Parameters;
|
---|
| 35 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[14544] | 36 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[14420] | 37 | using HeuristicLab.Random;
|
---|
[14544] | 38 | using ExecutionContext = HeuristicLab.Core.ExecutionContext;
|
---|
[14420] | 39 |
|
---|
| 40 | namespace HeuristicLab.Algorithms.MemPR {
|
---|
| 41 | [Item("MemPRContext", "Abstract base class for MemPR contexts.")]
|
---|
| 42 | [StorableClass]
|
---|
[14450] | 43 | public abstract class MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
[14552] | 44 | IPopulationBasedHeuristicAlgorithmContext<TProblem, TSolution>, ISolutionModelContext<TSolution>, IEvaluationServiceContext<TSolution>
|
---|
| 45 | where TProblem : class, IItem, ISingleObjectiveHeuristicOptimizationProblem
|
---|
[14420] | 46 | where TSolution : class, IItem
|
---|
[14450] | 47 | where TPopulationContext : MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext>
|
---|
| 48 | where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TPopulationContext, TSolutionContext> {
|
---|
[14420] | 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]
|
---|
[14450] | 68 | private IValueParameter<TProblem> problem;
|
---|
| 69 | public TProblem Problem {
|
---|
| 70 | get { return problem.Value; }
|
---|
| 71 | set { problem.Value = value; }
|
---|
[14420] | 72 | }
|
---|
[14552] | 73 | public bool Maximization {
|
---|
| 74 | get { return ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
|
---|
| 75 | }
|
---|
[14420] | 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]
|
---|
[14450] | 106 | private IValueParameter<TSolution> bestSolution;
|
---|
| 107 | public TSolution BestSolution {
|
---|
| 108 | get { return bestSolution.Value; }
|
---|
| 109 | set { bestSolution.Value = value; }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | [Storable]
|
---|
[14496] | 113 | private IValueParameter<IntValue> localSearchEvaluations;
|
---|
| 114 | public int LocalSearchEvaluations {
|
---|
| 115 | get { return localSearchEvaluations.Value.Value; }
|
---|
| 116 | set { localSearchEvaluations.Value.Value = value; }
|
---|
[14420] | 117 | }
|
---|
| 118 |
|
---|
| 119 | [Storable]
|
---|
[14550] | 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]
|
---|
[14420] | 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]
|
---|
[14544] | 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]
|
---|
[14420] | 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]
|
---|
[14544] | 162 | private IValueParameter<IntValue> byAdaptivewalking;
|
---|
| 163 | public int ByAdaptivewalking {
|
---|
| 164 | get { return byAdaptivewalking.Value.Value; }
|
---|
| 165 | set { byAdaptivewalking.Value.Value = value; }
|
---|
[14420] | 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 | }
|
---|
[14544] | 187 | public void SortPopulation() {
|
---|
[14552] | 188 | scope.SubScopes.Replace(scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>().OrderBy(x => Maximization ? -x.Fitness : x.Fitness).ToList());
|
---|
[14544] | 189 | }
|
---|
[14420] | 190 | public int PopulationCount {
|
---|
| 191 | get { return scope.SubScopes.Count; }
|
---|
| 192 | }
|
---|
[14666] | 193 |
|
---|
[14420] | 194 | [Storable]
|
---|
[14563] | 195 | private List<Tuple<double, double, double, double>> breedingStat;
|
---|
| 196 | public IEnumerable<Tuple<double, double, double, double>> BreedingStat {
|
---|
[14420] | 197 | get { return breedingStat; }
|
---|
| 198 | }
|
---|
| 199 | [Storable]
|
---|
[14563] | 200 | private List<Tuple<double, double, double, double>> relinkingStat;
|
---|
| 201 | public IEnumerable<Tuple<double, double, double, double>> RelinkingStat {
|
---|
[14544] | 202 | get { return relinkingStat; }
|
---|
| 203 | }
|
---|
| 204 | [Storable]
|
---|
[14563] | 205 | private List<Tuple<double, double, double, double>> delinkingStat;
|
---|
| 206 | public IEnumerable<Tuple<double, double, double, double>> DelinkingStat {
|
---|
[14544] | 207 | get { return delinkingStat; }
|
---|
| 208 | }
|
---|
| 209 | [Storable]
|
---|
| 210 | private List<Tuple<double, double>> samplingStat;
|
---|
[14563] | 211 | public IEnumerable<Tuple<double, double>> SamplingStat {
|
---|
[14544] | 212 | get { return samplingStat; }
|
---|
| 213 | }
|
---|
| 214 | [Storable]
|
---|
[14420] | 215 | private List<Tuple<double, double>> hillclimbingStat;
|
---|
[14563] | 216 | public IEnumerable<Tuple<double, double>> HillclimbingStat {
|
---|
[14420] | 217 | get { return hillclimbingStat; }
|
---|
| 218 | }
|
---|
| 219 | [Storable]
|
---|
[14544] | 220 | private List<Tuple<double, double>> adaptivewalkingStat;
|
---|
[14563] | 221 | public IEnumerable<Tuple<double, double>> AdaptivewalkingStat {
|
---|
[14544] | 222 | get { return adaptivewalkingStat; }
|
---|
| 223 | }
|
---|
[14420] | 224 |
|
---|
| 225 | [Storable]
|
---|
| 226 | public ISolutionModel<TSolution> Model { get; set; }
|
---|
| 227 |
|
---|
| 228 | [StorableConstructor]
|
---|
[14450] | 229 | protected MemPRPopulationContext(bool deserializing) : base(deserializing) { }
|
---|
| 230 | protected MemPRPopulationContext(MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> original, Cloner cloner)
|
---|
[14420] | 231 | : base(original, cloner) {
|
---|
| 232 | scope = cloner.Clone(original.scope);
|
---|
[14450] | 233 | problem = cloner.Clone(original.problem);
|
---|
[14420] | 234 | initialized = cloner.Clone(original.initialized);
|
---|
| 235 | iterations = cloner.Clone(original.iterations);
|
---|
| 236 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
| 237 | bestQuality = cloner.Clone(original.bestQuality);
|
---|
[14450] | 238 | bestSolution = cloner.Clone(original.bestSolution);
|
---|
[14496] | 239 | localSearchEvaluations = cloner.Clone(original.localSearchEvaluations);
|
---|
[14550] | 240 | localOptimaLevel = cloner.Clone(original.localOptimaLevel);
|
---|
[14420] | 241 | byBreeding = cloner.Clone(original.byBreeding);
|
---|
| 242 | byRelinking = cloner.Clone(original.byRelinking);
|
---|
[14544] | 243 | byDelinking = cloner.Clone(original.byDelinking);
|
---|
[14420] | 244 | bySampling = cloner.Clone(original.bySampling);
|
---|
| 245 | byHillclimbing = cloner.Clone(original.byHillclimbing);
|
---|
[14544] | 246 | byAdaptivewalking = cloner.Clone(original.byAdaptivewalking);
|
---|
[14420] | 247 | random = cloner.Clone(original.random);
|
---|
[14563] | 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();
|
---|
[14544] | 251 | samplingStat = original.samplingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
[14420] | 252 | hillclimbingStat = original.hillclimbingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
[14544] | 253 | adaptivewalkingStat = original.adaptivewalkingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
| 254 |
|
---|
[14420] | 255 | Model = cloner.Clone(original.Model);
|
---|
| 256 | }
|
---|
[14450] | 257 | public MemPRPopulationContext() : this("MemPRContext") { }
|
---|
| 258 | public MemPRPopulationContext(string name) : base(name) {
|
---|
[14420] | 259 | scope = new Scope("Global");
|
---|
| 260 |
|
---|
[14450] | 261 | Parameters.Add(problem = new ValueParameter<TProblem>("Problem"));
|
---|
[14420] | 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)));
|
---|
[14552] | 266 | Parameters.Add(bestSolution = new ValueParameter<TSolution>("BestFoundSolution"));
|
---|
[14496] | 267 | Parameters.Add(localSearchEvaluations = new ValueParameter<IntValue>("LocalSearchEvaluations", new IntValue(0)));
|
---|
[14550] | 268 | Parameters.Add(localOptimaLevel = new ValueParameter<DoubleValue>("LocalOptimaLevel", new DoubleValue(0)));
|
---|
[14420] | 269 | Parameters.Add(byBreeding = new ValueParameter<IntValue>("ByBreeding", new IntValue(0)));
|
---|
| 270 | Parameters.Add(byRelinking = new ValueParameter<IntValue>("ByRelinking", new IntValue(0)));
|
---|
[14544] | 271 | Parameters.Add(byDelinking = new ValueParameter<IntValue>("ByDelinking", new IntValue(0)));
|
---|
[14420] | 272 | Parameters.Add(bySampling = new ValueParameter<IntValue>("BySampling", new IntValue(0)));
|
---|
| 273 | Parameters.Add(byHillclimbing = new ValueParameter<IntValue>("ByHillclimbing", new IntValue(0)));
|
---|
[14544] | 274 | Parameters.Add(byAdaptivewalking = new ValueParameter<IntValue>("ByAdaptivewalking", new IntValue(0)));
|
---|
[14420] | 275 | Parameters.Add(random = new ValueParameter<IRandom>("Random", new MersenneTwister()));
|
---|
| 276 |
|
---|
[14563] | 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>>();
|
---|
[14544] | 280 | samplingStat = new List<Tuple<double, double>>();
|
---|
[14420] | 281 | hillclimbingStat = new List<Tuple<double, double>>();
|
---|
[14544] | 282 | adaptivewalkingStat = new List<Tuple<double, double>>();
|
---|
[14420] | 283 | }
|
---|
| 284 |
|
---|
[14552] | 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 |
|
---|
[14420] | 303 | public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution);
|
---|
| 304 |
|
---|
[14450] | 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 |
|
---|
[14573] | 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 | }
|
---|
[14563] | 316 | public bool BreedingSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
[14666] | 317 | return true;
|
---|
[14544] | 318 | }
|
---|
[14573] | 319 | #endregion
|
---|
[14544] | 320 |
|
---|
[14573] | 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 | }
|
---|
[14563] | 327 | public bool RelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
[14666] | 328 | return true;
|
---|
[14544] | 329 | }
|
---|
[14573] | 330 | #endregion
|
---|
[14544] | 331 |
|
---|
[14573] | 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 | }
|
---|
[14563] | 338 | public bool DelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
[14666] | 339 | return true;
|
---|
[14544] | 340 | }
|
---|
[14573] | 341 | #endregion
|
---|
[14544] | 342 |
|
---|
[14573] | 343 | #region Sampling Performance
|
---|
| 344 | public void AddSamplingResult(ISingleObjectiveSolutionScope<TSolution> sample, double avgDist) {
|
---|
| 345 | samplingStat.Add(Tuple.Create(avgDist, sample.Fitness));
|
---|
| 346 | }
|
---|
[14563] | 347 | public bool SamplingSuited(double avgDist) {
|
---|
[14666] | 348 | return true;
|
---|
[14544] | 349 | }
|
---|
[14573] | 350 | #endregion
|
---|
[14544] | 351 |
|
---|
[14573] | 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 | }
|
---|
[14544] | 356 | public bool HillclimbingSuited(double startingFitness) {
|
---|
[14666] | 357 | return true;
|
---|
[14544] | 358 | }
|
---|
[14573] | 359 | #endregion
|
---|
[14544] | 360 |
|
---|
[14573] | 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 | }
|
---|
[14544] | 365 | public bool AdaptivewalkingSuited(double startingFitness) {
|
---|
[14666] | 366 | return true;
|
---|
[14544] | 367 | }
|
---|
[14573] | 368 | #endregion
|
---|
[14544] | 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)
|
---|
[14552] | 377 | || Maximization && a > b
|
---|
| 378 | || !Maximization && a < b;
|
---|
[14544] | 379 | }
|
---|
| 380 |
|
---|
[14420] | 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
|
---|
[14544] | 398 |
|
---|
[14552] | 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
|
---|
[14420] | 426 | }
|
---|
| 427 |
|
---|
| 428 | [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")]
|
---|
| 429 | [StorableClass]
|
---|
[14450] | 430 | public abstract class MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
[14552] | 431 | ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>, IEvaluationServiceContext<TSolution>
|
---|
| 432 | where TProblem : class, IItem, ISingleObjectiveHeuristicOptimizationProblem
|
---|
[14420] | 433 | where TSolution : class, IItem
|
---|
[14450] | 434 | where TContext : MemPRPopulationContext<TProblem, TSolution, TContext, TSolutionContext>
|
---|
| 435 | where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> {
|
---|
[14420] | 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 | }
|
---|
[14450] | 452 |
|
---|
| 453 | public TProblem Problem {
|
---|
| 454 | get { return parent.Problem; }
|
---|
[14420] | 455 | }
|
---|
[14552] | 456 | public bool Maximization {
|
---|
| 457 | get { return parent.Maximization; }
|
---|
| 458 | }
|
---|
[14420] | 459 |
|
---|
[14450] | 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 |
|
---|
[14420] | 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 |
|
---|
[14450] | 488 | ISingleObjectiveSolutionScope<TSolution> ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>.Solution {
|
---|
[14420] | 489 | get { return scope; }
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | [StorableConstructor]
|
---|
[14450] | 493 | protected MemPRSolutionContext(bool deserializing) : base(deserializing) { }
|
---|
| 494 | protected MemPRSolutionContext(MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> original, Cloner cloner)
|
---|
[14420] | 495 | : base(original, cloner) {
|
---|
| 496 | scope = cloner.Clone(original.scope);
|
---|
| 497 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
| 498 | iterations = cloner.Clone(original.iterations);
|
---|
| 499 | }
|
---|
[14450] | 500 | public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
|
---|
[14420] | 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 |
|
---|
[14450] | 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 | }
|
---|
[14552] | 512 | public virtual double Evaluate(TSolution solution, CancellationToken token) {
|
---|
| 513 | return parent.Evaluate(solution, token);
|
---|
| 514 | }
|
---|
[14450] | 515 |
|
---|
[14552] | 516 | public virtual void Evaluate(ISingleObjectiveSolutionScope<TSolution> solScope, CancellationToken token) {
|
---|
| 517 | parent.Evaluate(solScope, token);
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[14420] | 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 | }
|
---|