[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 |
|
---|
| 22 | using HEAL.Attic;
|
---|
[16899] | 23 | using HeuristicLab.Common;
|
---|
[16722] | 24 | using HeuristicLab.Core;
|
---|
[16899] | 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[16722] | 27 | using System.Collections.Generic;
|
---|
[16899] | 28 | using System.IO;
|
---|
| 29 | using System.Linq;
|
---|
[16722] | 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
|
---|
[16734] | 32 | [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")]
|
---|
[16722] | 33 | public abstract class EMMMapBase<T> : Item where T : class {
|
---|
[16899] | 34 | [Storable]
|
---|
[16722] | 35 | public List<T> ModelSet { get; set; }
|
---|
[16899] | 36 | [Storable]
|
---|
[16722] | 37 | public List<int> ClusterNumber { get; set; }
|
---|
[16899] | 38 | [Storable]
|
---|
[16722] | 39 | public List<List<int>> Map { get; set; }
|
---|
[16899] | 40 | [Storable]
|
---|
[16722] | 41 | public double[,] Distances { get; set; }
|
---|
[16899] | 42 | [Storable]
|
---|
| 43 | public int K { get; protected set; }
|
---|
[16722] | 44 |
|
---|
[16899] | 45 | protected void CalculateDistances() {
|
---|
| 46 | if (ModelSet is List<ISymbolicExpressionTree> set) {
|
---|
| 47 | Distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(set, simplify: false, strict: true);
|
---|
| 48 | } else { /// for future work
|
---|
| 49 | for (int i = 0; i < ModelSet.Count - 1; i++) {
|
---|
| 50 | for (int j = 0; j <= i; j++) {
|
---|
| 51 | Distances[i, j] = 0;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | for (int i = 0; i < ModelSet.Count - 1; i++) {
|
---|
| 56 | for (int j = i + 1; j < ModelSet.Count; j++) {
|
---|
| 57 | Distances[j, i] = Distances[i, j] = 1 - Distances[i, j];
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
[16722] | 60 |
|
---|
[16899] | 61 | }
|
---|
| 62 | public abstract void CreateMap(IRandom random, int k);
|
---|
| 63 | public abstract T NewModelForInizializtionNotTree(IRandom random, out int cluster, out int treeNumber);
|
---|
| 64 | public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber) {
|
---|
| 65 | treeNumber = random.Next(ModelSet.Count);
|
---|
| 66 | cluster = ClusterNumber[treeNumber];
|
---|
| 67 | if (ModelSet[treeNumber] is ISymbolicExpressionTree model)
|
---|
| 68 | return (ISymbolicExpressionTree)(model.Clone());
|
---|
| 69 | return new SymbolicExpressionTree();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[16722] | 72 | [StorableConstructor]
|
---|
| 73 | protected EMMMapBase(StorableConstructorFlag _) : base(_) { }
|
---|
[16899] | 74 | protected EMMMapBase() : this(1) { }
|
---|
| 75 | public EMMMapBase(int k) {
|
---|
| 76 | K = k;
|
---|
| 77 | ClusterNumber = new List<int>();
|
---|
| 78 | Map = new List<List<int>>();
|
---|
| 79 | }
|
---|
| 80 | public EMMMapBase(EMMMapBase<T> original, Cloner cloner) {
|
---|
| 81 | if (original.ModelSet != null) {
|
---|
| 82 | if (original.ModelSet is List<ISymbolicExpressionTree> originalSet && ModelSet is List<ISymbolicExpressionTree> set)
|
---|
| 83 | set = originalSet.Select(cloner.Clone).ToList();
|
---|
| 84 | else ModelSet = original.ModelSet.ToList(); /// check this if you want to use it
|
---|
| 85 | }
|
---|
| 86 | if (original.ClusterNumber != null) {
|
---|
| 87 | ClusterNumber = original.ClusterNumber.ToList();
|
---|
| 88 | }
|
---|
| 89 | if (original.Map != null) {
|
---|
| 90 | Map = original.Map.Select(x => x.ToList()).ToList();
|
---|
| 91 | }
|
---|
| 92 | K = original.K;
|
---|
| 93 | }
|
---|
| 94 | //public EMMMapBase(IRandom random, IEnumerable<T> trees, int k) : this(k) {
|
---|
| 95 | // // constructor that should be used in case of creation of a new map from the start point
|
---|
| 96 | // ModelSet = trees.ToList();
|
---|
| 97 | // CalculateDistances();
|
---|
| 98 | // CreateMap(random, k);
|
---|
| 99 | //}
|
---|
| 100 | //public EMMMapBase(IRandom random, IEnumerable<T> trees, string fileName = "Map.txt") : this(1) {
|
---|
| 101 | // // constructor that shoud be used in case of using of "old" map, that was previously created and now shoud be readed from file
|
---|
| 102 | // ModelSet = trees.ToList();
|
---|
| 103 | // MapFromFileRead(fileName);
|
---|
| 104 | // K = Map.Count;
|
---|
| 105 | // MapPreparation();
|
---|
| 106 | //}
|
---|
| 107 | public void MapCreationPrepare(IRandom random, IEnumerable<T> trees, int k) {
|
---|
| 108 | ModelSet = trees.ToList();
|
---|
| 109 | CalculateDistances();
|
---|
| 110 | CreateMap(random, k);
|
---|
| 111 | }
|
---|
| 112 | public void MapRead(IRandom random, IEnumerable<T> trees, string fileName = "Map.txt") {
|
---|
| 113 | ModelSet = trees.ToList();
|
---|
| 114 | MapFromFileRead(fileName);
|
---|
| 115 | K = Map.Count;
|
---|
| 116 | MapPreparation();
|
---|
| 117 | if (this is EMMNetworkMap one) { one.NeghboorNumber = Map[0].Count; }
|
---|
| 118 | }
|
---|
| 119 | public void WriteMapToTxtFile(IRandom random) {
|
---|
| 120 | string s = random.ToString();
|
---|
| 121 | string fileName = "Map";
|
---|
| 122 | fileName += s;
|
---|
| 123 | fileName += ".txt";
|
---|
| 124 | File.WriteAllLines(fileName, MapToString());
|
---|
| 125 | string fileName2 = "MapToSee";
|
---|
| 126 | fileName2 += s;
|
---|
| 127 | fileName2 += ".txt";
|
---|
| 128 | File.WriteAllLines(fileName, MapToSee());
|
---|
| 129 | }
|
---|
| 130 | public string[] MapToString() { // Function that preapre Map to printing in .txt File: create a set of strings for future reading by computer
|
---|
| 131 | string[] s;
|
---|
| 132 | s = new string[K];
|
---|
| 133 | for (int i = 0; i < K; i++) {
|
---|
| 134 | s[i] = "";
|
---|
| 135 | for (int j = 0; j < Map[i].Count; j++) {
|
---|
| 136 | s[i] += Map[i][j].ToString();
|
---|
| 137 | s[i] += " ";
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | return s;
|
---|
| 141 | }
|
---|
| 142 | public string[] MapToSee() { // Function that prepare Map to printing in .txt File: create a set of strings in human readable view
|
---|
| 143 | var fmt = new InfixExpressionFormatter();
|
---|
| 144 | string[] s;
|
---|
| 145 | s = new string[(ModelSet.Count) + 1];
|
---|
| 146 | s[0] = "ClusterNumber" + "," + "ModelNumber" + "," + "Model";
|
---|
| 147 | for (int i = 1; i < ((ModelSet.Count) + 1); i++) {
|
---|
| 148 | s[i] = ClusterNumber[i - 1].ToString() + "," + (i - 1).ToString() + ",";
|
---|
| 149 | if (ModelSet[i - 1] is ISymbolicExpressionTree model) {
|
---|
| 150 | s[i] += fmt.Format(model);
|
---|
| 151 | } else { s[i] += ModelSet[i - 1].ToString(); }
|
---|
| 152 | }
|
---|
| 153 | return s;
|
---|
| 154 | }
|
---|
| 155 | public void MapFromFileRead(string fileName) {
|
---|
| 156 | string input = File.ReadAllText(fileName);
|
---|
| 157 | int i = 0;
|
---|
| 158 | foreach (var row in input.Split('\n')) {
|
---|
| 159 | Map.Add(new List<int>());
|
---|
| 160 | foreach (var col in row.Trim().Split(' ')) {
|
---|
| 161 | Map[i].Add(int.Parse(col.Trim()));
|
---|
| 162 | }
|
---|
| 163 | i++;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | protected void MapSizeCheck() {
|
---|
| 167 | if (Map != null) Map.Clear();
|
---|
| 168 | else Map = new List<List<int>>();
|
---|
| 169 | if (Map.Count != K) {
|
---|
| 170 | if (Map.Count != 0) {
|
---|
| 171 | Map.Clear();
|
---|
| 172 | }
|
---|
| 173 | for (int i = 0; i < K; i++) {
|
---|
| 174 | Map.Add(new List<int>());
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | protected void MapPreparation() {
|
---|
| 179 | for (int i = 0; i < Map.Count; i++) {
|
---|
| 180 | for (int j = 0; j < Map[i].Count; j++) {
|
---|
| 181 | ClusterNumber.Add(0);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | for (int i = 0; i < Map.Count; i++) {
|
---|
| 185 | for (int j = 0; j < Map[i].Count; j++) {
|
---|
| 186 | ClusterNumber[Map[i][j]] = i;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
[16722] | 190 | }
|
---|
| 191 | }
|
---|