#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.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.RealVectorEncoding; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.EGO { [StorableClass] [Item("InfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")] public sealed class InfillProblem : SingleObjectiveBasicProblem { public override bool Maximization => true; #region ProblemResultNames public const string BestInfillSolutionResultName = "BestInfillSolution"; public const string BestInfillQualityResultName = "BestInfillQuality"; #endregion #region Properties [Storable] private IInfillCriterion infillCriterion; public IInfillCriterion InfillCriterion { get { return infillCriterion; } set { infillCriterion = value; infillCriterion.Encoding = Encoding; } } #endregion #region Constructors [StorableConstructor] private InfillProblem(bool deserializing) : base(deserializing) { } private InfillProblem(InfillProblem original, Cloner cloner) : base(original, cloner) { infillCriterion = cloner.Clone(original.infillCriterion); } public InfillProblem() { } public override IDeepCloneable Clone(Cloner cloner) { return new InfillProblem(this, cloner); } #endregion public override double Evaluate(Individual individual, IRandom r) { return !InBounds(individual.RealVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.RealVector()); } public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { base.Analyze(individuals, qualities, results, random); var best = qualities.ArgMax(x => x); var newQuality = qualities[best]; if (!results.ContainsKey(BestInfillQualityResultName)) { results.Add(new Result(BestInfillSolutionResultName, (RealVector)individuals[best].RealVector().Clone())); results.Add(new Result(BestInfillQualityResultName, new DoubleValue(newQuality))); return; } var qold = results[BestInfillQualityResultName].Value as DoubleValue; if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?"); if (qold.Value >= newQuality) return; results[BestInfillSolutionResultName].Value = (RealVector)individuals[best].RealVector().Clone(); qold.Value = newQuality; } public override IEnumerable GetNeighbors(Individual individual, IRandom random) { var bounds = Encoding.Bounds; var michalewiczIteration = 0; while (true) { var neighbour = individual.Copy(); var r = neighbour.RealVector(); switch (random.Next(5)) { case 0: UniformOnePositionManipulator.Apply(random, r, bounds); break; case 1: UniformOnePositionManipulator.Apply(random, r, bounds); break;//FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break; case 2: MichalewiczNonUniformAllPositionsManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break; case 3: MichalewiczNonUniformOnePositionManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break; case 4: BreederGeneticAlgorithmManipulator.Apply(random, r, bounds, new DoubleValue(0.1)); break; default: throw new NotImplementedException(); } yield return neighbour; michalewiczIteration %= 10000; } } public void Initialize(IRegressionSolution model, bool expensiveMaximization) { infillCriterion.RegressionSolution = model; infillCriterion.ExpensiveMaximization = expensiveMaximization; infillCriterion.Encoding = Encoding; infillCriterion.Initialize(); } #region helpers private static bool InBounds(RealVector r, DoubleMatrix bounds) { return !r.Where((t, i) => t < bounds[i % bounds.Rows, 0] || t > bounds[i % bounds.Rows, 1]).Any(); } #endregion } }