Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16899 was 16899, checked in by msemenki, 5 years ago

#2988: New version of class structure.

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