#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.IntegerVectorEncoding; using HeuristicLab.Encodings.RealVectorEncoding; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.EGO { [StorableClass] [Item("DiscreteInfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")] public sealed class DiscreteInfillProblem : 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 = GetRealVectorEncoding(Encoding); } } #endregion #region Constructors [StorableConstructor] private DiscreteInfillProblem(bool deserializing) : base(deserializing) { } private DiscreteInfillProblem(DiscreteInfillProblem original, Cloner cloner) : base(original, cloner) { infillCriterion = cloner.Clone(original.infillCriterion); } public DiscreteInfillProblem() { } public override IDeepCloneable Clone(Cloner cloner) { return new DiscreteInfillProblem(this, cloner); } #endregion public override double Evaluate(Individual individual, IRandom r) { return !InBounds(individual.IntegerVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.IntegerVector().ToRealVector()); } 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, (IntegerVector)individuals[best].IntegerVector().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 = (IntegerVector)individuals[best].IntegerVector().Clone(); qold.Value = newQuality; } public override IEnumerable GetNeighbors(Individual individual, IRandom random) { var bounds = Encoding.Bounds; var michalewiczIteration = 0; var sigma = new DoubleArray(new double[] { 1.0 }); while (true) { var neighbour = individual.Copy(); var r = neighbour.IntegerVector(); switch (random.Next(3) % 3) { case 0: HeuristicLab.Encodings.IntegerVectorEncoding.UniformOnePositionManipulator.Apply(random, r, bounds); break; case 1: HeuristicLab.Encodings.IntegerVectorEncoding.RoundedNormalAllPositionsManipulator.Apply(random, r, bounds, sigma); break;//FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break; case 2: HeuristicLab.Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator.Apply(random, r, bounds, 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 = GetRealVectorEncoding(Encoding); infillCriterion.Initialize(); } #region helpers private static bool InBounds(IntegerVector r, IntMatrix bounds) { return !r.Where((t, i) => t < bounds[i % bounds.Rows, 0] || t > bounds[i % bounds.Rows, 1]).Any(); } private static RealVectorEncoding GetRealVectorEncoding(IntegerVectorEncoding enc) { var res = new RealVectorEncoding(enc.Length); res.Bounds = new DoubleMatrix(enc.Bounds.Rows, enc.Bounds.Columns); for (int r = 0; r < res.Bounds.Rows; r++) for (int c = 0; c < res.Bounds.Columns; c++) res.Bounds[r, c] = enc.Bounds[r, c]; return res; } #endregion } }