Changeset 17690


Ignore:
Timestamp:
07/21/20 17:06:15 (4 weeks ago)
Author:
abeham
Message:

#2521: worked on multi-objective test function

Location:
branches/2521_ProblemRefactoring
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/DoubleMatrix.cs

    r17253 r17690  
    4242    public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames, bool @readonly = false) : base(elements, columnNames, @readonly) { }
    4343    public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames, bool @readonly = false) : base(elements, columnNames, rowNames, @readonly) { }
     44   
     45    public static DoubleMatrix FromRows(IList<double[]> elements, bool @readonly = false, IEnumerable<string> columnNames = null, IEnumerable<string> rowNames = null) {
     46      if (elements.Count == 0) return new DoubleMatrix(0, 0);
     47      var mat = new double[elements.Count, elements[0].Length];
     48      for (var r = 0; r < mat.GetLength(0); r++)
     49        for (var c = 0; c < mat.GetLength(1); c++)
     50          mat[r, c] = elements[r][c];
     51      // TODO: We should avoid the memory copy in this case
     52      return new DoubleMatrix(mat, columnNames, rowNames, @readonly);
     53    }
     54    public static DoubleMatrix FromColumns(IList<double[]> elements, bool @readonly = false, IEnumerable<string> columnNames = null, IEnumerable<string> rowNames = null) {
     55      if (elements.Count == 0) return new DoubleMatrix(0, 0);
     56      var mat = new double[elements[0].Length, elements.Count];
     57      for (var c = 0; c < mat.GetLength(1); c++)
     58        for (var r = 0; r < mat.GetLength(0); r++)
     59          mat[r, c] = elements[c][r];
     60      // TODO: We should avoid the memory copy in this case
     61      return new DoubleMatrix(mat, columnNames, rowNames, @readonly);
     62    }
    4463
    4564    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/ValueTypeMatrix.cs

    r17253 r17690  
    202202    }
    203203
     204    public IEnumerable<T[]> CloneByRows() {
     205      for (var r = 0; r < matrix.GetLength(0); r++)
     206        yield return GetRow(r).ToArray();
     207    }
     208
     209    public IEnumerable<T[]> CloneByColumn() {
     210      for (var c = 0; c < matrix.GetLength(1); c++)
     211        yield return GetColumn(c).ToArray();
     212    }
     213
    204214    public virtual IEnumerable<T> GetRow(int row) {
    205215      for (var col = 0; col < Columns; col++) {
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs

    r17620 r17690  
    3838    where TEncoding : class, IEncoding<TEncodedSolution>
    3939    where TEncodedSolution : class, IEncodedSolution {
    40     #region Parameter names
    41     public const string BestKnownFrontParameterName = "BestKnownFront";
    42     public const string ReferencePointParameterName = "ReferencePoint";
    43     #endregion
    4440
    4541    #region Parameter properties
    4642    [Storable] public IValueParameter<BoolArray> MaximizationParameter { get; }
    47     public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
    48       get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; }
    49     }
    50     public IValueParameter<DoubleArray> ReferencePointParameter {
    51       get { return (IValueParameter<DoubleArray>)Parameters[ReferencePointParameterName]; }
    52     }
     43    [Storable] public IValueParameter<DoubleMatrix> BestKnownFrontParameter { get; }
     44    [Storable] public IValueParameter<DoubleArray> ReferencePointParameter { get; }
    5345    #endregion
    5446
     
    6052      : base(original, cloner) {
    6153      MaximizationParameter = cloner.Clone(original.MaximizationParameter);
     54      BestKnownFrontParameter = cloner.Clone(original.BestKnownFrontParameter);
     55      ReferencePointParameter = cloner.Clone(original.ReferencePointParameter);
    6256      ParameterizeOperators();
    6357    }
    6458
    6559    protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) {
    66       MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "The dimensions correspond to the different objectives: False if the objective should be minimized, true if it should be maximized..", new BoolArray(new bool[] { }, @readonly: true));
    67       Parameters.Add(MaximizationParameter);
    68       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."));
    69       Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem"));
     60      Parameters.Add(MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "The dimensions correspond to the different objectives: False if the objective should be minimized, true if it should be maximized..", new BoolArray(new bool[] { }, @readonly: true)));
     61      Parameters.Add(BestKnownFrontParameter = new OptionalValueParameter<DoubleMatrix>("Best Known Front", "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."));
     62      Parameters.Add(ReferencePointParameter = new OptionalValueParameter<DoubleArray>("Reference Point", "The reference point for hypervolume calculations on this problem"));
    7063      Operators.Add(Evaluator);
    7164      Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>());
     
    9285    public virtual IReadOnlyList<double[]> BestKnownFront {
    9386      get {
    94         if (!Parameters.ContainsKey(BestKnownFrontParameterName)) return null;
    9587        var mat = BestKnownFrontParameter.Value;
    9688        if (mat == null) return null;
    97         var v = new double[mat.Rows][];
    98         for (var i = 0; i < mat.Rows; i++) {
    99           var r = v[i] = new double[mat.Columns];
    100           for (var j = 0; j < mat.Columns; j++) {
    101             r[j] = mat[i, j];
    102           }
    103         }
    104         return v;
    105       }
    106       set {
    107         if (value == null || value.Count == 0) {
    108           BestKnownFrontParameter.Value = new DoubleMatrix();
    109           return;
    110         }
    111         var mat = new DoubleMatrix(value.Count, value[0].Length);
    112         for (int i = 0; i < value.Count; i++) {
    113           for (int j = 0; j < value[i].Length; j++) {
    114             mat[i, j] = value[i][j];
    115           }
    116         }
    117 
    118         BestKnownFrontParameter.Value = mat;
     89        return mat.CloneByRows().ToList();
    11990      }
    12091    }
     92    public virtual void SetBestKnownFront(IList<double[]> front) {
     93      if (front == null || front.Count == 0) {
     94        BestKnownFrontParameter.Value = null;
     95        return;
     96      }
     97      BestKnownFrontParameter.Value = DoubleMatrix.FromRows(front);
     98    }
    12199    public virtual double[] ReferencePoint {
    122       get { return ReferencePointParameter.Value != null ? ReferencePointParameter.Value.CloneAsArray() : null; }
     100      get { return ReferencePointParameter.Value?.CloneAsArray(); }
    123101      set { ReferencePointParameter.Value = new DoubleArray(value); }
    124102    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs

    r17587 r17690  
    1919 */
    2020#endregion
     21
    2122using System;
    2223using System.Threading;
     
    7576
    7677      BestKnownFrontParameter.Hidden = true;
     78      BestKnownFrontParameter.ReadOnly = true;
     79      ReferencePointParameter.ReadOnly = true;
    7780
    7881      UpdateParameterValues();
     
    8285
    8386    private void RegisterEventHandlers() {
    84       TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
    85       ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
     87      IntValueParameterChangeHandler.Create(ObjectivesParameter, ObjectivesOnChanged);
     88      ParameterChangeHandler<IMultiObjectiveTestFunction>.Create(TestFunctionParameter, TestFunctionOnChanged);
    8689    }
    8790
     
    113116
    114117    #region Events
    115     private void UpdateParameterValues() {
    116       Maximization = TestFunction.Maximization(Objectives);
    117 
    118       Parameters.Remove(BestKnownFrontParameterName);
    119       var front = TestFunction.OptimalParetoFront(Objectives);
    120       var bkf = front != null ? (DoubleMatrix)Utilities.ToMatrix(front).AsReadOnly() : null;
    121       Parameters.Add(new FixedValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualities for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion.", bkf));
    122 
    123       Parameters.Remove(ReferencePointParameterName);
    124       Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
    125 
    126       BoundsRefParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));
    127     }
    128 
    129     protected override void OnEncodingChanged() {
    130       base.OnEncodingChanged();
    131       UpdateParameterValues();
    132     }
    133 
    134     protected override void OnEvaluatorChanged() {
    135       base.OnEvaluatorChanged();
    136       UpdateParameterValues();
    137     }
    138 
    139118    protected override void DimensionOnChanged() {
    140119      base.DimensionOnChanged();
     
    142121        Dimension = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, Dimension));
    143122      UpdateParameterValues();
     123      OnReset();
    144124    }
    145125
    146     private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
     126    protected virtual void TestFunctionOnChanged() {
    147127      Dimension = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(Dimension, TestFunction.MaximumSolutionLength));
    148128      Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
    149       Parameters.Remove(ReferencePointParameterName);
    150       Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));
    151129      UpdateParameterValues();
    152130      OnReset();
    153131    }
    154132
    155     private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {
     133    protected virtual void ObjectivesOnChanged() {
    156134      Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
    157135      UpdateParameterValues();
     136      OnReset();
    158137    }
    159138    #endregion
    160139
    161140    #region Helpers
     141    private void UpdateParameterValues() {
     142      Maximization = TestFunction.Maximization(Objectives);
     143
     144      BestKnownFrontParameter.Value = DoubleMatrix.FromRows(TestFunction.OptimalParetoFront(Objectives));
     145      ReferencePoint = TestFunction.ReferencePoint(Objectives);
     146
     147      BoundsRefParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));
     148    }
     149
    162150    private void InitializeOperators() {
    163151      Operators.Add(new CrowdingAnalyzer());
Note: See TracChangeset for help on using the changeset viewer.