Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16752 was 16734, checked in by msemenki, 6 years ago

#2988: Add Model Symbol Frequency Analyzer and Model's Clusters Frequency Analyzer. Fix Bag's with Keys. Fix changing during mutation for Variables Types in SubModels .

File size: 18.1 KB
RevLine 
[16722]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.")]
[16734]42  [StorableType("A56A396B-965A-4ADE-8A2B-AE3A45F9C239")]
[16722]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 List<IEMMSolution> offspringPopulation;
51    [Storable]
52    protected List<IEMMSolution> jointPopulation;
53    [Storable]
54    protected int evaluatedSolutions;
55    [Storable]
56    protected ExecutionContext executionContext;
57    [Storable]
58    protected IScope globalScope;
59    [Storable]
60    protected ExecutionState previousExecutionState;
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
79    public IValueParameter<MultiAnalyzer> AnalyzerParameter {
80      get { return (ValueParameter<MultiAnalyzer>)Parameters[AnalyzerParameterName]; }
81    }
82    public IFixedValueParameter<IntValue> SeedParameter {
83      get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
84    }
85    public IValueParameter<IntValue> ClusterNumbersParameter {
86      get { return (IValueParameter<IntValue>)Parameters[ClusterNumbersParameterName]; }
87    }
88    public IFixedValueParameter<StringValue> InputFileParameter {
89      get { return (IFixedValueParameter<StringValue>)Parameters[InputFileParameterName]; }
90    }
91    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
92      get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
93    }
94    private IValueParameter<IntValue> PopulationSizeParameter {
95      get { return (IValueParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
96    }
97    public IValueParameter<PercentValue> CrossoverProbabilityParameter {
98      get { return (IValueParameter<PercentValue>)Parameters[CrossoverProbabilityParameterName]; }
99    }
100    public IValueParameter<IntValue> GroupSizeParameter {
101      get { return (IValueParameter<IntValue>)Parameters[GroupSizeParameterName]; }
102    }
103    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
104      get { return (IConstrainedValueParameter<ICrossover>)Parameters[CrossoverParameterName]; }
105    }
106    public IConstrainedValueParameter<ISelector> SelectorParameter {
107      get { return (IConstrainedValueParameter<ISelector>)Parameters[SelectorParameterName]; }
108    }
109    public IValueParameter<PercentValue> MutationProbabilityParameter {
110      get { return (IValueParameter<PercentValue>)Parameters[MutationProbabilityParameterName]; }
111    }
112    public IConstrainedValueParameter<IManipulator> MutatorParameter {
113      get { return (IConstrainedValueParameter<IManipulator>)Parameters[MutatorParameterName]; }
114    }
115    public IValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
116      get { return (IValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsParameterName]; }
117    }
118    public IValueParameter<IRandom> RandomParameter {
119      get { return (IValueParameter<IRandom>)Parameters[RandomParameterName]; }
120    }
121    #endregion
122
123    #region parameter properties
124    public ValueParameter<IntValue> ElitesParameter {
125      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
126    }
127    public int Seed {
128      get { return SeedParameter.Value.Value; }
129      set { SeedParameter.Value.Value = value; }
130    }
131    public IntValue ClusterNumbers {
132      get { return ClusterNumbersParameter.Value; }
133      set { ClusterNumbersParameter.Value = value; }
134    }
135    public StringValue InputFile {
136      get { return InputFileParameter.Value; }
137      set { InputFileParameter.Value.Value = value.Value; }
138    }
139    public bool SetSeedRandomly {
140      get { return SetSeedRandomlyParameter.Value.Value; }
141      set { SetSeedRandomlyParameter.Value.Value = value; }
142    }
143    public IntValue PopulationSize {
144      get { return PopulationSizeParameter.Value; }
145      set { PopulationSizeParameter.Value = value; }
146    }
147    public PercentValue CrossoverProbability {
148      get { return CrossoverProbabilityParameter.Value; }
149      set { CrossoverProbabilityParameter.Value = value; }
150    }
151    public IntValue GroupSize {
152      get { return GroupSizeParameter.Value; }
153      set { GroupSizeParameter.Value = value; }
154    }
155    public ICrossover Crossover {
156      get { return CrossoverParameter.Value; }
157      set { CrossoverParameter.Value = value; }
158    }
159    public ISelector Selector {
160      get { return SelectorParameter.Value; }
161      set { SelectorParameter.Value = value; }
162    }
163    public PercentValue MutationProbability {
164      get { return MutationProbabilityParameter.Value; }
165      set { MutationProbabilityParameter.Value = value; }
166    }
167    public IManipulator Mutator {
168      get { return MutatorParameter.Value; }
169      set { MutatorParameter.Value = value; }
170    }
171    public MultiAnalyzer Analyzer {
172      get { return AnalyzerParameter.Value; }
173      set { AnalyzerParameter.Value = value; }
174    }
175    public IntValue MaximumEvaluatedSolutions {
176      get { return MaximumEvaluatedSolutionsParameter.Value; }
177      set { MaximumEvaluatedSolutionsParameter.Value = value; }
178    }
179    public IntValue Elites {
180      get { return ElitesParameter.Value; }
181    }
182    #endregion
183
184    #region constructors
185    public EvolvmentModelsOfModelsAlgorithmBase() {
186
187      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
188      Parameters.Add(new FixedValueParameter<StringValue>(InputFileParameterName, "The file with set of models that will be .", new StringValue("input.txt")));
189      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
190      Parameters.Add(new ValueParameter<IntValue>(PopulationSizeParameterName, "The size of the population of solutions.", new IntValue(100)));
191      Parameters.Add(new ConstrainedValueParameter<ISelector>(SelectorParameterName, "The operator used to sellect parents."));
192      Parameters.Add(new ValueParameter<PercentValue>(CrossoverProbabilityParameterName, "The probability that the crossover operator is applied.", new PercentValue(0.9)));
193      Parameters.Add(new ValueParameter<IntValue>(GroupSizeParameterName, "The GoupSize that the Selector operator is applied.", new IntValue(3)));
194      Parameters.Add(new ConstrainedValueParameter<ICrossover>(CrossoverParameterName, "The operator used to cross solutions."));
195      Parameters.Add(new ValueParameter<PercentValue>(MutationProbabilityParameterName, "The probability that the mutation operator is applied on a solution.", new PercentValue(0.25)));
196      Parameters.Add(new ConstrainedValueParameter<IManipulator>(MutatorParameterName, "The operator used to mutate solutions."));
197      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
198      Parameters.Add(new ValueParameter<IntValue>(MaximumEvaluatedSolutionsParameterName, "The maximum number of evaluated solutions (approximately).", new IntValue(100_000)));
199      Parameters.Add(new ValueParameter<IRandom>(RandomParameterName, new MersenneTwister()));
200      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
201      Parameters.Add(new ValueParameter<IntValue>(ClusterNumbersParameterName, "The number of clusters for model Map.", new IntValue(100)));
202      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
203        SelectorParameter.ValidValues.Add(selector);
204      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
205      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
206      ParameterizeSelectors();
207
208      ProblemChanged += EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged;
209
210    }
211
212    private void EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged(object sender, EventArgs e) {
213      if (Problem != null) {
214        Problem.SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeBatchInterpreter();
215        //Problem.SymbolicExpressionTreeGrammar = new EMMGrammar();
216      }
217    }
218
219    protected EvolvmentModelsOfModelsAlgorithmBase(EvolvmentModelsOfModelsAlgorithmBase original, Cloner cloner) : base(original, cloner) {
220      evaluatedSolutions = original.evaluatedSolutions;
221      previousExecutionState = original.previousExecutionState;
222
223      if (original.solutions != null) {
224        solutions = original.solutions.Select(cloner.Clone).ToArray();
225      }
226
227      if (original.population != null) {
228        population = original.population.Select(cloner.Clone).ToList();
229      }
230
231      if (original.offspringPopulation != null) {
232        offspringPopulation = original.offspringPopulation.Select(cloner.Clone).ToList();
233      }
234
235      if (original.jointPopulation != null) {
236        jointPopulation = original.jointPopulation.Select(x => cloner.Clone(x)).ToList();
237      }
238
239      if (original.executionContext != null) {
240        executionContext = cloner.Clone(original.executionContext);
241      }
242
243      if (original.globalScope != null) {
244        globalScope = cloner.Clone(original.globalScope);
245      }
246    }
247
248    [StorableConstructor]
249    protected EvolvmentModelsOfModelsAlgorithmBase(StorableConstructorFlag _) : base(_) { }
250    #endregion
251
252
253    public override void Prepare() {
254      base.Prepare();
255    }
256
257    protected override void Initialize(CancellationToken cancellationToken) {
258      base.Initialize(cancellationToken);
259    }
260
261    public override bool SupportsPause => true;
262
263    // implements random number generation from https://en.wikipedia.org/wiki/Dirichlet_distribution#Random_number_generation
264
265    public IList<IEMMSolution> GetResult(IRandom random) {
266      return population;
267    }
268
269    #region operator wiring and events
270    private void ParameterizeStochasticOperator(IOperator op) {
271      IStochasticOperator stochasticOp = op as IStochasticOperator;
272      if (stochasticOp != null) {
273        stochasticOp.RandomParameter.ActualName = "Random";
274        stochasticOp.RandomParameter.Hidden = true;
275      }
276    }
277    private void ParameterizeSelectors() {
278      foreach (ISelector selector in SelectorParameter.ValidValues) {
279        selector.CopySelected = new BoolValue(true);
280        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
281        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
282        ParameterizeStochasticOperator(selector);
283      }
284      if (Problem != null) {
285        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
286          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
287          selector.MaximizationParameter.Hidden = true;
288          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
289          selector.QualityParameter.Hidden = true;
290        }
291      }
292    }
293    protected void ExecuteOperation(ExecutionContext executionContext, CancellationToken cancellationToken, IOperation operation) {
294      Stack<IOperation> executionStack = new Stack<IOperation>();
295      executionStack.Push(operation);
296      while (executionStack.Count > 0) {
297        cancellationToken.ThrowIfCancellationRequested();
298        IOperation next = executionStack.Pop();
299        if (next is OperationCollection) {
300          OperationCollection coll = (OperationCollection)next;
301          for (int i = coll.Count - 1; i >= 0; i--)
302            if (coll[i] != null) executionStack.Push(coll[i]);
303        } else if (next is IAtomicOperation) {
304          IAtomicOperation op = (IAtomicOperation)next;
305          next = op.Operator.Execute((IExecutionContext)op, cancellationToken);
306          if (next != null) executionStack.Push(next);
307        }
308      }
309    }
310
311    private void UpdateAnalyzers() {
312      Analyzer.Operators.Clear();
313      if (Problem != null) {
314        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
315          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
316            param.Depth = 1;
317          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
318        }
319      }
320    }
321
322    private void UpdateCrossovers() {
323      ICrossover oldCrossover = CrossoverParameter.Value;
324      CrossoverParameter.ValidValues.Clear();
325      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
326
327      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
328        CrossoverParameter.ValidValues.Add(crossover);
329
330      if (oldCrossover != null) {
331        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
332        if (crossover != null) CrossoverParameter.Value = crossover;
333        else oldCrossover = null;
334      }
335      if (oldCrossover == null && defaultCrossover != null)
336        CrossoverParameter.Value = defaultCrossover;
337    }
338
339    private void UpdateMutators() {
340      IManipulator oldMutator = MutatorParameter.Value;
341      MutatorParameter.ValidValues.Clear();
342      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
343
344      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
345        MutatorParameter.ValidValues.Add(mutator);
346
347      if (oldMutator != null) {
348        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
349        if (mutator != null) MutatorParameter.Value = mutator;
350        else oldMutator = null;
351      }
352
353      if (oldMutator == null && defaultMutator != null)
354        MutatorParameter.Value = defaultMutator;
355    }
356    private void UpdateSelectors() {
357      ISelector oldSelector = SelectorParameter.Value;
358      SelectorParameter.ValidValues.Clear();
359      ISelector defaultSelector = Problem.Operators.OfType<ISelector>().FirstOrDefault();
360
361      foreach (ISelector selector in Problem.Operators.OfType<ISelector>().OrderBy(x => x.Name))
362        SelectorParameter.ValidValues.Add(selector);
363
364      if (oldSelector != null) {
365        ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
366        if (selector != null) SelectorParameter.Value = selector;
367        else oldSelector = null;
368      }
369
370      if (oldSelector == null && defaultSelector != null)
371        SelectorParameter.Value = defaultSelector;
372    }
373    protected override void OnProblemChanged() {
374      UpdateCrossovers();
375      UpdateMutators();
376      UpdateAnalyzers();
377      base.OnProblemChanged();
378    }
379
380    protected override void OnExecutionStateChanged() {
381      previousExecutionState = ExecutionState;
382      base.OnExecutionStateChanged();
383    }
384
385    protected override void OnStopped() {
386      if (solutions != null) {
387        solutions.Clear();
388      }
389      if (population != null) {
390        population.Clear();
391      }
392      if (offspringPopulation != null) {
393        offspringPopulation.Clear();
394      }
395      if (jointPopulation != null) {
396        jointPopulation.Clear();
397      }
398      executionContext.Scope.SubScopes.Clear();
399      base.OnStopped();
400    }
401    #endregion
402  }
403}
Note: See TracBrowser for help on using the repository browser.