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

Last change on this file since 6473 was 6473, checked in by cneumuel, 11 years ago

#1215

  • adaptions for OSGA
File size: 4.1 KB
Line 
1using System.Linq;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6using HeuristicLab.Operators;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10
11namespace HeuristicLab.Problems.MetaOptimization {
12  [Item("AlgorithmEvaluator", "")]
13  [StorableClass]
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(bool deserializing) : base(deserializing) { }
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 = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value : null;
61      ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
62      IAlgorithm algorithm = AlgorithmParameter.ActualValue;
63      int repetitionIndex = RepetitionIndexParameter.ActualValue.Value;
64
65      if (solutionCache != null && solutionCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
66        algorithm.Runs.Add(solutionCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value.ElementAt(repetitionIndex));
67      } else {
68        algorithm.StartSync(CancellationToken);
69      }
70      return base.Apply();
71    }
72
73    // for debug purposes, remove later
74    private static string PrintGrammar(ISymbolicExpressionGrammar grammar) {
75      return string.Join(string.Empty, grammar.Symbols.Select(x => x.InitialFrequency).ToArray());
76      //foreach (var symbol in grammar.Symbols) {
77      //  Console.WriteLine("{0} ({1})", symbol.ToString(), symbol.InitialFrequency);
78      //}
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.