Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/29/17 11:28:16 (7 years ago)
Author:
bwerth
Message:

#2745 added discretized EGO-version for use with IntegerVectors

Location:
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO
Files:
1 added
1 copied

Legend:

Unmodified
Added
Removed
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO/DiscreteInfillSolver.cs

    r15340 r15343  
    2222using System;
    2323using System.Threading;
     24using System.Linq;
     25using System.Collections.Generic;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Core;
    2628using HeuristicLab.Data;
    27 using HeuristicLab.Encodings.RealVectorEncoding;
     29using HeuristicLab.Encodings.IntegerVectorEncoding;
    2830using HeuristicLab.Optimization;
    2931using HeuristicLab.Parameters;
     
    3234
    3335namespace HeuristicLab.Algorithms.EGO {
    34   /// <summary>
    35   /// A base class for operators that manipulate real-valued vectors.
    36   /// </summary>
    37   [Item("InfillSolver", "A RealVectorCreator that creates candidates by optimizing an infill-subproblem")]
     36  [Item("DiscreteInfillSolver", "An IntegerVectorCreator that creates candidates by optimizing an infill-subproblem")]
    3837  [StorableClass]
    39   public class InfillSolver : RealVectorCreator, ICancellableOperator {
     38  public class DiscreteInfillSolver : IntegerVectorCreator, ICancellableOperator {
    4039
    4140    public ILookupParameter<IAlgorithm> InfillOptimizationAlgorithmParamter => (ILookupParameter<IAlgorithm>)Parameters["InfillAlgorithm"];
     
    4443    public ILookupParameter<BoolValue> RemoveDuplicatesParameter => (ILookupParameter<BoolValue>)Parameters["RemoveDuplicates"];
    4544    public IFixedValueParameter<DoubleValue> DuplicateCutoffParameter => (IFixedValueParameter<DoubleValue>)Parameters["Duplicates Cutoff"];
    46     public ILookupParameter<DoubleMatrix> InfillBoundsParameter => (ILookupParameter<DoubleMatrix>)Parameters["InfillBounds"];
     45    public ILookupParameter<IntMatrix> InfillBoundsParameter => (ILookupParameter<IntMatrix>)Parameters["InfillBounds"];
    4746
    4847    public CancellationToken Cancellation { get; set; }
    4948
    5049    [StorableConstructor]
    51     protected InfillSolver(bool deserializing) : base(deserializing) { }
    52     protected InfillSolver(InfillSolver original, Cloner cloner) : base(original, cloner) { }
    53     public InfillSolver() {
     50    protected DiscreteInfillSolver(bool deserializing) : base(deserializing) { }
     51    protected DiscreteInfillSolver(DiscreteInfillSolver original, Cloner cloner) : base(original, cloner) { }
     52    public DiscreteInfillSolver() {
    5453      Parameters.Add(new LookupParameter<IAlgorithm>("InfillAlgorithm", "The algorithm used to optimize the infill problem") { Hidden = true });
    5554      Parameters.Add(new LookupParameter<IRegressionSolution>("Model", "The RegressionSolution upon which the InfillProblem operates") { Hidden = true });
     
    5756      Parameters.Add(new LookupParameter<BoolValue>("RemoveDuplicates", "Whether duplicates shall be removed") { Hidden = true });
    5857      Parameters.Add(new FixedValueParameter<DoubleValue>("Duplicates Cutoff", "The cut off radius for", new DoubleValue(0.01)) { Hidden = false });
    59       Parameters.Add(new LookupParameter<DoubleMatrix>("InfillBounds", "The bounds applied for infill solving") { Hidden = true });
     58      Parameters.Add(new LookupParameter<IntMatrix>("InfillBounds", "The bounds applied for infill solving") { Hidden = true });
    6059    }
    6160
    6261    public override IDeepCloneable Clone(Cloner cloner) {
    63       return new InfillSolver(this, cloner);
     62      return new DiscreteInfillSolver(this, cloner);
    6463    }
    6564
    66     protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) {
     65    protected override IntegerVector Create(IRandom random, IntValue length, IntMatrix bounds) {
    6766      var infillBounds = InfillBoundsParameter.ActualValue;
    6867      if (infillBounds != null && infillBounds.Rows > 0) {
     
    7574      var res = OptimizeInfillProblem(alg, model, max, bounds, length.Value, random);
    7675      var rad = DuplicateCutoffParameter.Value.Value;
    77       if (!RemoveDuplicatesParameter.ActualValue.Value || !(GetMinDifference(model.ProblemData.Dataset, res) < rad * rad)) return res;
    78       for (var i = 0; i < res.Length; i++) res[i] += random.NextDouble() * rad * 2;
     76      if (!RemoveDuplicatesParameter.ActualValue.Value || GetMinDifference(model.ProblemData.Dataset, res) >= rad * rad) return res;
     77
     78      bool changed = false;
     79      var steps = 0;
     80      var dims = new List<int>();
     81
     82      //TODO this may take a long time to compute if many samples have already been evaluated in the surrounding area
     83      //as the preferred region can not be sampled denser and denser due to the disceretization, the variance between two sampled points may be impossible to decease
     84
     85      //TODO speed up GetMinDifferecnce via tree-structure
     86      while (!changed || GetMinDifference(model.ProblemData.Dataset, res) < rad * rad) {
     87        if (dims.Count == 0) {
     88          if (!changed && steps > 0) throw new ArgumentException("Can not avoid duplicate");
     89          dims = Enumerable.Range(0, res.Length).ToList();
     90          steps++;
     91          changed = false;
     92        }
     93        var i = random.Next(dims.Count);
     94        var dim = dims[i];
     95        dims.RemoveAt(i);
     96        var step = bounds[dim % bounds.Rows, 2] * steps;
     97        var low = checkIntBounds(bounds, dim, res[dim] - step);
     98        var high = checkIntBounds(bounds, dim, res[dim] + step);
     99        if (!low && !high) continue;
     100        else if (low && high) res[dim] += (random.NextDouble() < 0.5 ? -step : step);
     101        else if (low) res[dim] -= step;
     102        else res[dim] += step;
     103        changed = true;
     104      }
    79105      return res;
    80106    }
    81107
    82     private RealVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, DoubleMatrix bounds, int length, IRandom random) {
    83       var infillProblem = algorithm.Problem as InfillProblem;
     108
     109    private bool checkIntBounds(IntMatrix b, int row, int value) {
     110      var bi = row % b.Rows;
     111      var l = b[bi, 0];
     112      var h = b[bi, 1];
     113      var s = b[bi, 2];
     114      return l <= value && h >= value && (value - l) % s == 0;
     115    }
     116
     117    private IntegerVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, IntMatrix bounds, int length, IRandom random) {
     118      var infillProblem = algorithm.Problem as DiscreteInfillProblem;
    84119      if (infillProblem == null) throw new ArgumentException("The algortihm has no InfillProblem to solve");
    85120      infillProblem.Encoding.Length = length;
     
    87122      infillProblem.Initialize(model, maximization);
    88123      var res = EgoUtilities.SyncRunSubAlgorithm(algorithm, random.Next(int.MaxValue), Cancellation);
    89       var v = res[InfillProblem.BestInfillSolutionResultName].Value as RealVector;
     124      var v = res[DiscreteInfillProblem.BestInfillSolutionResultName].Value as IntegerVector;
    90125      algorithm.Runs.Clear();
    91126      return v;
    92127    }
    93128
    94     private static double GetMinDifference(IDataset data, RealVector r) {
     129    private static double GetMinDifference(IDataset data, IntegerVector r) {
    95130      var mind = double.MaxValue;
    96131      for (var i = 0; i < data.Rows; i++) {
Note: See TracChangeset for help on using the changeset viewer.