Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMBaseMap.cs @ 17134

Last change on this file since 17134 was 17134, checked in by msemenki, 3 years ago

#2988:

  1. The file system was changed, folders was added and part of files was transferred in these folders.
  2. HelpFunctions class was divided on 2 parts: HelpFuctions for common purposes static functions and SelfConfiguration that include functions for self-configuration mechanism realization (is used in EMMSucsessMap).
  3. Parts of self-configuration mechanism was transferred from EMMSucsessMap.cs to SelfConfiguration.cs. Now EMMSucsessMap used SelfConfiguration like one of data member. Other parts of project was adopted for this changing.
  4. FileComunication class was added. It include the majority of functions for printing to files or reading from files. Here were realized possibility to write and read to hl files.
  5. ModelTreeNode.cs has additional possibility - to write sub-model in string (then it is possible to write it in file).
  6. InfixExpressionFormatter.cs can work with TreeModelNode.
  7. Possibility for different map types to be readable from files was extended and cheeked.
  8. Such parameters like - ClusterNumbers, ClusterNumbersShow, NegbourNumber, NegbourType (that is used only in several maps) was transferred from EMMAlgorithm to Map Parameters. Now EMMBaseMap class inherited from ParameterizedNamedItem (not from Item). And EMMIslandMap and EMMNetworkMap contains their parameters (constructors was modified). CreationMap calls functions were simplified.
  9. Functions for different distance metric calculation was added. Now, it is possible to calculate different types of distances between models (with different random values of constants).
  10. DistanceParametr was added. Now maps can be created according different types of distance calculations.
  11. The class EMMClustering has new name KMeansClusterizationAlgorithm. On KMeansClusterizationAlgorithm bug with bloating of centroids list was fixed. Algorithm was adopted for working with different type of distance metric and get maximum number of iterations.
  12. Possibilities for constants optimization in sub-models an whole tree was added. EMMAlgorithm get new function for evaluation of individuals (and some additional technical stuff for that). Function for trees with model in usual tree transformation and back was added.
  13. EMMAlgorithm was divided on 2 parts:
  • EMMAlgorithm, that contain evolutionary algorithm working with sub-models, and use ready to use maps;
  • ModelSetPreparation, that contain distance calculation, model set simplification and map creation.
