Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4/MOEADAlgorithm.cs @ 17958

Last change on this file since 17958 was 16657, checked in by bburlacu, 6 years ago

#2987: Improve performance by caching parameter values. Fix plugin dependencies Plugin.cs.frame

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22using HEAL.Attic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Random;
27using System.Linq;
28using CancellationToken = System.Threading.CancellationToken;
29
30namespace HeuristicLab.Algorithms.MOEAD {
31  [Item("Multi-objective Evolutionary Algorithm based on Decomposition (MOEA/D)", "MOEA/D implementation adapted from jMetal.")]
32  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 125)]
33  [StorableType("FE39AD23-B3BF-4368-BB79-FE5BF4C36272")]
34  public class MOEADAlgorithm : MOEADAlgorithmBase {
35    public MOEADAlgorithm() { }
36
37    protected MOEADAlgorithm(MOEADAlgorithm original, Cloner cloner) : base(original, cloner) { }
38
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new MOEADAlgorithm(this, cloner);
41    }
42
43    [StorableConstructor]
44    protected MOEADAlgorithm(StorableConstructorFlag deserializing) : base(deserializing) { }
45
46    protected override void Run(CancellationToken cancellationToken) {
47      if (previousExecutionState != ExecutionState.Paused) {
48        InitializeAlgorithm(cancellationToken);
49      }
50
51      var populationSize = PopulationSize.Value;
52      bool[] maximization = ((BoolArray)Problem.MaximizationParameter.ActualValue).CloneAsArray();
53      var maximumEvaluatedSolutions = MaximumEvaluatedSolutions.Value;
54      var crossover = Crossover;
55      var crossoverProbability = CrossoverProbability.Value;
56      var mutator = Mutator;
57      var mutationProbability = MutationProbability.Value;
58      var evaluator = Problem.Evaluator;
59      var analyzer = Analyzer;
60      var neighbourhoodSelectionProbability = NeighbourhoodSelectionProbability;
61      var rand = RandomParameter.Value;
62      var normalizeObjectives = NormalizeObjectives;
63      var maximumNumberOfReplacedSolutions = MaximumNumberOfReplacedSolutions;
64
65      // cancellation token for the inner operations which should not be immediately cancelled
66      var innerToken = new CancellationToken();
67
68      while (evaluatedSolutions < maximumEvaluatedSolutions && !cancellationToken.IsCancellationRequested) {
69        foreach (var subProblemId in Enumerable.Range(0, populationSize).Shuffle(rand)) {
70          var neighbourType = ChooseNeighborType(rand, neighbourhoodSelectionProbability);
71          var mates = MatingSelection(rand, subProblemId, 2, neighbourType); // select parents
72          var s1 = (IScope)population[mates[0]].Individual.Clone();
73          var s2 = (IScope)population[mates[1]].Individual.Clone();
74          s1.Parent = s2.Parent = globalScope;
75
76          IScope childScope = null;
77          // crossover
78          if (rand.NextDouble() < crossoverProbability) {
79            childScope = new Scope($"{mates[0]}+{mates[1]}") { Parent = executionContext.Scope };
80            childScope.SubScopes.Add(s1);
81            childScope.SubScopes.Add(s2);
82            var op = executionContext.CreateChildOperation(crossover, childScope);
83            ExecuteOperation(executionContext, innerToken, op);
84            childScope.SubScopes.Clear();
85          }
86
87          // mutation
88          if (rand.NextDouble() < mutationProbability) {
89            childScope = childScope ?? s1;
90            var op = executionContext.CreateChildOperation(mutator, childScope);
91            ExecuteOperation(executionContext, innerToken, op);
92          }
93
94          // evaluation
95          if (childScope != null) {
96            var op = executionContext.CreateChildOperation(evaluator, childScope);
97            ExecuteOperation(executionContext, innerToken, op);
98            var qualities = (DoubleArray)childScope.Variables["Qualities"].Value;
99            var childSolution = new MOEADSolution(childScope, maximization.Length, 0);
100            // set child qualities
101            for (int j = 0; j < maximization.Length; ++j) {
102              childSolution.Qualities[j] = maximization[j] ? 1 - qualities[j] : qualities[j];
103            }
104            IdealPoint.UpdateIdeal(childSolution.Qualities);
105            NadirPoint.UpdateNadir(childSolution.Qualities);
106            // update neighbourhood will insert the child into the population
107            UpdateNeighbourHood(rand, childSolution, subProblemId, neighbourType, maximumNumberOfReplacedSolutions, normalizeObjectives);
108
109            ++evaluatedSolutions;
110          } else {
111            // no crossover or mutation were applied, a child was not produced, do nothing
112          }
113
114          if (evaluatedSolutions >= maximumEvaluatedSolutions) {
115            break;
116          }
117        }
118        // run analyzer
119        var analyze = executionContext.CreateChildOperation(analyzer, globalScope);
120        ExecuteOperation(executionContext, innerToken, analyze);
121
122        UpdateParetoFronts();
123
124        Results.AddOrUpdateResult("IdealPoint", new DoubleArray(IdealPoint));
125        Results.AddOrUpdateResult("NadirPoint", new DoubleArray(NadirPoint));
126        Results.AddOrUpdateResult("Evaluated Solutions", new IntValue(evaluatedSolutions));
127
128        globalScope.SubScopes.Replace(population.Select(x => (IScope)x.Individual));
129      }
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.