[15045] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[15045] | 4 | * and the BEACON Center for the Study of Evolution in Action.
|
---|
| 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
| 21 | #endregion
|
---|
| 22 |
|
---|
| 23 | using System;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Threading;
|
---|
| 27 | using HeuristicLab.Analysis;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Data;
|
---|
| 31 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 32 | using HeuristicLab.Optimization;
|
---|
| 33 | using HeuristicLab.Parameters;
|
---|
| 34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 35 | using HeuristicLab.Problems.TestFunctions.MultiObjective;
|
---|
| 36 | using HeuristicLab.Random;
|
---|
| 37 |
|
---|
| 38 | namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy {
|
---|
[15226] | 39 | [Item("Multi-Objective CMA Evolution Strategy (MOCMAES)", "A multi objective evolution strategy based on covariance matrix adaptation. Code is based on 'Covariance Matrix Adaptation for Multi - objective Optimization' by Igel, Hansen and Roth")]
|
---|
[15045] | 40 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 210)]
|
---|
| 41 | [StorableClass]
|
---|
| 42 | public class MOCMAEvolutionStrategy : BasicAlgorithm {
|
---|
[15176] | 43 | public override Type ProblemType {
|
---|
[16171] | 44 | get { return typeof(IMultiObjectiveBasicProblem); }
|
---|
[15045] | 45 | }
|
---|
[16171] | 46 | public new IMultiObjectiveBasicProblem Problem {
|
---|
| 47 | get { return (IMultiObjectiveBasicProblem)base.Problem; }
|
---|
[15045] | 48 | set { base.Problem = value; }
|
---|
| 49 | }
|
---|
[15176] | 50 | public override bool SupportsPause {
|
---|
[15045] | 51 | get { return true; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[16171] | 54 | private RealVectorEncoding Encoding {
|
---|
| 55 | get {
|
---|
| 56 | if(Problem == null || !(Problem.Encoding is RealVectorEncoding))
|
---|
| 57 | throw new ArgumentException("Multiobjective CMA-ES is only applicable to problems with RealVectorEncodings");
|
---|
| 58 | return (RealVectorEncoding)Problem.Encoding;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[15176] | 62 | #region Storable fields
|
---|
[15045] | 63 | [Storable]
|
---|
| 64 | private IRandom random = new MersenneTwister();
|
---|
| 65 | [Storable]
|
---|
| 66 | private NormalDistributedRandom gauss;
|
---|
| 67 | [Storable]
|
---|
| 68 | private Individual[] solutions;
|
---|
| 69 | [Storable]
|
---|
| 70 | private double stepSizeLearningRate; //=cp learning rate in [0,1]
|
---|
| 71 | [Storable]
|
---|
| 72 | private double stepSizeDampeningFactor; //d
|
---|
| 73 | [Storable]
|
---|
| 74 | private double targetSuccessProbability;// p^target_succ
|
---|
| 75 | [Storable]
|
---|
| 76 | private double evolutionPathLearningRate;//cc
|
---|
| 77 | [Storable]
|
---|
| 78 | private double covarianceMatrixLearningRate;//ccov
|
---|
| 79 | [Storable]
|
---|
| 80 | private double covarianceMatrixUnlearningRate;
|
---|
| 81 | [Storable]
|
---|
| 82 | private double successThreshold; //ptresh
|
---|
[15089] | 83 |
|
---|
[15045] | 84 | #endregion
|
---|
| 85 |
|
---|
| 86 | #region ParameterNames
|
---|
| 87 | private const string MaximumRuntimeName = "Maximum Runtime";
|
---|
| 88 | private const string SeedName = "Seed";
|
---|
| 89 | private const string SetSeedRandomlyName = "SetSeedRandomly";
|
---|
| 90 | private const string PopulationSizeName = "PopulationSize";
|
---|
| 91 | private const string MaximumGenerationsName = "MaximumGenerations";
|
---|
| 92 | private const string MaximumEvaluatedSolutionsName = "MaximumEvaluatedSolutions";
|
---|
| 93 | private const string InitialSigmaName = "InitialSigma";
|
---|
| 94 | private const string IndicatorName = "Indicator";
|
---|
| 95 |
|
---|
| 96 | private const string EvaluationsResultName = "Evaluations";
|
---|
| 97 | private const string IterationsResultName = "Generations";
|
---|
| 98 | private const string TimetableResultName = "Timetable";
|
---|
| 99 | private const string HypervolumeResultName = "Hypervolume";
|
---|
| 100 | private const string GenerationalDistanceResultName = "Generational Distance";
|
---|
| 101 | private const string InvertedGenerationalDistanceResultName = "Inverted Generational Distance";
|
---|
| 102 | private const string CrowdingResultName = "Crowding";
|
---|
| 103 | private const string SpacingResultName = "Spacing";
|
---|
| 104 | private const string CurrentFrontResultName = "Pareto Front";
|
---|
| 105 | private const string BestHypervolumeResultName = "Best Hypervolume";
|
---|
| 106 | private const string BestKnownHypervolumeResultName = "Best known hypervolume";
|
---|
| 107 | private const string DifferenceToBestKnownHypervolumeResultName = "Absolute Distance to BestKnownHypervolume";
|
---|
| 108 | private const string ScatterPlotResultName = "ScatterPlot";
|
---|
| 109 | #endregion
|
---|
| 110 |
|
---|
| 111 | #region ParameterProperties
|
---|
[15176] | 112 | public IFixedValueParameter<IntValue> MaximumRuntimeParameter {
|
---|
[15045] | 113 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumRuntimeName]; }
|
---|
| 114 | }
|
---|
[15176] | 115 | public IFixedValueParameter<IntValue> SeedParameter {
|
---|
[15045] | 116 | get { return (IFixedValueParameter<IntValue>)Parameters[SeedName]; }
|
---|
| 117 | }
|
---|
[15176] | 118 | public FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
[15045] | 119 | get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
|
---|
| 120 | }
|
---|
[15176] | 121 | public IFixedValueParameter<IntValue> PopulationSizeParameter {
|
---|
[15045] | 122 | get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }
|
---|
| 123 | }
|
---|
[15176] | 124 | public IFixedValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
[15045] | 125 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
|
---|
| 126 | }
|
---|
[15176] | 127 | public IFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
|
---|
[15045] | 128 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsName]; }
|
---|
| 129 | }
|
---|
[15176] | 130 | public IValueParameter<DoubleArray> InitialSigmaParameter {
|
---|
[15045] | 131 | get { return (IValueParameter<DoubleArray>)Parameters[InitialSigmaName]; }
|
---|
| 132 | }
|
---|
[15176] | 133 | public IConstrainedValueParameter<IIndicator> IndicatorParameter {
|
---|
[15045] | 134 | get { return (IConstrainedValueParameter<IIndicator>)Parameters[IndicatorName]; }
|
---|
| 135 | }
|
---|
| 136 | #endregion
|
---|
| 137 |
|
---|
| 138 | #region Properties
|
---|
[15176] | 139 | public int MaximumRuntime {
|
---|
[15045] | 140 | get { return MaximumRuntimeParameter.Value.Value; }
|
---|
| 141 | set { MaximumRuntimeParameter.Value.Value = value; }
|
---|
| 142 | }
|
---|
[15176] | 143 | public int Seed {
|
---|
[15045] | 144 | get { return SeedParameter.Value.Value; }
|
---|
| 145 | set { SeedParameter.Value.Value = value; }
|
---|
| 146 | }
|
---|
[15176] | 147 | public bool SetSeedRandomly {
|
---|
[15045] | 148 | get { return SetSeedRandomlyParameter.Value.Value; }
|
---|
| 149 | set { SetSeedRandomlyParameter.Value.Value = value; }
|
---|
| 150 | }
|
---|
[15176] | 151 | public int PopulationSize {
|
---|
[15045] | 152 | get { return PopulationSizeParameter.Value.Value; }
|
---|
| 153 | set { PopulationSizeParameter.Value.Value = value; }
|
---|
| 154 | }
|
---|
[15176] | 155 | public int MaximumGenerations {
|
---|
[15045] | 156 | get { return MaximumGenerationsParameter.Value.Value; }
|
---|
| 157 | set { MaximumGenerationsParameter.Value.Value = value; }
|
---|
| 158 | }
|
---|
[15176] | 159 | public int MaximumEvaluatedSolutions {
|
---|
[15045] | 160 | get { return MaximumEvaluatedSolutionsParameter.Value.Value; }
|
---|
| 161 | set { MaximumEvaluatedSolutionsParameter.Value.Value = value; }
|
---|
| 162 | }
|
---|
[15176] | 163 | public DoubleArray InitialSigma {
|
---|
[15045] | 164 | get { return InitialSigmaParameter.Value; }
|
---|
| 165 | set { InitialSigmaParameter.Value = value; }
|
---|
| 166 | }
|
---|
[15176] | 167 | public IIndicator Indicator {
|
---|
[15045] | 168 | get { return IndicatorParameter.Value; }
|
---|
| 169 | set { IndicatorParameter.Value = value; }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | public double StepSizeLearningRate { get { return stepSizeLearningRate; } }
|
---|
| 173 | public double StepSizeDampeningFactor { get { return stepSizeDampeningFactor; } }
|
---|
| 174 | public double TargetSuccessProbability { get { return targetSuccessProbability; } }
|
---|
| 175 | public double EvolutionPathLearningRate { get { return evolutionPathLearningRate; } }
|
---|
| 176 | public double CovarianceMatrixLearningRate { get { return covarianceMatrixLearningRate; } }
|
---|
| 177 | public double CovarianceMatrixUnlearningRate { get { return covarianceMatrixUnlearningRate; } }
|
---|
| 178 | public double SuccessThreshold { get { return successThreshold; } }
|
---|
| 179 | #endregion
|
---|
| 180 |
|
---|
| 181 | #region ResultsProperties
|
---|
[15176] | 182 | private int ResultsEvaluations {
|
---|
[15045] | 183 | get { return ((IntValue)Results[EvaluationsResultName].Value).Value; }
|
---|
| 184 | set { ((IntValue)Results[EvaluationsResultName].Value).Value = value; }
|
---|
| 185 | }
|
---|
[15176] | 186 | private int ResultsIterations {
|
---|
[15045] | 187 | get { return ((IntValue)Results[IterationsResultName].Value).Value; }
|
---|
| 188 | set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
|
---|
| 189 | }
|
---|
| 190 | #region Datatable
|
---|
[15176] | 191 | private DataTable ResultsQualities {
|
---|
[15045] | 192 | get { return (DataTable)Results[TimetableResultName].Value; }
|
---|
| 193 | }
|
---|
[15176] | 194 | private DataRow ResultsBestHypervolumeDataLine {
|
---|
[15045] | 195 | get { return ResultsQualities.Rows[BestHypervolumeResultName]; }
|
---|
| 196 | }
|
---|
[15176] | 197 | private DataRow ResultsHypervolumeDataLine {
|
---|
[15045] | 198 | get { return ResultsQualities.Rows[HypervolumeResultName]; }
|
---|
| 199 | }
|
---|
[15176] | 200 | private DataRow ResultsGenerationalDistanceDataLine {
|
---|
[15045] | 201 | get { return ResultsQualities.Rows[GenerationalDistanceResultName]; }
|
---|
| 202 | }
|
---|
[15176] | 203 | private DataRow ResultsInvertedGenerationalDistanceDataLine {
|
---|
[15045] | 204 | get { return ResultsQualities.Rows[InvertedGenerationalDistanceResultName]; }
|
---|
| 205 | }
|
---|
[15176] | 206 | private DataRow ResultsCrowdingDataLine {
|
---|
[15045] | 207 | get { return ResultsQualities.Rows[CrowdingResultName]; }
|
---|
| 208 | }
|
---|
[15176] | 209 | private DataRow ResultsSpacingDataLine {
|
---|
[15045] | 210 | get { return ResultsQualities.Rows[SpacingResultName]; }
|
---|
| 211 | }
|
---|
[15176] | 212 | private DataRow ResultsHypervolumeDifferenceDataLine {
|
---|
[15045] | 213 | get { return ResultsQualities.Rows[DifferenceToBestKnownHypervolumeResultName]; }
|
---|
| 214 | }
|
---|
| 215 | #endregion
|
---|
| 216 | //QualityIndicators
|
---|
[15176] | 217 | private double ResultsHypervolume {
|
---|
[15045] | 218 | get { return ((DoubleValue)Results[HypervolumeResultName].Value).Value; }
|
---|
| 219 | set { ((DoubleValue)Results[HypervolumeResultName].Value).Value = value; }
|
---|
| 220 | }
|
---|
[15176] | 221 | private double ResultsGenerationalDistance {
|
---|
[15045] | 222 | get { return ((DoubleValue)Results[GenerationalDistanceResultName].Value).Value; }
|
---|
| 223 | set { ((DoubleValue)Results[GenerationalDistanceResultName].Value).Value = value; }
|
---|
| 224 | }
|
---|
[15176] | 225 | private double ResultsInvertedGenerationalDistance {
|
---|
[15045] | 226 | get { return ((DoubleValue)Results[InvertedGenerationalDistanceResultName].Value).Value; }
|
---|
| 227 | set { ((DoubleValue)Results[InvertedGenerationalDistanceResultName].Value).Value = value; }
|
---|
| 228 | }
|
---|
[15176] | 229 | private double ResultsCrowding {
|
---|
[15045] | 230 | get { return ((DoubleValue)Results[CrowdingResultName].Value).Value; }
|
---|
| 231 | set { ((DoubleValue)Results[CrowdingResultName].Value).Value = value; }
|
---|
| 232 | }
|
---|
[15176] | 233 | private double ResultsSpacing {
|
---|
[15045] | 234 | get { return ((DoubleValue)Results[SpacingResultName].Value).Value; }
|
---|
| 235 | set { ((DoubleValue)Results[SpacingResultName].Value).Value = value; }
|
---|
| 236 | }
|
---|
[15176] | 237 | private double ResultsBestHypervolume {
|
---|
[15045] | 238 | get { return ((DoubleValue)Results[BestHypervolumeResultName].Value).Value; }
|
---|
| 239 | set { ((DoubleValue)Results[BestHypervolumeResultName].Value).Value = value; }
|
---|
| 240 | }
|
---|
[15176] | 241 | private double ResultsBestKnownHypervolume {
|
---|
[15045] | 242 | get { return ((DoubleValue)Results[BestKnownHypervolumeResultName].Value).Value; }
|
---|
| 243 | set { ((DoubleValue)Results[BestKnownHypervolumeResultName].Value).Value = value; }
|
---|
| 244 | }
|
---|
[15176] | 245 | private double ResultsDifferenceBestKnownHypervolume {
|
---|
[15045] | 246 | get { return ((DoubleValue)Results[DifferenceToBestKnownHypervolumeResultName].Value).Value; }
|
---|
| 247 | set { ((DoubleValue)Results[DifferenceToBestKnownHypervolumeResultName].Value).Value = value; }
|
---|
| 248 |
|
---|
| 249 | }
|
---|
| 250 | //Solutions
|
---|
[15176] | 251 | private DoubleMatrix ResultsSolutions {
|
---|
[15045] | 252 | get { return (DoubleMatrix)Results[CurrentFrontResultName].Value; }
|
---|
| 253 | set { Results[CurrentFrontResultName].Value = value; }
|
---|
| 254 | }
|
---|
[15203] | 255 | private ParetoFrontScatterPlot ResultsScatterPlot {
|
---|
| 256 | get { return (ParetoFrontScatterPlot)Results[ScatterPlotResultName].Value; }
|
---|
[15045] | 257 | set { Results[ScatterPlotResultName].Value = value; }
|
---|
| 258 | }
|
---|
| 259 | #endregion
|
---|
| 260 |
|
---|
| 261 | #region Constructors
|
---|
| 262 | public MOCMAEvolutionStrategy() {
|
---|
| 263 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumRuntimeName, "The maximum runtime in seconds after which the algorithm stops. Use -1 to specify no limit for the runtime", new IntValue(3600)));
|
---|
| 264 | Parameters.Add(new FixedValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 265 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 266 | Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "λ (lambda) - the size of the offspring population.", new IntValue(20)));
|
---|
| 267 | Parameters.Add(new ValueParameter<DoubleArray>(InitialSigmaName, "The initial sigma can be a single value or a value for each dimension. All values need to be > 0.", new DoubleArray(new[] { 0.5 })));
|
---|
| 268 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
| 269 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluatedSolutionsName, "The maximum number of evaluated solutions that should be computed.", new IntValue(int.MaxValue)));
|
---|
| 270 | var set = new ItemSet<IIndicator> { new HypervolumeIndicator(), new CrowdingIndicator(), new MinimalDistanceIndicator() };
|
---|
| 271 | Parameters.Add(new ConstrainedValueParameter<IIndicator>(IndicatorName, "The selection mechanism on non-dominated solutions", set, set.First()));
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | [StorableConstructor]
|
---|
| 275 | protected MOCMAEvolutionStrategy(bool deserializing) : base(deserializing) { }
|
---|
| 276 |
|
---|
| 277 | protected MOCMAEvolutionStrategy(MOCMAEvolutionStrategy original, Cloner cloner) : base(original, cloner) {
|
---|
| 278 | random = cloner.Clone(original.random);
|
---|
| 279 | gauss = cloner.Clone(original.gauss);
|
---|
[15089] | 280 | solutions = original.solutions != null ? original.solutions.Select(cloner.Clone).ToArray() : null;
|
---|
[15045] | 281 | stepSizeLearningRate = original.stepSizeLearningRate;
|
---|
| 282 | stepSizeDampeningFactor = original.stepSizeDampeningFactor;
|
---|
| 283 | targetSuccessProbability = original.targetSuccessProbability;
|
---|
| 284 | evolutionPathLearningRate = original.evolutionPathLearningRate;
|
---|
| 285 | covarianceMatrixLearningRate = original.covarianceMatrixLearningRate;
|
---|
| 286 | covarianceMatrixUnlearningRate = original.covarianceMatrixUnlearningRate;
|
---|
| 287 | successThreshold = original.successThreshold;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | public override IDeepCloneable Clone(Cloner cloner) { return new MOCMAEvolutionStrategy(this, cloner); }
|
---|
| 291 | #endregion
|
---|
| 292 |
|
---|
| 293 | #region Initialization
|
---|
| 294 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
[16071] | 295 | if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
|
---|
[15045] | 296 | random.Reset(Seed);
|
---|
| 297 | gauss = new NormalDistributedRandom(random, 0, 1);
|
---|
| 298 |
|
---|
| 299 | InitResults();
|
---|
| 300 | InitStrategy();
|
---|
| 301 | InitSolutions();
|
---|
| 302 | Analyze();
|
---|
| 303 |
|
---|
| 304 | ResultsIterations = 1;
|
---|
| 305 | }
|
---|
| 306 | private Individual InitializeIndividual(RealVector x) {
|
---|
| 307 | var zeros = new RealVector(x.Length);
|
---|
| 308 | var c = new double[x.Length, x.Length];
|
---|
| 309 | var sigma = InitialSigma.Max();
|
---|
| 310 | for (var i = 0; i < x.Length; i++) {
|
---|
| 311 | var d = InitialSigma[i % InitialSigma.Length] / sigma;
|
---|
| 312 | c[i, i] = d * d;
|
---|
| 313 | }
|
---|
| 314 | return new Individual(x, targetSuccessProbability, sigma, zeros, c, this);
|
---|
| 315 | }
|
---|
| 316 | private void InitSolutions() {
|
---|
| 317 | solutions = new Individual[PopulationSize];
|
---|
| 318 | for (var i = 0; i < PopulationSize; i++) {
|
---|
[16171] | 319 | var x = new RealVector(Encoding.Length); // Uniform distibution in all dimensions assumed.
|
---|
| 320 | var bounds = Encoding.Bounds;
|
---|
| 321 | for (var j = 0; j < Encoding.Length; j++) {
|
---|
[15045] | 322 | var dim = j % bounds.Rows;
|
---|
| 323 | x[j] = random.NextDouble() * (bounds[dim, 1] - bounds[dim, 0]) + bounds[dim, 0];
|
---|
| 324 | }
|
---|
| 325 | solutions[i] = InitializeIndividual(x);
|
---|
| 326 | PenalizeEvaluate(solutions[i]);
|
---|
| 327 | }
|
---|
[15205] | 328 | ResultsEvaluations += solutions.Length;
|
---|
[15045] | 329 | }
|
---|
| 330 | private void InitStrategy() {
|
---|
| 331 | const int lambda = 1;
|
---|
[16171] | 332 | double n = Encoding.Length;
|
---|
[15045] | 333 | targetSuccessProbability = 1.0 / (5.0 + Math.Sqrt(lambda) / 2.0);
|
---|
| 334 | stepSizeDampeningFactor = 1.0 + n / (2.0 * lambda);
|
---|
| 335 | stepSizeLearningRate = targetSuccessProbability * lambda / (2.0 + targetSuccessProbability * lambda);
|
---|
| 336 | evolutionPathLearningRate = 2.0 / (n + 2.0);
|
---|
| 337 | covarianceMatrixLearningRate = 2.0 / (n * n + 6.0);
|
---|
| 338 | covarianceMatrixUnlearningRate = 0.4 / (Math.Pow(n, 1.6) + 1);
|
---|
| 339 | successThreshold = 0.44;
|
---|
| 340 | }
|
---|
| 341 | private void InitResults() {
|
---|
| 342 | Results.Add(new Result(IterationsResultName, "The number of gererations evaluated", new IntValue(0)));
|
---|
| 343 | Results.Add(new Result(EvaluationsResultName, "The number of function evaltions performed", new IntValue(0)));
|
---|
| 344 | Results.Add(new Result(HypervolumeResultName, "The hypervolume of the current front considering the Referencepoint defined in the Problem", new DoubleValue(0.0)));
|
---|
| 345 | Results.Add(new Result(BestHypervolumeResultName, "The best hypervolume of the current run considering the Referencepoint defined in the Problem", new DoubleValue(0.0)));
|
---|
| 346 | Results.Add(new Result(BestKnownHypervolumeResultName, "The best knwon hypervolume considering the Referencepoint defined in the Problem", new DoubleValue(double.NaN)));
|
---|
| 347 | Results.Add(new Result(DifferenceToBestKnownHypervolumeResultName, "The difference between the current and the best known hypervolume", new DoubleValue(double.NaN)));
|
---|
| 348 | Results.Add(new Result(GenerationalDistanceResultName, "The generational distance to an optimal pareto front defined in the Problem", new DoubleValue(double.NaN)));
|
---|
| 349 | Results.Add(new Result(InvertedGenerationalDistanceResultName, "The inverted generational distance to an optimal pareto front defined in the Problem", new DoubleValue(double.NaN)));
|
---|
| 350 | Results.Add(new Result(CrowdingResultName, "The average crowding value for the current front (excluding infinities)", new DoubleValue(0.0)));
|
---|
| 351 | Results.Add(new Result(SpacingResultName, "The spacing for the current front (excluding infinities)", new DoubleValue(0.0)));
|
---|
| 352 |
|
---|
| 353 | var table = new DataTable("QualityIndicators");
|
---|
| 354 | table.Rows.Add(new DataRow(BestHypervolumeResultName));
|
---|
| 355 | table.Rows.Add(new DataRow(HypervolumeResultName));
|
---|
| 356 | table.Rows.Add(new DataRow(CrowdingResultName));
|
---|
| 357 | table.Rows.Add(new DataRow(GenerationalDistanceResultName));
|
---|
| 358 | table.Rows.Add(new DataRow(InvertedGenerationalDistanceResultName));
|
---|
| 359 | table.Rows.Add(new DataRow(DifferenceToBestKnownHypervolumeResultName));
|
---|
| 360 | table.Rows.Add(new DataRow(SpacingResultName));
|
---|
| 361 | Results.Add(new Result(TimetableResultName, "Different quality meassures in a timeseries", table));
|
---|
| 362 | Results.Add(new Result(CurrentFrontResultName, "The current front", new DoubleMatrix()));
|
---|
[15203] | 363 | Results.Add(new Result(ScatterPlotResultName, "A scatterplot displaying the evaluated solutions and (if available) the analytically optimal front", new ParetoFrontScatterPlot()));
|
---|
[15045] | 364 |
|
---|
| 365 | var problem = Problem as MultiObjectiveTestFunctionProblem;
|
---|
| 366 | if (problem == null) return;
|
---|
| 367 | if (problem.BestKnownFront != null) {
|
---|
[16171] | 368 | ResultsBestKnownHypervolume = HypervolumeCalculator.CalculateHypervolume(problem.BestKnownFront.ToJaggedArray(), problem.TestFunction.ReferencePoint(problem.Objectives), Problem.Maximization);
|
---|
[15045] | 369 | ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume;
|
---|
| 370 | }
|
---|
[15203] | 371 | ResultsScatterPlot = new ParetoFrontScatterPlot(new double[0][], new double[0][], problem.BestKnownFront.ToJaggedArray(), problem.Objectives, problem.ProblemSize);
|
---|
[15045] | 372 | }
|
---|
| 373 | #endregion
|
---|
| 374 |
|
---|
| 375 | #region Mainloop
|
---|
| 376 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[15335] | 377 | while (ResultsIterations < MaximumGenerations && ResultsEvaluations < MaximumEvaluatedSolutions) {
|
---|
[15045] | 378 | try {
|
---|
| 379 | Iterate();
|
---|
| 380 | ResultsIterations++;
|
---|
| 381 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[15176] | 382 | } finally {
|
---|
[15045] | 383 | Analyze();
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | private void Iterate() {
|
---|
| 388 | var offspring = solutions.Select(i => {
|
---|
| 389 | var o = new Individual(i);
|
---|
| 390 | o.Mutate(gauss);
|
---|
| 391 | PenalizeEvaluate(o);
|
---|
| 392 | return o;
|
---|
| 393 | });
|
---|
[15205] | 394 | ResultsEvaluations += solutions.Length;
|
---|
[15045] | 395 | var parents = solutions.Concat(offspring).ToArray();
|
---|
| 396 | SelectParents(parents, solutions.Length);
|
---|
| 397 | UpdatePopulation(parents);
|
---|
| 398 | }
|
---|
| 399 | protected override void OnExecutionTimeChanged() {
|
---|
| 400 | base.OnExecutionTimeChanged();
|
---|
| 401 | if (CancellationTokenSource == null) return;
|
---|
| 402 | if (MaximumRuntime == -1) return;
|
---|
| 403 | if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();
|
---|
| 404 | }
|
---|
| 405 | #endregion
|
---|
| 406 |
|
---|
| 407 | #region Evaluation
|
---|
| 408 | private void PenalizeEvaluate(Individual individual) {
|
---|
| 409 | if (IsFeasable(individual.Mean)) {
|
---|
| 410 | individual.Fitness = Evaluate(individual.Mean);
|
---|
| 411 | individual.PenalizedFitness = individual.Fitness;
|
---|
| 412 | } else {
|
---|
| 413 | var t = ClosestFeasible(individual.Mean);
|
---|
| 414 | individual.Fitness = Evaluate(t);
|
---|
| 415 | individual.PenalizedFitness = Penalize(individual.Mean, t, individual.Fitness);
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 | private double[] Evaluate(RealVector x) {
|
---|
| 419 | var res = Problem.Evaluate(new SingleEncodingIndividual(Problem.Encoding, new Scope { Variables = { new Variable(Problem.Encoding.Name, x) } }), random);
|
---|
| 420 | return res;
|
---|
| 421 | }
|
---|
| 422 | private double[] Penalize(RealVector x, RealVector t, IEnumerable<double> fitness) {
|
---|
| 423 | var penalty = x.Zip(t, (a, b) => (a - b) * (a - b)).Sum() * 1E-6;
|
---|
| 424 | return fitness.Select((v, i) => Problem.Maximization[i] ? v - penalty : v + penalty).ToArray();
|
---|
| 425 | }
|
---|
| 426 | private RealVector ClosestFeasible(RealVector x) {
|
---|
[16171] | 427 | var bounds = Encoding.Bounds;
|
---|
[15045] | 428 | var r = new RealVector(x.Length);
|
---|
| 429 | for (var i = 0; i < x.Length; i++) {
|
---|
| 430 | var dim = i % bounds.Rows;
|
---|
| 431 | r[i] = Math.Min(Math.Max(bounds[dim, 0], x[i]), bounds[dim, 1]);
|
---|
| 432 | }
|
---|
| 433 | return r;
|
---|
| 434 | }
|
---|
| 435 | private bool IsFeasable(RealVector offspring) {
|
---|
[16171] | 436 | var bounds = Encoding.Bounds;
|
---|
[15045] | 437 | for (var i = 0; i < offspring.Length; i++) {
|
---|
| 438 | var dim = i % bounds.Rows;
|
---|
| 439 | if (bounds[dim, 0] > offspring[i] || offspring[i] > bounds[dim, 1]) return false;
|
---|
| 440 | }
|
---|
| 441 | return true;
|
---|
| 442 | }
|
---|
| 443 | #endregion
|
---|
| 444 |
|
---|
| 445 | private void SelectParents(IReadOnlyList<Individual> parents, int length) {
|
---|
| 446 | //perform a nondominated sort to assign the rank to every element
|
---|
[15089] | 447 | int[] ranks;
|
---|
[16171] | 448 | var fronts = DominationCalculator.CalculateAllParetoFronts(parents.ToArray(), parents.Select(i => i.PenalizedFitness).ToArray(), Problem.Maximization, out ranks);
|
---|
[15045] | 449 |
|
---|
| 450 | //deselect the highest rank fronts until we would end up with less or equal mu elements
|
---|
| 451 | var rank = fronts.Count - 1;
|
---|
| 452 | var popSize = parents.Count;
|
---|
| 453 | while (popSize - fronts[rank].Count >= length) {
|
---|
| 454 | var front = fronts[rank];
|
---|
[15089] | 455 | foreach (var i in front) i.Item1.Selected = false;
|
---|
[15045] | 456 | popSize -= front.Count;
|
---|
| 457 | rank--;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | //now use the indicator to deselect the approximatingly worst elements of the last selected front
|
---|
[15089] | 461 | var front1 = fronts[rank].OrderBy(x => x.Item1.PenalizedFitness[0]).ToList();
|
---|
[15045] | 462 | for (; popSize > length; popSize--) {
|
---|
[15089] | 463 | var lc = Indicator.LeastContributer(front1.Select(i => i.Item1).ToArray(), Problem);
|
---|
| 464 | front1[lc].Item1.Selected = false;
|
---|
[15045] | 465 | front1.Swap(lc, front1.Count - 1);
|
---|
| 466 | front1.RemoveAt(front1.Count - 1);
|
---|
| 467 | }
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | private void UpdatePopulation(IReadOnlyList<Individual> parents) {
|
---|
| 471 | foreach (var p in parents.Skip(solutions.Length).Where(i => i.Selected))
|
---|
| 472 | p.UpdateAsOffspring();
|
---|
| 473 | for (var i = 0; i < solutions.Length; i++)
|
---|
| 474 | if (parents[i].Selected)
|
---|
| 475 | parents[i].UpdateAsParent(parents[i + solutions.Length].Selected);
|
---|
| 476 | solutions = parents.Where(p => p.Selected).ToArray();
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | private void Analyze() {
|
---|
[16171] | 480 | var qualities = solutions.Select(x => x.Fitness).ToArray();
|
---|
| 481 |
|
---|
| 482 | //to do check for side effects
|
---|
| 483 | ResultsScatterPlot = new ParetoFrontScatterPlot(qualities, solutions.Select(x => x.Mean.ToArray()).ToArray(), ResultsScatterPlot.ParetoFront, ResultsScatterPlot.Objectives, ResultsScatterPlot.ProblemSize);
|
---|
[15045] | 484 | ResultsSolutions = solutions.Select(x => x.Mean.ToArray()).ToMatrix();
|
---|
| 485 |
|
---|
| 486 | var problem = Problem as MultiObjectiveTestFunctionProblem;
|
---|
| 487 | if (problem == null) return;
|
---|
| 488 |
|
---|
[16171] | 489 |
|
---|
| 490 | if (qualities.Length == 0) return;
|
---|
| 491 | ResultsCrowding = CrowdingCalculator.CalculateCrowding(qualities);
|
---|
| 492 | ResultsSpacing = SpacingCalculator.CalculateSpacing(qualities);
|
---|
| 493 | ResultsGenerationalDistance = problem.BestKnownFront != null ? GenerationalDistanceCalculator.CalculateGenerationalDistance(qualities, problem.BestKnownFront.ToJaggedArray(), 1) : double.NaN;
|
---|
| 494 | ResultsInvertedGenerationalDistance = problem.BestKnownFront != null ? GenerationalDistanceCalculator.CalculateInverseGenerationalDistance(qualities, problem.BestKnownFront.ToJaggedArray(), 1) : double.NaN;
|
---|
| 495 | ResultsHypervolume = HypervolumeCalculator.CalculateHypervolume(qualities, problem.ReferencePoint.CloneAsArray(), Problem.Maximization);
|
---|
[15045] | 496 | ResultsBestHypervolume = Math.Max(ResultsHypervolume, ResultsBestHypervolume);
|
---|
| 497 | ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume - ResultsBestHypervolume;
|
---|
| 498 |
|
---|
| 499 | ResultsBestHypervolumeDataLine.Values.Add(ResultsBestHypervolume);
|
---|
| 500 | ResultsHypervolumeDataLine.Values.Add(ResultsHypervolume);
|
---|
| 501 | ResultsCrowdingDataLine.Values.Add(ResultsCrowding);
|
---|
| 502 | ResultsGenerationalDistanceDataLine.Values.Add(ResultsGenerationalDistance);
|
---|
| 503 | ResultsInvertedGenerationalDistanceDataLine.Values.Add(ResultsInvertedGenerationalDistance);
|
---|
| 504 | ResultsSpacingDataLine.Values.Add(ResultsSpacing);
|
---|
| 505 | ResultsHypervolumeDifferenceDataLine.Values.Add(ResultsDifferenceBestKnownHypervolume);
|
---|
| 506 |
|
---|
| 507 | Problem.Analyze(
|
---|
| 508 | solutions.Select(x => (Optimization.Individual)new SingleEncodingIndividual(Problem.Encoding, new Scope { Variables = { new Variable(Problem.Encoding.Name, x.Mean) } })).ToArray(),
|
---|
| 509 | solutions.Select(x => x.Fitness).ToArray(),
|
---|
| 510 | Results,
|
---|
| 511 | random);
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 | }
|
---|