[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;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 27 | using System.Collections.Generic;
|
---|
[16734] | 28 | using System.IO;
|
---|
[16722] | 29 | using System.Linq;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
|
---|
| 32 | [Item("TreeModelMap", "A map of models of models of models")]
|
---|
[16734] | 33 | [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")]
|
---|
[16722] | 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) : this(k) {
|
---|
| 61 | ModelSet = trees.ToList();
|
---|
| 62 | CalculateDistances();
|
---|
| 63 | CreateMap(random, k);
|
---|
| 64 | }
|
---|
[16734] | 65 | public EMMMapTreeModel(IRandom random, IEnumerable<ISymbolicExpressionTree> trees) : this(1) {
|
---|
| 66 | ModelSet = trees.ToList();
|
---|
| 67 | string input = File.ReadAllText("Map.txt");
|
---|
| 68 |
|
---|
| 69 | int i = 0;
|
---|
| 70 | foreach (var row in input.Split('\n')) {
|
---|
| 71 | Map.Add(new List<int>());
|
---|
| 72 | foreach (var col in row.Trim().Split(' ')) {
|
---|
| 73 | Map[i].Add(int.Parse(col.Trim()));
|
---|
| 74 | }
|
---|
| 75 | i++;
|
---|
| 76 | }
|
---|
| 77 | K = Map.Count;
|
---|
| 78 | MapPreparation();
|
---|
| 79 | }
|
---|
[16722] | 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 81 | return new EMMMapTreeModel(this, cloner);
|
---|
| 82 | }
|
---|
| 83 | #endregion
|
---|
| 84 | #region MapTransformation
|
---|
| 85 | override protected void CalculateDistances() {
|
---|
| 86 | Distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(ModelSet, simplify: false, strict: true);
|
---|
| 87 | for (int i = 0; i < ModelSet.Count - 1; i++) {
|
---|
| 88 | for (int j = i + 1; j < ModelSet.Count; j++) {
|
---|
| 89 | Distances[j, i] = Distances[i, j] = 1 - Distances[i, j];
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[16734] | 93 | override protected void CreateMap(IRandom random, int k) {
|
---|
[16722] | 94 | K = k;
|
---|
| 95 | //Clusterization
|
---|
| 96 | EMModelsClusterizationAlgorithm clusteringAlgorithm = new EMModelsClusterizationAlgorithm(K);
|
---|
| 97 | K = clusteringAlgorithm.Apply(random, Distances, ClusterNumber);
|
---|
| 98 | // Cheking a Map size
|
---|
| 99 | if (Map != null) Map.Clear();
|
---|
| 100 | else Map = new List<List<int>>();
|
---|
| 101 | if (Map.Count != K) {
|
---|
| 102 | if (Map.Count != 0) {
|
---|
| 103 | Map.Clear();
|
---|
| 104 | }
|
---|
| 105 | for (int i = 0; i < K; i++) {
|
---|
| 106 | Map.Add(new List<int>());
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | // Map fulfilment
|
---|
| 110 | for (int i = 0; i < ModelSet.Count; i++) {
|
---|
| 111 | Map[ClusterNumber[i]].Add(i);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[16734] | 114 | protected void MapPreparation() {
|
---|
| 115 | for (int i = 0; i < Map.Count; i++) {
|
---|
| 116 | for (int j = 0; j < Map[i].Count; j++) {
|
---|
| 117 | ClusterNumber.Add(0);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | for (int i = 0; i < Map.Count; i++) {
|
---|
| 121 | for (int j = 0; j < Map[i].Count; j++) {
|
---|
| 122 | ClusterNumber[Map[i][j]] = i;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[16722] | 126 | #endregion
|
---|
| 127 | #region Dialog with surroudings
|
---|
| 128 | override public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber) {
|
---|
| 129 | treeNumber = random.Next(ModelSet.Count);
|
---|
| 130 | cluster = ClusterNumber[treeNumber];
|
---|
| 131 | return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
|
---|
| 132 | }
|
---|
[16734] | 133 | public string[] MapToString() {
|
---|
| 134 | string[] s;
|
---|
| 135 | s = new string[K];
|
---|
| 136 | for (int i = 0; i < K; i++) {
|
---|
| 137 | s[i] = "";
|
---|
| 138 | for (int j = 0; j < Map[i].Count; j++) {
|
---|
| 139 | s[i] += Map[i][j].ToString();
|
---|
| 140 | s[i] += " ";
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | return s;
|
---|
| 144 | }
|
---|
| 145 | public string[] MapToSee() {
|
---|
| 146 | var fmt = new InfixExpressionFormatter();
|
---|
| 147 | string[] s;
|
---|
| 148 | s = new string[(ModelSet.Count) + 1];
|
---|
| 149 | s[0] = "ClusterNumber" + "," + "Modfelnumber" + "," + "Model";
|
---|
| 150 | for (int i = 1; i < ((ModelSet.Count) + 1); i++) {
|
---|
| 151 | s[i] = ClusterNumber[i - 1].ToString() + "," + (i - 1).ToString() + "," + fmt.Format(ModelSet[i - 1]);
|
---|
| 152 | }
|
---|
| 153 | return s;
|
---|
| 154 | }
|
---|
[16722] | 155 |
|
---|
| 156 | #endregion
|
---|
| 157 | }
|
---|
| 158 | }
|
---|