1 | using System.Linq;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
6 | using HeuristicLab.Operators;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 |
|
---|
11 | namespace 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 | }
|
---|