Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/FileComuncations.cs @ 17842

Last change on this file since 17842 was 17134, checked in by msemenki, 5 years ago

#2988:

  1. The file system was changed, folders was added and part of files was transferred in these folders.
  2. HelpFunctions class was divided on 2 parts: HelpFuctions for common purposes static functions and SelfConfiguration that include functions for self-configuration mechanism realization (is used in EMMSucsessMap).
  3. Parts of self-configuration mechanism was transferred from EMMSucsessMap.cs to SelfConfiguration.cs. Now EMMSucsessMap used SelfConfiguration like one of data member. Other parts of project was adopted for this changing.
  4. FileComunication class was added. It include the majority of functions for printing to files or reading from files. Here were realized possibility to write and read to hl files.
  5. ModelTreeNode.cs has additional possibility - to write sub-model in string (then it is possible to write it in file).
  6. InfixExpressionFormatter.cs can work with TreeModelNode.
  7. Possibility for different map types to be readable from files was extended and cheeked.
  8. Such parameters like - ClusterNumbers, ClusterNumbersShow, NegbourNumber, NegbourType (that is used only in several maps) was transferred from EMMAlgorithm to Map Parameters. Now EMMBaseMap class inherited from ParameterizedNamedItem (not from Item). And EMMIslandMap and EMMNetworkMap contains their parameters (constructors was modified). CreationMap calls functions were simplified.
  9. Functions for different distance metric calculation was added. Now, it is possible to calculate different types of distances between models (with different random values of constants).
  10. DistanceParametr was added. Now maps can be created according different types of distance calculations.
  11. The class EMMClustering has new name KMeansClusterizationAlgorithm. On KMeansClusterizationAlgorithm bug with bloating of centroids list was fixed. Algorithm was adopted for working with different type of distance metric and get maximum number of iterations.
  12. Possibilities for constants optimization in sub-models an whole tree was added. EMMAlgorithm get new function for evaluation of individuals (and some additional technical stuff for that). Function for trees with model in usual tree transformation and back was added.
  13. EMMAlgorithm was divided on 2 parts:
  • EMMAlgorithm, that contain evolutionary algorithm working with sub-models, and use ready to use maps;
  • ModelSetPreparation, that contain distance calculation, model set simplification and map creation.
File size: 4.8 KB
Line 
1using HEAL.Attic;
2using HeuristicLab.Data;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4using HeuristicLab.Problems.DataAnalysis.Symbolic;
5using System.Collections.Generic;
6using System.IO;
7using System.Linq;
8
9namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
10  class FileComuncations {
11    public static void DoubleMatrixPrint(string fileName, double[,] matrix, int size) {
12      using (var sw = new StreamWriter(fileName)) {
13        for (int i = 0; i < size; i++) {
14          for (int j = 0; j < size; j++) {
15            sw.Write($"{matrix[i, j]}");
16            if (j < size - 1)
17              sw.Write(" ");
18          }
19          sw.WriteLine();
20        }
21      }
22    }
23    public static void DoubleMatrixPrint(string fileName, DoubleMatrix matrix) {
24      using (var sw = new StreamWriter(fileName)) {
25        for (int i = 0; i < matrix.Rows; i++) {
26          for (int j = 0; j < matrix.Columns; j++) {
27            sw.Write($"{matrix[i, j]}");
28            if (j < matrix.Columns - 1)
29              sw.Write(" ");
30          }
31          sw.WriteLine();
32        }
33      }
34    }
35    public static void DoubleMatrixPrint(string fileName, List<List<double>> matrix) {
36      using (var sw = new StreamWriter(fileName)) {
37        for (int i = 0; i < matrix.Count; i++) {
38          for (int j = 0; j < matrix[i].Count; j++) {
39            sw.Write($"{matrix[i][j]}");
40            if (j < matrix[i].Count - 1)
41              sw.Write(" ");
42          }
43          sw.WriteLine();
44        }
45      }
46    }
47    public static void DoubleMatrixSerialize(string fileName, double[,] matrix) {
48      var obj = new DoubleMatrix(matrix);
49      var proto = new ProtoBufSerializer();
50      proto.Serialize(obj, fileName);
51    }
52    public static void IntMatrixSerialize(string fileName, int[,] matrix) {
53      var obj = new IntMatrix(matrix);
54      var proto = new ProtoBufSerializer();
55      proto.Serialize(obj, fileName);
56    }
57    public static void IntMatrixSerialize(string fileName, IntMatrix matrix) {
58      var proto = new ProtoBufSerializer();
59      proto.Serialize(matrix, fileName);
60    }
61    public static void DoubleMatrixSerialize(string fileName, DoubleMatrix matrix) {
62      var proto = new ProtoBufSerializer();
63      proto.Serialize(matrix, fileName);
64    }
65    public static double[,] DoubleMatrixDeserialize(string fileName) {
66      var proto = new ProtoBufSerializer();
67      var m = (DoubleMatrix)proto.Deserialize(fileName);
68      double[,] matrix = m.CloneAsMatrix();
69      return matrix;
70    }
71    public static int[,] IntFromDeserialize(string fileName, int size) {
72      var proto = new ProtoBufSerializer();
73      var m = (IntMatrix)proto.Deserialize(fileName);
74      int[,] matrix = m.CloneAsMatrix();
75      return matrix;
76    }
77    public static List<List<int>> IntMatrixFromFileRead(string fileName) {
78      string input = File.ReadAllText(fileName);
79      var temp = new List<List<int>>();
80      int i = 0;
81      foreach (var row in input.Split('\n')) {
82        temp.Add(new List<int>());
83        foreach (var col in row.Trim().Split(' ')) {
84          temp[i].Add(int.Parse(col.Trim()));
85        }
86        i++;
87      }
88      return temp;
89    }
90    public static List<List<double>> DoubleMatrixFromFileRead(string fileName) {
91      string input = File.ReadAllText(fileName);
92      var temp = new List<List<double>>();
93      int i = 0;
94      foreach (var row in input.Split('\n')) {
95        temp.Add(new List<double>());
96        foreach (var col in row.Trim().Split(' ')) {
97          temp[i].Add(double.Parse(col.Trim()));
98        }
99        i++;
100      }
101      return temp;
102    }
103    public static double[,] DoubleMatrixFromFileRead(string fileName, int size) {
104      var matrix = new double[size, size];
105      string input = File.ReadAllText(fileName);
106      int i = 0, j = 0;
107      foreach (var row in input.Split('\n')) {
108        foreach (var col in row.Trim().Split(' ')) {
109          matrix[i, j] = double.Parse(col.Trim());
110          j++;
111        }
112        i++;
113      }
114      return matrix;
115    }
116    public static void TreesEvoluationResultsToTxtFile(ISymbolicDataAnalysisSingleObjectiveProblem problem, List<ISymbolicExpressionTree> trees) {
117      var problemData = problem.ProblemData;
118      var dataset = problemData.Dataset;
119      var rows = problemData.TrainingIndices;
120      var interpreter = problem.SymbolicExpressionTreeInterpreter;
121      string[] toWrite = new string[trees.Count()];
122      int i = 0;
123      foreach (var tree in trees) {
124        var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
125        string str = "";
126        foreach (var value in estimatedValues) {
127          str += value.ToString() + " ";
128        }
129        toWrite[i] = str;
130        i++;
131      }
132      File.WriteAllLines("evolv.txt", toWrite);
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.