Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17317


Ignore:
Timestamp:
10/04/19 16:03:51 (5 years ago)
Author:
abeham
Message:

#2521: refactored multi-objective problems' maximization

  • Add ForceValue method to IValueParameter to perform changes even when it is read-only
  • Add MaximizationChanged event handler
Location:
branches/2521_ProblemRefactoring
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Core/3.3/Interfaces/IValueParameter.cs

    r17226 r17317  
    3737  public interface IValueParameter<T> : IValueParameter where T : class, IItem {
    3838    new T Value { get; set; }
     39
     40    /// <summary>
     41    /// Sets property <see cref="Value"/> regardless of ReadOnly state
     42    /// </summary>
     43    /// <param name="value">The value to set.</param>
     44    void ForceValue(T value);
    3945  }
    4046}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblem.cs

    r17257 r17317  
    2020#endregion
    2121
     22using System;
    2223using HEAL.Attic;
    2324
    2425namespace HeuristicLab.Optimization {
    2526  [StorableType("251d79f1-a065-47f9-85a3-2e8dbdbf685e")]
    26   public interface IMultiObjectiveProblem : IProblem, IMultiObjectiveHeuristicOptimizationProblem { }
     27  public interface IMultiObjectiveProblem : IProblem, IMultiObjectiveHeuristicOptimizationProblem {
     28    event EventHandler MaximizationChanged;
     29  }
    2730
    2831  [StorableType("806fb361-1469-4903-9f54-f8678b0717b9")]
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblem.cs

    r17257 r17317  
    2020#endregion
    2121
    22 
     22using System;
    2323using HEAL.Attic;
    2424
    2525namespace HeuristicLab.Optimization {
    2626  [StorableType("24830fd5-7d97-41a5-9d7e-d84f1b7ab259")]
    27   public interface ISingleObjectiveProblem : ISingleObjectiveHeuristicOptimizationProblem { }
     27  public interface ISingleObjectiveProblem : ISingleObjectiveHeuristicOptimizationProblem {
     28    event EventHandler MaximizationChanged;
     29  }
    2830
    2931  [StorableType("9cc9422f-0bb5-41e8-9d9e-6e0b66a66449")]
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs

    r17315 r17317  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    3738    where TEncodedSolution : class, IEncodedSolution {
    3839    #region Parameternames
    39     public const string MaximizationParameterName = "Maximization";
    4040    public const string BestKnownFrontParameterName = "BestKnownFront";
    4141    public const string ReferencePointParameterName = "ReferencePoint";
     
    4343
    4444    #region Parameterproperties
    45     public IValueParameter<BoolArray> MaximizationParameter {
    46       get { return (IValueParameter<BoolArray>)Parameters[MaximizationParameterName]; }
    47     }
     45    [Storable] public IValueParameter<BoolArray> MaximizationParameter { get; }
    4846    public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
    4947      get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; }
     
    6058    protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
    6159      : base(original, cloner) {
     60      MaximizationParameter = cloner.Clone(original.MaximizationParameter);
    6261      ParameterizeOperators();
    6362    }
    6463
    6564    protected MultiObjectiveProblem() : base() {
    66       Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
     65      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));
     66      Parameters.Add(MaximizationParameter);
    6767      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."));
    6868      Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem"));
     
    7373
    7474    protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) {
    75       Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
     75      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));
     76      Parameters.Add(MaximizationParameter);
    7677      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."));
    7778      Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem"));
     
    8990      get { return Maximization.Length; }
    9091    }
    91     public abstract bool[] Maximization { get; }
     92    public bool[] Maximization {
     93      get { return MaximizationParameter.Value.CloneAsArray(); }
     94      protected set {
     95        if (MaximizationParameter.Value.SequenceEqual(value)) return;
     96        MaximizationParameter.ForceValue(new BoolArray(value, @readonly: true));
     97        OnMaximizationChanged();
     98      }
     99    }
    92100
    93101    public virtual IReadOnlyList<double[]> BestKnownFront {
     
    168176    #region IMultiObjectiveHeuristicOptimizationProblem Members
    169177    IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter {
    170       get { return Parameters[MaximizationParameterName]; }
     178      get { return MaximizationParameter; }
    171179    }
    172180    IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator {
     
    174182    }
    175183    #endregion
     184
     185    public event EventHandler MaximizationChanged;
     186    protected void OnMaximizationChanged() {
     187      MaximizationChanged?.Invoke(this, EventArgs.Empty);
     188    }
    176189  }
    177190}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs

    r17279 r17317  
    5959      get { return MaximizationParameter.Value.Value; }
    6060      protected set {
    61         if (MaximizationParameter.Value.Value == value) return;
    62         MaximizationParameter.ReadOnly = false;
    63         MaximizationParameter.Value = new BoolValue(value).AsReadOnly();
    64         MaximizationParameter.ReadOnly = true;
     61        if (Maximization == value) return;
     62        MaximizationParameter.ForceValue(new BoolValue(value, @readonly: true));
     63        OnMaximizationChanged();
    6564      }
    6665    }
     
    185184    }
    186185    #endregion
     186
     187    public event EventHandler MaximizationChanged;
     188    protected void OnMaximizationChanged() {
     189      MaximizationChanged?.Invoke(this, EventArgs.Empty);
     190    }
    187191  }
    188192}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/MultiObjectiveHeuristicOptimizationProblem.cs

    r17226 r17317  
    2020#endregion
    2121
     22using System;
     23using HEAL.Attic;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
    2426using HeuristicLab.Data;
    2527using HeuristicLab.Parameters;
    26 using HEAL.Attic;
    2728
    2829namespace HeuristicLab.Optimization {
     
    3637    [StorableConstructor]
    3738    protected MultiObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { }
    38     protected MultiObjectiveHeuristicOptimizationProblem(MultiObjectiveHeuristicOptimizationProblem<T, U> original, Cloner cloner) : base(original, cloner) { }
     39    protected MultiObjectiveHeuristicOptimizationProblem(MultiObjectiveHeuristicOptimizationProblem<T, U> original, Cloner cloner)
     40      : base(original, cloner) {
     41      RegisterEventHandlers();
     42    }
    3943    protected MultiObjectiveHeuristicOptimizationProblem()
    4044      : base() {
    4145      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Determines for each objective whether it should be maximized or minimized."));
     46
     47      RegisterEventHandlers();
    4248    }
    4349
     
    4551      : base(evaluator, solutionCreator) {
    4652      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Determines for each objective whether it should be maximized or minimized."));
     53
     54      RegisterEventHandlers();
     55    }
     56
     57    private void RegisterEventHandlers() {
     58      MaximizationParameter.ValueChanged += MaximizationParameterOnValueChanged;
     59    }
     60
     61    private void MaximizationParameterOnValueChanged(object sender, EventArgs e) {
     62      OnMaximizationChanged();
    4763    }
    4864
     
    6177      get { return Evaluator; }
    6278    }
     79
     80    public event EventHandler MaximizationChanged;
     81    protected void OnMaximizationChanged() {
     82      MaximizationChanged?.Invoke(this, EventArgs.Empty);
     83    }
    6384  }
    6485}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.cs

    r17226 r17317  
    2020#endregion
    2121
     22using System;
     23using HEAL.Attic;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
    2426using HeuristicLab.Data;
    2527using HeuristicLab.Parameters;
    26 using HEAL.Attic;
    2728
    2829namespace HeuristicLab.Optimization {
     
    3738    [StorableConstructor]
    3839    protected SingleObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { }
    39     protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem<T, U> original, Cloner cloner) : base(original, cloner) { }
     40    protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem<T, U> original, Cloner cloner)
     41      : base(original, cloner) {
     42      RegisterEventHandlers();
     43    }
    4044    protected SingleObjectiveHeuristicOptimizationProblem()
    4145      : base() {
    4246      Parameters.Add(new ValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue()));
    4347      Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
     48
     49      RegisterEventHandlers();
    4450    }
    4551
     
    4854      Parameters.Add(new ValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue()));
    4955      Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
     56
     57      RegisterEventHandlers();
     58    }
     59
     60    private void RegisterEventHandlers() {
     61      MaximizationParameter.ValueChanged += MaximizationParameterOnValueChanged;
     62    }
     63
     64    private void MaximizationParameterOnValueChanged(object sender, EventArgs e) {
     65      OnMaximizationChanged();
    5066    }
    5167
     
    5975      }
    6076      #endregion
     77      RegisterEventHandlers();
    6178    }
    6279    public ValueParameter<BoolValue> MaximizationParameter {
     
    6885    public BoolValue Maximization {
    6986      get { return MaximizationParameter.Value; }
    70       set { MaximizationParameter.Value = value; }
     87      set {
     88        if (Maximization == value) return;
     89        MaximizationParameter.Value = value;
     90      }
    7191    }
    7292
     
    85105      get { return Evaluator; }
    86106    }
     107
     108    public event EventHandler MaximizationChanged;
     109    protected void OnMaximizationChanged() {
     110      MaximizationChanged?.Invoke(this, EventArgs.Empty);
     111    }
    87112  }
    88113}
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/FixedValueParameter.cs

    r17226 r17317  
    3232  [StorableType("7787B99D-5F32-4639-B47A-76CB4D204392")]
    3333  public class FixedValueParameter<T> : ValueParameter<T>, IFixedValueParameter<T> where T : class, IItem, new() {
     34    IItem IFixedValueParameter.Value { get { return Value; } }
    3435
    35     public override T Value {
    36       get { return base.Value; }
    37       set { throw new NotSupportedException("FixedValueParameters do not support setting their value."); }
     36    public override void ForceValue(T value) {
     37      throw new NotSupportedException("FixedValueParameters do not support setting their value.");
    3838    }
    39 
    40     IItem IFixedValueParameter.Value { get { return Value; } }
    4139
    4240    [StorableConstructor]
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/OptionalConstrainedValueParameter.cs

    r17226 r17317  
    5353      set {
    5454        if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
    55         if (value != this.value) {
    56           if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
    57           DeregisterValueEvents();
    58           this.value = value;
    59           RegisterValueEvents();
    60           OnValueChanged();
    61         }
     55        DoSetValue(value);
     56      }
     57    }
     58    public virtual void ForceValue(T value) {
     59      DoSetValue(value);
     60    }
     61    private void DoSetValue(T value) {
     62      if (value != this.value) {
     63        if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
     64        DeregisterValueEvents();
     65        this.value = value;
     66        RegisterValueEvents();
     67        OnValueChanged();
    6268      }
    6369    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs

    r17226 r17317  
    4646      set {
    4747        if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
    48         if (value != this.value) {
    49           DeregisterValueEvents();
    50           this.value = value;
    51           RegisterValueEvents();
    52           OnValueChanged();
    53         }
     48        DoSetValue(value);
     49      }
     50    }
     51    public virtual void ForceValue(T value) {
     52      DoSetValue(value);
     53    }
     54    private void DoSetValue(T value) {
     55      if (value != this.value) {
     56        DeregisterValueEvents();
     57        this.value = value;
     58        RegisterValueEvents();
     59        OnValueChanged();
    5460      }
    5561    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ValueLookupParameter.cs

    r17226 r17317  
    4646      set {
    4747        if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
    48         if (value != this.value) {
    49           DeregisterValueEvents();
    50           this.value = value;
    51           RegisterValueEvents();
    52           OnValueChanged();
    53         }
    54       }
    55     }
     48        DoSetValue(value);
     49      }
     50    }
     51    public virtual void ForceValue(T value) {
     52      DoSetValue(value);
     53    }
     54    private void DoSetValue(T value) {
     55      if (value != this.value) {
     56        DeregisterValueEvents();
     57        this.value = value;
     58        RegisterValueEvents();
     59        OnValueChanged();
     60      }
     61    }
     62
    5663    IItem IValueParameter.Value {
    5764      get { return Value; }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisMultiObjectiveProblem.cs

    r17226 r17317  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
     24using HEAL.Attic;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    2628using HeuristicLab.Optimization;
    2729using HeuristicLab.Parameters;
    28 using HEAL.Attic;
    2930
    3031namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    7778
    7879    private void RegisterEventHandler() {
    79       Evaluator.QualitiesParameter.ActualNameChanged += new System.EventHandler(QualitiesParameter_ActualNameChanged);
     80      Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(QualitiesParameter_ActualNameChanged);
     81      MaximizationParameter.ValueChanged += MaximizationParameter_ValueChanged;
    8082    }
    8183
    8284    protected override void OnEvaluatorChanged() {
    8385      base.OnEvaluatorChanged();
    84       Evaluator.QualitiesParameter.ActualNameChanged += new System.EventHandler(QualitiesParameter_ActualNameChanged);
     86      Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(QualitiesParameter_ActualNameChanged);
    8587      Maximization = new BoolArray(Evaluator.Maximization.ToArray());
    8688      ParameterizeOperators();
    8789    }
    8890
    89     private void QualitiesParameter_ActualNameChanged(object sender, System.EventArgs e) {
     91    private void QualitiesParameter_ActualNameChanged(object sender, EventArgs e) {
    9092      ParameterizeOperators();
     93    }
     94
     95    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
     96      OnMaximizationChanged();
    9197    }
    9298
     
    98104      }
    99105    }
     106
     107    public event EventHandler MaximizationChanged;
     108    protected void OnMaximizationChanged() {
     109      MaximizationChanged?.Invoke(this, EventArgs.Empty);
     110    }
    100111  }
    101112}
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSingleObjectiveProblem.cs

    r17226 r17317  
    2222using System;
    2323using System.Linq;
     24using HEAL.Attic;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    2728using HeuristicLab.Optimization;
    2829using HeuristicLab.Parameters;
    29 using HEAL.Attic;
    3030
    3131namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    9595    private void RegisterEventHandler() {
    9696      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(QualityParameter_ActualNameChanged);
     97      MaximizationParameter.ValueChanged += MaximizationParameter_ValueChanged;
    9798    }
    9899
     
    108109    }
    109110
     111    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
     112      OnMaximizationChanged();
     113    }
     114
    110115    protected override void ParameterizeOperators() {
    111116      base.ParameterizeOperators();
     
    116121      }
    117122    }
     123
     124    public event EventHandler MaximizationChanged;
     125    protected void OnMaximizationChanged() {
     126      MaximizationChanged?.Invoke(this, EventArgs.Empty);
     127    }
    118128  }
    119129}
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/MultiObjectiveExternalEvaluationProblem.cs

    r17315 r17317  
    9090    public MultiObjectiveExternalEvaluationProblem(TEncoding encoding)
    9191      : base(encoding) {
    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()));
     92      MaximizationParameter.ReadOnly = false;
     93      MaximizationParameter.Value = 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 
    107101    public virtual void SetMaximization(bool[] maximization) {
    108102      ((IStringConvertibleArray)MaximizationParameter.Value).Length = maximization.Length;
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r17315 r17317  
    2626using HeuristicLab.Common.Resources;
    2727using HeuristicLab.Core;
    28 using HeuristicLab.Data;
    2928using HeuristicLab.Optimization;
    3029using HeuristicLab.Parameters;
     
    9493
    9594    private void OnProblemDefinitionChanged() {
    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});
     95      Maximization = ProblemDefinition.Maximization;
    9896      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
    9997
     
    107105    private void OnProblemScriptNameChanged() {
    108106      Name = ProblemScript.Name;
    109     }
    110 
    111     public override bool[] Maximization {
    112       get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.Maximization : new[] {false}; }
    113107    }
    114108
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs

    r17315 r17317  
    2020#endregion
    2121using System;
    22 using System.Linq;
    2322using HEAL.Attic;
    2423using HeuristicLab.Analysis;
     
    5251
    5352    #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 
    6253    public int ProblemSize {
    6354      get { return ProblemSizeParameter.Value.Value; }
     
    141132    #region Events
    142133    private void UpdateParameterValues() {
    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()));
     134      Maximization = TestFunction.Maximization(Objectives);
    145135
    146136      Parameters.Remove(BestKnownFrontParameterName);
Note: See TracChangeset for help on using the changeset viewer.