1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading;
|
---|
7 | using System.Threading.Tasks;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.ParallelEngine;
|
---|
10 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
11 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
12 | using HeuristicLab.SequentialEngine;
|
---|
13 | using Newtonsoft.Json.Linq;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.JsonInterface.App {
|
---|
16 | internal static class Runner {
|
---|
17 | internal static void Run(string template, string config, string outputFile) {
|
---|
18 | try {
|
---|
19 | InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
|
---|
20 | IOptimizer optimizer = instantiatorResult.Optimizer;
|
---|
21 | IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
|
---|
22 |
|
---|
23 | optimizer.Runs.Clear();
|
---|
24 | if (optimizer is EngineAlgorithm e)
|
---|
25 | e.Engine = new ParallelEngine.ParallelEngine();
|
---|
26 |
|
---|
27 | Task task = optimizer.StartAsync();
|
---|
28 | while (!task.IsCompleted) {
|
---|
29 | WriteResultsToFile(outputFile, optimizer, configuredResultItem);
|
---|
30 | Thread.Sleep(100);
|
---|
31 | }
|
---|
32 |
|
---|
33 | WriteResultsToFile(outputFile, optimizer, configuredResultItem);
|
---|
34 | } catch (Exception e) {
|
---|
35 | File.WriteAllText(outputFile, e.Message + "\n\n\n\n" + e.StackTrace);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
|
---|
40 | File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
|
---|
41 |
|
---|
42 | private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) {
|
---|
43 | JArray arr = new JArray();
|
---|
44 | IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name);
|
---|
45 |
|
---|
46 | foreach (var run in optimizer.Runs) {
|
---|
47 | JObject obj = new JObject();
|
---|
48 | arr.Add(obj);
|
---|
49 | obj.Add("Run", JToken.FromObject(run.ToString()));
|
---|
50 | foreach (var res in run.Results) {
|
---|
51 | if (configuredResults.Contains(res.Key)) {
|
---|
52 | if (res.Value is ISymbolicRegressionSolution solution) {
|
---|
53 | /* TEST */
|
---|
54 | var csFormatter = new CSharpSymbolicExpressionTreeStringFormatter();
|
---|
55 | File.WriteAllText(@"C:\Workspace\output\csFormatted.cs", csFormatter.Format(solution.Model.SymbolicExpressionTree));
|
---|
56 | /* END TEST */
|
---|
57 | var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
|
---|
58 | var x = formatter.Format(solution.Model.SymbolicExpressionTree);
|
---|
59 | obj.Add(res.Key, JToken.FromObject(x));
|
---|
60 | } else
|
---|
61 | obj.Add(res.Key, JToken.FromObject(res.Value.ToString()));
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | return SingleLineArrayJsonWriter.Serialize(arr);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|