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 |
|
---|
22 | using HEAL.Attic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
27 | using System.Collections.Generic;
|
---|
28 | using System.IO;
|
---|
29 | using System.Linq;
|
---|
30 |
|
---|
31 | namespace 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 | }
|
---|