Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/04/20 18:16:58 (5 years ago)
Author:
abeham
Message:

#2521: refactoring in progress

Location:
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorEncoding.cs

    r17571 r17587  
    3434  [Item("RealVectorEncoding", "Describes a real vector encoding.")]
    3535  [StorableType("155FFE02-931F-457D-AC95-A0389B0BFECD")]
    36   public sealed class RealVectorEncoding : Encoding<RealVector> {
    37     #region Encoding Parameters
    38     [Storable]
    39     private IFixedValueParameter<IntValue> lengthParameter;
    40     public IFixedValueParameter<IntValue> LengthParameter {
    41       get { return lengthParameter; }
    42       set {
    43         if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
    44         if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
    45         if (lengthParameter == value) return;
    46 
    47         if (lengthParameter != null) Parameters.Remove(lengthParameter);
    48         lengthParameter = value;
    49         Parameters.Add(lengthParameter);
    50         OnLengthParameterChanged();
    51       }
    52     }
    53     [Storable]
    54     private IValueParameter<DoubleMatrix> boundsParameter;
    55     public IValueParameter<DoubleMatrix> BoundsParameter {
    56       get { return boundsParameter; }
     36  public sealed class RealVectorEncoding : VectorEncoding<RealVector> {
     37    [Storable] public IValueParameter<DoubleMatrix> BoundsParameter { get; private set; }
     38
     39    public DoubleMatrix Bounds {
     40      get { return BoundsParameter.Value; }
    5741      set {
    5842        if (value == null) throw new ArgumentNullException("Bounds parameter must not be null.");
    59         if (boundsParameter == value) return;
    60 
    61         if (boundsParameter != null) Parameters.Remove(boundsParameter);
    62         boundsParameter = value;
    63         Parameters.Add(boundsParameter);
    64         OnBoundsParameterChanged();
    65       }
    66     }
    67     #endregion
    68 
    69     public int Length {
    70       get { return LengthParameter.Value.Value; }
    71       set { LengthParameter.Value.Value = value; }
    72     }
    73     public DoubleMatrix Bounds {
    74       get { return BoundsParameter.Value; }
    75       set { BoundsParameter.Value = value; }
     43        if (Bounds == value) return;
     44        BoundsParameter.Value = value;
     45      }
    7646    }
    7747
     
    8050    [StorableHook(HookType.AfterDeserialization)]
    8151    private void AfterDeserialization() {
    82       RegisterParameterEvents();
    8352      DiscoverOperators();
     53      RegisterParameterEvents();
    8454    }
    8555
     
    8757    private RealVectorEncoding(RealVectorEncoding original, Cloner cloner)
    8858      : base(original, cloner) {
    89       lengthParameter = cloner.Clone(original.lengthParameter);
    90       boundsParameter = cloner.Clone(original.boundsParameter);
     59      BoundsParameter = cloner.Clone(original.BoundsParameter);
    9160      RegisterParameterEvents();
    9261    }
     
    9665    public RealVectorEncoding(int length) : this("RealVector", length) { }
    9766    public RealVectorEncoding(string name, int length, double min = -1000, double max = 1000)
    98       : base(name) {
     67      : base(name, length) {
    9968      if (min >= max) throw new ArgumentException("min must be less than max", "min");
    10069
     
    10372      bounds[0, 1] = max;
    10473
    105       lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length)) { ReadOnly = true };
    106       boundsParameter = new ValueParameter<DoubleMatrix>(Name + ".Bounds", bounds) { ReadOnly = true };
    107       Parameters.Add(lengthParameter);
    108       Parameters.Add(boundsParameter);
     74      BoundsParameter = new ValueParameter<DoubleMatrix>(Name + ".Bounds", bounds);
     75      Parameters.Add(BoundsParameter);
    10976
    11077      SolutionCreator = new UniformRandomRealVectorCreator();
     
    11481
    11582    public RealVectorEncoding(string name, int length, IList<double> min, IList<double> max)
    116       : base(name) {
     83      : base(name, length) {
    11784      if (min.Count == 0) throw new ArgumentException("Bounds must be given for the real parameters.");
    11885      if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min");
     
    12491        bounds[i, 1] = max[i];
    12592      }
    126       lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
    127       boundsParameter = new ValueParameter<DoubleMatrix>(Name + ".Bounds", bounds);
    128       Parameters.Add(lengthParameter);
    129       Parameters.Add(boundsParameter);
     93      BoundsParameter = new ValueParameter<DoubleMatrix>(Name + ".Bounds", bounds);
     94      Parameters.Add(BoundsParameter);
    13095
    13196      SolutionCreator = new UniformRandomRealVectorCreator();
    132       RegisterParameterEvents();
    13397      DiscoverOperators();
    134     }
    135 
    136     private void OnLengthParameterChanged() {
    137       RegisterLengthParameterEvents();
    138       ConfigureOperators(Operators);
    139     }
    140     private void OnBoundsParameterChanged() {
    141       RegisterBoundsParameterEvents();
    142       ConfigureOperators(Operators);
     98      RegisterParameterEvents();
    14399    }
    144100
    145101    private void RegisterParameterEvents() {
    146       RegisterLengthParameterEvents();
    147       RegisterBoundsParameterEvents();
    148     }
    149     private void RegisterLengthParameterEvents() {
    150       LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
    151       LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
    152     }
    153     private void RegisterBoundsParameterEvents() {
    154       BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
    155       boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Operators);
     102      DoubleMatrixParameterChangeHandler.Create(BoundsParameter, () => {
     103        ConfigureOperators(Operators);
     104        OnBoundsChanged();
     105      });
    156106    }
    157107
     
    316266    }
    317267    #endregion
     268
     269    protected override void OnLengthChanged() {
     270      ConfigureOperators(Operators);
     271      base.OnLengthChanged();
     272    }
     273
     274    public event EventHandler BoundsChanged;
     275    private void OnBoundsChanged() {
     276      BoundsChanged?.Invoke(this, EventArgs.Empty);
     277    }
    318278  }
    319279}
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorMultiObjectiveProblem.cs

    r17544 r17587  
    2222#endregion
    2323
    24 using System;
    2524using System.Linq;
    2625using HEAL.Attic;
     
    3635  public abstract class RealVectorMultiObjectiveProblem : MultiObjectiveProblem<RealVectorEncoding, RealVector> {
    3736    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
    38     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
    3937    [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; }
    40     public IValueParameter<DoubleMatrix> BoundsParameter => BoundsRefParameter;
    4138
    4239    public int Dimension {
     
    4744    public DoubleMatrix Bounds {
    4845      get { return BoundsRefParameter.Value; }
    49       set { BoundsParameter.Value = value; }
     46      set { BoundsRefParameter.Value = value; }
    5047    }
    5148
     
    9996
    10097    private void RegisterEventHandlers() {
    101       DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
    102       BoundsRefParameter.ValueChanged += BoundsParameter_ValueChanged;
    103     }
    104 
    105     private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) {
    106       DimensionOnChanged();
    107     }
    108 
    109     private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
    110       BoundsOnChanged();
     98      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
     99      DoubleMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
    111100    }
    112101
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorProblem.cs

    r17544 r17587  
    2222#endregion
    2323
    24 using System;
    2524using System.Linq;
    2625using HEAL.Attic;
     
    3736  public abstract class RealVectorProblem : SingleObjectiveProblem<RealVectorEncoding, RealVector> {
    3837    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
    39     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
    4038    [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; }
    41     public IValueParameter<DoubleMatrix> BoundsParameter => BoundsRefParameter;
    4239
    4340    public int Dimension {
     
    4845    public DoubleMatrix Bounds {
    4946      get { return BoundsRefParameter.Value; }
    50       set { BoundsParameter.Value = value; }
     47      set { BoundsRefParameter.Value = value; }
    5148    }
    5249
     
    9996
    10097    private void RegisterEventHandlers() {
    101       DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
    102       BoundsRefParameter.ValueChanged += BoundsParameter_ValueChanged;
    103     }
    104 
    105     private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) {
    106       DimensionOnChanged();
    107     }
    108 
    109     private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
    110       BoundsOnChanged();
     98      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
     99      DoubleMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
    111100    }
    112101
Note: See TracChangeset for help on using the changeset viewer.