[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")]
|
---|
[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 | }
|
---|