Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17002 was 17002, checked in by msemenki, 3 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: 4.3 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
21using HEAL.Attic;
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.Problems.DataAnalysis.Symbolic;
26using System.Collections.Generic;
27using System.Linq;
28
29namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
30  [Item("SucsessMap", "A map of models of models of models")]
31  [StorableType("3880BA82-4CB0-4838-A17A-823E91BC046C")]
32  public class EMMSucsessMap : EMMMapBase<ISymbolicExpressionTree> {
33    [Storable]
34    public List<double> Probabilities { get; private set; }
35    [Storable]
36    public List<List<double>> SucsessStatistics { get; private set; }
37    #region conctructors
38    [StorableConstructor]
39    protected EMMSucsessMap(StorableConstructorFlag _) : base(_) { }
40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new EMMSucsessMap(this, cloner);
42    }
43    public EMMSucsessMap() : base() {
44      ModelSet = new List<ISymbolicExpressionTree>();
45      SucsessStatistics = new List<List<double>>();
46    }
47    public EMMSucsessMap(EMMSucsessMap original, Cloner cloner) : base(original, cloner) {
48      SucsessStatistics = original.SucsessStatistics.Select(x => x.ToList()).ToList();
49    }
50    #endregion
51    #region MapCreation
52    override public void CreateMap(IRandom random, int k) {
53
54      Probabilities = new List<double>();
55      Map.Clear();
56      Map.Add(new List<int>());
57      MapSizeCheck(ModelSet.Count);
58      ApplySucsessMapCreationAlgorithm(random, CalculateDistances(), Map, Probabilities, SucsessStatistics);
59    }
60    public static void ApplySucsessMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, List<double> probabilities, List<List<double>> sucsessStatistics) {
61      int mapSize = distances.GetLength(0);
62      for (int t = 0; t < mapSize; t++) {
63        map[t].Add(t);
64        probabilities.Add(1.0 / ((double)(mapSize))); // uniform distribution as start point
65      }
66    }
67    public override void MapUpDate(Dictionary<ISymbolicExpressionTree, double> population) {
68      SucsessStatisticCollection(population);
69      HelpFunctions.ProbabilitiesUpDate(SucsessStatistics, Probabilities);
70    }
71    private void SucsessStatisticCollection(Dictionary<ISymbolicExpressionTree, double> population) {
72      if (SucsessStatistics.Count != 0)
73        SucsessStatistics.Clear();
74      for (int t = 0; t < Probabilities.Count; t++) {
75        SucsessStatistics.Add(new List<double>());
76        SucsessStatistics[t].Add(0);
77        SucsessStatistics[t].Add(0);
78      }
79      foreach (var solution in population) {
80        TreeCheck(solution.Key, solution.Value);
81      }
82    }
83    private void TreeCheck(ISymbolicExpressionTree tree, double treeQuality) {
84      foreach (var treeNode in tree.IterateNodesPrefix().OfType<TreeModelTreeNode>()) {
85        SucsessStatistics[treeNode.TreeNumber][0] += 1;
86        SucsessStatistics[treeNode.TreeNumber][1] += treeQuality;
87      }
88    }
89    #endregion
90    #region MapApplayFunctions
91    public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
92      treeNumber = Map[HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities)][0];
93      return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
94    }
95    override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
96      return NewModelForInizializtion(random, out treeNumber);
97    }
98    #endregion
99  }
100}
Note: See TracBrowser for help on using the repository browser.