[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;
|
---|
[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;
|
---|
[14544] | 35 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[14420] | 36 | using HeuristicLab.Random;
|
---|
[14544] | 37 | using ExecutionContext = HeuristicLab.Core.ExecutionContext;
|
---|
[14420] | 38 |
|
---|
| 39 | namespace HeuristicLab.Algorithms.MemPR {
|
---|
| 40 | [Item("MemPRContext", "Abstract base class for MemPR contexts.")]
|
---|
| 41 | [StorableClass]
|
---|
[14450] | 42 | public abstract class MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
| 43 | IPopulationBasedHeuristicAlgorithmContext<TProblem, TSolution>, ISolutionModelContext<TSolution>
|
---|
| 44 | where TProblem : class, IItem, ISingleObjectiveProblemDefinition
|
---|
[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 | }
|
---|
| 72 |
|
---|
| 73 | [Storable]
|
---|
| 74 | private IValueParameter<BoolValue> initialized;
|
---|
| 75 | public bool Initialized {
|
---|
| 76 | get { return initialized.Value.Value; }
|
---|
| 77 | set { initialized.Value.Value = value; }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | [Storable]
|
---|
| 81 | private IValueParameter<IntValue> iterations;
|
---|
| 82 | public int Iterations {
|
---|
| 83 | get { return iterations.Value.Value; }
|
---|
| 84 | set { iterations.Value.Value = value; }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | [Storable]
|
---|
| 88 | private IValueParameter<IntValue> evaluatedSolutions;
|
---|
| 89 | public int EvaluatedSolutions {
|
---|
| 90 | get { return evaluatedSolutions.Value.Value; }
|
---|
| 91 | set { evaluatedSolutions.Value.Value = value; }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | [Storable]
|
---|
| 95 | private IValueParameter<DoubleValue> bestQuality;
|
---|
| 96 | public double BestQuality {
|
---|
| 97 | get { return bestQuality.Value.Value; }
|
---|
| 98 | set { bestQuality.Value.Value = value; }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | [Storable]
|
---|
[14450] | 102 | private IValueParameter<TSolution> bestSolution;
|
---|
| 103 | public TSolution BestSolution {
|
---|
| 104 | get { return bestSolution.Value; }
|
---|
| 105 | set { bestSolution.Value = value; }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | [Storable]
|
---|
[14496] | 109 | private IValueParameter<IntValue> localSearchEvaluations;
|
---|
| 110 | public int LocalSearchEvaluations {
|
---|
| 111 | get { return localSearchEvaluations.Value.Value; }
|
---|
| 112 | set { localSearchEvaluations.Value.Value = value; }
|
---|
[14420] | 113 | }
|
---|
| 114 |
|
---|
| 115 | [Storable]
|
---|
| 116 | private IValueParameter<IntValue> byBreeding;
|
---|
| 117 | public int ByBreeding {
|
---|
| 118 | get { return byBreeding.Value.Value; }
|
---|
| 119 | set { byBreeding.Value.Value = value; }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | [Storable]
|
---|
| 123 | private IValueParameter<IntValue> byRelinking;
|
---|
| 124 | public int ByRelinking {
|
---|
| 125 | get { return byRelinking.Value.Value; }
|
---|
| 126 | set { byRelinking.Value.Value = value; }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | [Storable]
|
---|
[14544] | 130 | private IValueParameter<IntValue> byDelinking;
|
---|
| 131 | public int ByDelinking {
|
---|
| 132 | get { return byDelinking.Value.Value; }
|
---|
| 133 | set { byDelinking.Value.Value = value; }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | [Storable]
|
---|
[14420] | 137 | private IValueParameter<IntValue> bySampling;
|
---|
| 138 | public int BySampling {
|
---|
| 139 | get { return bySampling.Value.Value; }
|
---|
| 140 | set { bySampling.Value.Value = value; }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | [Storable]
|
---|
| 144 | private IValueParameter<IntValue> byHillclimbing;
|
---|
| 145 | public int ByHillclimbing {
|
---|
| 146 | get { return byHillclimbing.Value.Value; }
|
---|
| 147 | set { byHillclimbing.Value.Value = value; }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | [Storable]
|
---|
[14544] | 151 | private IValueParameter<IntValue> byAdaptivewalking;
|
---|
| 152 | public int ByAdaptivewalking {
|
---|
| 153 | get { return byAdaptivewalking.Value.Value; }
|
---|
| 154 | set { byAdaptivewalking.Value.Value = value; }
|
---|
[14420] | 155 | }
|
---|
| 156 |
|
---|
| 157 | [Storable]
|
---|
| 158 | private IValueParameter<IRandom> random;
|
---|
| 159 | public IRandom Random {
|
---|
| 160 | get { return random.Value; }
|
---|
| 161 | set { random.Value = value; }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | public IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Population {
|
---|
| 165 | get { return scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>(); }
|
---|
| 166 | }
|
---|
| 167 | public void AddToPopulation(ISingleObjectiveSolutionScope<TSolution> solScope) {
|
---|
| 168 | scope.SubScopes.Add(solScope);
|
---|
| 169 | }
|
---|
| 170 | public void ReplaceAtPopulation(int index, ISingleObjectiveSolutionScope<TSolution> solScope) {
|
---|
| 171 | scope.SubScopes[index] = solScope;
|
---|
| 172 | }
|
---|
| 173 | public ISingleObjectiveSolutionScope<TSolution> AtPopulation(int index) {
|
---|
| 174 | return scope.SubScopes[index] as ISingleObjectiveSolutionScope<TSolution>;
|
---|
| 175 | }
|
---|
[14544] | 176 | public void SortPopulation() {
|
---|
| 177 | scope.SubScopes.Replace(scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>().OrderBy(x => Problem.Maximization ? -x.Fitness : x.Fitness).ToList());
|
---|
| 178 | }
|
---|
[14420] | 179 | public int PopulationCount {
|
---|
| 180 | get { return scope.SubScopes.Count; }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | [Storable]
|
---|
[14544] | 184 | private IConfidenceRegressionModel breedingPerformanceModel;
|
---|
| 185 | public IConfidenceRegressionModel BreedingPerformanceModel {
|
---|
| 186 | get { return breedingPerformanceModel; }
|
---|
| 187 | }
|
---|
| 188 | [Storable]
|
---|
[14420] | 189 | private List<Tuple<double, double, double>> breedingStat;
|
---|
| 190 | public List<Tuple<double, double, double>> BreedingStat {
|
---|
| 191 | get { return breedingStat; }
|
---|
| 192 | }
|
---|
| 193 | [Storable]
|
---|
[14544] | 194 | private IConfidenceRegressionModel relinkingPerformanceModel;
|
---|
| 195 | public IConfidenceRegressionModel RelinkingPerformanceModel {
|
---|
| 196 | get { return relinkingPerformanceModel; }
|
---|
| 197 | }
|
---|
| 198 | [Storable]
|
---|
| 199 | private List<Tuple<double, double, double>> relinkingStat;
|
---|
| 200 | public List<Tuple<double, double, double>> RelinkingStat {
|
---|
| 201 | get { return relinkingStat; }
|
---|
| 202 | }
|
---|
| 203 | [Storable]
|
---|
| 204 | private IConfidenceRegressionModel delinkingPerformanceModel;
|
---|
| 205 | public IConfidenceRegressionModel DelinkingPerformanceModel {
|
---|
| 206 | get { return delinkingPerformanceModel; }
|
---|
| 207 | }
|
---|
| 208 | [Storable]
|
---|
| 209 | private List<Tuple<double, double, double>> delinkingStat;
|
---|
| 210 | public List<Tuple<double, double, double>> DelinkingStat {
|
---|
| 211 | get { return delinkingStat; }
|
---|
| 212 | }
|
---|
| 213 | [Storable]
|
---|
| 214 | private IConfidenceRegressionModel samplingPerformanceModel;
|
---|
| 215 | public IConfidenceRegressionModel SamplingPerformanceModel {
|
---|
| 216 | get { return samplingPerformanceModel; }
|
---|
| 217 | }
|
---|
| 218 | [Storable]
|
---|
| 219 | private List<Tuple<double, double>> samplingStat;
|
---|
| 220 | public List<Tuple<double, double>> SamplingStat {
|
---|
| 221 | get { return samplingStat; }
|
---|
| 222 | }
|
---|
| 223 | [Storable]
|
---|
| 224 | private IConfidenceRegressionModel hillclimbingPerformanceModel;
|
---|
| 225 | public IConfidenceRegressionModel HillclimbingPerformanceModel {
|
---|
| 226 | get { return hillclimbingPerformanceModel; }
|
---|
| 227 | }
|
---|
| 228 | [Storable]
|
---|
[14420] | 229 | private List<Tuple<double, double>> hillclimbingStat;
|
---|
| 230 | public List<Tuple<double, double>> HillclimbingStat {
|
---|
| 231 | get { return hillclimbingStat; }
|
---|
| 232 | }
|
---|
| 233 | [Storable]
|
---|
[14544] | 234 | private IConfidenceRegressionModel adaptiveWalkPerformanceModel;
|
---|
| 235 | public IConfidenceRegressionModel AdaptiveWalkPerformanceModel {
|
---|
| 236 | get { return adaptiveWalkPerformanceModel; }
|
---|
[14420] | 237 | }
|
---|
[14544] | 238 | [Storable]
|
---|
| 239 | private List<Tuple<double, double>> adaptivewalkingStat;
|
---|
| 240 | public List<Tuple<double, double>> AdaptivewalkingStat {
|
---|
| 241 | get { return adaptivewalkingStat; }
|
---|
| 242 | }
|
---|
[14420] | 243 |
|
---|
| 244 | [Storable]
|
---|
| 245 | public ISolutionModel<TSolution> Model { get; set; }
|
---|
| 246 |
|
---|
| 247 | [StorableConstructor]
|
---|
[14450] | 248 | protected MemPRPopulationContext(bool deserializing) : base(deserializing) { }
|
---|
| 249 | protected MemPRPopulationContext(MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> original, Cloner cloner)
|
---|
[14420] | 250 | : base(original, cloner) {
|
---|
| 251 | scope = cloner.Clone(original.scope);
|
---|
[14450] | 252 | problem = cloner.Clone(original.problem);
|
---|
[14420] | 253 | initialized = cloner.Clone(original.initialized);
|
---|
| 254 | iterations = cloner.Clone(original.iterations);
|
---|
| 255 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
| 256 | bestQuality = cloner.Clone(original.bestQuality);
|
---|
[14450] | 257 | bestSolution = cloner.Clone(original.bestSolution);
|
---|
[14496] | 258 | localSearchEvaluations = cloner.Clone(original.localSearchEvaluations);
|
---|
[14420] | 259 | byBreeding = cloner.Clone(original.byBreeding);
|
---|
| 260 | byRelinking = cloner.Clone(original.byRelinking);
|
---|
[14544] | 261 | byDelinking = cloner.Clone(original.byDelinking);
|
---|
[14420] | 262 | bySampling = cloner.Clone(original.bySampling);
|
---|
| 263 | byHillclimbing = cloner.Clone(original.byHillclimbing);
|
---|
[14544] | 264 | byAdaptivewalking = cloner.Clone(original.byAdaptivewalking);
|
---|
[14420] | 265 | random = cloner.Clone(original.random);
|
---|
[14544] | 266 | breedingPerformanceModel = cloner.Clone(original.breedingPerformanceModel);
|
---|
[14420] | 267 | breedingStat = original.breedingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3)).ToList();
|
---|
[14544] | 268 | relinkingPerformanceModel = cloner.Clone(original.relinkingPerformanceModel);
|
---|
| 269 | relinkingStat = original.relinkingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3)).ToList();
|
---|
| 270 | delinkingPerformanceModel = cloner.Clone(original.delinkingPerformanceModel);
|
---|
| 271 | delinkingStat = original.delinkingStat.Select(x => Tuple.Create(x.Item1, x.Item2, x.Item3)).ToList();
|
---|
| 272 | samplingPerformanceModel = cloner.Clone(original.samplingPerformanceModel);
|
---|
| 273 | samplingStat = original.samplingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
| 274 | hillclimbingPerformanceModel = cloner.Clone(original.hillclimbingPerformanceModel);
|
---|
[14420] | 275 | hillclimbingStat = original.hillclimbingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
[14544] | 276 | adaptiveWalkPerformanceModel = cloner.Clone(original.adaptiveWalkPerformanceModel);
|
---|
| 277 | adaptivewalkingStat = original.adaptivewalkingStat.Select(x => Tuple.Create(x.Item1, x.Item2)).ToList();
|
---|
| 278 |
|
---|
[14420] | 279 | Model = cloner.Clone(original.Model);
|
---|
| 280 | }
|
---|
[14450] | 281 | public MemPRPopulationContext() : this("MemPRContext") { }
|
---|
| 282 | public MemPRPopulationContext(string name) : base(name) {
|
---|
[14420] | 283 | scope = new Scope("Global");
|
---|
| 284 |
|
---|
[14450] | 285 | Parameters.Add(problem = new ValueParameter<TProblem>("Problem"));
|
---|
[14420] | 286 | Parameters.Add(initialized = new ValueParameter<BoolValue>("Initialized", new BoolValue(false)));
|
---|
| 287 | Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
| 288 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
| 289 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
|
---|
[14450] | 290 | Parameters.Add(bestSolution = new ValueParameter<TSolution>("BestSolution"));
|
---|
[14496] | 291 | Parameters.Add(localSearchEvaluations = new ValueParameter<IntValue>("LocalSearchEvaluations", new IntValue(0)));
|
---|
[14420] | 292 | Parameters.Add(byBreeding = new ValueParameter<IntValue>("ByBreeding", new IntValue(0)));
|
---|
| 293 | Parameters.Add(byRelinking = new ValueParameter<IntValue>("ByRelinking", new IntValue(0)));
|
---|
[14544] | 294 | Parameters.Add(byDelinking = new ValueParameter<IntValue>("ByDelinking", new IntValue(0)));
|
---|
[14420] | 295 | Parameters.Add(bySampling = new ValueParameter<IntValue>("BySampling", new IntValue(0)));
|
---|
| 296 | Parameters.Add(byHillclimbing = new ValueParameter<IntValue>("ByHillclimbing", new IntValue(0)));
|
---|
[14544] | 297 | Parameters.Add(byAdaptivewalking = new ValueParameter<IntValue>("ByAdaptivewalking", new IntValue(0)));
|
---|
[14420] | 298 | Parameters.Add(random = new ValueParameter<IRandom>("Random", new MersenneTwister()));
|
---|
| 299 |
|
---|
| 300 | breedingStat = new List<Tuple<double, double, double>>();
|
---|
[14544] | 301 | relinkingStat = new List<Tuple<double, double, double>>();
|
---|
| 302 | delinkingStat = new List<Tuple<double, double, double>>();
|
---|
| 303 | samplingStat = new List<Tuple<double, double>>();
|
---|
[14420] | 304 | hillclimbingStat = new List<Tuple<double, double>>();
|
---|
[14544] | 305 | adaptivewalkingStat = new List<Tuple<double, double>>();
|
---|
[14420] | 306 | }
|
---|
| 307 |
|
---|
| 308 | public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution);
|
---|
| 309 |
|
---|
[14450] | 310 | public void IncrementEvaluatedSolutions(int byEvaluations) {
|
---|
| 311 | if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
|
---|
| 312 | EvaluatedSolutions += byEvaluations;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[14544] | 315 | public void RelearnBreedingPerformanceModel() {
|
---|
| 316 | breedingPerformanceModel = RunRegression(PrepareRegression(BreedingStat), breedingPerformanceModel).Model;
|
---|
| 317 | }
|
---|
| 318 | public bool BreedingSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2) {
|
---|
| 319 | if (breedingPerformanceModel == null) return true;
|
---|
| 320 | double minI1 = double.MaxValue, minI2 = double.MaxValue, maxI1 = double.MinValue, maxI2 = double.MinValue;
|
---|
| 321 | foreach (var d in BreedingStat) {
|
---|
| 322 | if (d.Item1 < minI1) minI1 = d.Item1;
|
---|
| 323 | if (d.Item1 > maxI1) maxI1 = d.Item1;
|
---|
| 324 | if (d.Item2 < minI2) minI2 = d.Item2;
|
---|
| 325 | if (d.Item2 > maxI2) maxI2 = d.Item2;
|
---|
| 326 | }
|
---|
| 327 | if (IsBetter(p1, p2)) {
|
---|
| 328 | if (p1.Fitness < minI1 || p1.Fitness > maxI1 || p2.Fitness < minI2 || p2.Fitness > maxI2)
|
---|
| 329 | return true;
|
---|
| 330 | return Random.NextDouble() < ProbabilityAccept3dModel(p1.Fitness, p2.Fitness, breedingPerformanceModel);
|
---|
| 331 | }
|
---|
| 332 | if (p1.Fitness < minI2 || p1.Fitness > maxI2 || p2.Fitness < minI1 || p2.Fitness > maxI1)
|
---|
| 333 | return true;
|
---|
| 334 | return Random.NextDouble() < ProbabilityAccept3dModel(p2.Fitness, p1.Fitness, breedingPerformanceModel);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | public void RelearnRelinkingPerformanceModel() {
|
---|
| 338 | relinkingPerformanceModel = RunRegression(PrepareRegression(RelinkingStat), relinkingPerformanceModel).Model;
|
---|
| 339 | }
|
---|
| 340 | public bool RelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2) {
|
---|
| 341 | if (relinkingPerformanceModel == null) return true;
|
---|
| 342 | double minI1 = double.MaxValue, minI2 = double.MaxValue, maxI1 = double.MinValue, maxI2 = double.MinValue;
|
---|
| 343 | foreach (var d in RelinkingStat) {
|
---|
| 344 | if (d.Item1 < minI1) minI1 = d.Item1;
|
---|
| 345 | if (d.Item1 > maxI1) maxI1 = d.Item1;
|
---|
| 346 | if (d.Item2 < minI2) minI2 = d.Item2;
|
---|
| 347 | if (d.Item2 > maxI2) maxI2 = d.Item2;
|
---|
| 348 | }
|
---|
| 349 | if (IsBetter(p1, p2)) {
|
---|
| 350 | if (p1.Fitness < minI1 || p1.Fitness > maxI1 || p2.Fitness < minI2 || p2.Fitness > maxI2)
|
---|
| 351 | return true;
|
---|
| 352 | return Random.NextDouble() < ProbabilityAccept3dModel(p1.Fitness, p2.Fitness, relinkingPerformanceModel);
|
---|
| 353 | }
|
---|
| 354 | if (p1.Fitness < minI2 || p1.Fitness > maxI2 || p2.Fitness < minI1 || p2.Fitness > maxI1)
|
---|
| 355 | return true;
|
---|
| 356 | return Random.NextDouble() < ProbabilityAccept3dModel(p2.Fitness, p1.Fitness, relinkingPerformanceModel);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | public void RelearnDelinkingPerformanceModel() {
|
---|
| 360 | delinkingPerformanceModel = RunRegression(PrepareRegression(DelinkingStat), delinkingPerformanceModel).Model;
|
---|
| 361 | }
|
---|
| 362 | public bool DelinkSuited(ISingleObjectiveSolutionScope<TSolution> p1, ISingleObjectiveSolutionScope<TSolution> p2) {
|
---|
| 363 | if (delinkingPerformanceModel == null) return true;
|
---|
| 364 | double minI1 = double.MaxValue, minI2 = double.MaxValue, maxI1 = double.MinValue, maxI2 = double.MinValue;
|
---|
| 365 | foreach (var d in DelinkingStat) {
|
---|
| 366 | if (d.Item1 < minI1) minI1 = d.Item1;
|
---|
| 367 | if (d.Item1 > maxI1) maxI1 = d.Item1;
|
---|
| 368 | if (d.Item2 < minI2) minI2 = d.Item2;
|
---|
| 369 | if (d.Item2 > maxI2) maxI2 = d.Item2;
|
---|
| 370 | }
|
---|
| 371 | if (IsBetter(p1, p2)) {
|
---|
| 372 | if (p1.Fitness < minI1 || p1.Fitness > maxI1 || p2.Fitness < minI2 || p2.Fitness > maxI2)
|
---|
| 373 | return true;
|
---|
| 374 | return Random.NextDouble() < ProbabilityAccept3dModel(p1.Fitness, p2.Fitness, delinkingPerformanceModel);
|
---|
| 375 | }
|
---|
| 376 | if (p1.Fitness < minI2 || p1.Fitness > maxI2 || p2.Fitness < minI1 || p2.Fitness > maxI1)
|
---|
| 377 | return true;
|
---|
| 378 | return Random.NextDouble() < ProbabilityAccept3dModel(p2.Fitness, p1.Fitness, delinkingPerformanceModel);
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | public void RelearnSamplingPerformanceModel() {
|
---|
| 382 | samplingPerformanceModel = RunRegression(PrepareRegression(SamplingStat), samplingPerformanceModel).Model;
|
---|
| 383 | }
|
---|
| 384 | public bool SamplingSuited() {
|
---|
| 385 | if (samplingPerformanceModel == null) return true;
|
---|
| 386 | return Random.NextDouble() < ProbabilityAccept2dModel(Population.Average(x => x.Fitness), samplingPerformanceModel);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | public void RelearnHillclimbingPerformanceModel() {
|
---|
| 390 | hillclimbingPerformanceModel = RunRegression(PrepareRegression(HillclimbingStat), hillclimbingPerformanceModel).Model;
|
---|
| 391 | }
|
---|
| 392 | public bool HillclimbingSuited(ISingleObjectiveSolutionScope<TSolution> scope) {
|
---|
| 393 | if (hillclimbingPerformanceModel == null) return true;
|
---|
| 394 | if (scope.Fitness < HillclimbingStat.Min(x => x.Item1) || scope.Fitness > HillclimbingStat.Max(x => x.Item1))
|
---|
| 395 | return true;
|
---|
| 396 | return Random.NextDouble() < ProbabilityAccept2dModel(scope.Fitness, hillclimbingPerformanceModel);
|
---|
| 397 | }
|
---|
| 398 | public bool HillclimbingSuited(double startingFitness) {
|
---|
| 399 | if (hillclimbingPerformanceModel == null) return true;
|
---|
| 400 | if (startingFitness < HillclimbingStat.Min(x => x.Item1) || startingFitness > HillclimbingStat.Max(x => x.Item1))
|
---|
| 401 | return true;
|
---|
| 402 | return Random.NextDouble() < ProbabilityAccept2dModel(startingFitness, hillclimbingPerformanceModel);
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | public void RelearnAdaptiveWalkPerformanceModel() {
|
---|
| 406 | adaptiveWalkPerformanceModel = RunRegression(PrepareRegression(AdaptivewalkingStat), adaptiveWalkPerformanceModel).Model;
|
---|
| 407 | }
|
---|
| 408 | public bool AdaptivewalkingSuited(ISingleObjectiveSolutionScope<TSolution> scope) {
|
---|
| 409 | if (adaptiveWalkPerformanceModel == null) return true;
|
---|
| 410 | if (scope.Fitness < AdaptivewalkingStat.Min(x => x.Item1) || scope.Fitness > AdaptivewalkingStat.Max(x => x.Item1))
|
---|
| 411 | return true;
|
---|
| 412 | return Random.NextDouble() < ProbabilityAccept2dModel(scope.Fitness, adaptiveWalkPerformanceModel);
|
---|
| 413 | }
|
---|
| 414 | public bool AdaptivewalkingSuited(double startingFitness) {
|
---|
| 415 | if (adaptiveWalkPerformanceModel == null) return true;
|
---|
| 416 | if (startingFitness < AdaptivewalkingStat.Min(x => x.Item1) || startingFitness > AdaptivewalkingStat.Max(x => x.Item1))
|
---|
| 417 | return true;
|
---|
| 418 | return Random.NextDouble() < ProbabilityAccept2dModel(startingFitness, adaptiveWalkPerformanceModel);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | public IConfidenceRegressionSolution GetSolution(IConfidenceRegressionModel model, List<Tuple<double, double>> data) {
|
---|
| 422 | return new ConfidenceRegressionSolution(model, PrepareRegression(data));
|
---|
| 423 | }
|
---|
| 424 | public IConfidenceRegressionSolution GetSolution(IConfidenceRegressionModel model, List<Tuple<double, double, double>> data) {
|
---|
| 425 | return new ConfidenceRegressionSolution(model, PrepareRegression(data));
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | protected RegressionProblemData PrepareRegression(List<Tuple<double, double>> sample) {
|
---|
| 429 | var inCol = new List<double>();
|
---|
| 430 | var outCol = new List<double>();
|
---|
| 431 | foreach (var next in sample.Shuffle(Random)) {
|
---|
| 432 | inCol.Add(next.Item1);
|
---|
| 433 | outCol.Add(next.Item2);
|
---|
| 434 | }
|
---|
| 435 | var ds = new Dataset(new[] { "in", "out" }, new[] { inCol, outCol });
|
---|
| 436 | var regPrb = new RegressionProblemData(ds, new[] { "in" }, "out") {
|
---|
| 437 | TrainingPartition = { Start = 0, End = Math.Min(50, sample.Count) },
|
---|
| 438 | TestPartition = { Start = Math.Min(50, sample.Count), End = sample.Count }
|
---|
| 439 | };
|
---|
| 440 | return regPrb;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | protected RegressionProblemData PrepareRegression(List<Tuple<double, double, double>> sample) {
|
---|
| 444 | var in1Col = new List<double>();
|
---|
| 445 | var in2Col = new List<double>();
|
---|
| 446 | var outCol = new List<double>();
|
---|
| 447 | foreach (var next in sample.Shuffle(Random)) {
|
---|
| 448 | in1Col.Add(next.Item1);
|
---|
| 449 | in2Col.Add(next.Item2);
|
---|
| 450 | outCol.Add(next.Item3);
|
---|
| 451 | }
|
---|
| 452 | var ds = new Dataset(new[] { "in1", "in2", "out" }, new[] { in1Col, in2Col, outCol });
|
---|
| 453 | var regPrb = new RegressionProblemData(ds, new[] { "in1", "in2" }, "out") {
|
---|
| 454 | TrainingPartition = { Start = 0, End = Math.Min(50, sample.Count) },
|
---|
| 455 | TestPartition = { Start = Math.Min(50, sample.Count), End = sample.Count }
|
---|
| 456 | };
|
---|
| 457 | return regPrb;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | protected static IConfidenceRegressionSolution RunRegression(RegressionProblemData trainingData, IConfidenceRegressionModel baseLineModel = null) {
|
---|
| 461 | var baseline = baseLineModel != null ? new ConfidenceRegressionSolution(baseLineModel, trainingData) : null;
|
---|
| 462 | var gpr = new GaussianProcessRegression { Problem = { ProblemData = trainingData } };
|
---|
| 463 | if (trainingData.InputVariables.CheckedItems.Any(x => alglib.pearsoncorr2(trainingData.Dataset.GetDoubleValues(x.Value.Value).ToArray(), trainingData.TargetVariableValues.ToArray()) > 0.8)) {
|
---|
| 464 | gpr.MeanFunction = new MeanZero();
|
---|
| 465 | var cov1 = new CovarianceSum();
|
---|
| 466 | cov1.Terms.Add(new CovarianceLinearArd());
|
---|
| 467 | cov1.Terms.Add(new CovarianceConst());
|
---|
| 468 | gpr.CovarianceFunction = cov1;
|
---|
| 469 | }
|
---|
| 470 | IConfidenceRegressionSolution solution = null;
|
---|
| 471 | var cnt = 0;
|
---|
| 472 | do {
|
---|
| 473 | ExecuteAlgorithm(gpr);
|
---|
| 474 | solution = (IConfidenceRegressionSolution)gpr.Results["Solution"].Value;
|
---|
| 475 | cnt++;
|
---|
| 476 | } while (cnt < 10 && (solution == null || solution.TrainingRSquared.IsAlmost(0)));
|
---|
| 477 | if (baseline == null) return solution;
|
---|
| 478 | if (trainingData.Dataset.Rows < 60)
|
---|
| 479 | return solution.TrainingMeanAbsoluteError < baseline.TrainingMeanAbsoluteError ? solution : baseline;
|
---|
| 480 | return solution.TestMeanAbsoluteError < baseline.TestMeanAbsoluteError ? solution : baseline;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | protected static void ExecuteAlgorithm(IAlgorithm algorithm) {
|
---|
| 484 | using (var evt = new AutoResetEvent(false)) {
|
---|
| 485 | EventHandler exeStateChanged = (o, args) => {
|
---|
| 486 | if (algorithm.ExecutionState == ExecutionState.Paused || algorithm.ExecutionState == ExecutionState.Stopped)
|
---|
| 487 | evt.Set();
|
---|
| 488 | };
|
---|
| 489 | algorithm.ExecutionStateChanged += exeStateChanged;
|
---|
| 490 | algorithm.Prepare(true);
|
---|
| 491 | algorithm.Start();
|
---|
| 492 | evt.WaitOne();
|
---|
| 493 | algorithm.ExecutionStateChanged -= exeStateChanged;
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | protected double ProbabilityAccept(ISingleObjectiveSolutionScope<TSolution> scope, IList<Tuple<double, double>> data) {
|
---|
| 498 | if (double.IsNaN(scope.Fitness)) throw new ArgumentException("solution not evaluated or quality unknown", "scope");
|
---|
| 499 | return ProbabilityAccept2d(scope.Fitness, data);
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | private double ProbabilityAccept2dModel(double a, IConfidenceRegressionModel model) {
|
---|
| 503 | var ds = new Dataset(new[] { "in", "out" }, new[] { new List<double> { a }, new List<double> { double.NaN } });
|
---|
| 504 | var mean = model.GetEstimatedValues(ds, new[] { 0 }).Single();
|
---|
| 505 | var sdev = Math.Sqrt(model.GetEstimatedVariances(ds, new[] { 0 }).Single());
|
---|
| 506 |
|
---|
| 507 | var goal = Problem.Maximization ? Population.Min(x => x.Fitness) : Population.Max(x => x.Fitness);
|
---|
| 508 | var z = (goal - mean) / sdev;
|
---|
| 509 | return Problem.Maximization ? 1.0 - Phi(z) /* P(X >= z) */ : Phi(z); // P(X <= z)
|
---|
| 510 | }
|
---|
| 511 | protected double ProbabilityAccept2d(double startingFitness, IList<Tuple<double, double>> data) {
|
---|
| 512 | if (data.Count < 10) return 1.0;
|
---|
| 513 | var samples = 0;
|
---|
| 514 | double meanStart = 0, meanStartOld = 0, meanEnd = 0, meanEndOld = 0;
|
---|
| 515 | double varStart = 0, varStartOld = 0, varEnd = 0, varEndOld = 0;
|
---|
| 516 | for (var i = 0; i < data.Count; i++) {
|
---|
| 517 | samples++;
|
---|
| 518 | var x = data[i].Item1;
|
---|
| 519 | var y = data[i].Item2;
|
---|
| 520 |
|
---|
| 521 | if (samples == 1) {
|
---|
| 522 | meanStartOld = x;
|
---|
| 523 | meanEndOld = y;
|
---|
| 524 | } else {
|
---|
| 525 | meanStart = meanStartOld + (x - meanStartOld) / samples;
|
---|
| 526 | meanEnd = meanEndOld + (y - meanEndOld) / samples;
|
---|
| 527 | varStart = varStartOld + (x - meanStartOld) * (x - meanStart) / (samples - 1);
|
---|
| 528 | varEnd = varEndOld + (y - meanEndOld) * (y - meanEnd) / (samples - 1);
|
---|
| 529 |
|
---|
| 530 | meanStartOld = meanStart;
|
---|
| 531 | meanEndOld = meanEnd;
|
---|
| 532 | varStartOld = varStart;
|
---|
| 533 | varEndOld = varEnd;
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 | var cov = data.Select((v, i) => new { Index = i, Value = v }).Select(x => x.Value).Sum(x => (x.Item1 - meanStart) * (x.Item2 - meanEnd)) / data.Count;
|
---|
| 537 |
|
---|
| 538 | var biasedMean = meanEnd + cov / varStart * (startingFitness - meanStart);
|
---|
| 539 | var biasedStdev = Math.Sqrt(varEnd - (cov * cov) / varStart);
|
---|
| 540 |
|
---|
| 541 | if (Problem.Maximization) {
|
---|
| 542 | var goal = Population.Min(x => x.Fitness);
|
---|
| 543 | var z = (goal - biasedMean) / biasedStdev;
|
---|
| 544 | return 1.0 - Phi(z); // P(X >= z)
|
---|
| 545 | } else {
|
---|
| 546 | var goal = Population.Max(x => x.Fitness);
|
---|
| 547 | var z = (goal - biasedMean) / biasedStdev;
|
---|
| 548 | return Phi(z); // P(X <= z)
|
---|
| 549 | }
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | private double ProbabilityAccept3dModel(double a, double b, IConfidenceRegressionModel model) {
|
---|
| 553 | var ds = new Dataset(new[] { "in1", "in2", "out" }, new[] { new List<double> { a }, new List<double> { b }, new List<double> { double.NaN } });
|
---|
| 554 | var mean = model.GetEstimatedValues(ds, new[] { 0 }).Single();
|
---|
| 555 | var sdev = Math.Sqrt(model.GetEstimatedVariances(ds, new[] { 0 }).Single());
|
---|
| 556 |
|
---|
| 557 | var goal = Problem.Maximization ? Population.Min(x => x.Fitness) : Population.Max(x => x.Fitness);
|
---|
| 558 | var z = (goal - mean) / sdev;
|
---|
| 559 | return Problem.Maximization ? 1.0 - Phi(z) /* P(X >= z) */ : Phi(z); // P(X <= z)
|
---|
| 560 | }
|
---|
| 561 | protected double ProbabilityAccept3d(double startingFitness1, double startingFitness2, IList<Tuple<double, double, double>> data) {
|
---|
| 562 | if (data.Count < 20) return 1.0;
|
---|
| 563 | var samples = 0;
|
---|
| 564 | double meanX1 = 0, meanX1Old = 0, meanX2 = 0, meanX2Old = 0, meanY = 0, meanYOld = 0;
|
---|
| 565 | double varX1 = 0, varX1Old = 0, varX2 = 0, varX2Old = 0, varY = 0, varYOld = 0;
|
---|
| 566 | for (var i = 0; i < data.Count; i++) {
|
---|
| 567 | samples++;
|
---|
| 568 | var x1 = data[i].Item1;
|
---|
| 569 | var x2 = data[i].Item2;
|
---|
| 570 | var y = data[i].Item3;
|
---|
| 571 |
|
---|
| 572 | if (samples == 1) {
|
---|
| 573 | meanX1Old = x1;
|
---|
| 574 | meanX2Old = x2;
|
---|
| 575 | meanYOld = y;
|
---|
| 576 | } else {
|
---|
| 577 | meanX1 = meanX1Old + (x1 - meanX1Old) / samples;
|
---|
| 578 | meanX2 = meanX2Old + (x2 - meanX2Old) / samples;
|
---|
| 579 | meanY = meanYOld + (y - meanYOld) / samples;
|
---|
| 580 | varX1 = varX1Old + (x1 - meanX1Old) * (x1 - meanX1) / (samples - 1);
|
---|
| 581 | varX2 = varX2Old + (x2 - meanX2Old) * (x2 - meanX2) / (samples - 1);
|
---|
| 582 | varY = varYOld + (y - meanYOld) * (y - meanY) / (samples - 1);
|
---|
| 583 |
|
---|
| 584 | meanX1Old = meanX1;
|
---|
| 585 | meanX2Old = meanX2;
|
---|
| 586 | meanYOld = meanY;
|
---|
| 587 | varX1Old = varX1;
|
---|
| 588 | varX2Old = varX2;
|
---|
| 589 | varYOld = varY;
|
---|
| 590 | }
|
---|
| 591 | }
|
---|
| 592 | double covX1X2 = 0, covX1Y = 0, covX2Y = 0;
|
---|
| 593 | for (var i = 0; i < data.Count; i++) {
|
---|
| 594 | covX1X2 += (data[i].Item1 - meanX1) * (data[i].Item2 - meanX2) / data.Count;
|
---|
| 595 | covX1Y += (data[i].Item1 - meanX1) * (data[i].Item3 - meanY) / data.Count;
|
---|
| 596 | covX2Y += (data[i].Item2 - meanX2) * (data[i].Item3 - meanY) / data.Count;
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | var biasedMean = meanY + ((covX1Y * varX2 - covX2Y * covX1X2) * (startingFitness1 - meanX1) - (covX1Y * covX1X2 - covX2Y * varX1) * (startingFitness2 - meanX2)) / (varX1 * varX2 - covX1X2 * covX1X2);
|
---|
| 600 | var biasedStdev = Math.Sqrt(varY - (covX1Y * covX1Y * varX2 - 2 * covX1Y * covX2Y * covX1X2 + covX2Y * covX2Y * varX1) / (varX1 * varX2 - covX1X2 * covX1X2));
|
---|
| 601 | if (Problem.Maximization) {
|
---|
| 602 | var goal = Population.Min(x => x.Fitness);
|
---|
| 603 | var z = (goal - biasedMean) / biasedStdev;
|
---|
| 604 | return 1.0 - Phi(z); // P(X >= z)
|
---|
| 605 | } else {
|
---|
| 606 | var goal = Population.Max(x => x.Fitness);
|
---|
| 607 | var z = (goal - biasedMean) / biasedStdev;
|
---|
| 608 | return Phi(z); // P(X <= z)
|
---|
| 609 | }
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
---|
| 613 | public bool IsBetter(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b) {
|
---|
| 614 | return IsBetter(a.Fitness, b.Fitness);
|
---|
| 615 | }
|
---|
| 616 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
---|
| 617 | public bool IsBetter(double a, double b) {
|
---|
| 618 | return double.IsNaN(b) && !double.IsNaN(a)
|
---|
| 619 | || Problem.Maximization && a > b
|
---|
| 620 | || !Problem.Maximization && a < b;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | public void AddBreedingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
| 624 | if (IsBetter(a, b))
|
---|
| 625 | BreedingStat.Add(Tuple.Create(a.Fitness, b.Fitness, child.Fitness));
|
---|
| 626 | else BreedingStat.Add(Tuple.Create(b.Fitness, a.Fitness, child.Fitness));
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | public void AddRelinkingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
| 630 | if (IsBetter(a, b))
|
---|
| 631 | RelinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, child.Fitness));
|
---|
| 632 | else RelinkingStat.Add(Tuple.Create(b.Fitness, a.Fitness, child.Fitness));
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | public void AddDelinkingResult(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b, ISingleObjectiveSolutionScope<TSolution> child) {
|
---|
| 636 | if (IsBetter(a, b))
|
---|
| 637 | DelinkingStat.Add(Tuple.Create(a.Fitness, b.Fitness, child.Fitness));
|
---|
| 638 | else DelinkingStat.Add(Tuple.Create(b.Fitness, a.Fitness, child.Fitness));
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | public void AddSamplingResult(ISingleObjectiveSolutionScope<TSolution> sample) {
|
---|
| 642 | SamplingStat.Add(Tuple.Create(Population.Average(x => x.Fitness), sample.Fitness));
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | public void AddHillclimbingResult(ISingleObjectiveSolutionScope<TSolution> input, ISingleObjectiveSolutionScope<TSolution> outcome) {
|
---|
| 646 | HillclimbingStat.Add(Tuple.Create(input.Fitness, outcome.Fitness));
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | public void AddTabuwalkingResult(ISingleObjectiveSolutionScope<TSolution> input, ISingleObjectiveSolutionScope<TSolution> outcome) {
|
---|
| 650 | AdaptivewalkingStat.Add(Tuple.Create(input.Fitness, outcome.Fitness));
|
---|
| 651 | }
|
---|
| 652 |
|
---|
[14420] | 653 | #region IExecutionContext members
|
---|
| 654 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
| 655 | return new ExecutionContext(this, op, Scope);
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
| 659 | return new ExecutionContext(this, op, s);
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
| 663 | return new ExecutionContext(this, op, Scope);
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
| 667 | return new ExecutionContext(this, op, s);
|
---|
| 668 | }
|
---|
| 669 | #endregion
|
---|
[14544] | 670 |
|
---|
| 671 | #region Math Helper
|
---|
| 672 | // normal distribution CDF (left of x) for N(0;1) standard normal distribution
|
---|
| 673 | // from http://www.johndcook.com/blog/csharp_phi/
|
---|
| 674 | // license: "This code is in the public domain. Do whatever you want with it, no strings attached."
|
---|
| 675 | // added: 2016-11-19 21:46 CET
|
---|
| 676 | protected static double Phi(double x) {
|
---|
| 677 | // constants
|
---|
| 678 | double a1 = 0.254829592;
|
---|
| 679 | double a2 = -0.284496736;
|
---|
| 680 | double a3 = 1.421413741;
|
---|
| 681 | double a4 = -1.453152027;
|
---|
| 682 | double a5 = 1.061405429;
|
---|
| 683 | double p = 0.3275911;
|
---|
| 684 |
|
---|
| 685 | // Save the sign of x
|
---|
| 686 | int sign = 1;
|
---|
| 687 | if (x < 0)
|
---|
| 688 | sign = -1;
|
---|
| 689 | x = Math.Abs(x) / Math.Sqrt(2.0);
|
---|
| 690 |
|
---|
| 691 | // A&S formula 7.1.26
|
---|
| 692 | double t = 1.0 / (1.0 + p * x);
|
---|
| 693 | double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x);
|
---|
| 694 |
|
---|
| 695 | return 0.5 * (1.0 + sign * y);
|
---|
| 696 | }
|
---|
| 697 | #endregion
|
---|
[14420] | 698 | }
|
---|
| 699 |
|
---|
| 700 | [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")]
|
---|
| 701 | [StorableClass]
|
---|
[14450] | 702 | public abstract class MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
|
---|
| 703 | ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>
|
---|
| 704 | where TProblem : class, IItem, ISingleObjectiveProblemDefinition
|
---|
[14420] | 705 | where TSolution : class, IItem
|
---|
[14450] | 706 | where TContext : MemPRPopulationContext<TProblem, TSolution, TContext, TSolutionContext>
|
---|
| 707 | where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> {
|
---|
[14420] | 708 |
|
---|
| 709 | private TContext parent;
|
---|
| 710 | public IExecutionContext Parent {
|
---|
| 711 | get { return parent; }
|
---|
| 712 | set { throw new InvalidOperationException("Cannot set the parent of a single-solution context."); }
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 | [Storable]
|
---|
| 716 | private ISingleObjectiveSolutionScope<TSolution> scope;
|
---|
| 717 | public IScope Scope {
|
---|
| 718 | get { return scope; }
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 | IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
|
---|
| 722 | get { return Parameters; }
|
---|
| 723 | }
|
---|
[14450] | 724 |
|
---|
| 725 | public TProblem Problem {
|
---|
| 726 | get { return parent.Problem; }
|
---|
[14420] | 727 | }
|
---|
| 728 |
|
---|
[14450] | 729 | public double BestQuality {
|
---|
| 730 | get { return parent.BestQuality; }
|
---|
| 731 | set { parent.BestQuality = value; }
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | public TSolution BestSolution {
|
---|
| 735 | get { return parent.BestSolution; }
|
---|
| 736 | set { parent.BestSolution = value; }
|
---|
| 737 | }
|
---|
| 738 |
|
---|
[14420] | 739 | public IRandom Random {
|
---|
| 740 | get { return parent.Random; }
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | [Storable]
|
---|
| 744 | private IValueParameter<IntValue> evaluatedSolutions;
|
---|
| 745 | public int EvaluatedSolutions {
|
---|
| 746 | get { return evaluatedSolutions.Value.Value; }
|
---|
| 747 | set { evaluatedSolutions.Value.Value = value; }
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | [Storable]
|
---|
| 751 | private IValueParameter<IntValue> iterations;
|
---|
| 752 | public int Iterations {
|
---|
| 753 | get { return iterations.Value.Value; }
|
---|
| 754 | set { iterations.Value.Value = value; }
|
---|
| 755 | }
|
---|
| 756 |
|
---|
[14450] | 757 | ISingleObjectiveSolutionScope<TSolution> ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>.Solution {
|
---|
[14420] | 758 | get { return scope; }
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | [StorableConstructor]
|
---|
[14450] | 762 | protected MemPRSolutionContext(bool deserializing) : base(deserializing) { }
|
---|
| 763 | protected MemPRSolutionContext(MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> original, Cloner cloner)
|
---|
[14420] | 764 | : base(original, cloner) {
|
---|
| 765 | scope = cloner.Clone(original.scope);
|
---|
| 766 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
| 767 | iterations = cloner.Clone(original.iterations);
|
---|
| 768 | }
|
---|
[14450] | 769 | public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
|
---|
[14420] | 770 | parent = baseContext;
|
---|
| 771 | scope = solution;
|
---|
| 772 |
|
---|
| 773 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
| 774 | Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
| 775 | }
|
---|
| 776 |
|
---|
[14450] | 777 | public void IncrementEvaluatedSolutions(int byEvaluations) {
|
---|
| 778 | if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
|
---|
| 779 | EvaluatedSolutions += byEvaluations;
|
---|
| 780 | }
|
---|
| 781 |
|
---|
[14420] | 782 | #region IExecutionContext members
|
---|
| 783 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
| 784 | return new ExecutionContext(this, op, Scope);
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
| 788 | return new ExecutionContext(this, op, s);
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
| 792 | return new ExecutionContext(this, op, Scope);
|
---|
| 793 | }
|
---|
| 794 |
|
---|
| 795 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
| 796 | return new ExecutionContext(this, op, s);
|
---|
| 797 | }
|
---|
| 798 | #endregion
|
---|
| 799 | }
|
---|
| 800 | }
|
---|