Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/26/17 09:10:56 (7 years ago)
Author:
bwerth
Message:

#2745 implemented EGO as EngineAlgorithm + some simplifications in the IInfillCriterion interface

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Problems/InfillProblem.cs

    r14818 r15064  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    2729using HeuristicLab.Optimization;
    2830using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.Problems.DataAnalysis;
    2932
    3033namespace HeuristicLab.Algorithms.EGO {
     
    3336  public sealed class InfillProblem : SingleObjectiveBasicProblem<RealVectorEncoding> {
    3437
    35     public override bool Maximization => true;  //This is necessary because algorithms do not expect the maximization to change
     38    public override bool Maximization => true;
    3639
    37     #region Properties;
     40    #region ProblemResultNames
     41    public const string BestInfillSolutionResultName = "BestInfillSolution";
     42    public const string BestInfillQualityResultName = "BestInfillQuality";
     43    #endregion
     44
     45    #region Properties
    3846    [Storable]
    3947    private IInfillCriterion infillCriterion;
    40     [Storable]
    41     private SingleObjectiveBasicProblem<IEncoding> problem;
    4248
    4349    public IInfillCriterion InfillCriterion
    4450    {
    4551      get { return infillCriterion; }
    46       set { infillCriterion = value; }
    47     }
    48     public SingleObjectiveBasicProblem<IEncoding> Problem
    49     {
    50       get { return problem; }
    5152      set
    5253      {
    53         problem = value;
    54         if (problem == null) return;
    55         var enc = problem.Encoding as RealVectorEncoding;
    56         if (enc == null) throw new ArgumentException("EGO can not be performed on non-RealVectorEncodings");
    57         Encoding = enc;
    58         SolutionCreator = new UniformRandomRealVectorCreator();//ignore Problem specific Solution Creation
    59 
     54        infillCriterion = value;
     55        infillCriterion.Encoding = Encoding;
    6056      }
    6157    }
    6258    #endregion
    6359
    64     #region HLConstructors
     60    #region Constructors
    6561    [StorableConstructor]
    6662    private InfillProblem(bool deserializing) : base(deserializing) { }
    6763    private InfillProblem(InfillProblem original, Cloner cloner) : base(original, cloner) {
    68       infillCriterion = cloner.Clone(original.InfillCriterion);
    69       problem = cloner.Clone(original.Problem);
     64      infillCriterion = cloner.Clone(original.infillCriterion);
    7065    }
    7166    public InfillProblem() { }
     
    7469
    7570    public override double Evaluate(Individual individual, IRandom r) {
    76       var q = InfillCriterion.Evaluate(individual.RealVector());
    77       return InfillCriterion.Maximization() ? q : -q;
     71      return !InBounds(individual.RealVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.RealVector());
    7872    }
    7973    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    8074      base.Analyze(individuals, qualities, results, random);
    8175      var best = qualities.ArgMax(x => x);
    82       var qnew = InfillCriterion.Maximization() ? qualities[best] : -qualities[best];
    83       const string qname = EfficientGlobalOptimizationAlgorithm.BestInfillQualityResultName;
    84       const string sname = EfficientGlobalOptimizationAlgorithm.BestInfillSolutionResultName;
    85       if (!results.ContainsKey(EfficientGlobalOptimizationAlgorithm.BestInfillQualityResultName)) {
    86         results.Add(new Result(sname, (RealVector)individuals[best].RealVector().Clone()));
    87         results.Add(new Result(qname, new DoubleValue(qnew)));
     76      var newQuality = qualities[best];
     77      if (!results.ContainsKey(BestInfillQualityResultName)) {
     78        results.Add(new Result(BestInfillSolutionResultName, (RealVector)individuals[best].RealVector().Clone()));
     79        results.Add(new Result(BestInfillQualityResultName, new DoubleValue(newQuality)));
    8880        return;
    8981      }
    90       var qold = results[qname].Value as DoubleValue;
     82      var qold = results[BestInfillQualityResultName].Value as DoubleValue;
    9183      if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?");
    92       if (qold.Value >= qnew == InfillCriterion.Maximization()) return;
    93       results[sname].Value = (RealVector)individuals[best].RealVector().Clone();
    94       qold.Value = qnew;
     84      if (qold.Value >= newQuality) return;
     85      results[BestInfillSolutionResultName].Value = (RealVector)individuals[best].RealVector().Clone();
     86      qold.Value = newQuality;
    9587    }
     88    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
     89      var bounds = Encoding.Bounds;
     90      var michalewiczIteration = 0;
     91      while (true) {
     92        var neighbour = individual.Copy();
     93        var r = neighbour.RealVector();
     94        switch (random.Next(5)) {
     95          case 0: UniformOnePositionManipulator.Apply(random, r, bounds); break;
     96          case 1: UniformOnePositionManipulator.Apply(random, r, bounds); break;//FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break;
     97          case 2: MichalewiczNonUniformAllPositionsManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break;
     98          case 3: MichalewiczNonUniformOnePositionManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break;
     99          case 4: BreederGeneticAlgorithmManipulator.Apply(random, r, bounds, new DoubleValue(0.1)); break;
     100          default: throw new NotImplementedException();
     101        }
     102        yield return neighbour;
     103        michalewiczIteration %= 10000;
     104      }
     105    }
     106
     107    public void Initialize(IRegressionSolution model, bool expensiveMaximization) {
     108      infillCriterion.RegressionSolution = model;
     109      infillCriterion.ExpensiveMaximization = expensiveMaximization;
     110      infillCriterion.Encoding = Encoding;
     111      infillCriterion.Initialize();
     112    }
     113
     114    #region helpers
     115    private static bool InBounds(RealVector r, DoubleMatrix bounds) {
     116      return !r.Where((t, i) => t < bounds[i % bounds.Rows, 0] || t > bounds[i % bounds.Rows, 1]).Any();
     117    }
     118    #endregion
     119
    96120  }
    97121}
Note: See TracChangeset for help on using the changeset viewer.