Changeset 14577


Ignore:
Timestamp:
01/16/17 14:25:56 (4 years ago)
Author:
bwerth
Message:

#2592 made MOCMAES compatible with MultiObjectiveBasicProblem instead of MultiObjectiveTestfunction, fixed Bug in CrowdingIndicator

Location:
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/CrowdingIndicator.cs

    r14404 r14577  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Encodings.RealVectorEncoding;
     28using HeuristicLab.Optimization;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using HeuristicLab.Problems.TestFunctions.MultiObjective;
    2930
    3031namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy {
     
    3334  internal class CrowdingIndicator : Item, IIndicator {
    3435
    35     public int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveTestFunctionProblem problem) {
    36       var bounds = problem.Bounds;
     36    public int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveBasicProblem<RealVectorEncoding> problem) {
     37      var bounds = problem.Encoding.Bounds;
    3738      var extracted = front.Select(extractor.Invoke).ToArray();
     39      if (extracted.Length <= 2) return 0;
    3840      var pointsums = new double[extracted.Length];
    3941
    40       for (var dim = 0; dim < problem.Objectives; dim++) {
     42      for (var dim = 0; dim < problem.Maximization.Length; dim++) {
    4143        var arr = extracted.Select(x => x[dim]).ToArray();
    4244        Array.Sort(arr);
    43         var fmax = problem.Bounds[dim % bounds.Rows, 1];
     45        var fmax = problem.Encoding.Bounds[dim % bounds.Rows, 1];
    4446        var fmin = bounds[dim % bounds.Rows, 0];
    4547        var pointIdx = 0;
     
    5254      }
    5355      //find min
    54       return pointsums.Where(x => !double.IsInfinity(x)).ArgMin();
     56      return pointsums.ArgMin();
    5557    }
    5658
  • branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/HeuristicLab.Algorithms.MOCMAEvolutionStrategy-3.3.csproj

    r14404 r14577  
    106106  </ItemGroup>
    107107  <ItemGroup>
     108    <Compile Include="MinimalDistanceIndicator.cs" />
    108109    <Compile Include="CrowdingIndicator.cs" />
    109110    <Compile Include="HypervolumeIndicator.cs" />
  • branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/HypervolumeIndicator.cs

    r14404 r14577  
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Core;
     28using HeuristicLab.Encodings.RealVectorEncoding;
     29using HeuristicLab.Optimization;
    2830using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2931using HeuristicLab.Problems.TestFunctions.MultiObjective;
     
    3335  internal class HypervolumeIndicator : Item, IIndicator {
    3436
    35     public int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveTestFunctionProblem problem) {
     37    public int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveBasicProblem<RealVectorEncoding> problem) {
    3638      var frontArr = front.Select(extractor.Invoke).ToList();
    37       var refPoint = BuildReference(frontArr.Concat(new[] { problem.TestFunction.ReferencePoint(problem.Objectives) }), problem.Maximization);
     39      if (frontArr.Count <= 1) return 0;
     40      var p = problem as MultiObjectiveTestFunctionProblem;
     41      var refPoint = BuildReference(p != null ? frontArr.Concat(new[] { p.TestFunction.ReferencePoint(p.Objectives) }) : frontArr, problem.Maximization);
    3842      var hv = Hypervolume.Calculate(frontArr, refPoint, problem.Maximization);
    39       return Enumerable.Range(0, frontArr.Count).Select(i => Contribution(frontArr, i, problem.Maximization, refPoint, hv)).ArgMin();
     43      var contributions = Enumerable.Range(0, frontArr.Count).Select(i => Contribution(frontArr, i, problem.Maximization, refPoint, hv));
     44      return contributions.ArgMin();
    4045    }
    4146
  • branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/IIndicator.cs

    r14404 r14577  
    2222using System.Collections.Generic;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Problems.TestFunctions.MultiObjective;
     24using HeuristicLab.Encodings.RealVectorEncoding;
     25using HeuristicLab.Optimization;
    2526
    2627namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy {
     
    3536    /// <param name="problem">The problem on which the front is evaluated (!! The function itself will NOT be evluated only bounds referencePoints & other metadate will be used</param>
    3637    /// <returns>the index of the least contributing point according to any type of quality criteria</returns>
    37     int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveTestFunctionProblem problem);
     38    int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveBasicProblem<RealVectorEncoding> problem);
    3839  }
    3940}
  • branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMASEvolutionStrategy.cs

    r14404 r14577  
    4444    public override Type ProblemType
    4545    {
    46       get { return typeof(MultiObjectiveTestFunctionProblem); }
    47     }
    48     public new MultiObjectiveTestFunctionProblem Problem
    49     {
    50       get { return (MultiObjectiveTestFunctionProblem)base.Problem; }
     46      get { return typeof(MultiObjectiveBasicProblem<RealVectorEncoding>); }
     47    }
     48    public new MultiObjectiveBasicProblem<RealVectorEncoding> Problem
     49    {
     50      get { return (MultiObjectiveBasicProblem<RealVectorEncoding>)base.Problem; }
    5151      set { base.Problem = value; }
    5252    }
     53    public override bool SupportsPause
     54    {
     55      get { return false; }
     56    }
     57
    5358    #region internal variables
    5459    private readonly IRandom random = new MersenneTwister();
     
    7681    private const string CrowdingResultName = "Crowding";
    7782    private const string SpacingResultName = "Spacing";
    78     private const string SolutionsResultName = "Pareto Front";
     83    private const string CurrentFrontResultName = "Pareto Front";
    7984    private const string BestHypervolumeResultName = "Best Hypervolume";
    8085    private const string BestKnownHypervolumeResultName = "Best known hypervolume";
     
    97102      get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
    98103    }
    99     private IFixedValueParameter<IntValue> PopulationSizeParameter
     104    public IFixedValueParameter<IntValue> PopulationSizeParameter
    100105    {
    101106      get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }
    102107    }
    103     private IFixedValueParameter<IntValue> MaximumGenerationsParameter
     108    public IFixedValueParameter<IntValue> MaximumGenerationsParameter
    104109    {
    105110      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
    106111    }
    107     private IFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter
     112    public IFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter
    108113    {
    109114      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsName]; }
    110115    }
    111     private IFixedValueParameter<DoubleValue> InitialSigmaParameter
     116    public IFixedValueParameter<DoubleValue> InitialSigmaParameter
    112117    {
    113118      get { return (IFixedValueParameter<DoubleValue>)Parameters[InitialSigmaName]; }
    114119    }
    115     private IConstrainedValueParameter<IIndicator> IndicatorParameter
     120    public IConstrainedValueParameter<IIndicator> IndicatorParameter
    116121    {
    117122      get { return (IConstrainedValueParameter<IIndicator>)Parameters[IndicatorName]; }
     
    254259    private DoubleMatrix ResultsSolutions
    255260    {
    256       get { return ((DoubleMatrix)Results[SolutionsResultName].Value); }
    257       set { Results[SolutionsResultName].Value = value; }
     261      get { return ((DoubleMatrix)Results[CurrentFrontResultName].Value); }
     262      set { Results[CurrentFrontResultName].Value = value; }
    258263    }
    259264    private ScatterPlotContent ResultsScatterPlot
     
    273278      Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
    274279      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluatedSolutionsName, "The maximum number of evaluated solutions that should be computed.", new IntValue(int.MaxValue)));
    275       var set = new ItemSet<IIndicator> { new HypervolumeIndicator(), new CrowdingIndicator() };
     280      var set = new ItemSet<IIndicator> { new HypervolumeIndicator(), new CrowdingIndicator(), new MinimalDistanceIndicator() };
    276281      Parameters.Add(new ConstrainedValueParameter<IIndicator>(IndicatorName, "The selection mechanism on non-dominated solutions", set, set.First()));
    277282    }
     
    331336      solutions = new MOCMAESIndividual[PopulationSize];
    332337      for (var i = 0; i < PopulationSize; i++) {
    333         var x = new RealVector(Problem.ProblemSize); // Uniform distibution in all dimensions assumed.
    334                                                      // There is the UniformSolutionCreater associated with the Encoding, but it was considered not usable here
    335         var bounds = Problem.Bounds;
    336         for (var j = 0; j < Problem.ProblemSize; j++) {
     338        var x = new RealVector(Problem.Encoding.Length); // Uniform distibution in all dimensions assumed.
     339                                                         // There is the UniformSolutionCreater associated with the Encoding, but it was considered not usable here
     340        var bounds = Problem.Encoding.Bounds;
     341        for (var j = 0; j < Problem.Encoding.Length; j++) {
    337342          var dim = j % bounds.Rows;
    338343          x[j] = random.NextDouble() * (bounds[dim, 1] - bounds[dim, 0]) + bounds[dim, 0];
     
    344349    private void InitStrategy() {
    345350      const int lambda = 1;
    346       double n = Problem.ProblemSize;
     351      double n = Problem.Encoding.Length;
    347352
    348353      var targetSuccessProbability = 1.0 / (5.0 + Math.Sqrt(lambda) / 2.0);
     
    377382      table.Rows.Add(new DataRow(SpacingResultName));
    378383      AddResult(TimetableResultName, "Different quality meassures in a timeseries", table);
    379       AddResult(SolutionsResultName, "The current front", new DoubleMatrix());
     384      AddResult(CurrentFrontResultName, "The current front", new DoubleMatrix());
    380385      AddResult(ScatterPlotResultName, "A scatterplot displaying the evaluated solutions and (if available) the analytically optimal front", new ScatterPlotContent(null, null, null, 2));
    381386
    382       if (Problem.BestKnownFront != null) {
    383         ResultsBestKnownHypervolume = Hypervolume.Calculate(Utilities.ToArray(Problem.BestKnownFront), Problem.TestFunction.ReferencePoint(Problem.Objectives), Problem.Maximization);
     387      var problem = Problem as MultiObjectiveTestFunctionProblem;
     388      if (problem == null) return;
     389      if (problem.BestKnownFront != null) {
     390        ResultsBestKnownHypervolume = Hypervolume.Calculate(Utilities.ToArray(problem.BestKnownFront), problem.TestFunction.ReferencePoint(problem.Objectives), Problem.Maximization);
    384391        ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume;
    385392      }
    386       ResultsScatterPlot = new ScatterPlotContent(null, null, Utilities.ToArray(Problem.BestKnownFront), Problem.Objectives);
     393      ResultsScatterPlot = new ScatterPlotContent(new double[0][], new double[0][], Utilities.ToArray(problem.BestKnownFront), problem.Objectives);
    387394    }
    388395    #endregion
     
    411418    }
    412419    private double[] Evaluate(RealVector x) {
    413       var res = Problem.Evaluate(x);
     420      var s = new Scope();
     421      s.Variables.Add(new Variable(Problem.Encoding.Name, x));
     422      var ind = new SingleEncodingIndividual(Problem.Encoding, s);
     423      var res = Problem.Evaluate(ind, random);
    414424      ResultsEvaluations++;
    415425      return res;
    416426    }
    417     private static double[] Penalize(RealVector x, RealVector t, IEnumerable<double> fitness) {
    418       var penalty = x.Select((t1, i) => t1 - t[i]).Sum(d => d * d);
    419       return fitness.Select(v => v + penalty).ToArray();
     427    private double[] Penalize(RealVector x, RealVector t, IEnumerable<double> fitness) {
     428      var penalty = x.Zip(t, (a, b) => (a - b) * (a - b)).Sum() * 1E-6;
     429      return fitness.Select((v, i) => Problem.Maximization[i] ? v - penalty : v + penalty).ToArray();
    420430    }
    421431    private RealVector ClosestFeasible(RealVector x) {
    422       var bounds = Problem.Bounds;
     432      var bounds = Problem.Encoding.Bounds;
    423433      var r = new RealVector(x.Length);
    424434      for (var i = 0; i < x.Length; i++) {
     
    429439    }
    430440    private bool IsFeasable(RealVector offspring) {
    431       var bounds = Problem.Bounds;
     441      var bounds = Problem.Encoding.Bounds;
    432442      for (var i = 0; i < offspring.Length; i++) {
    433443        var dim = i % bounds.Rows;
     
    452462      }
    453463
     464
    454465      //now use the indicator to deselect the approximatingly worst elements of the last selected front
    455466      var front1 = fronts[rank];
     467      front1.Sort(new HelpComparer());
    456468      for (; popSize > length; popSize--) {
    457469        var lc = Indicator.LeastContributer(front1.ToArray(), x => x.PenalizedFitness, Problem);
     
    486498    }
    487499    private void AnalyzeQualityIndicators() {
    488       var front = NonDominatedSelect.GetDominatingVectors(solutions.Select(x => x.Fitness), Problem.TestFunction.ReferencePoint(Problem.Objectives), Problem.Maximization, true).ToArray();
    489       var bounds = Problem.Bounds.CloneAsMatrix();
     500      var problem = Problem as MultiObjectiveTestFunctionProblem;
     501      if (problem == null) return;
     502
     503      var front = NonDominatedSelect.GetDominatingVectors(solutions.Select(x => x.Fitness), problem.TestFunction.ReferencePoint(problem.Objectives), Problem.Maximization, true).ToArray();
     504      if (front.Length == 0) return;
     505      var bounds = problem.Bounds.CloneAsMatrix();
    490506      ResultsCrowding = Crowding.Calculate(front, bounds);
    491507      ResultsSpacing = Spacing.Calculate(front);
    492       ResultsGenerationalDistance = Problem.BestKnownFront != null ? GenerationalDistance.Calculate(front, Utilities.ToArray(Problem.BestKnownFront), 1) : double.NaN;
    493 
    494       ResultsInvertedGenerationalDistance = Problem.BestKnownFront != null ? InvertedGenerationalDistance.Calculate(front, Utilities.ToArray(Problem.BestKnownFront), 1) : double.NaN;
    495 
    496       ResultsHypervolume = Hypervolume.Calculate(front, Problem.TestFunction.ReferencePoint(Problem.Objectives), Problem.Maximization);
     508      ResultsGenerationalDistance = problem.BestKnownFront != null ? GenerationalDistance.Calculate(front, Utilities.ToArray(problem.BestKnownFront), 1) : double.NaN;
     509
     510      ResultsInvertedGenerationalDistance = problem.BestKnownFront != null ? InvertedGenerationalDistance.Calculate(front, Utilities.ToArray(problem.BestKnownFront), 1) : double.NaN;
     511
     512      ResultsHypervolume = Hypervolume.Calculate(front, problem.TestFunction.ReferencePoint(problem.Objectives), Problem.Maximization);
    497513      ResultsBestHypervolume = Math.Max(ResultsHypervolume, ResultsBestHypervolume);
    498514      ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume - ResultsBestHypervolume;
     
    510526    #endregion
    511527
    512 
     528    private class HelpComparer : IComparer<MOCMAESIndividual> {
     529      public int Compare(MOCMAESIndividual x, MOCMAESIndividual y) {
     530        return x.PenalizedFitness[0].CompareTo(y.PenalizedFitness[0]);
     531      }
     532    }
    513533
    514534    #region Helpers
  • branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Plugin.cs.frame

    r14404 r14577  
    3333  [PluginDependency("HeuristicLab.Common", "3.3")]
    3434  [PluginDependency("HeuristicLab.Core", "3.3")]
    35   [PluginDependency("HeuristicLab.Core.Views", "3.3")]
    3635  [PluginDependency("HeuristicLab.Data", "3.3")]
    3736  [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")]
    38   [PluginDependency("HeuristicLab.Operators", "3.3")]
    3937  [PluginDependency("HeuristicLab.Optimization", "3.3")]
    40   [PluginDependency("HeuristicLab.Optimization.Operators", "3.3")]
    4138  [PluginDependency("HeuristicLab.Parameters", "3.3")]
    4239  [PluginDependency("HeuristicLab.Persistence", "3.3")]
    43   [PluginDependency("HeuristicLab.Problems.Instances", "3.3")]
    4440  [PluginDependency("HeuristicLab.Problems.TestFunctions.MultiObjective", "3.3")]
    4541  [PluginDependency("HeuristicLab.Random", "3.3")]
  • branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Utilities.cs

    r14404 r14577  
    55  internal static class Utilities {
    66    internal static double[][] ToArray(DoubleMatrix m) {
     7      if (m == null) return null;
    78      var i = m.Rows - 1;
    89      var a = new double[i][];
     
    1617
    1718    public static int ArgMin<T>(IEnumerable<T> values, IComparer<T> comparer) {
    18       var mindex = 0;
     19      var mindex = -1;
    1920      var min = default(T);
    2021      var i = 0;
     
    3031    }
    3132    public static int ArgMax<T>(IEnumerable<T> values, IComparer<T> comparer) {
    32       var mindex = 0;
     33      var mindex = -1;
    3334      var min = default(T);
    3435      var i = 0;
Note: See TracChangeset for help on using the changeset viewer.