Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMAlgorithm.cs @ 17002

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

#2988:
Class HelpFuction get new static functions that are used in different Map’s classes and possible in other classes.
Branch was adapted to Hive.
New version of class structure for Maps:

  1. 3 new variants of maps (RankMap, SuccessMap and ZeroMap) are added.
  2. BaseMap class was simplified, some class members were deleted and other were transported to child class, because some of them are not used in all kinds of maps.
  3. Functions between base class and child class were divided in other way.
  4. Mutation operators were adapted to work with new class structure. Now mutation make less work for ModelNodes than previously.
  5. ModelNode and Model symbols were simplified. They should not take into account a map type.
  6. Models frequency analyzers were adapted for new variants of maps.
  7. EMMAlgorithm class was adapted to new maps
File size: 12.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.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Problems.DataAnalysis.Symbolic;
28using HeuristicLab.Random;
29using HeuristicLab.Selection;
30using System.Collections.Generic;
31using System.Linq;
32using CancellationToken = System.Threading.CancellationToken;
33using Variable = HeuristicLab.Core.Variable;
34
35namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
36  [Item("Evolvment Models Of Models Algorithm (EMM) ", "EMM implementation")]
37  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 125)]
38  [StorableType("AD23B21F-089A-4C6C-AD2E-1B01E7939CF5")]
39  public class EMMAlgorithm : EvolvmentModelsOfModelsAlgorithmBase {
40    public EMMAlgorithm() : base() { }
41    protected EMMAlgorithm(EMMAlgorithm original, Cloner cloner) : base(original, cloner) { }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new EMMAlgorithm(this, cloner);
44    }
45
46    [StorableConstructor]
47    protected EMMAlgorithm(StorableConstructorFlag _) : base(_) { }
48
49    protected override void Run(CancellationToken cancellationToken) {
50      if (AlgorithmImplemetationType.Value == "Read") {
51        Map.MapRead(RandomParameter.Value, trees, "Map.txt");
52      } else {
53        Map.MapCreationPrepare(trees);
54        Map.CreateMap(RandomParameter.Value, ClusterNumbersParameter.Value.Value);
55        // Map.WriteMapToTxtFile(RandomParameter.Value); хайв этого не любит.. ворчит
56      }
57      ClusterNumbersShowParameter.Value.Value = Map.Map.Count;
58
59      if (AlgorithmImplemetationType.Value == "OnlyMap") {
60        globalScope = new Scope("Global Scope");
61        executionContext = new ExecutionContext(null, this, globalScope);
62      } else {
63        if (previousExecutionState != ExecutionState.Paused) {
64          InitializeAlgorithm(cancellationToken);
65        }
66        if (!globalScope.Variables.ContainsKey("TreeModelMap"))
67          globalScope.Variables.Add(new Variable("TreeModelMap", Map));
68        if (!globalScope.Variables.ContainsKey("Map"))
69          globalScope.Variables.Add(new Variable("Map", Map));
70        EMMAlgorithmRun(cancellationToken);
71      }
72    }
73    private void EMMAlgorithmRun(CancellationToken cancellationToken) {
74      var bestSelector = new BestSelector();
75      bestSelector.CopySelected = new BoolValue(false);
76      bestSelector.MaximizationParameter.ActualName = "Maximization";
77      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
78      bestSelector.QualityParameter.ActualName = "Quality";
79
80      var maximumEvaluatedSolutions = MaximumEvaluatedSolutions.Value;
81      var crossover = Crossover;
82      var selector = Selector;
83      var crossoverProbability = CrossoverProbability.Value;
84      var mutator = Mutator;
85      var mutationProbability = MutationProbability.Value;
86      var evaluator = Problem.Evaluator;
87      var analyzer = Analyzer;
88      var rand = RandomParameter.Value;
89      var elites = Elites.Value;
90
91      // cancellation token for the inner operations which should not be immediately cancelled
92      var innerToken = new CancellationToken();
93
94      while (EvaluatedSolutions < maximumEvaluatedSolutions && !cancellationToken.IsCancellationRequested) {
95
96        var op4 = executionContext.CreateChildOperation(bestSelector, executionContext.Scope); // select elites
97        ExecuteOperation(executionContext, innerToken, op4);
98
99        var remaining = executionContext.Scope.SubScopes.Single(x => x.Name == "Remaining");
100        executionContext.Scope.SubScopes.AddRange(remaining.SubScopes);
101        var selected = executionContext.Scope.SubScopes.Single(x => x.Name == "Selected");
102        executionContext.Scope.SubScopes.AddRange(selected.SubScopes);
103        Population.Clear();
104        Population.AddRange(selected.SubScopes.Select(x => new EMMSolution(x)));
105        executionContext.Scope.SubScopes.Remove(remaining);
106        executionContext.Scope.SubScopes.Remove(selected);
107
108        var op = executionContext.CreateChildOperation(selector, executionContext.Scope);// select the rest of the Population
109        ExecuteOperation(executionContext, innerToken, op);
110
111        remaining = executionContext.Scope.SubScopes.Single(x => x.Name == "Remaining");
112        selected = executionContext.Scope.SubScopes.Single(x => x.Name == "Selected");
113
114        for (int i = 0; i < selector.NumberOfSelectedSubScopesParameter.Value.Value; i += 2) {
115          // crossover
116          IScope childScope = null;
117          if (rand.NextDouble() < crossoverProbability) {
118            childScope = new Scope($"{i}+{i + 1}") { Parent = executionContext.Scope };
119            childScope.SubScopes.Add(selected.SubScopes[i]);
120            childScope.SubScopes.Add(selected.SubScopes[i + 1]);
121            var op1 = executionContext.CreateChildOperation(crossover, childScope);
122            ExecuteOperation(executionContext, innerToken, op1);
123            childScope.SubScopes.Clear();
124          }
125
126          childScope = childScope ?? selected.SubScopes[i];
127          // mutation
128          if (rand.NextDouble() < mutationProbability) {
129            var op2 = executionContext.CreateChildOperation(mutator, childScope);
130            ExecuteOperation(executionContext, innerToken, op2);
131          }
132
133          // evaluation
134          if (childScope != null) {
135            var op3 = executionContext.CreateChildOperation(evaluator, childScope);
136            ExecuteOperation(executionContext, innerToken, op3);
137            //if (Problem.ProblemData is IRegressionProblemData problemData) {
138            //  SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(Problem.SymbolicExpressionTreeInterpreter, (ISymbolicExpressionTree)childScope.Variables["SymbolicExpressionTree"].Value, problemData, problemData.TestIndices, true, 100 /*max iterration*/, true);
139            //}
140            ++EvaluatedSolutions;
141            Population.Add(new EMMSolution(childScope));
142          } else {// no crossover or mutation were applied, a child was not produced, do nothing
143            Population.Add(new EMMSolution(selected.SubScopes[i]));
144          }
145          if (EvaluatedSolutions >= maximumEvaluatedSolutions) {
146            break;
147          }
148
149        }
150        if (Map is EMMSucsessMap) {
151          var population = new Dictionary<ISymbolicExpressionTree, double>();
152          foreach (var individ in Population) {
153            var tree = (ISymbolicExpressionTree)(((IScope)individ.Individual).Variables["SymbolicExpressionTree"].Value);
154            population.Add(tree, individ.Qualities.Value);
155          }
156          Map.MapUpDate(population);
157          population.Clear();
158        }
159        //List<object> A = new List<object>();
160        //A.Add(new Scope ());
161        //A.Add(new SymbolicExpressionTree());
162
163        globalScope.SubScopes.Replace(Population.Select(x => (IScope)x.Individual));
164        // run analyzer
165        var analyze = executionContext.CreateChildOperation(analyzer, globalScope);
166        ExecuteOperation(executionContext, innerToken, analyze);
167        Results.AddOrUpdateResult("Evaluated Solutions", new IntValue(EvaluatedSolutions));
168      }
169    }
170    protected void InitializeAlgorithm(CancellationToken cancellationToken) {
171      globalScope = new Scope("Global Scope");
172      executionContext = new ExecutionContext(null, this, globalScope);
173
174      // set the execution context for parameters to allow lookup
175      foreach (var parameter in Problem.Parameters.OfType<IValueParameter>()) {
176        globalScope.Variables.Add(new Variable(parameter.Name, parameter.Value));
177      }
178      globalScope.Variables.Add(new Variable("Results", Results)); // make results available as a parameter for analyzers etc.
179
180      var rand = RandomParameter.Value;
181      if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
182      rand.Reset(Seed);
183
184      InitializePopulation(executionContext, cancellationToken, rand);
185      EvaluatedSolutions = PopulationSize.Value;
186
187      base.Initialize(cancellationToken);
188    }
189
190    private void InitializePopulation(ExecutionContext executionContext, CancellationToken cancellationToken, IRandom random) {
191      Population = new List<IEMMSolution>();
192      var evaluator = Problem.Evaluator;
193      var creator = Problem.SolutionCreator;
194      var parentScope = executionContext.Scope; //main scope for the next step work
195                                                // first, create all individuals
196      for (int i = 0; i < PopulationSize.Value; ++i) {
197        var childScope = new Scope(i.ToString()) { Parent = parentScope };
198        ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(creator, childScope));
199        var name = ((ISymbolicExpressionTreeCreator)creator).SymbolicExpressionTreeParameter.ActualName;
200        var tree = (ISymbolicExpressionTree)childScope.Variables[name].Value;
201        foreach (var node in tree.IterateNodesPostfix().OfType<TreeModelTreeNode>()) {
202          Map.NodeManipulationForInizializtion(random, node);
203        }
204        parentScope.SubScopes.Add(childScope);
205      }
206      // then, evaluate them and update qualities
207      for (int i = 0; i < PopulationSize.Value; ++i) {
208        var childScope = parentScope.SubScopes[i];
209        ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(evaluator, childScope));
210        Population.Add(new EMMSolution(childScope));  // Create solution and push individual inside. push solution to Population
211      }
212    }
213
214    // next function was not tested in real work
215    private void LocalDecent(ISymbolicDataAnalysisSingleObjectiveProblem problem, CancellationToken cancellationToken, IScope childScope) {
216      int maxStepNumber = 100;
217      var name = ((ISymbolicExpressionTreeCreator)Problem.SolutionCreator).SymbolicExpressionTreeParameter.ActualName;
218      var tree = (ISymbolicExpressionTree)childScope.Variables[name].Value;
219      var oldTree = tree.Clone();
220      ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(problem.Evaluator, childScope));
221      var rand = RandomParameter.Value;
222      if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
223      rand.Reset(Seed);
224      while (maxStepNumber > 0) {
225        maxStepNumber = TreeIterator(tree.Root, rand, cancellationToken, childScope, maxStepNumber);
226      }
227    }
228    int TreeIterator(ISymbolicExpressionTreeNode a, IRandom rand, CancellationToken cancellationToken, IScope childScope, int maxStepNumber) {
229      if (a is TreeModelTreeNode modelNode) {
230        ModelChange(modelNode, rand, cancellationToken, childScope);
231        maxStepNumber--;
232      }
233      if (a.Subtrees != null) {
234        for (int i = 0; i < (a.Subtrees.Count()); i++) {
235          TreeIterator(a.Subtrees.ToList()[i], rand, cancellationToken, childScope, maxStepNumber);
236        }
237      }
238      return maxStepNumber;
239    }
240    void ModelChange(TreeModelTreeNode tree, IRandom rand, CancellationToken cancellationToken, IScope childScope) {
241      int treeNumber = tree.TreeNumber;
242      var oldSubTree = (ISymbolicExpressionTree)tree.Tree.Clone();
243      double oldQuality = ((DoubleValue)childScope.Variables["Quality"].Value).Value;
244      int cluster;
245      if (Map is EMMIslandMap map)
246        cluster = map.ClusterNumber[treeNumber];
247      else cluster = treeNumber;
248      int newTreeNumber = rand.Next(Map.Map[cluster].Count);
249      tree.Tree = (ISymbolicExpressionTree)Map.ModelSet[newTreeNumber].Clone();
250      tree.Tree.Root.ShakeLocalParameters(rand, 1);
251      var evaluator = Problem.Evaluator;
252      ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(evaluator, childScope));
253      double currentQuality = ((DoubleValue)childScope.Variables["Quality"].Value).Value;
254      if (oldQuality > currentQuality) {
255        tree.Tree = (ISymbolicExpressionTree)oldSubTree.Clone();
256        ((DoubleValue)childScope.Variables["Quality"].Value).Value = oldQuality;
257      }
258    }
259  }
260
261
262}
263
Note: See TracBrowser for help on using the repository browser.