1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using System.Threading;
|
---|
28 | using CommandLine;
|
---|
29 | using HeuristicLab.Clients.Hive;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 | using HeuristicLab.Core;
|
---|
32 | using HeuristicLab.Data;
|
---|
33 | using HeuristicLab.Optimization;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
36 |
|
---|
37 | namespace HiveIraceSubmit {
|
---|
38 | class Program {
|
---|
39 | public const int EXIT_OK = 0;
|
---|
40 | public const int EXIT_ERR = 1;
|
---|
41 |
|
---|
42 | static void Main(string[] args) {
|
---|
43 | //AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "HiveIraceSubmit.exe.config");
|
---|
44 |
|
---|
45 |
|
---|
46 | var result = Parser.Default.ParseArguments<Options>(args)
|
---|
47 | .WithParsed(x => {
|
---|
48 | try {
|
---|
49 | Environment.Exit(Initialize(x));
|
---|
50 | } catch (Exception e) {
|
---|
51 | Console.Error.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
|
---|
52 | if (e.InnerException != null) {
|
---|
53 | Console.Error.WriteLine("Inner Exception");
|
---|
54 | Console.Error.WriteLine(e.InnerException.Message + Environment.NewLine + e.InnerException.StackTrace);
|
---|
55 | }
|
---|
56 | Environment.Exit(EXIT_ERR);
|
---|
57 | }
|
---|
58 | })
|
---|
59 | .WithNotParsed(x => Environment.Exit(EXIT_ERR));
|
---|
60 | }
|
---|
61 |
|
---|
62 | private static int Initialize(Options opts) {
|
---|
63 | #if __DRY_RUN__
|
---|
64 | Console.WriteLine(Guid.NewGuid());
|
---|
65 | return EXIT_OK;
|
---|
66 | #endif
|
---|
67 | var pluginManager = new PluginManager(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]));
|
---|
68 | pluginManager.DiscoverAndCheckPlugins();
|
---|
69 |
|
---|
70 | var appManagerType = Assembly.GetAssembly(typeof(IPlugin)).DefinedTypes.Single(x => x.Name == "DefaultApplicationManager");
|
---|
71 | var appManager = Activator.CreateInstance(appManagerType, true);
|
---|
72 | var method = appManagerType.GetMethod("PrepareApplicationDomain", BindingFlags.NonPublic | BindingFlags.Instance);
|
---|
73 | method.Invoke(appManager, new object[] { pluginManager.Applications, pluginManager.Plugins });
|
---|
74 |
|
---|
75 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
76 |
|
---|
77 | return Run(opts);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static int Run(Options opts) {
|
---|
81 | HiveClient.Instance.Refresh();
|
---|
82 |
|
---|
83 | var file = Path.Combine(opts.ProblemInstance, opts.Algorithm);
|
---|
84 | var optimizer = ContentManager.Load(file) as IAlgorithm;
|
---|
85 | if (optimizer == null) {
|
---|
86 | Console.Error.WriteLine("File does not contain item of type IAlgorithm");
|
---|
87 | return EXIT_ERR;
|
---|
88 | }
|
---|
89 |
|
---|
90 | foreach (var config in opts.Configuration) {
|
---|
91 | var split = config.Split('=');
|
---|
92 | var paramPath = split[0].Split('.');
|
---|
93 | var strValue = split[1].Trim();
|
---|
94 |
|
---|
95 | var paramItem = optimizer as IParameterizedItem;
|
---|
96 | for (var i = 0; i < paramPath.Length - 1; i++) {
|
---|
97 | var p = paramItem.Parameters[paramPath[i]];
|
---|
98 | if (p is IValueParameter) paramItem = (IParameterizedItem)((IValueParameter)p).Value;
|
---|
99 | else paramItem = (IParameterizedItem)((dynamic)p).Value;
|
---|
100 | }
|
---|
101 | var param = paramItem.Parameters[paramPath.Last()];
|
---|
102 | Parameterize(param, strValue);
|
---|
103 | }
|
---|
104 |
|
---|
105 | var hiveTask = ItemTask.GetItemTaskForItem(optimizer);
|
---|
106 | var task = hiveTask.CreateHiveTask();
|
---|
107 | var rJob = new RefreshableJob();
|
---|
108 | rJob.Job.Name = Properties.Settings.Default.JobPrefix + " " + string.Join("-", opts.ConfigId, opts.InstanceId, opts.Seed);
|
---|
109 | rJob.Job.Description = opts.Algorithm + " " + string.Join(", ", opts.Configuration);
|
---|
110 | rJob.Job.ResourceIds = new List<Guid>() { Guid.Parse(Properties.Settings.Default.HiveResources) };
|
---|
111 | //rJob.Job.ResourceNames = Properties.Settings.Default.HiveResources;
|
---|
112 | rJob.HiveTasks.Add(task);
|
---|
113 |
|
---|
114 | HiveClient.Store(rJob, CancellationToken.None);
|
---|
115 | //HiveClient.StartJob(_ => { }, rJob, CancellationToken.None); // this is async
|
---|
116 | //HiveClient.LoadJob(rJob);
|
---|
117 |
|
---|
118 | Console.WriteLine(rJob.Job.Id);
|
---|
119 |
|
---|
120 | return EXIT_OK;
|
---|
121 | }
|
---|
122 |
|
---|
123 | private static void Parameterize(IParameter param, string strValue) {
|
---|
124 | var valueParam = param as IValueParameter;
|
---|
125 | if (valueParam != null) {
|
---|
126 | var intParam = valueParam as IValueParameter<IntValue>;
|
---|
127 | if (intParam != null) {
|
---|
128 | var val = int.Parse(strValue);
|
---|
129 | intParam.Value.Value = val;
|
---|
130 | return;
|
---|
131 | }
|
---|
132 | var doubleParam = valueParam as IValueParameter<DoubleValue>;
|
---|
133 | if (doubleParam != null) {
|
---|
134 | var val = double.Parse(strValue);
|
---|
135 | doubleParam.Value.Value = val;
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | var percentParam = valueParam as IValueParameter<PercentValue>;
|
---|
139 | if (percentParam != null) {
|
---|
140 | var val = double.Parse(strValue);
|
---|
141 | percentParam.Value.Value = val;
|
---|
142 | return;
|
---|
143 | }
|
---|
144 | var boolParam = valueParam as IValueParameter<BoolValue>;
|
---|
145 | if (boolParam != null) {
|
---|
146 | var val = bool.Parse(strValue);
|
---|
147 | boolParam.Value.Value = val;
|
---|
148 | return;
|
---|
149 | }
|
---|
150 | var strParam = valueParam as IValueParameter<StringValue>;
|
---|
151 | if (strParam != null) {
|
---|
152 | strParam.Value.Value = strValue;
|
---|
153 | return;
|
---|
154 | }
|
---|
155 | #region IValueParameter<EnumValue<T>>
|
---|
156 | var isEnumParam = valueParam.Value != null && valueParam.Value.GetType().IsGenericType
|
---|
157 | && typeof(EnumValue<>).IsAssignableFrom(valueParam.Value.GetType().GetGenericTypeDefinition());
|
---|
158 | if (isEnumParam) {
|
---|
159 | dynamic enumValue = valueParam.Value; // EnumValue<T>
|
---|
160 | dynamic val = Enum.Parse(valueParam.Value.GetType().GetGenericArguments()[0], strValue);
|
---|
161 | enumValue.Value = val;
|
---|
162 | return;
|
---|
163 | }
|
---|
164 | #endregion
|
---|
165 | throw new InvalidOperationException("Unknown parameter type for parameter " + param.Name);
|
---|
166 | }
|
---|
167 |
|
---|
168 | #region IConstrainedValueParameter<T>
|
---|
169 | var isCstrParam = param != null && param.GetType().IsGenericType
|
---|
170 | && typeof(IConstrainedValueParameter<>).IsAssignableFrom(param.GetType().GetGenericTypeDefinition());
|
---|
171 | if (isCstrParam) {
|
---|
172 | // Abandon all hope, ye who enter here.
|
---|
173 | dynamic validValues = ((dynamic)param).ValidValues;
|
---|
174 | dynamic val = null;
|
---|
175 | foreach (dynamic v in validValues) {
|
---|
176 | if (v.Name == strValue) {
|
---|
177 | val = v;
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | ((dynamic)param).Value = val;
|
---|
182 | return;
|
---|
183 | }
|
---|
184 | #endregion
|
---|
185 |
|
---|
186 | throw new InvalidOperationException("Unknown parameter type for parameter " + param.Name);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|