- Timestamp:
- 06/04/20 18:16:58 (5 years ago)
- 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 34 34 [Item("RealVectorEncoding", "Describes a real vector encoding.")] 35 35 [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; } 57 41 set { 58 42 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 } 76 46 } 77 47 … … 80 50 [StorableHook(HookType.AfterDeserialization)] 81 51 private void AfterDeserialization() { 82 RegisterParameterEvents();83 52 DiscoverOperators(); 53 RegisterParameterEvents(); 84 54 } 85 55 … … 87 57 private RealVectorEncoding(RealVectorEncoding original, Cloner cloner) 88 58 : base(original, cloner) { 89 lengthParameter = cloner.Clone(original.lengthParameter); 90 boundsParameter = cloner.Clone(original.boundsParameter); 59 BoundsParameter = cloner.Clone(original.BoundsParameter); 91 60 RegisterParameterEvents(); 92 61 } … … 96 65 public RealVectorEncoding(int length) : this("RealVector", length) { } 97 66 public RealVectorEncoding(string name, int length, double min = -1000, double max = 1000) 98 : base(name ) {67 : base(name, length) { 99 68 if (min >= max) throw new ArgumentException("min must be less than max", "min"); 100 69 … … 103 72 bounds[0, 1] = max; 104 73 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); 109 76 110 77 SolutionCreator = new UniformRandomRealVectorCreator(); … … 114 81 115 82 public RealVectorEncoding(string name, int length, IList<double> min, IList<double> max) 116 : base(name ) {83 : base(name, length) { 117 84 if (min.Count == 0) throw new ArgumentException("Bounds must be given for the real parameters."); 118 85 if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min"); … … 124 91 bounds[i, 1] = max[i]; 125 92 } 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); 130 95 131 96 SolutionCreator = new UniformRandomRealVectorCreator(); 132 RegisterParameterEvents();133 97 DiscoverOperators(); 134 } 135 136 private void OnLengthParameterChanged() { 137 RegisterLengthParameterEvents(); 138 ConfigureOperators(Operators); 139 } 140 private void OnBoundsParameterChanged() { 141 RegisterBoundsParameterEvents(); 142 ConfigureOperators(Operators); 98 RegisterParameterEvents(); 143 99 } 144 100 145 101 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 }); 156 106 } 157 107 … … 316 266 } 317 267 #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 } 318 278 } 319 279 } -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorMultiObjectiveProblem.cs
r17544 r17587 22 22 #endregion 23 23 24 using System;25 24 using System.Linq; 26 25 using HEAL.Attic; … … 36 35 public abstract class RealVectorMultiObjectiveProblem : MultiObjectiveProblem<RealVectorEncoding, RealVector> { 37 36 [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; } 38 public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;39 37 [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; } 40 public IValueParameter<DoubleMatrix> BoundsParameter => BoundsRefParameter;41 38 42 39 public int Dimension { … … 47 44 public DoubleMatrix Bounds { 48 45 get { return BoundsRefParameter.Value; } 49 set { Bounds Parameter.Value = value; }46 set { BoundsRefParameter.Value = value; } 50 47 } 51 48 … … 99 96 100 97 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); 111 100 } 112 101 -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorProblem.cs
r17544 r17587 22 22 #endregion 23 23 24 using System;25 24 using System.Linq; 26 25 using HEAL.Attic; … … 37 36 public abstract class RealVectorProblem : SingleObjectiveProblem<RealVectorEncoding, RealVector> { 38 37 [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; } 39 public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;40 38 [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; } 41 public IValueParameter<DoubleMatrix> BoundsParameter => BoundsRefParameter;42 39 43 40 public int Dimension { … … 48 45 public DoubleMatrix Bounds { 49 46 get { return BoundsRefParameter.Value; } 50 set { Bounds Parameter.Value = value; }47 set { BoundsRefParameter.Value = value; } 51 48 } 52 49 … … 99 96 100 97 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); 111 100 } 112 101
Note: See TracChangeset
for help on using the changeset viewer.