- Timestamp:
- 05/18/20 18:55:08 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEncoding.cs
r17226 r17544 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 24 25 using System.Linq; 26 using HEAL.Attic; 25 27 using HeuristicLab.Common; 26 28 using HeuristicLab.Core; … … 28 30 using HeuristicLab.Optimization; 29 31 using HeuristicLab.Parameters; 30 using HEAL.Attic;31 32 using HeuristicLab.PluginInfrastructure; 32 33 … … 34 35 [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")] 35 36 [StorableType("889C5E1A-3FBF-4AB3-AB2E-199A781503B5")] 36 public sealed class BinaryVectorEncoding : Encoding<BinaryVector> {37 public sealed class BinaryVectorEncoding : Encoding<BinaryVector>, INotifyPropertyChanged { 37 38 #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 } 39 [Storable] public IValueParameter<IntValue> LengthParameter { get; private set; } 53 40 #endregion 54 41 55 42 public int Length { 56 43 get { return LengthParameter.Value.Value; } 57 set { LengthParameter.Value.Value = value; } 44 set { 45 if (Length == value) return; 46 LengthParameter.ForceValue(new IntValue(value, @readonly: LengthParameter.Value.ReadOnly)); 47 } 58 48 } 59 49 … … 62 52 [StorableHook(HookType.AfterDeserialization)] 63 53 private void AfterDeserialization() { 64 RegisterParameterEvents();65 54 DiscoverOperators(); 55 RegisterEventHandlers(); 66 56 } 67 57 public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorEncoding(this, cloner); } 68 58 private BinaryVectorEncoding(BinaryVectorEncoding original, Cloner cloner) 69 59 : base(original, cloner) { 70 lengthParameter = cloner.Clone(original.lengthParameter);71 Register ParameterEvents();60 LengthParameter = cloner.Clone(original.LengthParameter); 61 RegisterEventHandlers(); 72 62 } 73 63 … … 77 67 public BinaryVectorEncoding(string name, int length) 78 68 : base(name) { 79 lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length)); 80 Parameters.Add(lengthParameter); 69 Parameters.Add(LengthParameter = new ValueParameter<IntValue>(Name + ".Length", new IntValue(length, @readonly: true)) { ReadOnly = true }); 70 71 SolutionCreator = new RandomBinaryVectorCreator(); 72 DiscoverOperators(); 81 73 82 SolutionCreator = new RandomBinaryVectorCreator(); 83 RegisterParameterEvents(); 84 DiscoverOperators(); 74 RegisterEventHandlers(); 85 75 } 86 76 87 private void OnLengthParameterChanged() { 88 RegisterLengthParameterEvents(); 89 ConfigureOperators(Operators); 90 } 91 private void RegisterParameterEvents() { 92 RegisterLengthParameterEvents(); 93 } 94 private void RegisterLengthParameterEvents() { 95 LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators); 96 LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators); 77 private void RegisterEventHandlers() { 78 LengthParameter.ValueChanged += (_, __) => { 79 if (!LengthParameter.Value.ReadOnly) LengthParameter.Value.ValueChanged += (___, ____) => OnPropertyChanged(nameof(Length)); 80 OnPropertyChanged(nameof(Length)); 81 }; 97 82 } 98 83 99 84 #region Operator Discovery 100 85 private static readonly IEnumerable<Type> encodingSpecificOperatorTypes; 86 101 87 static BinaryVectorEncoding() { 102 88 encodingSpecificOperatorTypes = new List<Type>() { … … 169 155 } 170 156 #endregion 157 158 public event PropertyChangedEventHandler PropertyChanged; 159 private void OnPropertyChanged(string property) { 160 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); 161 } 171 162 } 172 163 } -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorMultiObjectiveProblem.cs
r17522 r17544 26 26 using HeuristicLab.Common; 27 27 using HeuristicLab.Core; 28 using HeuristicLab.Data; 28 29 using HeuristicLab.Optimization; 30 using HeuristicLab.Parameters; 29 31 30 32 namespace HeuristicLab.Encodings.BinaryVectorEncoding { … … 33 35 [Storable] protected IResultParameter<ParetoFrontScatterPlot<BinaryVector>> BestResultParameter { get; private set; } 34 36 public IResultDefinition<ParetoFrontScatterPlot<BinaryVector>> BestResult { get { return BestResultParameter; } } 37 [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; } 38 public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter; 35 39 36 public int Length{37 get { return Encoding.Length; }38 set { Encoding.Length= value; }40 public int Dimension { 41 get { return DimensionRefParameter.Value.Value; } 42 set { DimensionRefParameter.Value.Value = value; } 39 43 } 40 44 … … 49 53 : base(original, cloner) { 50 54 BestResultParameter = cloner.Clone(original.BestResultParameter); 55 DimensionRefParameter = cloner.Clone(original.DimensionRefParameter); 51 56 RegisterEventHandlers(); 52 57 } … … 56 61 EncodingParameter.ReadOnly = true; 57 62 Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<BinaryVector>>("Best Pareto Front", "The best Pareto front found.")); 63 Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter)); 58 64 59 65 Operators.Add(new HammingSimilarityCalculator()); … … 86 92 87 93 private void RegisterEventHandlers() { 88 Encoding.LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;94 DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged; 89 95 } 90 96 91 protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { } 97 private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) { 98 DimensionOnChanged(); 99 } 100 101 protected virtual void DimensionOnChanged() { } 92 102 } 93 103 } -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorProblem.cs
r17522 r17544 21 21 #endregion 22 22 23 using System;24 23 using System.Linq; 25 24 using HEAL.Attic; … … 27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Core; 28 using HeuristicLab.Data; 29 29 using HeuristicLab.Optimization; 30 30 using HeuristicLab.Optimization.Operators; 31 using HeuristicLab.Parameters; 31 32 32 33 namespace HeuristicLab.Encodings.BinaryVectorEncoding { … … 34 35 public abstract class BinaryVectorProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> { 35 36 [Storable] protected IResultParameter<ISingleObjectiveSolutionContext<BinaryVector>> BestResultParameter { get; private set; } 36 public IResultDefinition<ISingleObjectiveSolutionContext<BinaryVector>> BestResult { get { return BestResultParameter; } } 37 public IResultDefinition<ISingleObjectiveSolutionContext<BinaryVector>> BestResult => BestResultParameter; 38 [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; } 37 39 38 public int Length{40 public int Dimension { 39 41 get { return Encoding.Length; } 40 42 set { Encoding.Length = value; } … … 51 53 : base(original, cloner) { 52 54 BestResultParameter = cloner.Clone(original.BestResultParameter); 55 DimensionRefParameter = cloner.Clone(original.DimensionRefParameter); 53 56 RegisterEventHandlers(); 54 57 } … … 58 61 EncodingParameter.ReadOnly = true; 59 62 Parameters.Add(BestResultParameter = new ResultParameter<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution", "The best solution.")); 63 Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter)); 60 64 61 65 Operators.Add(new HammingSimilarityCalculator()); … … 74 78 } 75 79 76 protected override void OnEncodingChanged() {77 base.OnEncodingChanged();78 Parameterize();79 }80 81 80 private void Parameterize() { 82 81 foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) { … … 87 86 88 87 private void RegisterEventHandlers() { 89 Encoding.LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged; 88 Encoding.PropertyChanged += (sender, args) => { 89 if (args.PropertyName == nameof(Encoding.Length)) 90 DimensionOnChanged(); 91 }; 90 92 } 91 93 92 protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }94 protected virtual void DimensionOnChanged() { } 93 95 } 94 96 }
Note: See TracChangeset
for help on using the changeset viewer.