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 |
|
---|
51 | // add empty values for configured results
|
---|
52 | var emptyToken = JToken.FromObject("");
|
---|
53 | foreach (var cr in configuredResults) {
|
---|
54 | obj.Add(cr, emptyToken);
|
---|
55 | }
|
---|
56 |
|
---|
57 | // change empty values with calculated values
|
---|
58 | var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
|
---|
59 | foreach (var res in run.Results) {
|
---|
60 | if(obj.ContainsKey(res.Key)) {
|
---|
61 | if (res.Value is ISymbolicRegressionSolution solution) {
|
---|
62 | var formattedModel = formatter.Format(solution.Model.SymbolicExpressionTree);
|
---|
63 | obj[res.Key] = JToken.FromObject(formattedModel);
|
---|
64 | } else {
|
---|
65 | obj[res.Key] = JToken.FromObject(res.Value.ToString());
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | return SingleLineArrayJsonWriter.Serialize(arr);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|