1 | using HEAL.Attic;
|
---|
2 | using HeuristicLab.Data;
|
---|
3 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
4 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.IO;
|
---|
7 | using System.Linq;
|
---|
8 |
|
---|
9 | namespace 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 | }
|
---|