[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;
|
---|
[14450] | 27 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
[14678] | 28 | using HeuristicLab.Analysis.FitnessLandscape;
|
---|
[14420] | 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;
|
---|
[14544] | 36 | using ExecutionContext = HeuristicLab.Core.ExecutionContext;
|
---|
[14420] | 37 |
|
---|
| 38 | namespace HeuristicLab.Algorithms.MemPR {
|
---|
| 39 | [Item("MemPRContext", "Abstract base class for MemPR contexts.")]
|
---|
| 40 | [StorableClass]
|
---|
[14450] | 41 | public abstract class MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
[14690] | 42 | IPopulationBasedHeuristicAlgorithmContext<TProblem, TSolution>, ISolutionModelContext<TSolution>, IEvaluationServiceContext<TSolution>,
|
---|
| 43 | ILocalSearchPathContext<TSolution>
|
---|
| 44 | where TProblem : class, IItem, ISingleObjectiveHeuristicOptimizationProblem
|
---|
[14420] | 45 | where TSolution : class, IItem
|
---|
[14450] | 46 | where TPopulationContext : MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext>
|
---|
| 47 | where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TPopulationContext, TSolutionContext> {
|
---|
[14420] | 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]
|
---|
[14450] | 67 | private IValueParameter<TProblem> problem;
|
---|
| 68 | public TProblem Problem {
|
---|
| 69 | get { return problem.Value; }
|
---|
| 70 | set { problem.Value = value; }
|
---|
[14420] | 71 | }
|
---|
[14552] | 72 | public bool Maximization {
|
---|
| 73 | get { return ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
|
---|
| 74 | }
|
---|
[14420] | 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]
|
---|
[14450] | 105 | private IValueParameter<TSolution> bestSolution;
|
---|
| 106 | public TSolution BestSolution {
|
---|
| 107 | get { return bestSolution.Value; }
|
---|
| 108 | set { bestSolution.Value = value; }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | [Storable]
|
---|
[14496] | 112 | private IValueParameter<IntValue> localSearchEvaluations;
|
---|
| 113 | public int LocalSearchEvaluations {
|
---|
| 114 | get { return localSearchEvaluations.Value.Value; }
|
---|
| 115 | set { localSearchEvaluations.Value.Value = value; }
|
---|
[14420] | 116 | }
|
---|
| 117 |
|
---|
| 118 | [Storable]
|
---|
[14550] | 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]
|
---|
[14420] | 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]
|
---|
[14544] | 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]
|
---|
[14420] | 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]
|
---|
[14544] | 161 | private IValueParameter<IntValue> byAdaptivewalking;
|
---|
| 162 | public int ByAdaptivewalking {
|
---|
| 163 | get { return byAdaptivewalking.Value.Value; }
|
---|
| 164 | set { byAdaptivewalking.Value.Value = value; }
|
---|
[14420] | 165 | }
|
---|
| 166 |
|
---|
| 167 | [Storable]
|
---|
[14678] | 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]
|
---|
[14690] | 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]
|
---|
[14420] | 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 | }
|
---|
[14544] | 200 | public void SortPopulation() {
|
---|
[14552] | 201 | scope.SubScopes.Replace(scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>().OrderBy(x => Maximization ? -x.Fitness : x.Fitness).ToList());
|
---|
[14544] | 202 | }
|
---|
[14420] | 203 | public int PopulationCount {
|
---|
| 204 | get { return scope.SubScopes.Count; }
|
---|
| 205 | }
|
---|
[14666] | 206 |
|
---|
[14420] | 207 | [Storable]
|
---|
[14563] | 208 | private List<Tuple<double, double, double, double>> breedingStat;
|
---|
| 209 | public IEnumerable<Tuple<double, double, double, double>> BreedingStat {
|
---|
[14420] | 210 | get { return breedingStat; }
|
---|
| 211 | }
|
---|
| 212 | [Storable]
|
---|
[14563] | 213 | private List<Tuple<double, double, double, double>> relinkingStat;
|
---|
| 214 | public IEnumerable<Tuple<double, double, double, double>> RelinkingStat {
|
---|
[14544] | 215 | get { return relinkingStat; }
|
---|
| 216 | }
|
---|
| 217 | [Storable]
|
---|
[14563] | 218 | private List<Tuple<double, double, double, double>> delinkingStat;
|
---|
| 219 | public IEnumerable<Tuple<double, double, double, double>> DelinkingStat {
|
---|
[14544] | 220 | get { return delinkingStat; }
|
---|
| 221 | }
|
---|
| 222 | [Storable]
|
---|
| 223 | private List<Tuple<double, double>> samplingStat;
|
---|
[14563] | 224 | public IEnumerable<Tuple<double, double>> SamplingStat {
|
---|
[14544] | 225 | get { return samplingStat; }
|
---|
| 226 | }
|
---|
| 227 | [Storable]
|
---|
[14420] | 228 | private List<Tuple<double, double>> hillclimbingStat;
|
---|
[14563] | 229 | public IEnumerable<Tuple<double, double>> HillclimbingStat {
|
---|
[14420] | 230 | get { return hillclimbingStat; }
|
---|
| 231 | }
|
---|
| 232 | [Storable]
|
---|
[14544] | 233 | private List<Tuple<double, double>> adaptivewalkingStat;
|
---|
[14563] | 234 | public IEnumerable<Tuple<double, double>> AdaptivewalkingStat {
|
---|
[14544] | 235 | get { return adaptivewalkingStat; }
|
---|
| 236 | }
|
---|
[14420] | 237 |
|
---|
[14678] | 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 |
|
---|
[14420] | 254 | [Storable]
|
---|
| 255 | public ISolutionModel<TSolution> Model { get; set; }
|
---|
| 256 |
|
---|
| 257 | [StorableConstructor]
|
---|
[14450] | 258 | protected MemPRPopulationContext(bool deserializing) : base(deserializing) { }
|
---|
| 259 | protected MemPRPopulationContext(MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> original, Cloner cloner)
|
---|
[14420] | 260 | : base(original, cloner) {
|
---|
| 261 | scope = cloner.Clone(original.scope);
|
---|
[14450] | 262 | problem = cloner.Clone(original.problem);
|
---|
[14420] | 263 | initialized = cloner.Clone(original.initialized);
|
---|
| 264 | iterations = cloner.Clone(original.iterations);
|
---|
| 265 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
| 266 | bestQuality = cloner.Clone(original.bestQuality);
|
---|
[14450] | 267 | bestSolution = cloner.Clone(original.bestSolution);
|
---|
[14496] | 268 | localSearchEvaluations = cloner.Clone(original.localSearchEvaluations);
|
---|
[14550] | 269 | localOptimaLevel = cloner.Clone(original.localOptimaLevel);
|
---|
[14420] | 270 | byBreeding = cloner.Clone(original.byBreeding);
|
---|
| 271 | byRelinking = cloner.Clone(original.byRelinking);
|
---|
[14544] | 272 | byDelinking = cloner.Clone(original.byDelinking);
|
---|
[14420] | 273 | bySampling = cloner.Clone(original.bySampling);
|
---|
| 274 | byHillclimbing = cloner.Clone(original.byHillclimbing);
|
---|
[14544] | 275 | byAdaptivewalking = cloner.Clone(original.byAdaptivewalking);
|
---|
[14678] | 276 | relinkedPaths = cloner.Clone(original.relinkedPaths);
|
---|
[14690] | 277 | localSearchPaths = cloner.Clone(original.localSearchPaths);
|
---|
[14420] | 278 | random = cloner.Clone(original.random);
|
---|
[14563] | 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();
|
---|
[14544] | 282 | samplingStat = original.samplingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
[14420] | 283 | hillclimbingStat = original.hillclimbingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
[14544] | 284 | adaptivewalkingStat = original.adaptivewalkingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
| 285 |
|
---|
[14420] | 286 | Model = cloner.Clone(original.Model);
|
---|
| 287 | }
|
---|
[14450] | 288 | public MemPRPopulationContext() : this("MemPRContext") { }
|
---|
| 289 | public MemPRPopulationContext(string name) : base(name) {
|
---|
[14420] | 290 | scope = new Scope("Global");
|
---|
| 291 |
|
---|
[14450] | 292 | Parameters.Add(problem = new ValueParameter<TProblem>("Problem"));
|
---|
[14420] | 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)));
|
---|
[14552] | 297 | Parameters.Add(bestSolution = new ValueParameter<TSolution>("BestFoundSolution"));
|
---|
[14496] | 298 | Parameters.Add(localSearchEvaluations = new ValueParameter<IntValue>("LocalSearchEvaluations", new IntValue(0)));
|
---|
[14550] | 299 | Parameters.Add(localOptimaLevel = new ValueParameter<DoubleValue>("LocalOptimaLevel", new DoubleValue(0)));
|
---|
[14420] | 300 | Parameters.Add(byBreeding = new ValueParameter<IntValue>("ByBreeding", new IntValue(0)));
|
---|
| 301 | Parameters.Add(byRelinking = new ValueParameter<IntValue>("ByRelinking", new IntValue(0)));
|
---|
[14544] | 302 | Parameters.Add(byDelinking = new ValueParameter<IntValue>("ByDelinking", new IntValue(0)));
|
---|
[14420] | 303 | Parameters.Add(bySampling = new ValueParameter<IntValue>("BySampling", new IntValue(0)));
|
---|
| 304 | Parameters.Add(byHillclimbing = new ValueParameter<IntValue>("ByHillclimbing", new IntValue(0)));
|
---|
[14544] | 305 | Parameters.Add(byAdaptivewalking = new ValueParameter<IntValue>("ByAdaptivewalking", new IntValue(0)));
|
---|
[14678] | 306 | Parameters.Add(relinkedPaths = new ValueParameter<DirectedPath<TSolution>>("RelinkedPaths", new DirectedPath<TSolution>()));
|
---|
[14690] | 307 | Parameters.Add(localSearchPaths = new ValueParameter<DirectedPath<TSolution>>("LocalSearchPaths", new DirectedPath<TSolution>()));
|
---|
[14420] | 308 | Parameters.Add(random = new ValueParameter<IRandom>("Random", new MersenneTwister()));
|
---|
| 309 |
|
---|
[14563] | 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>>();
|
---|
[14544] | 313 | samplingStat = new List<Tuple<double, double>>();
|
---|
[14420] | 314 | hillclimbingStat = new List<Tuple<double, double>>();
|
---|
[14544] | 315 | adaptivewalkingStat = new List<Tuple<double, double>>();
|
---|
[14420] | 316 | }
|
---|
| 317 |
|
---|
[14552] | 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);
|
---|
[14694] | 323 | solScope.Solution = null;
|
---|
[14552] | 324 | return solScope.Fitness;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | public virtual void Evaluate(ISingleObjectiveSolutionScope<TSolution> solScope, CancellationToken token) {
|
---|
| 328 | var pdef = Problem as ISingleObjectiveProblemDefinition;
|
---|
| 329 | if (pdef != null) {
|
---|
| 330 | var ind = new SingleEncodingIndividual(pdef.Encoding, solScope);
|
---|
| 331 | solScope.Fitness = pdef.Evaluate(ind, Random);
|
---|
| 332 | } else {
|
---|
| 333 | RunOperator(Problem.Evaluator, solScope, token);
|
---|
| 334 | }
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[14420] | 337 | public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution);
|
---|
| 338 |
|
---|
[14450] | 339 | public void IncrementEvaluatedSolutions(int byEvaluations) {
|
---|
| 340 | if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
|
---|
| 341 | EvaluatedSolutions += byEvaluations;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
[14573] | 344 | #region Breeding Performance
|
---|
| 345 | public void AddBreedingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
| 346 | if (IsBetter(a, b))
|
---|
| 347 | breedingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, child.Fitness));
|
---|
| 348 | else breedingStat.Add(Tuple.Create(b.Fitness, a.Fitness, parentDist, child.Fitness));
|
---|
| 349 | }
|
---|
[14563] | 350 | public bool BreedingSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
[14666] | 351 | return true;
|
---|
[14544] | 352 | }
|
---|
[14573] | 353 | #endregion
|
---|
[14544] | 354 |
|
---|
[14573] | 355 | #region Relinking Performance
|
---|
| 356 | public void AddRelinkingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
| 357 | if (IsBetter(a, b))
|
---|
| 358 | relinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - a.Fitness : a.Fitness - child.Fitness));
|
---|
| 359 | else relinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - b.Fitness : b.Fitness - child.Fitness));
|
---|
| 360 | }
|
---|
[14563] | 361 | public bool RelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
[14666] | 362 | return true;
|
---|
[14544] | 363 | }
|
---|
[14573] | 364 | #endregion
|
---|
[14544] | 365 |
|
---|
[14573] | 366 | #region Delinking Performance
|
---|
| 367 | public void AddDelinkingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, double parentDist, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
| 368 | if (IsBetter(a, b))
|
---|
| 369 | delinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - a.Fitness : a.Fitness - child.Fitness));
|
---|
| 370 | else delinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, parentDist, Maximization ? child.Fitness - b.Fitness : b.Fitness - child.Fitness));
|
---|
| 371 | }
|
---|
[14563] | 372 | public bool DelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2, double dist) {
|
---|
[14666] | 373 | return true;
|
---|
[14544] | 374 | }
|
---|
[14573] | 375 | #endregion
|
---|
[14544] | 376 |
|
---|
[14573] | 377 | #region Sampling Performance
|
---|
| 378 | public void AddSamplingResult(ISingleObjectiveSolutionScope<TSolution> sample, double avgDist) {
|
---|
| 379 | samplingStat.Add(Tuple.Create(avgDist, sample.Fitness));
|
---|
| 380 | }
|
---|
[14563] | 381 | public bool SamplingSuited(double avgDist) {
|
---|
[14666] | 382 | return true;
|
---|
[14544] | 383 | }
|
---|
[14573] | 384 | #endregion
|
---|
[14544] | 385 |
|
---|
[14573] | 386 | #region Hillclimbing Performance
|
---|
| 387 | public void AddHillclimbingResult(ISingleObjectiveSolutionScope<TSolution> input, ISingleObjectiveSolutionScope<TSolution> outcome) {
|
---|
| 388 | hillclimbingStat.Add(Tuple.Create(input.Fitness, Maximization ? outcome.Fitness - input.Fitness : input.Fitness - outcome.Fitness));
|
---|
| 389 | }
|
---|
[14544] | 390 | public bool HillclimbingSuited(double startingFitness) {
|
---|
[14666] | 391 | return true;
|
---|
[14544] | 392 | }
|
---|
[14573] | 393 | #endregion
|
---|
[14544] | 394 |
|
---|
[14573] | 395 | #region Adaptivewalking Performance
|
---|
| 396 | public void AddAdaptivewalkingResult(ISingleObjectiveSolutionScope<TSolution> input, ISingleObjectiveSolutionScope<TSolution> outcome) {
|
---|
| 397 | adaptivewalkingStat.Add(Tuple.Create(input.Fitness, Maximization ? outcome.Fitness - input.Fitness : input.Fitness - outcome.Fitness));
|
---|
| 398 | }
|
---|
[14544] | 399 | public bool AdaptivewalkingSuited(double startingFitness) {
|
---|
[14666] | 400 | return true;
|
---|
[14544] | 401 | }
|
---|
[14573] | 402 | #endregion
|
---|
[14544] | 403 |
|
---|
| 404 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
---|
| 405 | public bool IsBetter(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b) {
|
---|
| 406 | return IsBetter(a.Fitness, b.Fitness);
|
---|
| 407 | }
|
---|
| 408 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
---|
| 409 | public bool IsBetter(double a, double b) {
|
---|
| 410 | return double.IsNaN(b) && !double.IsNaN(a)
|
---|
[14552] | 411 | || Maximization && a > b
|
---|
| 412 | || !Maximization && a < b;
|
---|
[14544] | 413 | }
|
---|
| 414 |
|
---|
[14420] | 415 | #region IExecutionContext members
|
---|
| 416 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
| 417 | return new ExecutionContext(this, op, Scope);
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
| 421 | return new ExecutionContext(this, op, s);
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
| 425 | return new ExecutionContext(this, op, Scope);
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
| 429 | return new ExecutionContext(this, op, s);
|
---|
| 430 | }
|
---|
| 431 | #endregion
|
---|
[14544] | 432 |
|
---|
[14552] | 433 | #region Engine Helper
|
---|
| 434 | public void RunOperator(IOperator op, IScope scope, CancellationToken cancellationToken) {
|
---|
| 435 | var stack = new Stack<IOperation>();
|
---|
| 436 | stack.Push(CreateChildOperation(op, scope));
|
---|
| 437 |
|
---|
| 438 | while (stack.Count > 0) {
|
---|
| 439 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 440 |
|
---|
| 441 | var next = stack.Pop();
|
---|
| 442 | if (next is OperationCollection) {
|
---|
| 443 | var coll = (OperationCollection)next;
|
---|
| 444 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
| 445 | if (coll[i] != null) stack.Push(coll[i]);
|
---|
| 446 | } else if (next is IAtomicOperation) {
|
---|
| 447 | var operation = (IAtomicOperation)next;
|
---|
| 448 | try {
|
---|
| 449 | next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
|
---|
| 450 | } catch (Exception ex) {
|
---|
| 451 | stack.Push(operation);
|
---|
| 452 | if (ex is OperationCanceledException) throw ex;
|
---|
| 453 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
| 454 | }
|
---|
| 455 | if (next != null) stack.Push(next);
|
---|
| 456 | }
|
---|
| 457 | }
|
---|
| 458 | }
|
---|
| 459 | #endregion
|
---|
[14420] | 460 | }
|
---|
| 461 |
|
---|
| 462 | [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")]
|
---|
| 463 | [StorableClass]
|
---|
[14450] | 464 | public abstract class MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
[14552] | 465 | ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>, IEvaluationServiceContext<TSolution>
|
---|
| 466 | where TProblem : class, IItem, ISingleObjectiveHeuristicOptimizationProblem
|
---|
[14420] | 467 | where TSolution : class, IItem
|
---|
[14450] | 468 | where TContext : MemPRPopulationContext<TProblem, TSolution, TContext, TSolutionContext>
|
---|
| 469 | where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> {
|
---|
[14420] | 470 |
|
---|
| 471 | private TContext parent;
|
---|
[14691] | 472 | protected TContext BaseContext {
|
---|
| 473 | get { return parent;}
|
---|
| 474 | }
|
---|
[14420] | 475 | public IExecutionContext Parent {
|
---|
| 476 | get { return parent; }
|
---|
| 477 | set { throw new InvalidOperationException("Cannot set the parent of a single-solution context."); }
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | [Storable]
|
---|
| 481 | private ISingleObjectiveSolutionScope<TSolution> scope;
|
---|
| 482 | public IScope Scope {
|
---|
| 483 | get { return scope; }
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
|
---|
| 487 | get { return Parameters; }
|
---|
| 488 | }
|
---|
[14450] | 489 |
|
---|
| 490 | public TProblem Problem {
|
---|
| 491 | get { return parent.Problem; }
|
---|
[14420] | 492 | }
|
---|
[14552] | 493 | public bool Maximization {
|
---|
| 494 | get { return parent.Maximization; }
|
---|
| 495 | }
|
---|
[14420] | 496 |
|
---|
[14450] | 497 | public double BestQuality {
|
---|
| 498 | get { return parent.BestQuality; }
|
---|
| 499 | set { parent.BestQuality = value; }
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | public TSolution BestSolution {
|
---|
| 503 | get { return parent.BestSolution; }
|
---|
| 504 | set { parent.BestSolution = value; }
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[14420] | 507 | public IRandom Random {
|
---|
| 508 | get { return parent.Random; }
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | [Storable]
|
---|
| 512 | private IValueParameter<IntValue> evaluatedSolutions;
|
---|
| 513 | public int EvaluatedSolutions {
|
---|
| 514 | get { return evaluatedSolutions.Value.Value; }
|
---|
| 515 | set { evaluatedSolutions.Value.Value = value; }
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | [Storable]
|
---|
| 519 | private IValueParameter<IntValue> iterations;
|
---|
| 520 | public int Iterations {
|
---|
| 521 | get { return iterations.Value.Value; }
|
---|
| 522 | set { iterations.Value.Value = value; }
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[14450] | 525 | ISingleObjectiveSolutionScope<TSolution> ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>.Solution {
|
---|
[14420] | 526 | get { return scope; }
|
---|
| 527 | }
|
---|
| 528 |
|
---|
| 529 | [StorableConstructor]
|
---|
[14450] | 530 | protected MemPRSolutionContext(bool deserializing) : base(deserializing) { }
|
---|
| 531 | protected MemPRSolutionContext(MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> original, Cloner cloner)
|
---|
[14420] | 532 | : base(original, cloner) {
|
---|
| 533 | scope = cloner.Clone(original.scope);
|
---|
| 534 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
| 535 | iterations = cloner.Clone(original.iterations);
|
---|
| 536 | }
|
---|
[14450] | 537 | public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
|
---|
[14420] | 538 | parent = baseContext;
|
---|
| 539 | scope = solution;
|
---|
| 540 |
|
---|
| 541 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
| 542 | Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[14450] | 545 | public void IncrementEvaluatedSolutions(int byEvaluations) {
|
---|
| 546 | if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
|
---|
| 547 | EvaluatedSolutions += byEvaluations;
|
---|
| 548 | }
|
---|
[14552] | 549 | public virtual double Evaluate(TSolution solution, CancellationToken token) {
|
---|
| 550 | return parent.Evaluate(solution, token);
|
---|
| 551 | }
|
---|
[14450] | 552 |
|
---|
[14552] | 553 | public virtual void Evaluate(ISingleObjectiveSolutionScope<TSolution> solScope, CancellationToken token) {
|
---|
| 554 | parent.Evaluate(solScope, token);
|
---|
| 555 | }
|
---|
| 556 |
|
---|
[14420] | 557 | #region IExecutionContext members
|
---|
| 558 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
| 559 | return new ExecutionContext(this, op, Scope);
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
| 563 | return new ExecutionContext(this, op, s);
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
| 567 | return new ExecutionContext(this, op, Scope);
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
| 571 | return new ExecutionContext(this, op, s);
|
---|
| 572 | }
|
---|
| 573 | #endregion
|
---|
| 574 | }
|
---|
| 575 | }
|
---|