Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization (trunk integration)/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmEvaluator.cs @ 10015

Last change on this file since 10015 was 8576, checked in by jkarder, 12 years ago

#1853: created branch for MetaOptimization (trunk integration)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.ParameterConfigurationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.MetaOptimization {
33  /// <summary>
34  /// TODO
35  /// </summary>
36  [Item("AlgorithmEvaluator", "TODO")]
37  [StorableClass]
38  public class AlgorithmEvaluator : SingleSuccessorOperator {
39    #region Parameter properties
40    public ILookupParameter<IRandom> RandomParameter {
41      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
42    }
43    public ILookupParameter<IAlgorithm> AlgorithmParameter {
44      get { return (LookupParameter<IAlgorithm>)Parameters["Algorithm"]; }
45    }
46    public ILookupParameter<IntValue> ProblemIndexParameter {
47      get { return (LookupParameter<IntValue>)Parameters["ProblemIndex"]; }
48    }
49    public ILookupParameter<IntValue> RepetitionIndexParameter {
50      get { return (LookupParameter<IntValue>)Parameters["RepetitionIndex"]; }
51    }
52    public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
53      get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
54    }
55    public LookupParameter<ResultCollection> ResultsParameter {
56      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
57    }
58    #endregion
59
60    #region Constructors and Cloning
61    public AlgorithmEvaluator()
62      : base() {
63      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used to initialize the new random permutation."));
64      Parameters.Add(new LookupParameter<IAlgorithm>("Algorithm", ""));
65      Parameters.Add(new LookupParameter<IntValue>("ProblemIndex", ""));
66      Parameters.Add(new LookupParameter<IntValue>("RepetitionIndex", ""));
67      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
68      Parameters.Add(new LookupParameter<ResultCollection>("Results", ""));
69    }
70
71    [StorableConstructor]
72    protected AlgorithmEvaluator(bool deserializing) : base(deserializing) { }
73    protected AlgorithmEvaluator(AlgorithmEvaluator original, Cloner cloner) : base(original, cloner) { }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new AlgorithmEvaluator(this, cloner);
76    }
77    #endregion
78
79    public override IOperation Apply() {
80      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
81      if (!isValidScope) return base.Apply();
82
83      ItemDictionary<StringValue, RunCollection> solutionCache =
84        ResultsParameter.ActualValue.ContainsKey("SolutionCache") ?
85        (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value :
86        null;
87      ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
88      IAlgorithm algorithm = AlgorithmParameter.ActualValue;
89      int repetitionIndex = RepetitionIndexParameter.ActualValue.Value;
90
91      if (solutionCache != null && solutionCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
92        algorithm.Runs.Add(solutionCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value.ElementAt(repetitionIndex));
93      } else {
94        algorithm.StartSync(CancellationToken);
95      }
96      return base.Apply();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.