1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Threading;
|
---|
4 | using HEAL.Attic;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.PluginInfrastructure;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.ConsoleApplications {
|
---|
11 | [StorableType("33B32897-258B-4E29-8E2E-0A9A8AE67B1D")]
|
---|
12 | [Application("CLIOptimize", "")]
|
---|
13 | public class ConsoleOptimizer : ApplicationBase {
|
---|
14 |
|
---|
15 | #region Vars
|
---|
16 | private IExecutable executable;
|
---|
17 | private readonly ManualResetEvent stoppedSignal = new ManualResetEvent(false);
|
---|
18 | private readonly ManualResetEvent startedSignal = new ManualResetEvent(false);
|
---|
19 | private bool isFinished = false;
|
---|
20 | private static object locker = new object();
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region Properties
|
---|
24 | [Storable]
|
---|
25 | public UniPath InputFilePath { get; set; } = null;
|
---|
26 | [Storable]
|
---|
27 | public UniPath OutputPath { get; set; } = null;
|
---|
28 | #endregion
|
---|
29 |
|
---|
30 | #region Constructors
|
---|
31 | public ConsoleOptimizer() { }
|
---|
32 | public ConsoleOptimizer(UniPath inputFilePath, UniPath outputPath) {
|
---|
33 | InputFilePath = inputFilePath;
|
---|
34 | OutputPath = outputPath;
|
---|
35 | }
|
---|
36 | #endregion
|
---|
37 |
|
---|
38 |
|
---|
39 | public override void Run(ICommandLineArgument[] args) {
|
---|
40 | lock (locker) {
|
---|
41 | Init();
|
---|
42 | executable.Started += Executable_Started;
|
---|
43 | executable.Stopped += Executable_Stopped;
|
---|
44 | startedSignal.Reset();
|
---|
45 | executable.StartAsync();
|
---|
46 | startedSignal.WaitOne();
|
---|
47 | }
|
---|
48 | stoppedSignal.WaitOne();
|
---|
49 |
|
---|
50 | startedSignal.Close();
|
---|
51 | stoppedSignal.Close();
|
---|
52 | }
|
---|
53 |
|
---|
54 | private void Executable_Started(object sender, EventArgs e) {
|
---|
55 | startedSignal.Set();
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override void OnCancel() {
|
---|
59 | base.OnCancel();
|
---|
60 | startedSignal.WaitOne();
|
---|
61 | lock (locker) {
|
---|
62 | if (isFinished) throw new InvalidOperationException("Executable has already finished!");
|
---|
63 | executable.Stop();
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override void OnPause() {
|
---|
68 | base.OnPause();
|
---|
69 | startedSignal.WaitOne();
|
---|
70 | lock (locker) {
|
---|
71 | if (isFinished) throw new InvalidOperationException("Executable has already finished!");
|
---|
72 | executable.Pause();
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override void OnResume() {
|
---|
77 | base.OnResume();
|
---|
78 | startedSignal.WaitOne();
|
---|
79 | lock (locker) {
|
---|
80 | if (isFinished) throw new InvalidOperationException("Executable has already finished!");
|
---|
81 | startedSignal.Reset();
|
---|
82 | executable.StartAsync();
|
---|
83 | startedSignal.WaitOne();
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | #region Helper
|
---|
88 | private void Init() {
|
---|
89 | lock (locker) {
|
---|
90 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
91 | IStorableContent content = ContentManager.Load(InputFilePath.ToString());
|
---|
92 | executable = content as IExecutable;
|
---|
93 | if (executable == null)
|
---|
94 | throw new NotSupportedException("The given file does not contain any algorithm to start.");
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void Executable_Stopped(object sender, EventArgs e) {
|
---|
99 | lock (locker) {
|
---|
100 | isFinished = true;
|
---|
101 | IOptimizer optimizer = executable as IOptimizer;
|
---|
102 | if (optimizer != null) PrintResults(optimizer);
|
---|
103 | ContentManager.Save((IStorableContent)executable, OutputPath.ToString() + Path.DirectorySeparatorChar + "result.hl", true);
|
---|
104 | }
|
---|
105 | stoppedSignal.Set();
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void PrintResults(IOptimizer optimizer) {
|
---|
109 | lock (locker) {
|
---|
110 | Console.WriteLine("\nRESULT(S):");
|
---|
111 | int i = 1;
|
---|
112 | foreach (var run in optimizer.Runs) {
|
---|
113 | Console.WriteLine($"{"-------------------------------- RUN",35} {$"{i++:D3}" + " --------------------------------",-35}");
|
---|
114 | foreach (var res in run.Results) {
|
---|
115 | Console.WriteLine($"{res.Key,35} : {res.Value,-35}");
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | #endregion
|
---|
121 | }
|
---|
122 | }
|
---|