Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18183 was 17134, checked in by msemenki, 5 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
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;
[16899]23using HeuristicLab.Common;
[16722]24using HeuristicLab.Core;
[16899]25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Problems.DataAnalysis.Symbolic;
[16722]27using System.Collections.Generic;
[16899]28using System.IO;
29using System.Linq;
[16722]30
31namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
[16734]32  [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")]
[17134]33  public abstract class EMMMapBase<T> : ParameterizedNamedItem where T : class {
[17002]34    #region data members
[16899]35    [Storable]
[17134]36    public List<T> ModelSet { get; protected set; }
[16899]37    [Storable]
[16722]38    public List<List<int>> Map { get; set; }
[17134]39    public string DistanceParametr { get; set; }
[17002]40    #endregion
41    #region constructors
42    [StorableConstructor]
43    protected EMMMapBase(StorableConstructorFlag _) : base(_) { }
44    public EMMMapBase() {
45      Map = new List<List<int>>();
[17134]46      DistanceParametr = "Symbolic";
[17002]47    }
[17134]48    public EMMMapBase(EMMMapBase<T> original, Cloner cloner) : base(original, cloner) {
[17002]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      }
[17134]57      DistanceParametr = original.DistanceParametr;
[17002]58    }
59    #endregion
60    #region map creation functions
[17134]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();
[16899]69      }
[17134]70      CreateMap(random);
71    }
72    public virtual void CreateMap(IRandom random, double[,] totalDistance) {
73      if (Map != null) {
74        Map.Clear();
[16899]75      }
[17134]76      CreateMap(random);
[16899]77    }
78
[17002]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        }
[16899]89      }
90    }
[17002]91    #endregion
92    #region map and files
[17134]93    public virtual void MapRead(IEnumerable<T> trees) {
[16899]94      ModelSet = trees.ToList();
95    }
96    public void WriteMapToTxtFile(IRandom random) {
[17134]97      string s = random.NextDouble().ToString();
98      string fileName = "MapToAnalize";
[16899]99      fileName += s;
[17134]100      fileName += DistanceParametr;
[16899]101      fileName += ".txt";
102      File.WriteAllLines(fileName, MapToString());
103      string fileName2 = "MapToSee";
104      fileName2 += s;
[17134]105      fileName2 += DistanceParametr;
[16899]106      fileName2 += ".txt";
[17002]107      File.WriteAllLines(fileName2, MapToSee());
[17134]108      string fileName3 = "Map";
109      fileName3 += DistanceParametr;
110      fileName3 += ".txt";
111      File.WriteAllLines(fileName3, MapToStoreInFile());
[16899]112    }
[17134]113    public string[] MapToString() { // Function that prepare Map to printing in .txt File: create a set of strings for future analyzing
[16899]114      string[] s;
[17002]115      s = new string[Map.Count];
116      for (int i = 0; i < Map.Count; i++) {
[17134]117        s[i] = i.ToString() + ": ";
[16899]118        for (int j = 0; j < Map[i].Count; j++) {
119          s[i] += Map[i][j].ToString();
120          s[i] += " ";
121        }
[17134]122        if (this is EMMIslandMap island) {
123          s[i] += "  Average distance:" + island.AverageDistance[i].ToString();
124        }
[16899]125      }
126      return s;
127    }
[17134]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    }
[16899]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++) {
[17002]146        s[i] = "";
147        if (this is EMMIslandMap island) {
148          s[i] += island.ClusterNumber[i - 1].ToString() + ",";
149        }
150        s[i] += (i - 1).ToString() + ",";
[16899]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    }
[17002]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();
[16899]166    }
[17002]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;
[17134]178      HelpFunctions.SetLocalParametersForTree(random, 0.5, treeNode.Tree);
[17002]179    }
180    public virtual void MapUpDate(Dictionary<ISymbolicExpressionTree, double> population) { }
181    #endregion
[16722]182  }
183}
Note: See TracBrowser for help on using the repository browser.