Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMapTreeModel.cs @ 16824

Last change on this file since 16824 was 16760, checked in by msemenki, 6 years ago

#2988: ADD:

  1. new type of mutation that respect node type and do not change trigonometric node to "add" node;
  2. variation of previous mutation type that do not change type of terminal node;
  3. some interface adjusting for comfort work with algorithm;
  4. second variant of map, that is not "island" map but provide "net" map.
File size: 6.1 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  [Item("TreeModelMap", "A map of models of models of models")]
33  [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")]
34  public class EMMMapTreeModel : EMMMapBase<ISymbolicExpressionTree> {
35    #region conctructors
36    [StorableConstructor]
37    protected EMMMapTreeModel(StorableConstructorFlag _) : base(_) { }
38    public EMMMapTreeModel() : this(1) { }
39    public EMMMapTreeModel(int k) {
40      K = k;
41      ModelSet = new List<ISymbolicExpressionTree>();
42      ClusterNumber = new List<int>();
43      Map = new List<List<int>>();
44    }
45    public EMMMapTreeModel(EMMMapTreeModel original, Cloner cloner) {
46      //original.ModelSet.ForEach(x => ModelSet.Add((ISymbolicExpressionTree)x.Clone(cloner)));
47      //original.ClusterNumber.ForEach(x => ClusterNumber.Add(x));
48      //original.Map.ForEach(x => Map.Add(x));
49      if (original.ModelSet != null) {
50        ModelSet = original.ModelSet.Select(cloner.Clone).ToList();
51      }
52      if (original.ClusterNumber != null) {
53        ClusterNumber = original.ClusterNumber.ToList();
54      }
55      if (original.Map != null) {
56        Map = original.Map.Select(x => x.ToList()).ToList();
57      }
58      K = original.K;
59    }
60    public EMMMapTreeModel(IRandom random, IEnumerable<ISymbolicExpressionTree> trees, int k, int neghboorNumber) : this(k) {
61      ModelSet = trees.ToList();
62      CalculateDistances();
63      if (k < ModelSet.Count)
64        CreateIslandMap(random, k);
65      else if (k == ModelSet.Count) {
66        CreateFullConnectedMap(random, k, neghboorNumber);
67      } else {
68        k -= ModelSet.Count;
69        CreateIslandMap(random, k);
70      }
71    }
72    public EMMMapTreeModel(IRandom random, IEnumerable<ISymbolicExpressionTree> trees) : this(1) {
73      ModelSet = trees.ToList();
74      string input = File.ReadAllText("Map.txt");
75
76      int i = 0;
77      foreach (var row in input.Split('\n')) {
78        Map.Add(new List<int>());
79        foreach (var col in row.Trim().Split(' ')) {
80          Map[i].Add(int.Parse(col.Trim()));
81        }
82        i++;
83      }
84      K = Map.Count;
85      MapPreparation();
86    }
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new EMMMapTreeModel(this, cloner);
89    }
90    #endregion
91    #region MapTransformation
92    override protected void CalculateDistances() {
93      Distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(ModelSet, simplify: false, strict: true);
94      for (int i = 0; i < ModelSet.Count - 1; i++) {
95        for (int j = i + 1; j < ModelSet.Count; j++) {
96          Distances[j, i] = Distances[i, j] = 1 - Distances[i, j];
97        }
98      }
99    }
100    protected void CreateFullConnectedMap(IRandom random, int k, int neghboorNumber) {
101      EMModelsClusterizationAlgorithm.ApplyFullConectedMapCreationAlgorithm(random, Distances, Map, k, neghboorNumber);
102      K = k;
103      for (int i = 0; i < Map.Count; i++) {
104        ClusterNumber.Add(i);
105      }
106    }
107    override protected void CreateIslandMap(IRandom random, int k) {
108      //Clusterization
109      K = EMModelsClusterizationAlgorithm.ApplyClusteringAlgorithm(random, Distances, ClusterNumber, k);
110      // Cheking a Map size
111      if (Map != null) Map.Clear();
112      else Map = new List<List<int>>();
113      if (Map.Count != K) {
114        if (Map.Count != 0) {
115          Map.Clear();
116        }
117        for (int i = 0; i < K; i++) {
118          Map.Add(new List<int>());
119        }
120      }
121      // Map fulfilment
122      for (int i = 0; i < ModelSet.Count; i++) {
123        Map[ClusterNumber[i]].Add(i);
124      }
125    }
126    protected void MapPreparation() {
127      for (int i = 0; i < Map.Count; i++) {
128        for (int j = 0; j < Map[i].Count; j++) {
129          ClusterNumber.Add(0);
130        }
131      }
132      for (int i = 0; i < Map.Count; i++) {
133        for (int j = 0; j < Map[i].Count; j++) {
134          ClusterNumber[Map[i][j]] = i;
135        }
136      }
137    }
138    #endregion
139    #region Dialog with surroudings
140    override public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber) {
141      treeNumber = random.Next(ModelSet.Count);
142      cluster = ClusterNumber[treeNumber];
143      return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
144    }
145    public string[] MapToString() {
146      string[] s;
147      s = new string[K];
148      for (int i = 0; i < K; i++) {
149        s[i] = "";
150        for (int j = 0; j < Map[i].Count; j++) {
151          s[i] += Map[i][j].ToString();
152          s[i] += " ";
153        }
154      }
155      return s;
156    }
157    public string[] MapToSee() {
158      var fmt = new InfixExpressionFormatter();
159      string[] s;
160      s = new string[(ModelSet.Count) + 1];
161      s[0] = "ClusterNumber" + "," + "Modfelnumber" + "," + "Model";
162      for (int i = 1; i < ((ModelSet.Count) + 1); i++) {
163        s[i] = ClusterNumber[i - 1].ToString() + "," + (i - 1).ToString() + "," + fmt.Format(ModelSet[i - 1]);
164      }
165      return s;
166    }
167
168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.