- Timestamp:
- 07/11/19 16:30:22 (5 years ago)
- Location:
- branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMBaseMap.cs
r17133 r17134 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 26 using HeuristicLab.Problems.DataAnalysis.Symbolic; 27 using HeuristicLab.Random;28 27 using System.Collections.Generic; 29 28 using System.IO; … … 32 31 namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { 33 32 [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")] 34 public abstract class EMMMapBase<T> : Item where T : class {33 public abstract class EMMMapBase<T> : ParameterizedNamedItem where T : class { 35 34 #region data members 36 35 [Storable] 37 public List<T> ModelSet { get; set; }36 public List<T> ModelSet { get; protected set; } 38 37 [Storable] 39 38 public List<List<int>> Map { get; set; } 39 public string DistanceParametr { get; set; } 40 40 #endregion 41 41 #region constructors … … 44 44 public EMMMapBase() { 45 45 Map = new List<List<int>>(); 46 DistanceParametr = "Symbolic"; 46 47 } 47 public EMMMapBase(EMMMapBase<T> original, Cloner cloner) {48 public EMMMapBase(EMMMapBase<T> original, Cloner cloner) : base(original, cloner) { 48 49 if (original.ModelSet != null) { 49 50 if (original.ModelSet is List<ISymbolicExpressionTree> originalSet && ModelSet is List<ISymbolicExpressionTree> set) … … 54 55 Map = original.Map.Select(x => x.ToList()).ToList(); 55 56 } 57 DistanceParametr = original.DistanceParametr; 56 58 } 57 59 #endregion 58 60 #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); 61 62 public abstract void CreateMap(IRandom random); 79 63 public void MapCreationPrepare(IEnumerable<T> trees) { 80 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); 81 77 } 82 78 … … 95 91 #endregion 96 92 #region map and files 97 public v oid MapRead(IRandom random, IEnumerable<T> trees, string fileName = "Map.txt") {93 public virtual void MapRead(IEnumerable<T> trees) { 98 94 ModelSet = trees.ToList(); 99 MapFromFileRead(fileName);100 if (this is EMMIslandMap island) { island.ClusterNumbersCalculate(); }101 if (this is EMMNetworkMap one) { one.NeghboorNumber = Map[0].Count; }102 95 } 103 96 public void WriteMapToTxtFile(IRandom random) { 104 string s = random. ToString();105 string fileName = "Map ";97 string s = random.NextDouble().ToString(); 98 string fileName = "MapToAnalize"; 106 99 fileName += s; 100 fileName += DistanceParametr; 107 101 fileName += ".txt"; 108 102 File.WriteAllLines(fileName, MapToString()); 109 103 string fileName2 = "MapToSee"; 110 104 fileName2 += s; 105 fileName2 += DistanceParametr; 111 106 fileName2 += ".txt"; 112 107 File.WriteAllLines(fileName2, MapToSee()); 108 string fileName3 = "Map"; 109 fileName3 += DistanceParametr; 110 fileName3 += ".txt"; 111 File.WriteAllLines(fileName3, MapToStoreInFile()); 113 112 } 114 public string[] MapToString() { // Function that preapre Map to printing in .txt File: create a set of strings for future reading by computer 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 115 129 string[] s; 116 130 s = new string[Map.Count]; … … 119 133 for (int j = 0; j < Map[i].Count; j++) { 120 134 s[i] += Map[i][j].ToString(); 121 s[i] += " ";135 if (j != (Map[i].Count - 1)) { s[i] += " "; } 122 136 } 123 137 } … … 140 154 } 141 155 return s; 142 }143 public void MapFromFileRead(string fileName) {144 string input = File.ReadAllText(fileName);145 int i = 0;146 foreach (var row in input.Split('\n')) {147 Map.Add(new List<int>());148 foreach (var col in row.Trim().Split(' ')) {149 Map[i].Add(int.Parse(col.Trim()));150 }151 i++;152 }153 156 } 154 157 #endregion … … 173 176 treeNode.Tree = new SymbolicExpressionTree(NewModelForMutation(random, out treeNumber, treeNumber2).Root); 174 177 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); 184 } 185 } 178 HelpFunctions.SetLocalParametersForTree(random, 0.5, treeNode.Tree); 186 179 } 187 180 public virtual void MapUpDate(Dictionary<ISymbolicExpressionTree, double> population) { }
Note: See TracChangeset
for help on using the changeset viewer.