- Timestamp:
- 06/03/19 14:15:11 (5 years ago)
- Location:
- branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM
- Property svn:ignore
-
old new 12 12 *.nuget.props 13 13 *.nuget.targets 14 Plugin.cs
-
- Property svn:ignore
-
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMBaseMap.cs
r16899 r17002 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 26 using HeuristicLab.Problems.DataAnalysis.Symbolic; 27 using HeuristicLab.Random; 27 28 using System.Collections.Generic; 28 29 using System.IO; … … 32 33 [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")] 33 34 public abstract class EMMMapBase<T> : Item where T : class { 35 #region data members 34 36 [Storable] 35 37 public List<T> ModelSet { get; set; } 36 38 [Storable] 37 public List<int> ClusterNumber { get; set; }38 [Storable]39 39 public List<List<int>> Map { get; set; } 40 [Storable] 41 public double[,] Distances { get; set; } 42 [Storable] 43 public int K { get; protected set; } 44 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 } 60 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 40 #endregion 41 #region constructors 72 42 [StorableConstructor] 73 43 protected EMMMapBase(StorableConstructorFlag _) : base(_) { } 74 protected EMMMapBase() : this(1) { } 75 public EMMMapBase(int k) { 76 K = k; 77 ClusterNumber = new List<int>(); 44 public EMMMapBase() { 78 45 Map = new List<List<int>>(); 79 46 } … … 84 51 else ModelSet = original.ModelSet.ToList(); /// check this if you want to use it 85 52 } 86 if (original.ClusterNumber != null) {87 ClusterNumber = original.ClusterNumber.ToList();88 }89 53 if (original.Map != null) { 90 54 Map = original.Map.Select(x => x.ToList()).ToList(); 91 55 } 92 K = original.K;93 56 } 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) { 57 #endregion 58 #region map creation functions 59 protected double[,] CalculateDistances() { 60 double[,] distances; 61 if (ModelSet is List<ISymbolicExpressionTree> set) { 62 distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(set, simplify: false, strict: true); 63 } else { /// for future work 64 distances = new double[ModelSet.Count, ModelSet.Count]; 65 for (int i = 0; i < ModelSet.Count - 1; i++) { 66 for (int j = 0; j <= i; j++) { 67 distances[i, j] = 0; 68 } 69 } 70 } 71 for (int i = 0; i < ModelSet.Count - 1; i++) { 72 for (int j = i + 1; j < ModelSet.Count; j++) { 73 distances[j, i] = distances[i, j] = 1 - distances[i, j]; 74 } 75 } 76 return distances; 77 } 78 public abstract void CreateMap(IRandom random, int k); 79 public void MapCreationPrepare(IEnumerable<T> trees) { 108 80 ModelSet = trees.ToList(); 109 CalculateDistances();110 CreateMap(random, k);111 81 } 82 83 protected void MapSizeCheck(int k) { 84 if (Map != null) Map.Clear(); 85 else Map = new List<List<int>>(); 86 if (Map.Count != k) { 87 if (Map.Count != 0) { 88 Map.Clear(); 89 } 90 for (int i = 0; i < k; i++) { 91 Map.Add(new List<int>()); 92 } 93 } 94 } 95 #endregion 96 #region map and files 112 97 public void MapRead(IRandom random, IEnumerable<T> trees, string fileName = "Map.txt") { 113 98 ModelSet = trees.ToList(); 114 99 MapFromFileRead(fileName); 115 K = Map.Count; 116 MapPreparation(); 100 if (this is EMMIslandMap island) { island.ClusterNumbersCalculate(); } 117 101 if (this is EMMNetworkMap one) { one.NeghboorNumber = Map[0].Count; } 118 102 } … … 126 110 fileName2 += s; 127 111 fileName2 += ".txt"; 128 File.WriteAllLines(fileName , MapToSee());112 File.WriteAllLines(fileName2, MapToSee()); 129 113 } 130 114 public string[] MapToString() { // Function that preapre Map to printing in .txt File: create a set of strings for future reading by computer 131 115 string[] s; 132 s = new string[ K];133 for (int i = 0; i < K; i++) {116 s = new string[Map.Count]; 117 for (int i = 0; i < Map.Count; i++) { 134 118 s[i] = ""; 135 119 for (int j = 0; j < Map[i].Count; j++) { … … 146 130 s[0] = "ClusterNumber" + "," + "ModelNumber" + "," + "Model"; 147 131 for (int i = 1; i < ((ModelSet.Count) + 1); i++) { 148 s[i] = ClusterNumber[i - 1].ToString() + "," + (i - 1).ToString() + ","; 132 s[i] = ""; 133 if (this is EMMIslandMap island) { 134 s[i] += island.ClusterNumber[i - 1].ToString() + ","; 135 } 136 s[i] += (i - 1).ToString() + ","; 149 137 if (ModelSet[i - 1] is ISymbolicExpressionTree model) { 150 138 s[i] += fmt.Format(model); … … 164 152 } 165 153 } 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>()); 154 #endregion 155 156 #region map use functions 157 public abstract T NewModelForInizializtionNotTree(IRandom random, out int treeNumber); 158 public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int treeNumber) { 159 treeNumber = random.Next(ModelSet.Count); 160 if (ModelSet[treeNumber] is ISymbolicExpressionTree model) 161 return (ISymbolicExpressionTree)(model.Clone()); 162 return new SymbolicExpressionTree(); 163 } 164 public void NodeManipulationForInizializtion(IRandom random, TreeModelTreeNode node) { 165 node.Tree = NewModelForInizializtion(random, out int treeNumber); 166 node.SetLocalParameters(random, 0.5); 167 node.TreeNumber = treeNumber; 168 } 169 public abstract ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber); 170 public virtual void NodeForMutationChange(IRandom random, TreeModelTreeNode treeNode) { 171 int treeNumber = treeNode.TreeNumber; 172 int treeNumber2 = treeNode.TreeNumber; 173 treeNode.Tree = new SymbolicExpressionTree(NewModelForMutation(random, out treeNumber, treeNumber2).Root); 174 treeNode.TreeNumber = treeNumber; 175 SetLocalParametersForTree(random, 0.5, treeNode.Tree); 176 } 177 public void SetLocalParametersForTree(IRandom random, double shakingFactor, ISymbolicExpressionTree tree) { 178 foreach (var node in tree.IterateNodesPrefix().Where(x => x.HasLocalParameters)) { 179 if (node is VariableTreeNode variableTreeNode) { 180 var symbol = variableTreeNode.Symbol; 181 variableTreeNode.Weight = NormalDistributedRandom.NextDouble(random, symbol.WeightManipulatorMu, symbol.WeightManipulatorSigma); 182 } else { 183 node.ResetLocalParameters(random); 175 184 } 176 185 } 177 186 } 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 } 187 public virtual void MapUpDate(Dictionary<ISymbolicExpressionTree, double> population) { } 188 #endregion 190 189 } 191 190 }
Note: See TracChangeset
for help on using the changeset viewer.