- Timestamp:
- 06/04/20 18:16:58 (5 years ago)
- 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 34 34 [Item("IntegerVectorEncoding", "Describes an integer vector encoding.")] 35 35 [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 74 39 public IntMatrix Bounds { 75 40 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 } 77 46 } 78 47 … … 81 50 [StorableHook(HookType.AfterDeserialization)] 82 51 private void AfterDeserialization() { 83 RegisterParameterEvents();84 52 DiscoverOperators(); 53 RegisterEventHandlers(); 85 54 } 86 55 87 56 private IntegerVectorEncoding(IntegerVectorEncoding original, Cloner cloner) 88 57 : 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(); 92 60 } 93 61 public override IDeepCloneable Clone(Cloner cloner) { return new IntegerVectorEncoding(this, cloner); } … … 98 66 public IntegerVectorEncoding(int length) : this("integerVector", length) { } 99 67 public IntegerVectorEncoding(string name, int length, int min = int.MinValue, int max = int.MaxValue, int? step = null) 100 : base(name ) {68 : base(name, length) { 101 69 if (min >= max) throw new ArgumentException("min must be less than max", "min"); 102 70 if (step.HasValue && step.Value <= 0) throw new ArgumentException("step must be greater than zero or null", "step"); … … 107 75 if (step.HasValue) bounds[0, 2] = step.Value; 108 76 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); 113 79 114 80 SolutionCreator = new UniformRandomIntegerVectorCreator(); 115 RegisterParameterEvents();116 81 DiscoverOperators(); 82 RegisterEventHandlers(); 117 83 } 118 84 public IntegerVectorEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null) 119 : base(name ) {85 : base(name, length) { 120 86 if (min.Count == 0) throw new ArgumentException("Bounds must be given for the integer parameters."); 121 87 if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min"); … … 130 96 } 131 97 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); 136 100 137 101 SolutionCreator = new UniformRandomIntegerVectorCreator(); 138 RegisterParameterEvents();139 102 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 }); 162 111 } 163 112 … … 271 220 } 272 221 #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 } 273 232 } 274 233 } -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorMultiObjectiveProblem.cs
r17544 r17587 22 22 #endregion 23 23 24 using System;25 24 using System.Linq; 26 25 using HEAL.Attic; … … 38 37 public IResultDefinition<ParetoFrontScatterPlot<IntegerVector>> BestResult { get { return BestResultParameter; } } 39 38 [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; } 40 public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;39 [Storable] protected ReferenceParameter<IntMatrix> BoundsRefParameter { get; private set; } 41 40 42 41 public int Dimension { 43 42 get { return DimensionRefParameter.Value.Value; } 44 43 set { DimensionRefParameter.Value.Value = value; } 44 } 45 46 public IntMatrix Bounds { 47 get { return BoundsRefParameter.Value; } 48 set { BoundsRefParameter.Value = value; } 45 49 } 46 50 … … 56 60 BestResultParameter = cloner.Clone(original.BestResultParameter); 57 61 DimensionRefParameter = cloner.Clone(original.DimensionRefParameter); 62 BoundsRefParameter = cloner.Clone(original.BoundsRefParameter); 58 63 RegisterEventHandlers(); 59 64 } … … 64 69 Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<IntegerVector>>("Best Pareto Front", "The best Pareto front found.")); 65 70 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)); 66 72 67 73 Operators.Add(new HammingSimilarityCalculator()); … … 94 100 95 101 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); 101 104 } 102 105 103 106 protected virtual void DimensionOnChanged() { } 107 108 protected virtual void BoundsOnChanged() { } 104 109 } 105 110 } -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorProblem.cs
r17544 r17587 22 22 #endregion 23 23 24 using System;25 24 using System.Linq; 26 25 using HEAL.Attic; … … 39 38 public IResultDefinition<IntegerVector> BestResult { get => BestResultParameter; } 40 39 [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; } 41 public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;42 40 [Storable] protected ReferenceParameter<IntMatrix> BoundsRefParameter { get; private set; } 43 public IValueParameter<IntMatrix> BoundsParameter => BoundsRefParameter;44 41 45 42 public int Dimension { 46 43 get { return DimensionRefParameter.Value.Value; } 47 set { DimensionRefParameter.Value.Value = value; }44 protected set { DimensionRefParameter.Value.Value = value; } 48 45 } 49 46 50 47 public IntMatrix Bounds { 51 48 get { return BoundsRefParameter.Value; } 52 set { BoundsRefParameter.Value = value; }49 protected set { BoundsRefParameter.Value = value; } 53 50 } 54 51 … … 103 100 104 101 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); 115 104 } 116 105
Note: See TracChangeset
for help on using the changeset viewer.