[17330] | 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;
|
---|
[17453] | 9 | using HeuristicLab.ParallelEngine;
|
---|
[17464] | 10 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 11 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
[17439] | 12 | using HeuristicLab.SequentialEngine;
|
---|
[17442] | 13 | using Newtonsoft.Json.Linq;
|
---|
[17330] | 14 |
|
---|
| 15 | namespace HeuristicLab.JsonInterface.App {
|
---|
| 16 | internal static class Runner {
|
---|
[17442] | 17 | internal static void Run(string template, string config, string outputFile) {
|
---|
[17477] | 18 | InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
|
---|
| 19 | IOptimizer optimizer = instantiatorResult.Optimizer;
|
---|
| 20 | IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
|
---|
| 21 |
|
---|
[17453] | 22 | optimizer.Runs.Clear();
|
---|
[17439] | 23 | if(optimizer is EngineAlgorithm e)
|
---|
[17453] | 24 | e.Engine = new ParallelEngine.ParallelEngine();
|
---|
[17439] | 25 |
|
---|
[17395] | 26 | Task task = optimizer.StartAsync();
|
---|
[17330] | 27 | while(!task.IsCompleted) {
|
---|
[17477] | 28 | WriteResultsToFile(outputFile, optimizer, configuredResultItem);
|
---|
[17339] | 29 | Thread.Sleep(100);
|
---|
[17330] | 30 | }
|
---|
[17451] | 31 |
|
---|
[17477] | 32 | WriteResultsToFile(outputFile, optimizer, configuredResultItem);
|
---|
[17330] | 33 | }
|
---|
| 34 |
|
---|
[17477] | 35 | private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
|
---|
| 36 | File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
|
---|
[17330] | 37 |
|
---|
[17477] | 38 | private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) {
|
---|
[17442] | 39 | JArray arr = new JArray();
|
---|
[17477] | 40 | IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name);
|
---|
| 41 |
|
---|
[17395] | 42 | foreach (var run in optimizer.Runs) {
|
---|
[17442] | 43 | JObject obj = new JObject();
|
---|
| 44 | arr.Add(obj);
|
---|
| 45 | obj.Add("Run", JToken.FromObject(run.ToString()));
|
---|
[17395] | 46 | foreach (var res in run.Results) {
|
---|
[17477] | 47 | if (configuredResults.Contains(res.Key)) {
|
---|
[17464] | 48 | if (res.Value is ISymbolicRegressionSolution solution) {
|
---|
| 49 | var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
|
---|
| 50 | var x = formatter.Format(solution.Model.SymbolicExpressionTree);
|
---|
| 51 | obj.Add(res.Key, JToken.FromObject(x));
|
---|
| 52 | } else
|
---|
| 53 | obj.Add(res.Key, JToken.FromObject(res.Value.ToString()));
|
---|
| 54 | }
|
---|
[17330] | 55 | }
|
---|
[17395] | 56 | }
|
---|
[17442] | 57 | return SingleLineArrayJsonWriter.Serialize(arr);
|
---|
[17330] | 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|