Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMBaseAlgorithm.cs @ 16832

Last change on this file since 16832 was 16760, checked in by msemenki, 6 years ago

#2988: ADD:

  1. new type of mutation that respect node type and do not change trigonometric node to "add" node;
  2. variation of previous mutation type that do not change type of terminal node;
  3. some interface adjusting for comfort work with algorithm;
  4. second variant of map, that is not "island" map but provide "net" map.
File size: 20.5 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.Algorithms.DataAnalysis;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Problems.DataAnalysis;
32using HeuristicLab.Problems.DataAnalysis.Symbolic;
33using HeuristicLab.Random;
34using System;
35using System.Collections.Generic;
36using System.Linq;
37using CancellationToken = System.Threading.CancellationToken;
38using ExecutionContext = HeuristicLab.Core.ExecutionContext;
39
40namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
41  [Item("MOEADAlgorithmBase", "Base class for all MOEA/D algorithm variants.")]
42  [StorableType("A56A396B-965A-4ADE-8A2B-AE3A45F9C239")]
43  public abstract class EvolvmentModelsOfModelsAlgorithmBase : FixedDataAnalysisAlgorithm<ISymbolicDataAnalysisSingleObjectiveProblem> {
44    #region data members
45    [Storable]
46    protected IList<IEMMSolution> Solutions;
47    [Storable]
48    protected List<IEMMSolution> Population;
49    [Storable]
50    protected int EvaluatedSolutions;
51    [Storable]
52    protected ExecutionContext executionContext;
53    [Storable]
54    protected IScope globalScope;
55    [Storable]
56    protected ExecutionState previousExecutionState;
57    #endregion
58
59    #region parameters
60    private const string SeedParameterName = "Seed";
61    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
62    private const string PopulationSizeParameterName = "PopulationSize";
63    private const string SelectorParameterName = "Selector";
64    private const string GroupSizeParameterName = "GroupSize";
65    private const string CrossoverProbabilityParameterName = "CrossoverProbability";
66    private const string CrossoverParameterName = "Crossover";
67    private const string MutationProbabilityParameterName = "MutationProbability";
68    private const string MutatorParameterName = "Mutator";
69    private const string MaximumEvaluatedSolutionsParameterName = "MaximumEvaluatedSolutions";
70    private const string RandomParameterName = "Random";
71    private const string AnalyzerParameterName = "Analyzer";
72    private const string InputFileParameterName = "InputFile";
73    private const string ClusterNumbersParameterName = "ClusterNumbers";
74    private const string AlgorithmImplementationTypeParameterName = "AlgorithmImplemetationType";
75    private const string MapCreationTypeParameterName = "MapCreationType";
76    private const string NegbourTypeParameterName = "NegbourType";
77    private const string NegbourNumberParameterName = "NegbourNumber";
78    public IValueParameter<MultiAnalyzer> AnalyzerParameter {
79      get { return (ValueParameter<MultiAnalyzer>)Parameters[AnalyzerParameterName]; }
80    }
81    public IFixedValueParameter<IntValue> SeedParameter {
82      get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
83    }
84    public IValueParameter<IntValue> ClusterNumbersParameter {
85      get { return (IValueParameter<IntValue>)Parameters[ClusterNumbersParameterName]; }
86    }
87    public IFixedValueParameter<StringValue> AlgorithmImplementationTypeParameter {
88      get { return (IFixedValueParameter<StringValue>)Parameters[AlgorithmImplementationTypeParameterName]; }
89    }
90    public IFixedValueParameter<StringValue> MapCreationTypeParameter {
91      get { return (IFixedValueParameter<StringValue>)Parameters[MapCreationTypeParameterName]; }
92    }
93    public IFixedValueParameter<StringValue> NegbourTypeParameter {
94      get { return (IFixedValueParameter<StringValue>)Parameters[NegbourTypeParameterName]; }
95    }
96    public IFixedValueParameter<IntValue> NegbourNumberParameter {
97      get { return (IFixedValueParameter<IntValue>)Parameters[NegbourNumberParameterName]; }
98    }
99    public IFixedValueParameter<StringValue> InputFileParameter {
100      get { return (IFixedValueParameter<StringValue>)Parameters[InputFileParameterName]; }
101    }
102    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
103      get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
104    }
105    private IValueParameter<IntValue> PopulationSizeParameter {
106      get { return (IValueParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
107    }
108    public IValueParameter<PercentValue> CrossoverProbabilityParameter {
109      get { return (IValueParameter<PercentValue>)Parameters[CrossoverProbabilityParameterName]; }
110    }
111    public IValueParameter<IntValue> GroupSizeParameter {
112      get { return (IValueParameter<IntValue>)Parameters[GroupSizeParameterName]; }
113    }
114    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
115      get { return (IConstrainedValueParameter<ICrossover>)Parameters[CrossoverParameterName]; }
116    }
117    public IConstrainedValueParameter<ISelector> SelectorParameter {
118      get { return (IConstrainedValueParameter<ISelector>)Parameters[SelectorParameterName]; }
119    }
120    public IValueParameter<PercentValue> MutationProbabilityParameter {
121      get { return (IValueParameter<PercentValue>)Parameters[MutationProbabilityParameterName]; }
122    }
123    public IConstrainedValueParameter<IManipulator> MutatorParameter {
124      get { return (IConstrainedValueParameter<IManipulator>)Parameters[MutatorParameterName]; }
125    }
126    public IValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
127      get { return (IValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsParameterName]; }
128    }
129    public IValueParameter<IRandom> RandomParameter {
130      get { return (IValueParameter<IRandom>)Parameters[RandomParameterName]; }
131    }
132    #endregion
133
134    #region parameter properties
135    public ValueParameter<IntValue> ElitesParameter {
136      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
137    }
138    public int Seed {
139      get { return SeedParameter.Value.Value; }
140      set { SeedParameter.Value.Value = value; }
141    }
142    public IntValue ClusterNumbers {
143      get { return ClusterNumbersParameter.Value; }
144      set { ClusterNumbersParameter.Value = value; }
145    }
146    public StringValue AlgorithmImplemetationType {
147      get { return AlgorithmImplementationTypeParameter.Value; }
148      set { AlgorithmImplementationTypeParameter.Value.Value = value.Value; }
149    }
150    public StringValue MapCreationType {
151      get { return MapCreationTypeParameter.Value; }
152      set { MapCreationTypeParameter.Value.Value = value.Value; }
153    }
154    public StringValue NegbourType {
155      get { return NegbourTypeParameter.Value; }
156      set { NegbourTypeParameter.Value.Value = value.Value; }
157    }
158    public IntValue NegbourNumber {
159      get { return NegbourNumberParameter.Value; }
160      set { NegbourNumberParameter.Value.Value = value.Value; }
161    }
162    public StringValue InputFile {
163      get { return InputFileParameter.Value; }
164      set { InputFileParameter.Value.Value = value.Value; }
165    }
166    public bool SetSeedRandomly {
167      get { return SetSeedRandomlyParameter.Value.Value; }
168      set { SetSeedRandomlyParameter.Value.Value = value; }
169    }
170    public IntValue PopulationSize {
171      get { return PopulationSizeParameter.Value; }
172      set { PopulationSizeParameter.Value = value; }
173    }
174    public PercentValue CrossoverProbability {
175      get { return CrossoverProbabilityParameter.Value; }
176      set { CrossoverProbabilityParameter.Value = value; }
177    }
178    public IntValue GroupSize {
179      get { return GroupSizeParameter.Value; }
180      set { GroupSizeParameter.Value = value; }
181    }
182    public ICrossover Crossover {
183      get { return CrossoverParameter.Value; }
184      set { CrossoverParameter.Value = value; }
185    }
186    public ISelector Selector {
187      get { return SelectorParameter.Value; }
188      set { SelectorParameter.Value = value; }
189    }
190    public PercentValue MutationProbability {
191      get { return MutationProbabilityParameter.Value; }
192      set { MutationProbabilityParameter.Value = value; }
193    }
194    public IManipulator Mutator {
195      get { return MutatorParameter.Value; }
196      set { MutatorParameter.Value = value; }
197    }
198    public MultiAnalyzer Analyzer {
199      get { return AnalyzerParameter.Value; }
200      set { AnalyzerParameter.Value = value; }
201    }
202    public IntValue MaximumEvaluatedSolutions {
203      get { return MaximumEvaluatedSolutionsParameter.Value; }
204      set { MaximumEvaluatedSolutionsParameter.Value = value; }
205    }
206    public IntValue Elites {
207      get { return ElitesParameter.Value; }
208    }
209    #endregion
210
211    #region constructors
212    public EvolvmentModelsOfModelsAlgorithmBase() {
213
214      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
215      Parameters.Add(new FixedValueParameter<StringValue>(InputFileParameterName, "The file with set of models that will be.", new StringValue("input.txt")));
216      Parameters.Add(new FixedValueParameter<StringValue>(AlgorithmImplementationTypeParameterName, "The Type of possible algorith, implemetation, choose one: OnlyMapCreation, CreateMapAndGo, ReadMapAndGo.", new StringValue("CreateMapAndGo")));
217      Parameters.Add(new FixedValueParameter<StringValue>(MapCreationTypeParameterName, "The type of map crearion algorithm. Use one from: IslandMap, FullMap.", new StringValue("IslandMap")));
218      Parameters.Add(new FixedValueParameter<IntValue>(NegbourNumberParameterName, "The parametr for FullMap type of map crearion algorithm. Use one from: 10, 20.", new IntValue(10)));
219      Parameters.Add(new FixedValueParameter<StringValue>(NegbourTypeParameterName, "The parametr for FullMap type of map crearion algorithm. Use one from: Percent, Number.", new StringValue("Number")));
220      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
221      Parameters.Add(new ValueParameter<IntValue>(PopulationSizeParameterName, "The size of the population of Solutions.", new IntValue(100)));
222      Parameters.Add(new ConstrainedValueParameter<ISelector>(SelectorParameterName, "The operator used to sellect parents."));
223      Parameters.Add(new ValueParameter<PercentValue>(CrossoverProbabilityParameterName, "The probability that the crossover operator is applied.", new PercentValue(0.9)));
224      Parameters.Add(new ValueParameter<IntValue>(GroupSizeParameterName, "The GoupSize that the Selector operator is applied.", new IntValue(3)));
225      Parameters.Add(new ConstrainedValueParameter<ICrossover>(CrossoverParameterName, "The operator used to cross Solutions."));
226      Parameters.Add(new ValueParameter<PercentValue>(MutationProbabilityParameterName, "The probability that the mutation operator is applied on a solution.", new PercentValue(0.25)));
227      Parameters.Add(new ConstrainedValueParameter<IManipulator>(MutatorParameterName, "The operator used to mutate Solutions."));
228      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
229      Parameters.Add(new ValueParameter<IntValue>(MaximumEvaluatedSolutionsParameterName, "The maximum number of evaluated Solutions (approximately).", new IntValue(100_000)));
230      Parameters.Add(new ValueParameter<IRandom>(RandomParameterName, new MersenneTwister()));
231      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite Solutions which are kept in each generation.", new IntValue(1)));
232      Parameters.Add(new ValueParameter<IntValue>(ClusterNumbersParameterName, "The number of clusters for model Map.", new IntValue(100)));
233      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
234        SelectorParameter.ValidValues.Add(selector);
235      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
236      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
237      ParameterizeSelectors();
238
239      ProblemChanged += EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged;
240
241    }
242
243    private void EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged(object sender, EventArgs e) {
244      if (Problem != null) {
245        Problem.SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeBatchInterpreter();
246        //Problem.SymbolicExpressionTreeGrammar = new EMMGrammar();
247      }
248    }
249
250    protected EvolvmentModelsOfModelsAlgorithmBase(EvolvmentModelsOfModelsAlgorithmBase original, Cloner cloner) : base(original, cloner) {
251      EvaluatedSolutions = original.EvaluatedSolutions;
252      previousExecutionState = original.previousExecutionState;
253
254      if (original.Solutions != null) {
255        Solutions = original.Solutions.Select(cloner.Clone).ToArray();
256      }
257
258      if (original.Population != null) {
259        Population = original.Population.Select(cloner.Clone).ToList();
260      }
261
262      //if (original.offspringPopulation != null) {
263      //  offspringPopulation = original.offspringPopulation.Select(cloner.Clone).ToList();
264      //}
265
266      //if (original.jointPopulation != null) {
267      //  jointPopulation = original.jointPopulation.Select(x => cloner.Clone(x)).ToList();
268      //}
269
270      if (original.executionContext != null) {
271        executionContext = cloner.Clone(original.executionContext);
272      }
273
274      if (original.globalScope != null) {
275        globalScope = cloner.Clone(original.globalScope);
276      }
277    }
278
279    [StorableConstructor]
280    protected EvolvmentModelsOfModelsAlgorithmBase(StorableConstructorFlag _) : base(_) { }
281    #endregion
282
283
284    public override void Prepare() {
285      base.Prepare();
286    }
287
288    protected override void Initialize(CancellationToken cancellationToken) {
289      base.Initialize(cancellationToken);
290    }
291
292    public override bool SupportsPause => true;
293
294    // implements random number generation from https://en.wikipedia.org/wiki/Dirichlet_distribution#Random_number_generation
295
296    public IList<IEMMSolution> GetResult(IRandom random) {
297      return Population;
298    }
299
300    #region operator wiring and events
301    private void ParameterizeStochasticOperator(IOperator op) {
302      IStochasticOperator stochasticOp = op as IStochasticOperator;
303      if (stochasticOp != null) {
304        stochasticOp.RandomParameter.ActualName = "Random";
305        stochasticOp.RandomParameter.Hidden = true;
306      }
307    }
308    private void ParameterizeSelectors() {
309      foreach (ISelector selector in SelectorParameter.ValidValues) {
310        selector.CopySelected = new BoolValue(true);
311        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
312        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
313        ParameterizeStochasticOperator(selector);
314      }
315      if (Problem != null) {
316        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
317          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
318          selector.MaximizationParameter.Hidden = true;
319          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
320          selector.QualityParameter.Hidden = true;
321        }
322      }
323    }
324    protected void ExecuteOperation(ExecutionContext executionContext, CancellationToken cancellationToken, IOperation operation) {
325      Stack<IOperation> executionStack = new Stack<IOperation>();
326      executionStack.Push(operation);
327      while (executionStack.Count > 0) {
328        cancellationToken.ThrowIfCancellationRequested();
329        IOperation next = executionStack.Pop();
330        if (next is OperationCollection) {
331          OperationCollection coll = (OperationCollection)next;
332          for (int i = coll.Count - 1; i >= 0; i--)
333            if (coll[i] != null) executionStack.Push(coll[i]);
334        } else if (next is IAtomicOperation) {
335          IAtomicOperation op = (IAtomicOperation)next;
336          next = op.Operator.Execute((IExecutionContext)op, cancellationToken);
337          if (next != null) executionStack.Push(next);
338        }
339      }
340    }
341
342    private void UpdateAnalyzers() {
343      Analyzer.Operators.Clear();
344      if (Problem != null) {
345        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
346          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
347            param.Depth = 1;
348          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
349        }
350      }
351    }
352
353    private void UpdateCrossovers() {
354      ICrossover oldCrossover = CrossoverParameter.Value;
355      CrossoverParameter.ValidValues.Clear();
356      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
357
358      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
359        CrossoverParameter.ValidValues.Add(crossover);
360
361      if (oldCrossover != null) {
362        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
363        if (crossover != null) CrossoverParameter.Value = crossover;
364        else oldCrossover = null;
365      }
366      if (oldCrossover == null && defaultCrossover != null)
367        CrossoverParameter.Value = defaultCrossover;
368    }
369
370    private void UpdateMutators() {
371      IManipulator oldMutator = MutatorParameter.Value;
372      MutatorParameter.ValidValues.Clear();
373      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
374
375      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
376        MutatorParameter.ValidValues.Add(mutator);
377
378      if (oldMutator != null) {
379        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
380        if (mutator != null) MutatorParameter.Value = mutator;
381        else oldMutator = null;
382      }
383
384      if (oldMutator == null && defaultMutator != null)
385        MutatorParameter.Value = defaultMutator;
386    }
387    private void UpdateSelectors() {
388      ISelector oldSelector = SelectorParameter.Value;
389      SelectorParameter.ValidValues.Clear();
390      ISelector defaultSelector = Problem.Operators.OfType<ISelector>().FirstOrDefault();
391
392      foreach (ISelector selector in Problem.Operators.OfType<ISelector>().OrderBy(x => x.Name))
393        SelectorParameter.ValidValues.Add(selector);
394
395      if (oldSelector != null) {
396        ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
397        if (selector != null) SelectorParameter.Value = selector;
398        else oldSelector = null;
399      }
400
401      if (oldSelector == null && defaultSelector != null)
402        SelectorParameter.Value = defaultSelector;
403    }
404    protected override void OnProblemChanged() {
405      UpdateCrossovers();
406      UpdateMutators();
407      UpdateAnalyzers();
408      base.OnProblemChanged();
409    }
410
411    protected override void OnExecutionStateChanged() {
412      previousExecutionState = ExecutionState;
413      base.OnExecutionStateChanged();
414    }
415
416    protected override void OnStopped() {
417      if (Solutions != null) {
418        Solutions.Clear();
419      }
420      if (Population != null) {
421        Population.Clear();
422      }
423      //if (offspringPopulation != null) {
424      //  offspringPopulation.Clear();
425      //}
426      //if (jointPopulation != null) {
427      //  jointPopulation.Clear();
428      //}
429      executionContext.Scope.SubScopes.Clear();
430      base.OnStopped();
431    }
432    #endregion
433  }
434}
Note: See TracBrowser for help on using the repository browser.