[17330] | 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;
|
---|
[17442] | 8 | using Newtonsoft.Json.Linq;
|
---|
[17330] | 9 |
|
---|
| 10 | namespace HeuristicLab.JsonInterface.App {
|
---|
| 11 | internal static class Runner {
|
---|
[17442] | 12 | internal static void Run(string template, string config, string outputFile) {
|
---|
[17519] | 13 | try {
|
---|
| 14 | InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
|
---|
| 15 | IOptimizer optimizer = instantiatorResult.Optimizer;
|
---|
| 16 | IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
|
---|
[17477] | 17 |
|
---|
[17519] | 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 |
|
---|
[17477] | 28 | WriteResultsToFile(outputFile, optimizer, configuredResultItem);
|
---|
[17519] | 29 | } catch (Exception e) {
|
---|
[17828] | 30 | Console.Error.WriteLine($"{e.Message} \n\n\n\n {e.StackTrace}");
|
---|
[17519] | 31 | File.WriteAllText(outputFile, e.Message + "\n\n\n\n" + e.StackTrace);
|
---|
[17828] | 32 | Environment.Exit(-1);
|
---|
[17330] | 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[17477] | 36 | private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
|
---|
| 37 | File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
|
---|
[17330] | 38 |
|
---|
[17834] | 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) {
|
---|
[17442] | 46 | JArray arr = new JArray();
|
---|
[17834] | 47 | IEnumerable<string> configuredResults = configuredResultItems.Select(x => x.Name);
|
---|
[17477] | 48 |
|
---|
[17395] | 49 | foreach (var run in optimizer.Runs) {
|
---|
[17442] | 50 | JObject obj = new JObject();
|
---|
| 51 | arr.Add(obj);
|
---|
| 52 | obj.Add("Run", JToken.FromObject(run.ToString()));
|
---|
[17560] | 53 |
|
---|
[17834] | 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 });
|
---|
[17560] | 58 |
|
---|
[17834] | 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));
|
---|
[17330] | 63 | }
|
---|
[17395] | 64 | }
|
---|
[17442] | 65 | return SingleLineArrayJsonWriter.Serialize(arr);
|
---|
[17330] | 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|