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