#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.Linq;
using HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
namespace HeuristicLab.Problems.Modifiers {
[StorableType("1D86AA34-3ACE-432B-9615-8972BE0D9724")]
[Item("HiveEvaluationProblemModifier", "A problem modifier that logs evaluations in a file")]
public class HiveEvaluationProblemModifier : ProblemModifier {
private readonly HiveEvaluationResultPoller poller = new HiveEvaluationResultPoller(TimeSpan.FromSeconds(40));
#region ParameterNames
private const string ProjectResourcesParameterName = "ProjectResources";
private const string DistributeTasksParameterName = "DistributeTasks";
private const string PollerLogParameterName = "LogFile";
#endregion
#region Parameters
public IValueParameter ProjectResourcesParameter => (IValueParameter)Parameters[ProjectResourcesParameterName];
public IFixedValueParameter DistributeTasksParameter => (IFixedValueParameter)Parameters[DistributeTasksParameterName];
public IFixedValueParameter PollerLogParameter => (IFixedValueParameter)Parameters[PollerLogParameterName];
#endregion
#region ParameterProperties
public ProjectResourcesValue ProjectResources => ProjectResourcesParameter.Value;
public bool DistributeTasks => DistributeTasksParameter.Value.Value;
public FileValue PollerLog => PollerLogParameter.Value;
#endregion
#region Constructors & Cloning
[StorableConstructor]
protected HiveEvaluationProblemModifier(StorableConstructorFlag _) : base(_) { }
protected HiveEvaluationProblemModifier(HiveEvaluationProblemModifier original, Cloner cloner) : base(original,
cloner) {
Initialize();
}
public HiveEvaluationProblemModifier() {
Parameters.Add(new ValueParameter(ProjectResourcesParameterName, "The project/resources where Hive jobs/tasks should be assigned to."));
Parameters.Add(new FixedValueParameter(DistributeTasksParameterName, "Has currently no effect"));
Parameters.Add(new FixedValueParameter(PollerLogParameterName, "Where to log Hive related exceptions", new FileValue()));
Initialize();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new HiveEvaluationProblemModifier(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
if (!Parameters.ContainsKey(PollerLogParameterName))
Parameters.Add(new FixedValueParameter(PollerLogParameterName, "Where to log Hive related exceptions", new FileValue()));
PollerLogParameter.Value.Value = poller.LogFile;
Initialize();
}
#endregion
public override double[] ModifiedEvaluate(Individual individual, IRandom random) {
var individualScope = new Scope();
foreach (var value in individual.Values)
individualScope.Variables.Add(new Variable(value.Key, value.Value));
var evaluation = new ModifiedBasicProblemEvaluationExecutionAlgorithm(NextModifier, individualScope, random) { Problem = new MultiObjectiveModifiedProblem() };//this is just so that algorithm.problem is not null
evaluation.Prepare();
var options = new HiveApi.Options {
ProjectId = ProjectResources.ProjectId,
ResourceIds = ProjectResources.ResourceIds,
Distribute = DistributeTasks,
JobName = $"{evaluation}+{Guid.NewGuid().ToString().ToUpperInvariant()}"
};
try {
var taskCompletionSource = poller.StartEvaluation(evaluation, options);
taskCompletionSource.Task.Wait();
if (taskCompletionSource.Task.Status == System.Threading.Tasks.TaskStatus.Faulted)
if (taskCompletionSource.Task.Exception != null)
throw taskCompletionSource.Task.Exception;
evaluation = taskCompletionSource.Task.Result;
} catch (Exception e) {
individual["Exception"] = new StringValue("ERROR: " + e.ToString().Replace(Environment.NewLine, " // "));
return Maximization.Select(x => x ? double.MinValue : double.MaxValue).ToArray();
}
foreach (var variable in evaluation.IndividualScope.Variables)
individual[variable.Name] = variable.Value;
return evaluation.Qualities;
}
public override void Initialize() {
base.Initialize();
PollerLogParameter.Value.ToStringChanged += (o, e) => { poller.LogFile = PollerLog.Value; };
}
}
}