#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.Programmable {
[Item("Single-objective Evaluator", "Calls the script's Evaluate method to get the quality value of the parameter vector.")]
[StorableClass]
public class SingleObjectiveEvaluator : SingleSuccessorOperator, ISingleObjectiveProgrammableProblemEvaluator, IStochasticOperator {
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public ILookupParameter ScriptParameter {
get { return (ILookupParameter)Parameters["Script"]; }
}
public ILookupParameter ConfigurationParameter {
get { return (ILookupParameter)Parameters["Configuration"]; }
}
public ILookupParameter QualityParameter {
get { return (ILookupParameter)Parameters["Quality"]; }
}
[StorableConstructor]
protected SingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
protected SingleObjectiveEvaluator(SingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
public SingleObjectiveEvaluator() {
Parameters.Add(new LookupParameter("Random", "The random number generator to use."));
Parameters.Add(new LookupParameter("Script", "The script that will execute the evaluation function and define the parameter vector."));
Parameters.Add(new LookupParameter("Configuration", "An item that holds the problem's configuration."));
Parameters.Add(new LookupParameter("Quality", "The quality of the parameter vector."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SingleObjectiveEvaluator(this, cloner);
}
public override IOperation Apply() {
var random = RandomParameter.ActualValue;
var runner = ScriptParameter.ActualValue;
if (runner.Instance == null) throw new InvalidOperationException("Script instance is null, maybe the code doesn't compile.");
var config = ConfigurationParameter.ActualValue;
var binDict = new Dictionary();
var intDict = new Dictionary();
var realDict = new Dictionary();
var permDict = new Dictionary();
foreach (var param in config.Parameters) {
var binConfig = param.Value as BinaryParameterConfiguration;
if (binConfig != null) {
binDict.Add(param.Key, (BinaryVector)ExecutionContext.Scope.Variables[param.Key].Value);
continue;
}
var intConfig = param.Value as IntegerParameterConfiguration;
if (intConfig != null) {
intDict.Add(param.Key, (IntegerVector)ExecutionContext.Scope.Variables[param.Key].Value);
continue;
}
var realConfig = param.Value as RealParameterConfiguration;
if (realConfig != null) {
realDict.Add(param.Key, (RealVector)ExecutionContext.Scope.Variables[param.Key].Value);
continue;
}
var permConfig = param.Value as PermutationParameterConfiguration;
if (permConfig != null) {
permDict.Add(param.Key, (Permutation)ExecutionContext.Scope.Variables[param.Key].Value);
continue;
}
throw new InvalidOperationException("Parameter " + param.Key + " not found.");
}
var vector = new ParameterVector(
binaryVectors: binDict.Count > 0 ? binDict : null,
integerVectors: intDict.Count > 0 ? intDict : null,
realVectors: realDict.Count > 0 ? realDict : null,
permutations: permDict.Count > 0 ? permDict : null);
QualityParameter.ActualValue = new DoubleValue(runner.Instance.Evaluate(random, vector));
return base.Apply();
}
}
}