#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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.Threading;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Algorithms.EGO {
[Item("InfillSolver", "A RealVectorCreator that creates candidates by optimizing an infill-subproblem")]
[StorableClass]
public class InfillSolver : RealVectorCreator, ICancellableOperator {
public ILookupParameter InfillOptimizationAlgorithmParamter => (ILookupParameter)Parameters["InfillAlgorithm"];
public ILookupParameter ModelParameter => (ILookupParameter)Parameters["Model"];
public ILookupParameter MaximizationParameter => (ILookupParameter)Parameters["Maximization"];
public ILookupParameter RemoveDuplicatesParameter => (ILookupParameter)Parameters["RemoveDuplicates"];
public IFixedValueParameter DuplicateCutoffParameter => (IFixedValueParameter)Parameters["Duplicates Cutoff"];
public ILookupParameter InfillBoundsParameter => (ILookupParameter)Parameters["InfillBounds"];
public CancellationToken Cancellation { get; set; }
[StorableConstructor]
protected InfillSolver(bool deserializing) : base(deserializing) { }
protected InfillSolver(InfillSolver original, Cloner cloner) : base(original, cloner) { }
public InfillSolver() {
Parameters.Add(new LookupParameter("InfillAlgorithm", "The algorithm used to optimize the infill problem") { Hidden = true });
Parameters.Add(new LookupParameter("Model", "The RegressionSolution upon which the InfillProblem operates") { Hidden = true });
Parameters.Add(new LookupParameter("Maximization", "Whether the original problem is a maximization problem") { Hidden = true });
Parameters.Add(new LookupParameter("RemoveDuplicates", "Whether duplicates shall be removed") { Hidden = true });
Parameters.Add(new FixedValueParameter("Duplicates Cutoff", "The cut off radius for", new DoubleValue(0.01)) { Hidden = false });
Parameters.Add(new LookupParameter("InfillBounds", "The bounds applied for infill solving") { Hidden = true });
}
public override IDeepCloneable Clone(Cloner cloner) {
return new InfillSolver(this, cloner);
}
protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) {
var infillBounds = InfillBoundsParameter.ActualValue;
if (infillBounds != null && infillBounds.Rows > 0) {
bounds = infillBounds;
}
var alg = InfillOptimizationAlgorithmParamter.ActualValue;
var model = ModelParameter.ActualValue;
var max = MaximizationParameter.ActualValue.Value;
var res = OptimizeInfillProblem(alg, model, max, bounds, length.Value, random);
var rad = DuplicateCutoffParameter.Value.Value;
if (!RemoveDuplicatesParameter.ActualValue.Value || !(GetMinDifference(model.ProblemData.Dataset, res) < rad * rad)) return res;
for (var i = 0; i < res.Length; i++) res[i] += random.NextDouble() * rad * 2;
return res;
}
private RealVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, DoubleMatrix bounds, int length, IRandom random) {
var infillProblem = algorithm.Problem as InfillProblem;
if (infillProblem == null) throw new ArgumentException("The algortihm has no InfillProblem to solve");
infillProblem.Encoding.Length = length;
infillProblem.Encoding.Bounds = bounds;
infillProblem.Initialize(model, maximization);
var res = EgoUtilities.SyncRunSubAlgorithm(algorithm, random.Next(int.MaxValue), Cancellation);
var v = res[InfillProblem.BestInfillSolutionResultName].Value as RealVector;
algorithm.Runs.Clear();
return v;
}
private static double GetMinDifference(IDataset data, RealVector r) {
var mind = double.MaxValue;
for (var i = 0; i < data.Rows; i++) {
var d = 0.0;
for (var j = 0; j < r.Length; j++) {
var d2 = data.GetDoubleValue("input" + j, i) - r[j];
d += d2 * d2;
}
if (!(d < mind)) continue;
mind = d;
}
return mind;
}
}
}