[10753] | 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;
|
---|
[10850] | 23 | using System.Collections.Generic;
|
---|
[10753] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[10850] | 27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 30 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[10753] | 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 {
|
---|
[10754] | 37 | [Item("Single-objective Evaluator", "Calls the script's Evaluate method to get the quality value of the parameter vector.")]
|
---|
[10753] | 38 | [StorableClass]
|
---|
[10754] | 39 | public class SingleObjectiveEvaluator : SingleSuccessorOperator, ISingleObjectiveProgrammableProblemEvaluator, IStochasticOperator {
|
---|
[10753] | 40 |
|
---|
| 41 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 42 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[10754] | 45 | public ILookupParameter<SingleObjectiveScript> ScriptParameter {
|
---|
| 46 | get { return (ILookupParameter<SingleObjectiveScript>)Parameters["Script"]; }
|
---|
[10753] | 47 | }
|
---|
| 48 |
|
---|
[10850] | 49 | public ILookupParameter<Configuration> ConfigurationParameter {
|
---|
| 50 | get { return (ILookupParameter<Configuration>)Parameters["Configuration"]; }
|
---|
[10753] | 51 | }
|
---|
| 52 |
|
---|
| 53 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 54 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | [StorableConstructor]
|
---|
[10754] | 58 | protected SingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 59 | protected SingleObjectiveEvaluator(SingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 60 | public SingleObjectiveEvaluator() {
|
---|
[10753] | 61 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
[10754] | 62 | Parameters.Add(new LookupParameter<SingleObjectiveScript>("Script", "The script that will execute the evaluation function and define the parameter vector."));
|
---|
[10850] | 63 | Parameters.Add(new LookupParameter<Configuration>("Configuration", "An item that holds the problem's configuration."));
|
---|
[10753] | 64 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10754] | 68 | return new SingleObjectiveEvaluator(this, cloner);
|
---|
[10753] | 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.");
|
---|
[10850] | 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);
|
---|
[10753] | 108 | QualityParameter.ActualValue = new DoubleValue(runner.Instance.Evaluate(random, vector));
|
---|
| 109 | return base.Apply();
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|