Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmEvaluator.cs @ 16574

Last change on this file since 16574 was 16574, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.MetaOptimization addon to compile with new HL.Persistence

File size: 3.8 KB
Line 
1using System.Linq;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HEAL.Attic;
10
11namespace HeuristicLab.Problems.MetaOptimization {
12  [Item("AlgorithmEvaluator", "")]
13  [StorableType("955A1FA9-487C-4EB6-B6D4-4BE07BCD3DE3")]
14  public class AlgorithmEvaluator : SingleSuccessorOperator {
15
16    #region Parameter properties
17    public ILookupParameter<IRandom> RandomParameter {
18      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
19    }
20    public ILookupParameter<IAlgorithm> AlgorithmParameter {
21      get { return (LookupParameter<IAlgorithm>)Parameters["Algorithm"]; }
22    }
23    public ILookupParameter<IntValue> ProblemIndexParameter {
24      get { return (LookupParameter<IntValue>)Parameters["ProblemIndex"]; }
25    }
26    public ILookupParameter<IntValue> RepetitionIndexParameter {
27      get { return (LookupParameter<IntValue>)Parameters["RepetitionIndex"]; }
28    }
29    public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
30      get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
31    }
32    //public IValueParameter<IItemList<IRun>> RunsParameter {
33    //  get { return (IValueParameter<IItemList<IRun>>)Parameters["Runs"]; }
34    //}
35    public LookupParameter<ResultCollection> ResultsParameter {
36      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
37    }
38    #endregion
39
40    [StorableConstructor]
41    protected AlgorithmEvaluator(StorableConstructorFlag _) : base(_) { }
42    public AlgorithmEvaluator()
43      : base() {
44      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used to initialize the new random permutation."));
45      Parameters.Add(new LookupParameter<IAlgorithm>("Algorithm", ""));
46      Parameters.Add(new LookupParameter<IntValue>("ProblemIndex", ""));
47      Parameters.Add(new LookupParameter<IntValue>("RepetitionIndex", ""));
48      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
49      Parameters.Add(new LookupParameter<ResultCollection>("Results", ""));
50    }
51    protected AlgorithmEvaluator(AlgorithmEvaluator original, Cloner cloner) : base(original, cloner) { }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new AlgorithmEvaluator(this, cloner);
54    }
55
56    public override IOperation Apply() {
57      bool isValidScope = RepetitionIndexParameter.ActualValue != null; // this is a workaround for OSGA, where the PMOEvaluator is executed on scopes which happen to contain additional subscopes which collide with the subscopes created for the repetitions/problems in PMOEvaluator
58      if (!isValidScope) return base.Apply();
59
60      ItemDictionary<StringValue, RunCollection> solutionCache =
61        ResultsParameter.ActualValue.ContainsKey("SolutionCache") ?
62        (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value :
63        null;
64      ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
65      IAlgorithm algorithm = AlgorithmParameter.ActualValue;
66      int repetitionIndex = RepetitionIndexParameter.ActualValue.Value;
67
68      if (solutionCache != null && solutionCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
69        algorithm.Runs.Add(solutionCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value.ElementAt(repetitionIndex));
70      } else {
71        algorithm.StartSync(CancellationToken);
72      }
73      return base.Apply();
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.