File size: 7.0 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.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Problems.DataAnalysis.Symbolic;
27using System.Collections.Generic;
28using System.IO;
29using System.Linq;
30
31namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
32  [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")]
33  public abstract class EMMMapBase<T> : ParameterizedNamedItem where T : class {
34    #region data members
35    [Storable]
36    public List<T> ModelSet { get; protected set; }
37    [Storable]
38    public List<List<int>> Map { get; set; }
39    public string DistanceParametr { get; set; }
40    #endregion
41    #region constructors
42    [StorableConstructor]
43    protected EMMMapBase(StorableConstructorFlag _) : base(_) { }
44    public EMMMapBase() {
45      Map = new List<List<int>>();
46      DistanceParametr = "Symbolic";
47    }
48    public EMMMapBase(EMMMapBase<T> original, Cloner cloner) : base(original, cloner) {
49      if (original.ModelSet != null) {
50        if (original.ModelSet is List<ISymbolicExpressionTree> originalSet && ModelSet is List<ISymbolicExpressionTree> set)
51          set = originalSet.Select(cloner.Clone).ToList();
52        else ModelSet = original.ModelSet.ToList(); /// check this if you want to use it
53      }
54      if (original.Map != null) {
55        Map = original.Map.Select(x => x.ToList()).ToList();
56      }
57      DistanceParametr = original.DistanceParametr;
58    }
59    #endregion
60    #region map creation functions
61
62    public abstract void CreateMap(IRandom random);
63    public void MapCreationPrepare(IEnumerable<T> trees) {
64      ModelSet = trees.ToList();
65    }
66    public virtual void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) {
67      if (Map != null) {
68        Map.Clear();
69      }
70      CreateMap(random);
71    }
72    public virtual void CreateMap(IRandom random, double[,] totalDistance) {
73      if (Map != null) {
74        Map.Clear();
75      }
76      CreateMap(random);
77    }
78
79    protected void MapSizeCheck(int k) {
80      if (Map != null) Map.Clear();
81      else Map = new List<List<int>>();
82      if (Map.Count != k) {
83        if (Map.Count != 0) {
84          Map.Clear();
85        }
86        for (int i = 0; i < k; i++) {
87          Map.Add(new List<int>());
88        }
89      }
90    }
91    #endregion
92    #region map and files
93    public virtual void MapRead(IEnumerable<T> trees) {
94      ModelSet = trees.ToList();
95    }
96    public void WriteMapToTxtFile(IRandom random) {
97      string s = random.NextDouble().ToString();
98      string fileName = "MapToAnalize";
99      fileName += s;
100      fileName += DistanceParametr;
101      fileName += ".txt";
102      File.WriteAllLines(fileName, MapToString());
103      string fileName2 = "MapToSee";
104      fileName2 += s;
105      fileName2 += DistanceParametr;
106      fileName2 += ".txt";
107      File.WriteAllLines(fileName2, MapToSee());
108      string fileName3 = "Map";
109      fileName3 += DistanceParametr;
110      fileName3 += ".txt";
111      File.WriteAllLines(fileName3, MapToStoreInFile());
112    }
113    public string[] MapToString() { // Function that prepare Map to printing in .txt File: create a set of strings for future analyzing
114      string[] s;
115      s = new string[Map.Count];
116      for (int i = 0; i < Map.Count; i++) {
117        s[i] = i.ToString() + ": ";
118        for (int j = 0; j < Map[i].Count; j++) {
119          s[i] += Map[i][j].ToString();
120          s[i] += " ";
121        }
122        if (this is EMMIslandMap island) {
123          s[i] += "  Average distance:" + island.AverageDistance[i].ToString();
124        }
125      }
126      return s;
127    }
128    public virtual string[] MapToStoreInFile() { // Function that prepare Map to printing in .txt File: create a set of strings for future reading by computer
129      string[] s;
130      s = new string[Map.Count];
131      for (int i = 0; i < Map.Count; i++) {
132        s[i] = "";
133        for (int j = 0; j < Map[i].Count; j++) {
134          s[i] += Map[i][j].ToString();
135          if (j != (Map[i].Count - 1)) { s[i] += " "; }
136        }
137      }
138      return s;
139    }
140    public string[] MapToSee() { // Function that prepare Map to printing in .txt File: create a set of strings in human readable view
141      var fmt = new InfixExpressionFormatter();
142      string[] s;
143      s = new string[(ModelSet.Count) + 1];
144      s[0] = "ClusterNumber" + "," + "ModelNumber" + "," + "Model";
145      for (int i = 1; i < ((ModelSet.Count) + 1); i++) {
146        s[i] = "";
147        if (this is EMMIslandMap island) {
148          s[i] += island.ClusterNumber[i - 1].ToString() + ",";
149        }
150        s[i] += (i - 1).ToString() + ",";
151        if (ModelSet[i - 1] is ISymbolicExpressionTree model) {
152          s[i] += fmt.Format(model);
153        } else { s[i] += ModelSet[i - 1].ToString(); }
154      }
155      return s;
156    }
157    #endregion
158
159    #region map use functions
160    public abstract T NewModelForInizializtionNotTree(IRandom random, out int treeNumber);
161    public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int treeNumber) {
162      treeNumber = random.Next(ModelSet.Count);
163      if (ModelSet[treeNumber] is ISymbolicExpressionTree model)
164        return (ISymbolicExpressionTree)(model.Clone());
165      return new SymbolicExpressionTree();
166    }
167    public void NodeManipulationForInizializtion(IRandom random, TreeModelTreeNode node) {
168      node.Tree = NewModelForInizializtion(random, out int treeNumber);
169      node.SetLocalParameters(random, 0.5);
170      node.TreeNumber = treeNumber;
171    }
172    public abstract ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber);
173    public virtual void NodeForMutationChange(IRandom random, TreeModelTreeNode treeNode) {
174      int treeNumber = treeNode.TreeNumber;
175      int treeNumber2 = treeNode.TreeNumber;
176      treeNode.Tree = new SymbolicExpressionTree(NewModelForMutation(random, out treeNumber, treeNumber2).Root);
177      treeNode.TreeNumber = treeNumber;
178      HelpFunctions.SetLocalParametersForTree(random, 0.5, treeNode.Tree);
179    }
180    public virtual void MapUpDate(Dictionary<ISymbolicExpressionTree, double> population) { }
181    #endregion
182  }
183}
Note: See TracBrowser for help on using the repository browser.