Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3133_ProblemModifiers/HeuristicLab.Problems.Modifiers/Hive/HiveEvaluationProblemModifier.cs @ 18029

Last change on this file since 18029 was 18029, checked in by bwerth, 3 years ago

#3133 added implementation of problem modifiers

File size: 5.6 KB
Line 
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
22using System;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.Modifiers {
32  [StorableType("1D86AA34-3ACE-432B-9615-8972BE0D9724")]
33  [Item("HiveEvaluationProblemModifier", "A problem modifier that logs evaluations in a file")]
34  public class HiveEvaluationProblemModifier : ProblemModifier {
35    private readonly HiveEvaluationResultPoller<ModifiedBasicProblemEvaluationExecutionAlgorithm> poller = new HiveEvaluationResultPoller<ModifiedBasicProblemEvaluationExecutionAlgorithm>(TimeSpan.FromSeconds(40));
36
37    #region ParameterNames
38    private const string ProjectResourcesParameterName = "ProjectResources";
39    private const string DistributeTasksParameterName = "DistributeTasks";
40    private const string PollerLogParameterName = "LogFile";
41    #endregion
42
43    #region Parameters
44    public IValueParameter<ProjectResourcesValue> ProjectResourcesParameter => (IValueParameter<ProjectResourcesValue>)Parameters[ProjectResourcesParameterName];
45    public IFixedValueParameter<BoolValue> DistributeTasksParameter => (IFixedValueParameter<BoolValue>)Parameters[DistributeTasksParameterName];
46    public IFixedValueParameter<FileValue> PollerLogParameter => (IFixedValueParameter<FileValue>)Parameters[PollerLogParameterName];
47    #endregion
48
49    #region ParameterProperties
50    public ProjectResourcesValue ProjectResources => ProjectResourcesParameter.Value;
51    public bool DistributeTasks => DistributeTasksParameter.Value.Value;
52    public FileValue PollerLog => PollerLogParameter.Value;
53    #endregion
54
55    #region Constructors & Cloning
56    [StorableConstructor]
57    protected HiveEvaluationProblemModifier(StorableConstructorFlag _) : base(_) { }
58
59    protected HiveEvaluationProblemModifier(HiveEvaluationProblemModifier original, Cloner cloner) : base(original,
60      cloner) {
61      Initialize();
62    }
63    public HiveEvaluationProblemModifier() {
64      Parameters.Add(new ValueParameter<ProjectResourcesValue>(ProjectResourcesParameterName, "The project/resources where Hive jobs/tasks should be assigned to."));
65      Parameters.Add(new FixedValueParameter<BoolValue>(DistributeTasksParameterName, "Has currently no effect"));
66      Parameters.Add(new FixedValueParameter<FileValue>(PollerLogParameterName, "Where to log Hive related exceptions", new FileValue()));
67      Initialize();
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new HiveEvaluationProblemModifier(this, cloner);
71    }
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserialization() {
74      if (!Parameters.ContainsKey(PollerLogParameterName))
75        Parameters.Add(new FixedValueParameter<FileValue>(PollerLogParameterName, "Where to log Hive related exceptions", new FileValue()));
76      PollerLogParameter.Value.Value = poller.LogFile;
77      Initialize();
78    }
79    #endregion
80
81    public override double[] ModifiedEvaluate(Individual individual, IRandom random) {
82      var individualScope = new Scope();
83      foreach (var value in individual.Values)
84        individualScope.Variables.Add(new Variable(value.Key, value.Value));
85      var evaluation = new ModifiedBasicProblemEvaluationExecutionAlgorithm(NextModifier, individualScope, random) { Problem = new MultiObjectiveModifiedProblem() };//this is just so that algorithm.problem is not null
86      evaluation.Prepare();
87      var options = new HiveApi.Options {
88        ProjectId = ProjectResources.ProjectId,
89        ResourceIds = ProjectResources.ResourceIds,
90        Distribute = DistributeTasks,
91        JobName = $"{evaluation}+{Guid.NewGuid().ToString().ToUpperInvariant()}"
92      };
93
94      try {
95        var taskCompletionSource = poller.StartEvaluation(evaluation, options);
96        taskCompletionSource.Task.Wait();
97        if (taskCompletionSource.Task.Status == System.Threading.Tasks.TaskStatus.Faulted)
98          if (taskCompletionSource.Task.Exception != null)
99            throw taskCompletionSource.Task.Exception;
100        evaluation = taskCompletionSource.Task.Result;
101      } catch (Exception e) {
102        individual["Exception"] = new StringValue("ERROR: " + e.ToString().Replace(Environment.NewLine, " // "));
103        return Maximization.Select(x => x ? double.MinValue : double.MaxValue).ToArray();
104      }
105
106      foreach (var variable in evaluation.IndividualScope.Variables)
107        individual[variable.Name] = variable.Value;
108      return evaluation.Qualities;
109    }
110
111
112    public override void Initialize() {
113      base.Initialize();
114      PollerLogParameter.Value.ToStringChanged += (o, e) => { poller.LogFile = PollerLog.Value; };
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.