[15896] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
| 5 | using HeuristicLab.Data;
|
---|
| 6 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 7 | using HeuristicLab.Optimization;
|
---|
| 8 | using HeuristicLab.Parameters;
|
---|
| 9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 10 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 11 |
|
---|
| 12 | namespace HeuristicLab.Networks.IntegratedOptimization.SurrogateModeling {
|
---|
| 13 | [Item("Assisted Optimizer Problem", "")]
|
---|
| 14 | [StorableClass]
|
---|
| 15 | public sealed class AssistedOptimizerProblem : SingleObjectiveBasicProblem<RealVectorEncoding> {
|
---|
| 16 | private const string RegressionSolutionParameterName = "RegressionSolution";
|
---|
| 17 | private const string OptimizeExpectedImprovementParameterName = "OptimizeExpectedImprovement";
|
---|
| 18 |
|
---|
| 19 | public IValueParameter<IRegressionSolution> RegressionSolutionParameter {
|
---|
| 20 | get { return (IValueParameter<IRegressionSolution>)Parameters[RegressionSolutionParameterName]; }
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public IValueParameter<BoolValue> OptimizeExpectedImprovementParameter {
|
---|
| 24 | get { return (IValueParameter<BoolValue>)Parameters[OptimizeExpectedImprovementParameterName]; }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | public IRegressionSolution RegressionSolution {
|
---|
| 28 | get { return RegressionSolutionParameter.Value; }
|
---|
| 29 | set { RegressionSolutionParameter.Value = value; }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | public BoolValue OptimizeExpectedImprovement {
|
---|
| 33 | get { return OptimizeExpectedImprovementParameter.Value; }
|
---|
| 34 | set { OptimizeExpectedImprovementParameter.Value = value; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public Action<RealVector, double> NotifyEvaluation { get; set; }
|
---|
| 38 |
|
---|
| 39 | public override bool Maximization {
|
---|
| 40 | get { return false; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | #region Constructors & Cloning
|
---|
| 44 | [StorableConstructor]
|
---|
| 45 | private AssistedOptimizerProblem(bool deserializing) : base(deserializing) { }
|
---|
| 46 | private AssistedOptimizerProblem(AssistedOptimizerProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
| 47 | public AssistedOptimizerProblem() {
|
---|
| 48 | Parameters.Add(new ValueParameter<IRegressionSolution>(RegressionSolutionParameterName, ""));
|
---|
| 49 | Parameters.Add(new ValueParameter<BoolValue>(OptimizeExpectedImprovementParameterName, "", new BoolValue(true)));
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 53 | return new AssistedOptimizerProblem(this, cloner);
|
---|
| 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
| 58 | var point = individual.RealVector();
|
---|
| 59 | var solution = RegressionSolutionParameter.Value;
|
---|
| 60 | var useExpectedImprovement = OptimizeExpectedImprovement.Value;
|
---|
| 61 |
|
---|
| 62 | var result = ExpectedImprovementHelpers.Evaluate(new[] { point }, solution, useExpectedImprovement).Single();
|
---|
| 63 | var target = useExpectedImprovement ? -result : result;
|
---|
| 64 |
|
---|
| 65 | NotifyEvaluation((RealVector)point.Clone(), target);
|
---|
| 66 |
|
---|
| 67 | return target;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 71 | base.Analyze(individuals, qualities, results, random);
|
---|
| 72 |
|
---|
| 73 | var bestIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).MinItems(x => x.Quality);
|
---|
| 74 | var best = bestIndividuals.First();
|
---|
| 75 |
|
---|
| 76 | IResult bestSolutionResult;
|
---|
| 77 | if (!results.TryGetValue("BestSolution", out bestSolutionResult)) {
|
---|
| 78 | results.Add(bestSolutionResult = new Result("BestSolution", typeof(RealVector)));
|
---|
| 79 | } else bestSolutionResult = results["BestSolution"];
|
---|
| 80 |
|
---|
| 81 | IResult bestQualityResult;
|
---|
| 82 | if (!results.TryGetValue("BestQuality", out bestQualityResult)) {
|
---|
| 83 | results.Add(bestQualityResult = new Result("BestQuality", typeof(DoubleValue)));
|
---|
| 84 | } else bestQualityResult = results["BestQuality"];
|
---|
| 85 |
|
---|
| 86 | var bestQuality = (DoubleValue)bestQualityResult.Value;
|
---|
| 87 | if (bestQuality == null || best.Quality <= bestQuality.Value) {
|
---|
| 88 | bestSolutionResult.Value = (RealVector)best.Individual.RealVector().Clone();
|
---|
| 89 | bestQualityResult.Value = new DoubleValue(best.Quality);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|