Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmEvaluator.cs @ 11255

Last change on this file since 11255 was 7253, checked in by gkronber, 13 years ago

#1727 set the number of evaluated solutions to 1 if the result is missing in the algorithm.

File size: 3.7 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;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  [Item("AlgorithmEvaluator", "")]
12  [StorableClass]
13  public class AlgorithmEvaluator : SingleSuccessorOperator {
14
15    #region Parameter properties
16    public ILookupParameter<IRandom> RandomParameter {
17      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
18    }
19    public ILookupParameter<IAlgorithm> AlgorithmParameter {
20      get { return (LookupParameter<IAlgorithm>)Parameters["Algorithm"]; }
21    }
22    public ILookupParameter<IntValue> ProblemIndexParameter {
23      get { return (LookupParameter<IntValue>)Parameters["ProblemIndex"]; }
24    }
25    public ILookupParameter<IntValue> RepetitionIndexParameter {
26      get { return (LookupParameter<IntValue>)Parameters["RepetitionIndex"]; }
27    }
28    public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
29      get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
30    }
31    //public IValueParameter<IItemList<IRun>> RunsParameter {
32    //  get { return (IValueParameter<IItemList<IRun>>)Parameters["Runs"]; }
33    //}
34    public LookupParameter<ResultCollection> ResultsParameter {
35      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
36    }
37    #endregion
38
39    [StorableConstructor]
40    protected AlgorithmEvaluator(bool deserializing) : base(deserializing) { }
41    public AlgorithmEvaluator()
42      : base() {
43      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used to initialize the new random permutation."));
44      Parameters.Add(new LookupParameter<IAlgorithm>("Algorithm", ""));
45      Parameters.Add(new LookupParameter<IntValue>("ProblemIndex", ""));
46      Parameters.Add(new LookupParameter<IntValue>("RepetitionIndex", ""));
47      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
48      Parameters.Add(new LookupParameter<ResultCollection>("Results", ""));
49    }
50    protected AlgorithmEvaluator(AlgorithmEvaluator original, Cloner cloner) : base(original, cloner) { }
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new AlgorithmEvaluator(this, cloner);
53    }
54
55    public override IOperation Apply() {
56      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
57      if (!isValidScope) return base.Apply();
58
59      ItemDictionary<StringValue, RunCollection> solutionCache =
60        ResultsParameter.ActualValue.ContainsKey("SolutionCache") ?
61        (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value :
62        null;
63      ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
64      IAlgorithm algorithm = AlgorithmParameter.ActualValue;
65      int repetitionIndex = RepetitionIndexParameter.ActualValue.Value;
66
67      if (solutionCache != null && solutionCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
68        algorithm.Runs.Add(solutionCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value.ElementAt(repetitionIndex));
69      } else {
70        algorithm.StartSync(CancellationToken);
71      }
72      return base.Apply();
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.