Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorMoveEvaluator.cs @ 11369

Last change on this file since 11369 was 11363, checked in by abeham, 10 years ago

#2174:

  • Removed SimSharp reference (not the purpose of this branch anymore)
  • Fixed bugs regarding parameter names when no parameter have been defined
  • Added a method to the problem definition to retrieve a neighborhood solution
    • Programmable problem now works with LocalSearch and SimulatedAnnealing
File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Encodings.PermutationEncoding;
30using HeuristicLab.Encodings.RealVectorEncoding;
31using HeuristicLab.Operators;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35
36namespace HeuristicLab.Problems.Programmable {
37  [Item("ParameterVectorMoveEvaluator", "Evaluates a parameter vector that results from a move.")]
38  [StorableClass]
39  public class ParameterVectorMoveEvaluator : InstrumentedOperator, IParameterVectorMoveOperator, ISingleObjectiveMoveEvaluator, IStochasticOperator {
40
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
43    }
44
45    public ILookupParameter<SingleObjectiveScript> ScriptParameter {
46      get { return (ILookupParameter<SingleObjectiveScript>)Parameters["Script"]; }
47    }
48
49    public ILookupParameter<Configuration> ConfigurationParameter {
50      get { return (ILookupParameter<Configuration>)Parameters["Configuration"]; }
51    }
52
53    public ILookupParameter<DoubleValue> QualityParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
55    }
56
57    public ILookupParameter<DoubleValue> MoveQualityParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
59    }
60
61    [StorableConstructor]
62    protected ParameterVectorMoveEvaluator(bool deserializing) : base(deserializing) { }
63    protected ParameterVectorMoveEvaluator(ParameterVectorMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
64    public ParameterVectorMoveEvaluator() {
65      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
66      Parameters.Add(new LookupParameter<SingleObjectiveScript>("Script", "The script that will execute the evaluation function and define the parameter vector."));
67      Parameters.Add(new LookupParameter<Configuration>("Configuration", "An item that holds the problem's configuration."));
68      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
69      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move."));
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new ParameterVectorMoveEvaluator(this, cloner);
74    }
75
76    public override IOperation InstrumentedApply() {
77      var random = RandomParameter.ActualValue;
78      var runner = ScriptParameter.ActualValue;
79      if (runner.Instance == null) throw new InvalidOperationException("Script instance is null, maybe the code doesn't compile.");
80      var config = ConfigurationParameter.ActualValue;
81      var binDict = new Dictionary<string, BinaryVector>();
82      var intDict = new Dictionary<string, IntegerVector>();
83      var realDict = new Dictionary<string, RealVector>();
84      var permDict = new Dictionary<string, Permutation>();
85      foreach (var param in config.Parameters) {
86        var binConfig = param.Value as BinaryParameterConfiguration;
87        if (binConfig != null) {
88          binDict.Add(param.Key, (BinaryVector)ExecutionContext.Scope.Variables[param.Key].Value);
89          continue;
90        }
91        var intConfig = param.Value as IntegerParameterConfiguration;
92        if (intConfig != null) {
93          intDict.Add(param.Key, (IntegerVector)ExecutionContext.Scope.Variables[param.Key].Value);
94          continue;
95        }
96        var realConfig = param.Value as RealParameterConfiguration;
97        if (realConfig != null) {
98          realDict.Add(param.Key, (RealVector)ExecutionContext.Scope.Variables[param.Key].Value);
99          continue;
100        }
101        var permConfig = param.Value as PermutationParameterConfiguration;
102        if (permConfig != null) {
103          permDict.Add(param.Key, (Permutation)ExecutionContext.Scope.Variables[param.Key].Value);
104          continue;
105        }
106        throw new InvalidOperationException("Parameter " + param.Key + " not found.");
107      }
108      var vector = new ParameterVector(
109        binaryVectors: binDict.Count > 0 ? binDict : null,
110        integerVectors: intDict.Count > 0 ? intDict : null,
111        realVectors: realDict.Count > 0 ? realDict : null,
112        permutations: permDict.Count > 0 ? permDict : null);
113      MoveQualityParameter.ActualValue = new DoubleValue(runner.Instance.Evaluate(random, vector));
114      return base.InstrumentedApply();
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.