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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
30 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
31 | using HeuristicLab.Operators;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.Programmable {
|
---|
37 | [Item("Single-objective Evaluator", "Calls the script's Evaluate method to get the quality value of the parameter vector.")]
|
---|
38 | [StorableClass]
|
---|
39 | public class SingleObjectiveEvaluator : SingleSuccessorOperator, ISingleObjectiveProgrammableProblemEvaluator, 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 | [StorableConstructor]
|
---|
58 | protected SingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
59 | protected SingleObjectiveEvaluator(SingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
60 | public SingleObjectiveEvaluator() {
|
---|
61 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
62 | Parameters.Add(new LookupParameter<SingleObjectiveScript>("Script", "The script that will execute the evaluation function and define the parameter vector."));
|
---|
63 | Parameters.Add(new LookupParameter<Configuration>("Configuration", "An item that holds the problem's configuration."));
|
---|
64 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
68 | return new SingleObjectiveEvaluator(this, cloner);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override IOperation Apply() {
|
---|
72 | var random = RandomParameter.ActualValue;
|
---|
73 | var runner = ScriptParameter.ActualValue;
|
---|
74 | if (runner.Instance == null) throw new InvalidOperationException("Script instance is null, maybe the code doesn't compile.");
|
---|
75 | var config = ConfigurationParameter.ActualValue;
|
---|
76 | var binDict = new Dictionary<string, BinaryVector>();
|
---|
77 | var intDict = new Dictionary<string, IntegerVector>();
|
---|
78 | var realDict = new Dictionary<string, RealVector>();
|
---|
79 | var permDict = new Dictionary<string, Permutation>();
|
---|
80 | foreach (var param in config.Parameters) {
|
---|
81 | var binConfig = param.Value as BinaryParameterConfiguration;
|
---|
82 | if (binConfig != null) {
|
---|
83 | binDict.Add(param.Key, (BinaryVector)ExecutionContext.Scope.Variables[param.Key].Value);
|
---|
84 | continue;
|
---|
85 | }
|
---|
86 | var intConfig = param.Value as IntegerParameterConfiguration;
|
---|
87 | if (intConfig != null) {
|
---|
88 | intDict.Add(param.Key, (IntegerVector)ExecutionContext.Scope.Variables[param.Key].Value);
|
---|
89 | continue;
|
---|
90 | }
|
---|
91 | var realConfig = param.Value as RealParameterConfiguration;
|
---|
92 | if (realConfig != null) {
|
---|
93 | realDict.Add(param.Key, (RealVector)ExecutionContext.Scope.Variables[param.Key].Value);
|
---|
94 | continue;
|
---|
95 | }
|
---|
96 | var permConfig = param.Value as PermutationParameterConfiguration;
|
---|
97 | if (permConfig != null) {
|
---|
98 | permDict.Add(param.Key, (Permutation)ExecutionContext.Scope.Variables[param.Key].Value);
|
---|
99 | continue;
|
---|
100 | }
|
---|
101 | throw new InvalidOperationException("Parameter " + param.Key + " not found.");
|
---|
102 | }
|
---|
103 | var vector = new ParameterVector(
|
---|
104 | binaryVectors: binDict.Count > 0 ? binDict : null,
|
---|
105 | integerVectors: intDict.Count > 0 ? intDict : null,
|
---|
106 | realVectors: realDict.Count > 0 ? realDict : null,
|
---|
107 | permutations: permDict.Count > 0 ? permDict : null);
|
---|
108 | QualityParameter.ActualValue = new DoubleValue(runner.Instance.Evaluate(random, vector));
|
---|
109 | return base.Apply();
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|