- Timestamp:
- 07/11/19 16:30:22 (5 years ago)
- Location:
- branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps
- Files:
-
- 1 added
- 7 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) { } -
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMDistanceMap.cs
r17133 r17134 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Problems.DataAnalysis.Symbolic; 26 27 using System; 27 28 using System.Collections.Generic; 29 using System.Linq; 28 30 29 31 namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { … … 33 35 [Storable] 34 36 public List<List<double>> Probabilities { get; set; } 35 #region con ctructors37 #region constructors 36 38 [StorableConstructor] 37 39 protected EMMDisatanceMap(StorableConstructorFlag _) : base(_) { } … … 39 41 return new EMMDisatanceMap(this, cloner); 40 42 } 41 public EMMDisatanceMap() : base() { ModelSet = new List<ISymbolicExpressionTree>(); } 42 public EMMDisatanceMap(EMMDisatanceMap original, Cloner cloner) : base(original, cloner) { } 43 public EMMDisatanceMap() : base() { 44 ModelSet = new List<ISymbolicExpressionTree>(); 45 Probabilities = new List<List<double>>(); 46 } 47 public EMMDisatanceMap(EMMDisatanceMap original, Cloner cloner) : base(original, cloner) { 48 if (original.Probabilities != null) { 49 Probabilities = original.Probabilities.Select(x => x.ToList()).ToList(); 50 } 51 } 43 52 #endregion 44 #region MapCreation 45 override public void CreateMap(IRandom random, int k) { 46 47 Probabilities = new List<List<double>>(); 53 #region Map Creation 54 override public void CreateMap(IRandom random) { 48 55 MapSizeCheck(ModelSet.Count); 49 ApplyDistanceMapCreationAlgorithm(random, CalculateDistances(), Map, Probabilities);56 ApplyDistanceMapCreationAlgorithm(random, ModelSetPreparation.CalculateDistances(ModelSet), Map, Probabilities); 50 57 } 51 public static void ApplyDistanceMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, List<List<double>> probabilities) { 58 override public string[] MapToStoreInFile() { // Function that prepare Map to printing in .txt File: create a set of strings for future reading by computer 59 string[] s; 60 s = new string[Map.Count]; 61 for (int i = 0; i < Map.Count; i++) { 62 s[i] = ""; 63 for (int j = 0; j < (Map.Count - 1); j++) { 64 s[i] += Probabilities[i][j].ToString(); 65 if (j != (Map.Count - 2)) { s[i] += " "; } 66 } 67 } 68 return s; 69 } 70 override public void MapRead(IEnumerable<ISymbolicExpressionTree> trees) { 71 base.MapRead(trees); 72 MapFullment(trees.Count()); 73 string fileName = ("Map" + DistanceParametr + ".txt"); 74 Probabilities = FileComuncations.DoubleMatrixFromFileRead(fileName); 75 } 76 override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) { 77 MapSizeCheck(ModelSet.Count); 78 if (Map != null) { 79 Map.Clear(); 80 } 81 ApplyDistanceMapCreationAlgorithm(random, ModelSetPreparation.DistanceMatrixCalculation(ModelSet, DistanceParametr, problem), Map, Probabilities); 82 } 83 protected void ApplyDistanceMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, List<List<double>> probabilities) { 52 84 int mapSize = distances.GetLength(0); 53 85 for (int t = 0; t < mapSize; t++) { … … 65 97 } 66 98 } 99 protected void MapFullment(int mapSize) { 100 if (Map != null) { 101 Map.Clear(); 102 } 103 for (int t = 0; t < mapSize; t++) { 104 for (int i = 0; i < mapSize; i++) { 105 if (i == t) 106 continue; 107 Map[t].Add(i); 108 } 109 } 110 } 67 111 #endregion 68 #region Map ApplayFunctions112 #region Map Apply Functions 69 113 public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) { 70 114 treeNumber = HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities[parentTreeNumber]); -
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMIslandMap.cs
r17133 r17134 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 25 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Parameters; 28 using HeuristicLab.Problems.DataAnalysis.Symbolic; 26 29 using HeuristicLab.Random; 27 30 using System.Collections.Generic; … … 32 35 [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")] 33 36 public class EMMIslandMap : EMMMapBase<ISymbolicExpressionTree> { 37 34 38 [Storable] 35 39 public List<int> ClusterNumber { get; set; } // May be only Island Map really need it 36 #region conctructors 40 public double[] AverageDistance { get; private set; } 41 private const string ClusterNumbersParameterName = "ClusterNumbers"; 42 private const string ClusterNumbersShowParameterName = "ClusterNumbersShow"; 43 public IValueParameter<IntValue> ClusterNumbersParameter { 44 get { return (IValueParameter<IntValue>)Parameters[ClusterNumbersParameterName]; } 45 } 46 public IValueParameter<IntValue> ClusterNumbersShowParameter { 47 get { return (IValueParameter<IntValue>)Parameters[ClusterNumbersShowParameterName]; } 48 } 49 public IntValue ClusterNumbers { 50 get { return ClusterNumbersParameter.Value; } 51 set { ClusterNumbersParameter.Value = value; } 52 } 53 public IntValue ClusterNumbersShow { 54 get { return ClusterNumbersShowParameter.Value; } 55 set { ClusterNumbersShowParameter.Value = value; } 56 } 57 #region constructors 37 58 [StorableConstructor] 38 59 protected EMMIslandMap(StorableConstructorFlag _) : base(_) { } 39 60 public EMMIslandMap() { 61 Parameters.Add(new ValueParameter<IntValue>(ClusterNumbersParameterName, "The number of clusters for model Map.", new IntValue(10))); 62 Parameters.Add(new ValueParameter<IntValue>(ClusterNumbersShowParameterName, "The number of clusters for model Map.", new IntValue(10))); 40 63 ModelSet = new List<ISymbolicExpressionTree>(); 41 64 ClusterNumber = new List<int>(); … … 50 73 } 51 74 #endregion 52 #region MapApplayFunctions 53 override public void CreateMap(IRandom random, int k) { 54 55 k = EMModelsClusterizationAlgorithm.ApplyClusteringAlgorithm(random, CalculateDistances(), ClusterNumber, k); 56 MapSizeCheck(k); 75 #region Map Apply Functions 76 override public void CreateMap(IRandom random) { 77 var totalDistance = ModelSetPreparation.CalculateDistances(ModelSet); //structure distances 78 CreateMap(random, totalDistance); 79 } 80 override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) { 81 CreateMap(random, ModelSetPreparation.TotalDistanceMatrixCalculation(random, problem, ModelSet, DistanceParametr)); 82 } 83 override public void CreateMap(IRandom random, double[,] totalDistance) { 84 if (Map != null) { 85 Map.Clear(); 86 } 87 ClusterNumbersShow.Value = KMeansClusterizationAlgorithm.ApplyClusteringAlgorithm(random, totalDistance, ClusterNumber, ClusterNumbers.Value); 88 MapSizeCheck(ClusterNumbersShow.Value); 57 89 for (int i = 0; i < ModelSet.Count; i++) { 58 90 Map[ClusterNumber[i]].Add(i); 59 91 } 92 AverageDistanceInClusterCalculation(totalDistance, ClusterNumbersShow.Value); 93 } 94 override public void MapRead(IEnumerable<ISymbolicExpressionTree> trees) { 95 base.MapRead(trees); 96 string fileName = ("Map" + DistanceParametr + ".txt"); 97 Map = FileComuncations.IntMatrixFromFileRead(fileName); 98 ClusterNumbers.Value = Map.Count; 99 ClusterNumbersShow.Value = ClusterNumbers.Value; 100 ClusterNumbersCalculate(); 101 AverageDistanceInClusterCalculation(ModelSetPreparation.CalculateDistances(ModelSet), Map.Count); 60 102 } 61 103 override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) { 62 104 return NewModelForInizializtion(random, out treeNumber); 63 105 } 64 106 private void AverageDistanceInClusterCalculation(double[,] distances, int k) { 107 AverageDistance = new double[k]; 108 var temp = new List<double>(); 109 for (int i = 0; i < k; i++) { 110 KMeansClusterizationAlgorithm.AverageClusterDistanceCalculation(temp, distances, ClusterNumber, ClusterNumber.Count, i); 111 var number = HelpFunctions.ChooseMinElementIndex(temp); 112 AverageDistance[i] = temp[number] / Map[i].Count; 113 temp.Clear(); 114 } 115 } 65 116 public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) { 66 117 if (parentTreeNumber == -10) { … … 71 122 return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone(); 72 123 } 73 public void ClusterNumbersCalculate() { // May be it should be transported to Child Clase (IslandMap)124 public void ClusterNumbersCalculate() { 74 125 for (int i = 0; i < Map.Count; i++) { 75 126 for (int j = 0; j < Map[i].Count; j++) { -
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMNetworkMap.cs
r17133 r17134 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 25 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Parameters; 28 using HeuristicLab.Problems.DataAnalysis.Symbolic; 26 29 using HeuristicLab.Random; 30 using System; 27 31 using System.Collections.Generic; 28 32 … … 31 35 [StorableType("C200ECC2-6D33-4468-A538-580B07D75B3C")] 32 36 public class EMMNetworkMap : EMMMapBase<ISymbolicExpressionTree> { 33 public int NeghboorNumber { get; set; } 34 #region conctructors 37 private const string NegbourTypeParameterName = "NegbourType"; 38 private const string NegbourNumberParameterName = "NegbourNumber"; 39 public IFixedValueParameter<StringValue> NegbourTypeParameter { 40 get { return (IFixedValueParameter<StringValue>)Parameters[NegbourTypeParameterName]; } 41 } 42 public IValueParameter<IntValue> NegbourNumberParameter { 43 get { return (IValueParameter<IntValue>)Parameters[NegbourNumberParameterName]; } 44 } 45 public StringValue NegbourType { 46 get { return NegbourTypeParameter.Value; } 47 set { NegbourTypeParameter.Value.Value = value.Value; } 48 } 49 public IntValue NegbourNumber { 50 get { return NegbourNumberParameter.Value; } 51 set { NegbourNumberParameter.Value.Value = value.Value; } 52 } 53 #region constructors 35 54 [StorableConstructor] 36 55 protected EMMNetworkMap(StorableConstructorFlag _) : base(_) { } 37 public EMMNetworkMap() : this(1) { } 56 38 57 public override IDeepCloneable Clone(Cloner cloner) { 39 58 return new EMMNetworkMap(this, cloner); 40 59 } 41 public EMMNetworkMap(int neghboorNumber = 10) : base() { 42 NeghboorNumber = neghboorNumber; 60 public EMMNetworkMap() : base() { 61 Parameters.Add(new ValueParameter<IntValue>(NegbourNumberParameterName, "The parameter for FullMap type of map creation algorithm. Use one from: 10, 20.", new IntValue(10))); 62 Parameters.Add(new FixedValueParameter<StringValue>(NegbourTypeParameterName, "The parameter for FullMap type of map creation algorithm. Use one from: Percent, Number.", new StringValue("Number"))); 63 MapParameterUpdate(); 64 ModelSet = new List<ISymbolicExpressionTree>(); 43 65 } 44 public EMMNetworkMap(EMMNetworkMap original, Cloner cloner) : base(original, cloner) { Neg hboorNumber = original.NeghboorNumber; }66 public EMMNetworkMap(EMMNetworkMap original, Cloner cloner) : base(original, cloner) { NegbourNumber = original.NegbourNumber; } 45 67 #endregion 46 #region MapTransformation 47 override public void CreateMap(IRandom random, int k) { 48 ApplyNetworkMapCreationAlgorithm(random, CalculateDistances(), Map, NeghboorNumber); 68 #region Map Transformation 69 override public void CreateMap(IRandom random) { 70 MapParameterUpdate(); 71 if (Map != null) { 72 Map.Clear(); 73 } 74 ApplyNetworkMapCreationAlgorithm(random, ModelSetPreparation.CalculateDistances(ModelSet), Map, NegbourNumber.Value); 75 } 76 override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) { 77 MapParameterUpdate(); 78 if (Map != null) { 79 Map.Clear(); 80 } 81 ApplyNetworkMapCreationAlgorithm(random, ModelSetPreparation.DistanceMatrixCalculation(ModelSet, DistanceParametr, problem), Map, NegbourNumber.Value); 82 } 83 override public void MapRead(IEnumerable<ISymbolicExpressionTree> trees) { 84 base.MapRead(trees); 85 string fileName = ("Map" + DistanceParametr + ".txt"); 86 Map = FileComuncations.IntMatrixFromFileRead(fileName); 87 NegbourNumber.Value = Map[0].Count; 49 88 } 50 89 public static void ApplyNetworkMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, int neghboorNumber = 10) { … … 63 102 } 64 103 } 104 protected void MapParameterUpdate() { 105 switch (NegbourType.Value) { 106 case "Percent": NegbourNumber.Value = Convert.ToInt32((Convert.ToDouble(ModelSet.Count)) * (Convert.ToDouble(NegbourNumber.Value)) / 100.0); break; 107 case "Number": NegbourNumber.Value = NegbourNumber.Value; break; 108 default: NegbourNumber.Value = NegbourNumber.Value; break; 109 } 110 } 65 111 #endregion 66 #region Dialog with surrou dings112 #region Dialog with surroundings 67 113 public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) { 68 114 treeNumber = Map[parentTreeNumber].SampleRandom(random); -
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMRankMap.cs
r17133 r17134 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Problems.DataAnalysis.Symbolic; 26 27 using System; 27 28 using System.Collections.Generic; 29 using System.Linq; 28 30 29 31 namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { 30 32 [Item("RankMap", "A map of models of models of models")] 31 33 [StorableType("1D4DD90E-553A-46DB-B0CD-6A899AA0B6D0")] 32 public class EMMRankMap : EMMMapBase<ISymbolicExpressionTree> { 34 public class EMMRankMap : EMMMapBase<ISymbolicExpressionTree> { // it do not work absolutely 33 35 [Storable] 34 36 public List<List<double>> Probabilities { get; set; } 35 #region con ctructors37 #region constructors 36 38 [StorableConstructor] 37 39 protected EMMRankMap(StorableConstructorFlag _) : base(_) { } … … 39 41 return new EMMRankMap(this, cloner); 40 42 } 41 public EMMRankMap() : base() { ModelSet = new List<ISymbolicExpressionTree>(); } 42 public EMMRankMap(EMMRankMap original, Cloner cloner) : base(original, cloner) { } 43 public EMMRankMap() : base() { 44 ModelSet = new List<ISymbolicExpressionTree>(); 45 Probabilities = new List<List<double>>(); 46 } 47 public EMMRankMap(EMMRankMap original, Cloner cloner) : base(original, cloner) { 48 if (original.Probabilities != null) { 49 Probabilities = original.Probabilities.Select(x => x.ToList()).ToList(); 50 } 51 } 43 52 #endregion 44 53 #region MapCreation 45 override public void CreateMap(IRandom random, int k) { 46 47 Probabilities = new List<List<double>>(); 54 override public void CreateMap(IRandom random) { 48 55 MapSizeCheck(ModelSet.Count); 49 ApplyRankMapCreationAlgorithm( random, CalculateDistances(), Map, Probabilities);56 ApplyRankMapCreationAlgorithm(ModelSetPreparation.CalculateDistances(ModelSet), Map, Probabilities); 50 57 } 51 public static void ApplyRankMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, List<List<double>> probabilities) { 58 override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) { 59 MapSizeCheck(ModelSet.Count); 60 ApplyRankMapCreationAlgorithm(ModelSetPreparation.DistanceMatrixCalculation(ModelSet, DistanceParametr, problem), Map, Probabilities); 61 } 62 override public void MapRead(IEnumerable<ISymbolicExpressionTree> trees) { 63 base.MapRead(trees); 64 MapFullment(trees.Count()); 65 string fileName = ("Map" + DistanceParametr + ".txt"); 66 Probabilities = FileComuncations.DoubleMatrixFromFileRead(fileName); 67 } 68 protected void MapFullment(int mapSize) { 69 if (Map != null) { 70 Map.Clear(); 71 } 72 for (int t = 0; t < mapSize; t++) { 73 for (int i = 0; i < mapSize; i++) { 74 if (i == t) 75 continue; 76 Map[t].Add(i); 77 } 78 } 79 } 80 override public string[] MapToStoreInFile() { // Function that prepare Map to printing in .txt File: create a set of strings for future reading by computer 81 string[] s; 82 s = new string[Map.Count]; 83 for (int i = 0; i < Map.Count; i++) { 84 s[i] = ""; 85 for (int j = 0; j < (Map.Count - 1); j++) { 86 s[i] += Probabilities[i][j].ToString(); 87 if (j != (Map.Count - 2)) { s[i] += " "; } 88 } 89 } 90 return s; 91 } 92 public static void ApplyRankMapCreationAlgorithm(double[,] distances, List<List<int>> map, List<List<double>> probabilities) { 52 93 int mapSize = distances.GetLength(0); 53 94 double tempSum = 0; … … 64 105 currentList[i].Add(distances[i, t]); 65 106 } 66 currentList.Sort((a, b) => a[1].CompareTo(b[1])); /// загадкой является то нафига оно мне вообще понадобилось in DistanceMap.. но это работающая сортировка.107 currentList.Sort((a, b) => a[1].CompareTo(b[1])); ///workable sorting 67 108 for (int i = 0; i < currentList.Count; i++) { 68 109 currentList[i].Add(currentList.Count - i); … … 79 120 } 80 121 #endregion 81 #region Map ApplayFunctions122 #region Map Apply Functions 82 123 public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) { 83 124 treeNumber = Map[parentTreeNumber][HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities[parentTreeNumber])]; -
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMSucsessMap.cs
r17133 r17134 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Problems.DataAnalysis.Symbolic;26 25 using System.Collections.Generic; 27 using System.Linq;28 26 29 27 namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { … … 32 30 public class EMMSucsessMap : EMMMapBase<ISymbolicExpressionTree> { 33 31 [Storable] 34 public List<double> Probabilities { get; private set; } 35 [Storable] 36 public List<List<double>> SucsessStatistics { get; private set; } 37 #region conctructors 32 public SelfConfiguration SelfConfigurationMechanism { get; private set; } 33 #region constructors 38 34 [StorableConstructor] 39 35 protected EMMSucsessMap(StorableConstructorFlag _) : base(_) { } … … 43 39 public EMMSucsessMap() : base() { 44 40 ModelSet = new List<ISymbolicExpressionTree>(); 45 S ucsessStatistics = new List<List<double>>();41 SelfConfigurationMechanism = new SelfConfiguration(); 46 42 } 47 43 public EMMSucsessMap(EMMSucsessMap original, Cloner cloner) : base(original, cloner) { 48 S ucsessStatistics = original.SucsessStatistics.Select(x => x.ToList()).ToList();44 SelfConfigurationMechanism = new SelfConfiguration(original.SelfConfigurationMechanism, cloner); 49 45 } 50 46 #endregion 51 #region Map Creation52 override public void CreateMap(IRandom random , int k) {53 54 Probabilities = new List<double>();55 Map.Clear();47 #region Map Creation 48 override public void CreateMap(IRandom random) { 49 if (Map != null) { 50 Map.Clear(); 51 } 56 52 Map.Add(new List<int>()); 57 53 MapSizeCheck(ModelSet.Count); 58 ApplySucsessMapCreationAlgorithm(random, CalculateDistances(), Map, Probabilities, SucsessStatistics);54 ApplySucsessMapCreationAlgorithm(random, ModelSet.Count); 59 55 } 60 public static void ApplySucsessMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, List<double> probabilities, List<List<double>> sucsessStatistics) { 61 int mapSize = distances.GetLength(0); 56 override public void MapRead(IEnumerable<ISymbolicExpressionTree> trees) { 57 base.MapRead(trees); 58 string fileName = ("Map" + DistanceParametr + ".txt"); 59 SelfConfigurationMechanism.ReadFromFile(ModelSet.Count, fileName); 60 } 61 override public string[] MapToStoreInFile() { // Function that prepare Map to printing in .txt File: create a set of strings for future reading by computer 62 string[] s; 63 s = new string[1]; 64 for (int i = 0; i < Map.Count; i++) { 65 s[0] = ""; 66 s[0] += SelfConfigurationMechanism.Probabilities[i].ToString(); 67 } 68 return s; 69 } 70 private void ApplySucsessMapCreationAlgorithm(IRandom random, int mapSize) { 62 71 for (int t = 0; t < mapSize; t++) { 63 map[t].Add(t); 64 probabilities.Add(1.0 / ((double)(mapSize))); // uniform distribution as start point 72 Map[t].Add(t); 65 73 } 74 SelfConfigurationMechanism.Initialization(mapSize); 66 75 } 67 76 public override void MapUpDate(Dictionary<ISymbolicExpressionTree, double> population) { 68 SucsessStatisticCollection(population); 69 HelpFunctions.ProbabilitiesUpDate(SucsessStatistics, Probabilities); 70 } 71 private void SucsessStatisticCollection(Dictionary<ISymbolicExpressionTree, double> population) { 72 if (SucsessStatistics.Count != 0) 73 SucsessStatistics.Clear(); 74 for (int t = 0; t < Probabilities.Count; t++) { 75 SucsessStatistics.Add(new List<double>()); 76 SucsessStatistics[t].Add(0); 77 SucsessStatistics[t].Add(0); 78 } 79 foreach (var solution in population) { 80 TreeCheck(solution.Key, solution.Value); 81 } 82 } 83 private void TreeCheck(ISymbolicExpressionTree tree, double treeQuality) { 84 foreach (var treeNode in tree.IterateNodesPrefix().OfType<TreeModelTreeNode>()) { 85 SucsessStatistics[treeNode.TreeNumber][0] += 1; 86 SucsessStatistics[treeNode.TreeNumber][1] += treeQuality; 87 } 77 SelfConfigurationMechanism.UpDate(population); 88 78 } 89 79 #endregion 90 #region Map ApplayFunctions80 #region Map Apply Functions 91 81 public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) { 92 treeNumber = Map[ HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities)][0];82 treeNumber = Map[SelfConfigurationMechanism.Aplay(random)][0]; 93 83 return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone(); 94 84 } -
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMZeroMap.cs
r17133 r17134 30 30 [StorableType("FF199AE7-DF0A-4E2A-99BC-BECD647E18F0")] 31 31 public class EMMZeroMap : EMMMapBase<ISymbolicExpressionTree> { 32 #region con ctructors32 #region constructors 33 33 [StorableConstructor] 34 34 protected EMMZeroMap(StorableConstructorFlag _) : base(_) { } … … 41 41 public EMMZeroMap(EMMZeroMap original, Cloner cloner) : base(original, cloner) { } 42 42 #endregion 43 #region Map ApplayFunctions44 override public void CreateMap(IRandom random , int k) { }43 #region Map Apply Functions 44 override public void CreateMap(IRandom random) { } 45 45 override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) { 46 46 return NewModelForInizializtion(random, out treeNumber);
Note: See TracChangeset
for help on using the changeset viewer.