Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3022-FastFunctionExtraction/TestFFX/Program.cs @ 17738

Last change on this file since 17738 was 17738, checked in by lleko, 4 years ago

#3022 implement ffx

File size: 4.5 KB
Line 
1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Globalization;
5using System.Linq;
6using HeuristicLab.Problems.DataAnalysis;
7using System.Data;
8using System.Diagnostics;
9using System.Threading;
10using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
11
12namespace HeuristicLab.Algorithms.DataAnalysis.FastFunctionExtraction {
13    class Program {
14        private static void TestPennML() {
15            string projectDirectoryPath = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "../branches/3022-FastFunctionExtraction/TestFFX/";
16            string pennMLDataPath = projectDirectoryPath + "/pennML_data/";
17            string targetPath = projectDirectoryPath + "/results/results.txt";
18            var dInfo = new DirectoryInfo(pennMLDataPath);
19            StreamWriter streamWriter = new StreamWriter(targetPath);
20            streamWriter.WriteLine("dataset,algorithm,num_bases,train_mse,train_mae,test_mse,test_mae,runtime");
21            streamWriter.Close();
22            int len = dInfo.GetFiles().Length;
23            int idx = 0;
24            // foreach dataset
25            foreach (var fInfo in dInfo.GetFiles()) {
26                Console.Write($"{++idx}/{len}\t{fInfo.Name,-35}:");
27                RunFFXOnDataset(fInfo, targetPath, out var elapsedSeconds);
28                Console.WriteLine($"{elapsedSeconds + " sec",-15}");
29            }
30        }
31
32        private static void SaveAccuracyInFile(ISymbolicRegressionSolution[] regressionSolutions, int[] numBases, double runtime, string filePath, string problemName) {
33            if (filePath == "") return;
34            CultureInfo culture = new CultureInfo("en-US");
35            StreamWriter sw = new StreamWriter(filePath, true);
36            int i = 0;
37            foreach (var solution in regressionSolutions) {
38                string outputStr = String.Join(",", new[]{
39                    problemName.Substring(0, problemName.Length - 4),
40                    "hl_ffx",
41                    numBases[i++].ToString(),
42                    solution.TrainingMeanSquaredError.ToString(culture),
43                    solution.TrainingMeanAbsoluteError.ToString(culture),
44                    solution.TestMeanSquaredError.ToString(culture),
45                    solution.TestMeanAbsoluteError.ToString(culture),
46                    runtime.ToString(culture)
47                });
48                sw.WriteLine(outputStr, culture);
49            }
50            sw.Dispose();
51            Thread.Sleep(100); // to prevent race conditions with stream writers
52        }
53
54        private static void RunFFXOnDataset(FileInfo fInfo, string outFilePath, out double elapsedSeconds) {
55            var data = ParseProblemDataFromFile(fInfo.FullName);
56            Stopwatch sw = new Stopwatch();
57            sw.Start();
58            var regressionModels = FastFunctionExtraction.Fit(data, 0.95, out var numBases, true, true, true, true, true, maxNumBases: 20).ToArray();
59            sw.Stop();
60            var regressionSolutions = regressionModels.Select(model => new SymbolicRegressionSolution(model, data)).ToArray();
61            elapsedSeconds = sw.Elapsed.TotalSeconds;
62            SaveAccuracyInFile(regressionSolutions, numBases.ToArray(), elapsedSeconds, outFilePath, fInfo.Name);
63        }
64
65        private static IRegressionProblemData ParseProblemDataFromFile(string filepath, char separator = ';') {
66            CultureInfo culture = new CultureInfo("en-US");
67            var reader = new StreamReader(filepath);
68            var variables = reader.ReadLine().Split(separator);
69            var vals = Enumerable.Range(0, variables.Length).Select(_ => new List<double>()).ToArray();
70            var targetVar = variables.Last();
71            var allowedInputVars = variables.Where(val => val != targetVar);
72            string line;
73
74            line = reader.ReadLine();
75            while (!String.IsNullOrWhiteSpace(line)) {
76                int i = 0;
77                foreach (var strVal in line.Split(separator)) {
78                    vals[i++].Add(Convert.ToDouble(strVal, culture));
79                }
80                line = reader.ReadLine();
81            }
82            IDataset dataset = new Dataset(variables, vals);
83            var temp = new RegressionProblemData(dataset, allowedInputVars, targetVar);
84            return temp;
85        }
86
87        static void Main() {
88            TestPennML();
89            Console.WriteLine("Done.");
90            Console.ReadLine();
91        }
92    }
93}
Note: See TracBrowser for help on using the repository browser.