1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Threading;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using Newtonsoft.Json.Linq;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.JsonInterface.App {
|
---|
12 | internal static class Runner {
|
---|
13 | internal static void Run(string template, string config, string outputFile) {
|
---|
14 | try {
|
---|
15 | InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
|
---|
16 | IOptimizer optimizer = instantiatorResult.Optimizer;
|
---|
17 | IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
|
---|
18 |
|
---|
19 | optimizer.Runs.Clear();
|
---|
20 | if (optimizer is EngineAlgorithm e)
|
---|
21 | e.Engine = new ParallelEngine.ParallelEngine();
|
---|
22 |
|
---|
23 | Task task = optimizer.StartAsync();
|
---|
24 | while (!task.IsCompleted) {
|
---|
25 | WriteResultsToFile(outputFile, optimizer, configuredResultItem);
|
---|
26 | Thread.Sleep(100);
|
---|
27 | }
|
---|
28 |
|
---|
29 | WriteResultsToFile(outputFile, optimizer, configuredResultItem);
|
---|
30 | } catch (Exception e) {
|
---|
31 | Console.Error.WriteLine($"{e.Message} \n\n\n\n {e.StackTrace}");
|
---|
32 | File.WriteAllText(outputFile, e.Message + "\n\n\n\n" + e.StackTrace);
|
---|
33 | Environment.Exit(-1);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
|
---|
38 | File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
|
---|
39 |
|
---|
40 | private static IEnumerable<IResultFormatter> ResultFormatter { get; } =
|
---|
41 | PluginInfrastructure.ApplicationManager.Manager.GetInstances<IResultFormatter>();
|
---|
42 |
|
---|
43 | private static IResultFormatter GetResultFormatter(string fullName) =>
|
---|
44 | ResultFormatter?.Where(x => x.GetType().FullName == fullName).Last();
|
---|
45 |
|
---|
46 | private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems) {
|
---|
47 | JArray arr = new JArray();
|
---|
48 | IEnumerable<string> configuredResults = configuredResultItems.Select(x => x.Name);
|
---|
49 |
|
---|
50 | foreach (var run in optimizer.Runs) {
|
---|
51 | JObject obj = new JObject();
|
---|
52 | arr.Add(obj);
|
---|
53 | obj.Add("Run", JToken.FromObject(run.ToString()));
|
---|
54 |
|
---|
55 | // zip and filter the results with the ResultJsonItems
|
---|
56 | var filteredResults = new List<Tuple<IResultJsonItem, IItem>>();
|
---|
57 | foreach(var resultItem in configuredResultItems) {
|
---|
58 | foreach(var result in run.Results) {
|
---|
59 | if(resultItem.Name == result.Key) {
|
---|
60 | filteredResults.Add(Tuple.Create(resultItem, result.Value));
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | // add results to the JObject
|
---|
66 | foreach(var result in filteredResults) {
|
---|
67 | var formatter = GetResultFormatter(result.Item1.ResultFormatterType);
|
---|
68 | obj.Add(result.Item1.Name, formatter.Format(result.Item2));
|
---|
69 | }
|
---|
70 | }
|
---|
71 | return SingleLineArrayJsonWriter.Serialize(arr);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|