[14741] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[15064] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[14741] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[15343] | 28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
[14741] | 29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[15064] | 32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[14741] | 33 |
|
---|
[14768] | 34 | namespace HeuristicLab.Algorithms.EGO {
|
---|
[14741] | 35 | [StorableClass]
|
---|
[15343] | 36 | [Item("DiscreteInfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")]
|
---|
| 37 | public sealed class DiscreteInfillProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {
|
---|
[14741] | 38 |
|
---|
[15064] | 39 | public override bool Maximization => true;
|
---|
[14741] | 40 |
|
---|
[15064] | 41 | #region ProblemResultNames
|
---|
| 42 | public const string BestInfillSolutionResultName = "BestInfillSolution";
|
---|
| 43 | public const string BestInfillQualityResultName = "BestInfillQuality";
|
---|
| 44 | #endregion
|
---|
| 45 |
|
---|
| 46 | #region Properties
|
---|
[14741] | 47 | [Storable]
|
---|
| 48 | private IInfillCriterion infillCriterion;
|
---|
| 49 |
|
---|
[15343] | 50 | public IInfillCriterion InfillCriterion {
|
---|
[14741] | 51 | get { return infillCriterion; }
|
---|
[15343] | 52 | set {
|
---|
[15064] | 53 | infillCriterion = value;
|
---|
[15343] | 54 | infillCriterion.Encoding = GetRealVectorEncoding(Encoding);
|
---|
[14741] | 55 | }
|
---|
| 56 | }
|
---|
| 57 | #endregion
|
---|
| 58 |
|
---|
[15064] | 59 | #region Constructors
|
---|
[14741] | 60 | [StorableConstructor]
|
---|
[15343] | 61 | private DiscreteInfillProblem(bool deserializing) : base(deserializing) { }
|
---|
| 62 | private DiscreteInfillProblem(DiscreteInfillProblem original, Cloner cloner) : base(original, cloner) {
|
---|
[15064] | 63 | infillCriterion = cloner.Clone(original.infillCriterion);
|
---|
[14741] | 64 | }
|
---|
[15343] | 65 | public DiscreteInfillProblem() { }
|
---|
| 66 | public override IDeepCloneable Clone(Cloner cloner) { return new DiscreteInfillProblem(this, cloner); }
|
---|
[14741] | 67 | #endregion
|
---|
| 68 |
|
---|
| 69 | public override double Evaluate(Individual individual, IRandom r) {
|
---|
[15343] | 70 | return !InBounds(individual.IntegerVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.IntegerVector().ToRealVector());
|
---|
[14741] | 71 | }
|
---|
| 72 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 73 | base.Analyze(individuals, qualities, results, random);
|
---|
| 74 | var best = qualities.ArgMax(x => x);
|
---|
[15064] | 75 | var newQuality = qualities[best];
|
---|
| 76 | if (!results.ContainsKey(BestInfillQualityResultName)) {
|
---|
[15343] | 77 | results.Add(new Result(BestInfillSolutionResultName, (IntegerVector)individuals[best].IntegerVector().Clone()));
|
---|
[15064] | 78 | results.Add(new Result(BestInfillQualityResultName, new DoubleValue(newQuality)));
|
---|
[14741] | 79 | return;
|
---|
| 80 | }
|
---|
[15064] | 81 | var qold = results[BestInfillQualityResultName].Value as DoubleValue;
|
---|
[14741] | 82 | if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?");
|
---|
[15064] | 83 | if (qold.Value >= newQuality) return;
|
---|
[15343] | 84 | results[BestInfillSolutionResultName].Value = (IntegerVector)individuals[best].IntegerVector().Clone();
|
---|
[15064] | 85 | qold.Value = newQuality;
|
---|
[14741] | 86 | }
|
---|
[15064] | 87 | public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
| 88 | var bounds = Encoding.Bounds;
|
---|
| 89 | var michalewiczIteration = 0;
|
---|
[15343] | 90 | var sigma = new DoubleArray(new double[] { 1.0 });
|
---|
| 91 |
|
---|
[15064] | 92 | while (true) {
|
---|
| 93 | var neighbour = individual.Copy();
|
---|
[15343] | 94 | var r = neighbour.IntegerVector();
|
---|
| 95 | switch (random.Next(3) % 3) {
|
---|
| 96 | case 0: HeuristicLab.Encodings.IntegerVectorEncoding.UniformOnePositionManipulator.Apply(random, r, bounds); break;
|
---|
| 97 | case 1: HeuristicLab.Encodings.IntegerVectorEncoding.RoundedNormalAllPositionsManipulator.Apply(random, r, bounds, sigma); break;//FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break;
|
---|
| 98 | case 2: HeuristicLab.Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator.Apply(random, r, bounds, 0.1); break;
|
---|
[15064] | 99 | default: throw new NotImplementedException();
|
---|
| 100 | }
|
---|
| 101 | yield return neighbour;
|
---|
| 102 | michalewiczIteration %= 10000;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public void Initialize(IRegressionSolution model, bool expensiveMaximization) {
|
---|
| 107 | infillCriterion.RegressionSolution = model;
|
---|
| 108 | infillCriterion.ExpensiveMaximization = expensiveMaximization;
|
---|
[15343] | 109 | infillCriterion.Encoding = GetRealVectorEncoding(Encoding);
|
---|
[15064] | 110 | infillCriterion.Initialize();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | #region helpers
|
---|
[15343] | 114 | private static bool InBounds(IntegerVector r, IntMatrix bounds) {
|
---|
[15064] | 115 | return !r.Where((t, i) => t < bounds[i % bounds.Rows, 0] || t > bounds[i % bounds.Rows, 1]).Any();
|
---|
| 116 | }
|
---|
[15343] | 117 |
|
---|
| 118 | private static RealVectorEncoding GetRealVectorEncoding(IntegerVectorEncoding enc) {
|
---|
| 119 | var res = new RealVectorEncoding(enc.Length);
|
---|
| 120 | res.Bounds = new DoubleMatrix(enc.Bounds.Rows, enc.Bounds.Columns);
|
---|
| 121 | for (int r = 0; r < res.Bounds.Rows; r++)
|
---|
| 122 | for (int c = 0; c < res.Bounds.Columns; c++)
|
---|
| 123 | res.Bounds[r, c] = enc.Bounds[r, c];
|
---|
| 124 | return res;
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | }
|
---|
[15064] | 128 | #endregion
|
---|
| 129 |
|
---|
[14741] | 130 | }
|
---|
| 131 | }
|
---|