#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using CommandLine;
using HeuristicLab.Clients.Hive;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.PluginInfrastructure;
using HeuristicLab.PluginInfrastructure.Manager;
namespace HiveIraceSubmit {
class Program {
public const int EXIT_OK = 0;
public const int EXIT_ERR = 1;
static void Main(string[] args) {
//AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "HiveIraceSubmit.exe.config");
var result = Parser.Default.ParseArguments(args)
.WithParsed(x => {
try {
Environment.Exit(Initialize(x));
} catch (Exception e) {
Console.Error.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
if (e.InnerException != null) {
Console.Error.WriteLine("Inner Exception");
Console.Error.WriteLine(e.InnerException.Message + Environment.NewLine + e.InnerException.StackTrace);
}
Environment.Exit(EXIT_ERR);
}
})
.WithNotParsed(x => Environment.Exit(EXIT_ERR));
}
private static int Initialize(Options opts) {
#if __DRY_RUN__
Console.WriteLine(Guid.NewGuid());
return EXIT_OK;
#endif
var pluginManager = new PluginManager(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]));
pluginManager.DiscoverAndCheckPlugins();
var appManagerType = Assembly.GetAssembly(typeof(IPlugin)).DefinedTypes.Single(x => x.Name == "DefaultApplicationManager");
var appManager = Activator.CreateInstance(appManagerType, true);
var method = appManagerType.GetMethod("PrepareApplicationDomain", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(appManager, new object[] { pluginManager.Applications, pluginManager.Plugins });
ContentManager.Initialize(new PersistenceContentManager());
return Run(opts);
}
private static int Run(Options opts) {
HiveClient.Instance.Refresh();
var file = Path.Combine(opts.ProblemInstance, opts.Algorithm);
var optimizer = ContentManager.Load(file) as IAlgorithm;
if (optimizer == null) {
Console.Error.WriteLine("File does not contain item of type IAlgorithm");
return EXIT_ERR;
}
foreach (var config in opts.Configuration) {
var split = config.Split('=');
var paramPath = split[0].Split('.');
var strValue = split[1].Trim();
var paramItem = optimizer as IParameterizedItem;
for (var i = 0; i < paramPath.Length - 1; i++) {
var p = paramItem.Parameters[paramPath[i]];
if (p is IValueParameter) paramItem = (IParameterizedItem)((IValueParameter)p).Value;
else paramItem = (IParameterizedItem)((dynamic)p).Value;
}
var param = paramItem.Parameters[paramPath.Last()];
Parameterize(param, strValue);
}
var hiveTask = ItemTask.GetItemTaskForItem(optimizer);
var task = hiveTask.CreateHiveTask();
var rJob = new RefreshableJob();
rJob.Job.Name = Properties.Settings.Default.JobPrefix + " " + string.Join("-", opts.ConfigId, opts.InstanceId, opts.Seed);
rJob.Job.Description = opts.Algorithm + " " + string.Join(", ", opts.Configuration);
rJob.Job.ResourceIds = new List() { Guid.Parse(Properties.Settings.Default.HiveResources) };
//rJob.Job.ResourceNames = Properties.Settings.Default.HiveResources;
rJob.HiveTasks.Add(task);
HiveClient.Store(rJob, CancellationToken.None);
//HiveClient.StartJob(_ => { }, rJob, CancellationToken.None); // this is async
//HiveClient.LoadJob(rJob);
Console.WriteLine(rJob.Job.Id);
return EXIT_OK;
}
private static void Parameterize(IParameter param, string strValue) {
var valueParam = param as IValueParameter;
if (valueParam != null) {
var intParam = valueParam as IValueParameter;
if (intParam != null) {
var val = int.Parse(strValue);
intParam.Value.Value = val;
return;
}
var doubleParam = valueParam as IValueParameter;
if (doubleParam != null) {
var val = double.Parse(strValue);
doubleParam.Value.Value = val;
return;
}
var percentParam = valueParam as IValueParameter;
if (percentParam != null) {
var val = double.Parse(strValue);
percentParam.Value.Value = val;
return;
}
var boolParam = valueParam as IValueParameter;
if (boolParam != null) {
var val = bool.Parse(strValue);
boolParam.Value.Value = val;
return;
}
var strParam = valueParam as IValueParameter;
if (strParam != null) {
strParam.Value.Value = strValue;
return;
}
#region IValueParameter>
var isEnumParam = valueParam.Value != null && valueParam.Value.GetType().IsGenericType
&& typeof(EnumValue<>).IsAssignableFrom(valueParam.Value.GetType().GetGenericTypeDefinition());
if (isEnumParam) {
dynamic enumValue = valueParam.Value; // EnumValue
dynamic val = Enum.Parse(valueParam.Value.GetType().GetGenericArguments()[0], strValue);
enumValue.Value = val;
return;
}
#endregion
throw new InvalidOperationException("Unknown parameter type for parameter " + param.Name);
}
#region IConstrainedValueParameter
var isCstrParam = param != null && param.GetType().IsGenericType
&& typeof(IConstrainedValueParameter<>).IsAssignableFrom(param.GetType().GetGenericTypeDefinition());
if (isCstrParam) {
// Abandon all hope, ye who enter here.
dynamic validValues = ((dynamic)param).ValidValues;
dynamic val = null;
foreach (dynamic v in validValues) {
if (v.Name == strValue) {
val = v;
break;
}
}
((dynamic)param).Value = val;
return;
}
#endregion
throw new InvalidOperationException("Unknown parameter type for parameter " + param.Name);
}
}
}