Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6090 was 6090, checked in by cneumuel, 13 years ago

#1215

  • added weight parameters for quality, stddev and evaluated solutions
  • lots of fixes
File size: 3.8 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      ItemDictionary<StringValue, RunCollection> solutionCache = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value : null;
58      ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
59      IAlgorithm algorithm = AlgorithmParameter.ActualValue;
60      int repetitionIndex = RepetitionIndexParameter.ActualValue.Value;
61
62      if (solutionCache != null && solutionCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
63        algorithm.Runs.Add(solutionCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value.ElementAt(repetitionIndex));
64      } else {
65        algorithm.StartSync(CancellationToken);
66      }
67      return base.Apply();
68    }
69
70    // for debug purposes, remove later
71    private static string PrintGrammar(ISymbolicExpressionGrammar grammar) {
72      return string.Join(string.Empty, grammar.Symbols.Select(x => x.InitialFrequency).ToArray());
73      //foreach (var symbol in grammar.Symbols) {
74      //  Console.WriteLine("{0} ({1})", symbol.ToString(), symbol.InitialFrequency);
75      //}
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.