using HEAL.Attic; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Problems.DataAnalysis.Symbolic; using System.Collections.Generic; using System.IO; using System.Linq; namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { class FileComuncations { public static void DoubleMatrixPrint(string fileName, double[,] matrix, int size) { using (var sw = new StreamWriter(fileName)) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { sw.Write($"{matrix[i, j]}"); if (j < size - 1) sw.Write(" "); } sw.WriteLine(); } } } public static void DoubleMatrixPrint(string fileName, DoubleMatrix matrix) { using (var sw = new StreamWriter(fileName)) { for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { sw.Write($"{matrix[i, j]}"); if (j < matrix.Columns - 1) sw.Write(" "); } sw.WriteLine(); } } } public static void DoubleMatrixPrint(string fileName, List> matrix) { using (var sw = new StreamWriter(fileName)) { for (int i = 0; i < matrix.Count; i++) { for (int j = 0; j < matrix[i].Count; j++) { sw.Write($"{matrix[i][j]}"); if (j < matrix[i].Count - 1) sw.Write(" "); } sw.WriteLine(); } } } public static void DoubleMatrixSerialize(string fileName, double[,] matrix) { var obj = new DoubleMatrix(matrix); var proto = new ProtoBufSerializer(); proto.Serialize(obj, fileName); } public static void IntMatrixSerialize(string fileName, int[,] matrix) { var obj = new IntMatrix(matrix); var proto = new ProtoBufSerializer(); proto.Serialize(obj, fileName); } public static void IntMatrixSerialize(string fileName, IntMatrix matrix) { var proto = new ProtoBufSerializer(); proto.Serialize(matrix, fileName); } public static void DoubleMatrixSerialize(string fileName, DoubleMatrix matrix) { var proto = new ProtoBufSerializer(); proto.Serialize(matrix, fileName); } public static double[,] DoubleMatrixDeserialize(string fileName) { var proto = new ProtoBufSerializer(); var m = (DoubleMatrix)proto.Deserialize(fileName); double[,] matrix = m.CloneAsMatrix(); return matrix; } public static int[,] IntFromDeserialize(string fileName, int size) { var proto = new ProtoBufSerializer(); var m = (IntMatrix)proto.Deserialize(fileName); int[,] matrix = m.CloneAsMatrix(); return matrix; } public static List> IntMatrixFromFileRead(string fileName) { string input = File.ReadAllText(fileName); var temp = new List>(); int i = 0; foreach (var row in input.Split('\n')) { temp.Add(new List()); foreach (var col in row.Trim().Split(' ')) { temp[i].Add(int.Parse(col.Trim())); } i++; } return temp; } public static List> DoubleMatrixFromFileRead(string fileName) { string input = File.ReadAllText(fileName); var temp = new List>(); int i = 0; foreach (var row in input.Split('\n')) { temp.Add(new List()); foreach (var col in row.Trim().Split(' ')) { temp[i].Add(double.Parse(col.Trim())); } i++; } return temp; } public static double[,] DoubleMatrixFromFileRead(string fileName, int size) { var matrix = new double[size, size]; string input = File.ReadAllText(fileName); int i = 0, j = 0; foreach (var row in input.Split('\n')) { foreach (var col in row.Trim().Split(' ')) { matrix[i, j] = double.Parse(col.Trim()); j++; } i++; } return matrix; } public static void TreesEvoluationResultsToTxtFile(ISymbolicDataAnalysisSingleObjectiveProblem problem, List trees) { var problemData = problem.ProblemData; var dataset = problemData.Dataset; var rows = problemData.TrainingIndices; var interpreter = problem.SymbolicExpressionTreeInterpreter; string[] toWrite = new string[trees.Count()]; int i = 0; foreach (var tree in trees) { var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows); string str = ""; foreach (var value in estimatedValues) { str += value.ToString() + " "; } toWrite[i] = str; i++; } File.WriteAllLines("evolv.txt", toWrite); } } }