Free cookie consent management tool by TermsFeed Policy Generator

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

#2521: refactoring in progress

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

Legend:

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

    r17567 r17587  
    3434  [Item("IntegerVectorEncoding", "Describes an integer vector encoding.")]
    3535  [StorableType("15D6E55E-C39F-4784-8350-14A0FD47CF0E")]
    36   public sealed class IntegerVectorEncoding : Encoding<IntegerVector> {
    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 
    54     [Storable]
    55     private IValueParameter<IntMatrix> boundsParameter;
    56     public IValueParameter<IntMatrix> BoundsParameter {
    57       get { return boundsParameter; }
    58       set {
    59         if (value == null) throw new ArgumentNullException("Bounds parameter must not be null.");
    60         if (boundsParameter == value) return;
    61 
    62         if (boundsParameter != null) Parameters.Remove(boundsParameter);
    63         boundsParameter = value;
    64         Parameters.Add(boundsParameter);
    65         OnBoundsParameterChanged();
    66       }
    67     }
    68     #endregion
    69 
    70     public int Length {
    71       get { return LengthParameter.Value.Value; }
    72       set { LengthParameter.Value.Value = value; }
    73     }
     36  public sealed class IntegerVectorEncoding : VectorEncoding<IntegerVector> {
     37    [Storable] public IValueParameter<IntMatrix> BoundsParameter { get; private set; }
     38
    7439    public IntMatrix Bounds {
    7540      get { return BoundsParameter.Value; }
    76       set { BoundsParameter.Value = value; }
     41      set {
     42        if (value == null) throw new ArgumentNullException("Bounds must not be null.");
     43        if (Bounds == value) return;
     44        BoundsParameter.Value = value;
     45      }
    7746    }
    7847
     
    8150    [StorableHook(HookType.AfterDeserialization)]
    8251    private void AfterDeserialization() {
    83       RegisterParameterEvents();
    8452      DiscoverOperators();
     53      RegisterEventHandlers();
    8554    }
    8655
    8756    private IntegerVectorEncoding(IntegerVectorEncoding original, Cloner cloner)
    8857      : base(original, cloner) {
    89       lengthParameter = cloner.Clone(original.lengthParameter);
    90       boundsParameter = cloner.Clone(original.boundsParameter);
    91       RegisterParameterEvents();
     58      BoundsParameter = cloner.Clone(original.BoundsParameter);
     59      RegisterEventHandlers();
    9260    }
    9361    public override IDeepCloneable Clone(Cloner cloner) { return new IntegerVectorEncoding(this, cloner); }
     
    9866    public IntegerVectorEncoding(int length) : this("integerVector", length) { }
    9967    public IntegerVectorEncoding(string name, int length, int min = int.MinValue, int max = int.MaxValue, int? step = null)
    100       : base(name) {
     68      : base(name, length) {
    10169      if (min >= max) throw new ArgumentException("min must be less than max", "min");
    10270      if (step.HasValue && step.Value <= 0) throw new ArgumentException("step must be greater than zero or null", "step");
     
    10775      if (step.HasValue) bounds[0, 2] = step.Value;
    10876
    109       lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
    110       boundsParameter = new ValueParameter<IntMatrix>(Name + ".Bounds", bounds);
    111       Parameters.Add(lengthParameter);
    112       Parameters.Add(boundsParameter);
     77      BoundsParameter = new ValueParameter<IntMatrix>(Name + ".Bounds", bounds);
     78      Parameters.Add(BoundsParameter);
    11379
    11480      SolutionCreator = new UniformRandomIntegerVectorCreator();
    115       RegisterParameterEvents();
    11681      DiscoverOperators();
     82      RegisterEventHandlers();
    11783    }
    11884    public IntegerVectorEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null)
    119       : base(name) {
     85      : base(name, length) {
    12086      if (min.Count == 0) throw new ArgumentException("Bounds must be given for the integer parameters.");
    12187      if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min");
     
    13096      }
    13197
    132       lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
    133       boundsParameter = new ValueParameter<IntMatrix>(Name + ".Bounds", bounds);
    134       Parameters.Add(lengthParameter);
    135       Parameters.Add(boundsParameter);
     98      BoundsParameter = new ValueParameter<IntMatrix>(Name + ".Bounds", bounds);
     99      Parameters.Add(BoundsParameter);
    136100
    137101      SolutionCreator = new UniformRandomIntegerVectorCreator();
    138       RegisterParameterEvents();
    139102      DiscoverOperators();
    140     }
    141 
    142     private void OnLengthParameterChanged() {
    143       RegisterLengthParameterEvents();
    144       ConfigureOperators(Operators);
    145     }
    146     private void OnBoundsParameterChanged() {
    147       RegisterBoundsParameterEvents();
    148       ConfigureOperators(Operators);
    149     }
    150 
    151     private void RegisterParameterEvents() {
    152       RegisterLengthParameterEvents();
    153       RegisterBoundsParameterEvents();
    154     }
    155     private void RegisterLengthParameterEvents() {
    156       LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
    157       LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
    158     }
    159     private void RegisterBoundsParameterEvents() {
    160       BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
    161       boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Operators);
     103      RegisterEventHandlers();
     104    }
     105
     106    private void RegisterEventHandlers() {
     107      IntMatrixParameterChangeHandler.Create(BoundsParameter, () => {
     108        ConfigureOperators(Operators);
     109        OnBoundsChanged();
     110      });
    162111    }
    163112
     
    271220    }
    272221    #endregion
     222
     223    protected override void OnLengthChanged() {
     224      ConfigureOperators(Operators);
     225      base.OnLengthChanged();
     226    }
     227
     228    public event EventHandler BoundsChanged;
     229    private void OnBoundsChanged() {
     230      BoundsChanged?.Invoke(this, EventArgs.Empty);
     231    }
    273232  }
    274233}
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorMultiObjectiveProblem.cs

    r17544 r17587  
    2222#endregion
    2323
    24 using System;
    2524using System.Linq;
    2625using HEAL.Attic;
     
    3837    public IResultDefinition<ParetoFrontScatterPlot<IntegerVector>> BestResult { get { return BestResultParameter; } }
    3938    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
    40     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
     39    [Storable] protected ReferenceParameter<IntMatrix> BoundsRefParameter { get; private set; }
    4140
    4241    public int Dimension {
    4342      get { return DimensionRefParameter.Value.Value; }
    4443      set { DimensionRefParameter.Value.Value = value; }
     44    }
     45
     46    public IntMatrix Bounds {
     47      get { return BoundsRefParameter.Value; }
     48      set { BoundsRefParameter.Value = value; }
    4549    }
    4650
     
    5660      BestResultParameter = cloner.Clone(original.BestResultParameter);
    5761      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
     62      BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
    5863      RegisterEventHandlers();
    5964    }
     
    6469      Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<IntegerVector>>("Best Pareto Front", "The best Pareto front found."));
    6570      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the integer vector problem.", Encoding.LengthParameter));
     71      Parameters.Add(BoundsRefParameter = new ReferenceParameter<IntMatrix>("Bounds", "The bounds of the integer vector problem.", Encoding.BoundsParameter));
    6672
    6773      Operators.Add(new HammingSimilarityCalculator());
     
    94100
    95101    private void RegisterEventHandlers() {
    96       DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
    97     }
    98 
    99     private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) {
    100       DimensionOnChanged();
     102      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
     103      IntMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
    101104    }
    102105
    103106    protected virtual void DimensionOnChanged() { }
     107
     108    protected virtual void BoundsOnChanged() { }
    104109  }
    105110}
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorProblem.cs

    r17544 r17587  
    2222#endregion
    2323
    24 using System;
    2524using System.Linq;
    2625using HEAL.Attic;
     
    3938    public IResultDefinition<IntegerVector> BestResult { get => BestResultParameter; }
    4039    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
    41     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
    4240    [Storable] protected ReferenceParameter<IntMatrix> BoundsRefParameter { get; private set; }
    43     public IValueParameter<IntMatrix> BoundsParameter => BoundsRefParameter;
    4441
    4542    public int Dimension {
    4643      get { return DimensionRefParameter.Value.Value; }
    47       set { DimensionRefParameter.Value.Value = value; }
     44      protected set { DimensionRefParameter.Value.Value = value; }
    4845    }
    4946
    5047    public IntMatrix Bounds {
    5148      get { return BoundsRefParameter.Value; }
    52       set { BoundsRefParameter.Value = value; }
     49      protected set { BoundsRefParameter.Value = value; }
    5350    }
    5451
     
    103100
    104101    private void RegisterEventHandlers() {
    105       DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
    106       BoundsRefParameter.ValueChanged += BoundsParameter_ValueChanged;
    107     }
    108 
    109     private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) {
    110       DimensionOnChanged();
    111     }
    112 
    113     private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
    114       BoundsOnChanged();
     102      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
     103      IntMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
    115104    }
    116105
Note: See TracChangeset for help on using the changeset viewer.