Changeset 17315


Ignore:
Timestamp:
10/04/19 14:23:08 (10 days ago)
Author:
abeham
Message:

#2521: reverted r17309

Location:
branches/2521_ProblemRefactoring
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Indicators/HypervolumeIndicator.cs

    r17309 r17315  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Optimization;
     28using HeuristicLab.Problems.TestFunctions.MultiObjective;
    2829
    2930namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy {
     
    5253
    5354    #region Helpers
    54     private static double Contribution(IList<double[]> front, int idx, IReadOnlyList<bool> maximization, double[] refPoint) {
     55    private static double Contribution(IList<double[]> front, int idx, bool[] maximization, double[] refPoint) {
    5556      var point = front[idx];
    5657      front.RemoveAt(idx);
  • branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs

    r17309 r17315  
    376376      var bkf = problem.BestKnownFront == null ? null : problem.BestKnownFront.ToArray();
    377377      if (bkf != null && problem.ReferencePoint != null) {
    378         ResultsBestKnownHypervolume = HypervolumeCalculator.CalculateHypervolume(bkf, problem.ReferencePoint, Problem.Maximization.CloneAsArray());
     378        ResultsBestKnownHypervolume = HypervolumeCalculator.CalculateHypervolume(bkf, problem.ReferencePoint, Problem.Maximization);
    379379        ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume;
    380380      }
     
    456456      //perform a non-dominated sort to assign the rank to every element
    457457      int[] ranks;
    458       var fronts = DominationCalculator.CalculateAllParetoFronts(parents.ToArray(), parents.Select(i => i.PenalizedFitness).ToArray(), Problem.Maximization.CloneAsArray(), out ranks);
     458      var fronts = DominationCalculator.CalculateAllParetoFronts(parents.ToArray(), parents.Select(i => i.PenalizedFitness).ToArray(), Problem.Maximization, out ranks);
    459459
    460460      //deselect the highest rank fronts until we would end up with less or equal mu elements
  • branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/BoolArray.cs

    r17309 r17315  
    2020#endregion
    2121
    22 using System.Collections.Generic;
    2322using System.Text;
    2423using HEAL.Attic;
     
    3837    public BoolArray(int length) : base(length) { }
    3938    public BoolArray(bool[] elements, bool @readonly = false) : base(elements, @readonly) { }
    40     public BoolArray(IReadOnlyList<bool> elements) : base(elements) { }
    4139
    4240    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/StringConvertibleArray.cs

    r17309 r17315  
    2222#endregion
    2323
    24 using System.Collections.Generic;
    2524using HEAL.Attic;
    2625using HeuristicLab.Common;
     
    3938    protected StringConvertibleArray(int length) : base(length) { }
    4039    protected StringConvertibleArray(T[] elements, bool @readonly = false) : base(elements, @readonly) { }
    41     protected StringConvertibleArray(IReadOnlyList<T> elements) : base(elements) { }
    4240
    4341    protected abstract bool Validate(string value, out string errorMessage);
  • branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/ValueTypeArray.cs

    r17309 r17315  
    138138    }
    139139    protected ValueTypeArray(T[] elements, bool @readonly = false) {
    140       if (elements == null) throw new ArgumentNullException(nameof(elements));
     140      if (elements == null) throw new ArgumentNullException();
    141141      array = (T[])elements.Clone();
    142142      readOnly = @readonly;
    143143      resizable = true;
    144144      elementNames = new List<string>();
    145     }
    146    
    147     protected ValueTypeArray(IReadOnlyList<T> elements) {
    148       if (elements == null) throw new ArgumentNullException(nameof(elements));
    149       array = elements.ToArray(); // TODO: array should be an IList<T>
    150       readOnly = true;
    151       resizable = true; // TODO: really?
    152       elementNames = new List<string>(); // TODO: wasteful
    153145    }
    154146
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeMultiObjectiveProblem.cs

    r17309 r17315  
    5454      base.Analyze(trees, qualities, results, random);
    5555
    56       var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(trees, qualities, Maximization.CloneAsArray());
     56      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(trees, qualities, Maximization);
    5757      var plot = new ParetoFrontScatterPlot<ISymbolicExpressionTree>(fronts, trees, qualities, Objectives, BestKnownFront);
    5858      results.AddOrUpdateResult("Pareto Front Scatter Plot", plot);
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblemDefinition.cs

    r17309 r17315  
    2828  public interface IMultiObjectiveProblemDefinition {
    2929    int Objectives { get; }
    30     IReadOnlyList<bool> Maximization { get; }
     30    bool[] Maximization { get; }
    3131    IReadOnlyList<double[]> BestKnownFront { get; }
    3232    double[] ReferencePoint { get; }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs

    r17309 r17315  
    3737    where TEncodedSolution : class, IEncodedSolution {
    3838    #region Parameternames
     39    public const string MaximizationParameterName = "Maximization";
    3940    public const string BestKnownFrontParameterName = "BestKnownFront";
    4041    public const string ReferencePointParameterName = "ReferencePoint";
     
    4243
    4344    #region Parameterproperties
    44     [Storable] protected IValueParameter<BoolArray> MaximizationParameter { get; }
     45    public IValueParameter<BoolArray> MaximizationParameter {
     46      get { return (IValueParameter<BoolArray>)Parameters[MaximizationParameterName]; }
     47    }
    4548    public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
    4649      get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; }
     
    5760    protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
    5861      : base(original, cloner) {
    59       MaximizationParameter = cloner.Clone(original.MaximizationParameter);
    6062      ParameterizeOperators();
    6163    }
    6264
    6365    protected MultiObjectiveProblem() : base() {
    64       MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray(new[] { false, false }).AsReadOnly()) { Hidden = true, ReadOnly = true };
    65       Parameters.Add(MaximizationParameter);
     66      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
    6667      Parameters.Add(new OptionalValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualites for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion."));
    6768      Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem"));
     
    7273
    7374    protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) {
    74       MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray(new[] { false, false }).AsReadOnly()) { Hidden = true, ReadOnly = true };
    75       Parameters.Add(MaximizationParameter);
     75      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
    7676      Parameters.Add(new OptionalValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualites for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion."));
    7777      Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem"));
     
    8989      get { return Maximization.Length; }
    9090    }
    91     IReadOnlyList<bool> IMultiObjectiveProblemDefinition.Maximization {
    92       get { return Maximization.CloneAsArray(); }
    93     }
    94     public BoolArray Maximization {
    95       get { return MaximizationParameter.Value; }
    96       set {
    97         if (Maximization == value) return;
    98         MaximizationParameter.ReadOnly = false;
    99         MaximizationParameter.Value = value.AsReadOnly();
    100         MaximizationParameter.ReadOnly = true;
    101       }
    102     }
     91    public abstract bool[] Maximization { get; }
    10392
    10493    public virtual IReadOnlyList<double[]> BestKnownFront {
     
    179168    #region IMultiObjectiveHeuristicOptimizationProblem Members
    180169    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
    181       get { return MaximizationParameter; }
     170      get { return Parameters[MaximizationParameterName]; }
    182171    }
    183172    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/MultiObjective/DominationCalculator.cs

    r17309 r17315  
    4747    /// <param name="dominateOnEqualQualities">Whether solutions of exactly equal quality should dominate one another.</param>
    4848    /// <returns>The pareto front containing the best solutions and their associated quality resp. fitness.</returns>
    49     public static List<Tuple<T, double[]>> CalculateBestParetoFront<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, bool dominateOnEqualQualities = true) {
     49    public static List<Tuple<T, double[]>> CalculateBestParetoFront<T>(T[] solutions, double[][] qualities, bool[] maximization, bool dominateOnEqualQualities = true) {
    5050      var populationSize = solutions.Length;
    5151      Dictionary<T, List<int>> dominatedIndividuals;
     
    7272    /// <param name="dominateOnEqualQualities">Whether solutions of exactly equal quality should dominate one another.</param>
    7373    /// <returns>A sorted list of the pareto fronts from best to worst.</returns>
    74     public static List<List<Tuple<T, double[]>>> CalculateAllParetoFronts<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, out int[] rank, bool dominateOnEqualQualities = true) {
     74    public static List<List<Tuple<T, double[]>>> CalculateAllParetoFronts<T>(T[] solutions, double[][] qualities, bool[] maximization, out int[] rank, bool dominateOnEqualQualities = true) {
    7575      var populationSize = solutions.Length;
    7676
     
    101101    }
    102102
    103     private static List<Tuple<T, double[]>> CalculateBestFront<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, bool dominateOnEqualQualities, int populationSize, out Dictionary<T, List<int>> dominatedIndividuals, out int[] dominationCounter, out int[] rank) {
     103    private static List<Tuple<T, double[]>> CalculateBestFront<T>(T[] solutions, double[][] qualities, bool[] maximization, bool dominateOnEqualQualities, int populationSize, out Dictionary<T, List<int>> dominatedIndividuals, out int[] dominationCounter, out int[] rank) {
    104104      var front = new List<Tuple<T, double[]>>();
    105105      dominatedIndividuals = new Dictionary<T, List<int>>();
     
    155155    /// <param name="dominateOnEqualQualities">Whether solutions of exactly equal quality should dominate one another.</param>
    156156    /// <returns>A sorted list of the pareto fronts where each front contains the indices of the <paramref name="solutions"/> and <paramref name="qualities"/>.</returns>
    157     public static List<List<int>> CalculateAllParetoFrontsIndices<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, bool dominateOnEqualQualities = true) {
     157    public static List<List<int>> CalculateAllParetoFrontsIndices<T>(T[] solutions, double[][] qualities, bool[] maximization, bool dominateOnEqualQualities = true) {
    158158      return CalculateAllParetoFrontsIndices(solutions, qualities, maximization, out var rank, dominateOnEqualQualities);
    159159    }
     
    178178    /// <param name="dominateOnEqualQualities">Whether solutions of exactly equal quality should dominate one another.</param>
    179179    /// <returns>A sorted list of the pareto fronts where each front contains the indices of the <paramref name="solutions"/> and <paramref name="qualities"/>.</returns>
    180     public static List<List<int>> CalculateAllParetoFrontsIndices<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, out int[] rank, bool dominateOnEqualQualities = true) {
     180    public static List<List<int>> CalculateAllParetoFrontsIndices<T>(T[] solutions, double[][] qualities, bool[] maximization, out int[] rank, bool dominateOnEqualQualities = true) {
    181181      var populationSize = solutions.Length;
    182182
     
    207207    }
    208208
    209     private static List<int> CalculateBestFrontIndices<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, bool dominateOnEqualQualities, int populationSize, List<int>[] dominatedIndividuals, int[] dominationCounter, int[] rank) {
     209    private static List<int> CalculateBestFrontIndices<T>(T[] solutions, double[][] qualities, bool[] maximization, bool dominateOnEqualQualities, int populationSize, List<int>[] dominatedIndividuals, int[] dominationCounter, int[] rank) {
    210210      var front = new List<int>();
    211211      for (var pI = 0; pI < populationSize - 1; pI++) {
     
    244244    /// <param name="dominateOnEqualQualities">Whether the result should be Dominates in case both fitness vectors are exactly equal</param>
    245245    /// <returns>Dominates if left dominates right, IsDominated if right dominates left and IsNonDominated otherwise.</returns>
    246     public static DominationResult Dominates(double[] left, double[] right, IReadOnlyList<bool> maximizations, bool dominateOnEqualQualities) {
     246    public static DominationResult Dominates(double[] left, double[] right, bool[] maximizations, bool dominateOnEqualQualities) {
    247247      //mkommend Caution: do not use LINQ.SequenceEqual for comparing the two quality arrays (left and right) due to performance reasons
    248248      if (dominateOnEqualQualities) {
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/MultiObjective/HypervolumeCalculator.cs

    r17309 r17315  
    2626namespace HeuristicLab.Optimization {
    2727  public static class HypervolumeCalculator {
    28     public static double[] CalculateNadirPoint(IEnumerable<double[]> qualities, IReadOnlyList<bool> maximization) {
     28    public static double[] CalculateNadirPoint(IEnumerable<double[]> qualities, bool[] maximization) {
    2929      var res = maximization.Select(m => m ? double.MaxValue : double.MinValue).ToArray();
    3030      foreach (var quality in qualities)
     
    5959    /// </summary>
    6060    ///
    61     public static double CalculateHypervolume(IList<double[]> qualities, double[] referencePoint, IReadOnlyList<bool> maximization) {
     61    public static double CalculateHypervolume(IList<double[]> qualities, double[] referencePoint, bool[] maximization) {
    6262      qualities = qualities.Where(vec => DominationCalculator.Dominates(vec, referencePoint, maximization, false) == DominationResult.Dominates).ToArray();
    6363      if (qualities.Count== 0) return 0; //TODO computation for negative hypervolume?
    64       if (maximization.Count == 2)
     64      if (maximization.Length == 2)
    6565        return Calculate2D(qualities, referencePoint, maximization);
    6666
    67       if (maximization.All(x => !x))
     67      if (Array.TrueForAll(maximization, x => !x))
    6868        return CalculateMultiDimensional(qualities, referencePoint);
    6969      throw new NotImplementedException("HypervolumeCalculator calculation for more than two dimensions is supported only with minimization problems.");
     
    7878    /// <param name="maximization"></param>
    7979    /// <returns></returns>
    80     public static double Calculate2D(IList<double[]> front, double[] referencePoint, IReadOnlyList<bool> maximization) {
     80    public static double Calculate2D(IList<double[]> front, double[] referencePoint, bool[] maximization) {
    8181      if (front == null) throw new ArgumentNullException("front");
    8282      if (referencePoint == null) throw new ArgumentNullException("referencePoint");
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/MultiObjectiveExternalEvaluationProblem.cs

    r17309 r17315  
    9090    public MultiObjectiveExternalEvaluationProblem(TEncoding encoding)
    9191      : base(encoding) {
    92       MaximizationParameter.ReadOnly = false;
    93       MaximizationParameter.Value = new BoolArray(new [] { false, false });
     92      Parameters.Remove("Maximization"); // readonly in base class
     93      Parameters.Add(new FixedValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray()));
    9494      Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
    9595      Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
     
    9999
    100100    #region Multi Objective Problem Overrides
     101    public override bool[] Maximization {
     102      get {
     103        return Parameters.ContainsKey("Maximization") ? ((IValueParameter<BoolArray>)Parameters["Maximization"]).Value.ToArray() : new bool[0];
     104      }
     105    }
     106
    101107    public virtual void SetMaximization(bool[] maximization) {
    102108      ((IStringConvertibleArray)MaximizationParameter.Value).Length = maximization.Length;
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/CompiledProblemDefinition.cs

    r17309 r17315  
    2323using System.Collections.Generic;
    2424using HeuristicLab.Core;
     25using HeuristicLab.Data;
    2526using HeuristicLab.Optimization;
    2627
     
    7778
    7879    #region ISingleObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
    79     public int Objectives => Maximization.Count;
    80     public abstract IReadOnlyList<bool> Maximization { get; }
     80    public int Objectives => Maximization.Length;
     81    public abstract bool[] Maximization { get; }
    8182    public abstract IReadOnlyList<double[]> BestKnownFront { get; }
    8283    public abstract double[] ReferencePoint { get; }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProblemDefinitionScript.cs

    r17309 r17315  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Optimization;
    2728
     
    5152    public IReadOnlyList<double[]> BestKnownFront => CompiledProblemDefinition.BestKnownFront;
    5253    public double[] ReferencePoint => CompiledProblemDefinition.ReferencePoint;
    53     public IReadOnlyList<bool> Maximization => CompiledProblemDefinition.Maximization;
     54    public bool[] Maximization => CompiledProblemDefinition.Maximization;
    5455
    5556
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r17309 r17315  
    9494
    9595    private void OnProblemDefinitionChanged() {
    96       Maximization = new BoolArray(ProblemDefinition.Maximization);
     96      Parameters.Remove("Maximization");
     97      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()) {Hidden = true});
    9798      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
    9899
     
    106107    private void OnProblemScriptNameChanged() {
    107108      Name = ProblemScript.Name;
     109    }
     110
     111    public override bool[] Maximization {
     112      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.Maximization : new[] {false}; }
    108113    }
    109114
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/MultiObjectiveCombinedEncodingProblem_Template.cs

    r17309 r17315  
    1616namespace HeuristicLab.Problems.Programmable {
    1717  public class CompiledMultiObjectiveProblemDefinition : CompiledMultiObjectiveProblemDefinition<CombinedEncoding, CombinedSolution> {
    18     public override IReadOnlyList<bool> Maximization { get { return new[] { true, false }; } }
     18    public override bool[] Maximization { get { return new[] { true, false }; } }
    1919    public override double[] ReferencePoint { get { return null; } }
    2020    public override IReadOnlyList<double[]> BestKnownFront { get { return null; } }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs

    r17309 r17315  
    2020#endregion
    2121using System;
     22using System.Linq;
    2223using HEAL.Attic;
    2324using HeuristicLab.Analysis;
     
    5152
    5253    #region Properties
     54    public override bool[] Maximization {
     55      get {
     56        //necessary because of virtual member call in base ctor
     57        if (!Parameters.ContainsKey("TestFunction")) return new bool[0];
     58        return TestFunction.Maximization(Objectives).ToArray();
     59      }
     60    }
     61
    5362    public int ProblemSize {
    5463      get { return ProblemSizeParameter.Value.Value; }
     
    132141    #region Events
    133142    private void UpdateParameterValues() {
    134       Maximization = new BoolArray(TestFunction.Maximization(Objectives));
     143      Parameters.Remove(MaximizationParameterName);
     144      Parameters.Add(new FixedValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(TestFunction.Maximization(Objectives)).AsReadOnly()));
    135145
    136146      Parameters.Remove(BestKnownFrontParameterName);
Note: See TracChangeset for help on using the changeset viewer.