Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/18/20 18:55:08 (4 years ago)
Author:
abeham
Message:

#2521: worked on refactoring, worked a lot on binary encoding / problems

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  
    2222using System;
    2323using System.Collections.Generic;
     24using System.ComponentModel;
    2425using System.Linq;
     26using HEAL.Attic;
    2527using HeuristicLab.Common;
    2628using HeuristicLab.Core;
     
    2830using HeuristicLab.Optimization;
    2931using HeuristicLab.Parameters;
    30 using HEAL.Attic;
    3132using HeuristicLab.PluginInfrastructure;
    3233
     
    3435  [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")]
    3536  [StorableType("889C5E1A-3FBF-4AB3-AB2E-199A781503B5")]
    36   public sealed class BinaryVectorEncoding : Encoding<BinaryVector> {
     37  public sealed class BinaryVectorEncoding : Encoding<BinaryVector>, INotifyPropertyChanged {
    3738    #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; }
    5340    #endregion
    5441
    5542    public int Length {
    5643      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      }
    5848    }
    5949
     
    6252    [StorableHook(HookType.AfterDeserialization)]
    6353    private void AfterDeserialization() {
    64       RegisterParameterEvents();
    6554      DiscoverOperators();
     55      RegisterEventHandlers();
    6656    }
    6757    public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorEncoding(this, cloner); }
    6858    private BinaryVectorEncoding(BinaryVectorEncoding original, Cloner cloner)
    6959      : base(original, cloner) {
    70       lengthParameter = cloner.Clone(original.lengthParameter);
    71       RegisterParameterEvents();
     60      LengthParameter = cloner.Clone(original.LengthParameter);
     61      RegisterEventHandlers();
    7262    }
    7363
     
    7767    public BinaryVectorEncoding(string name, int length)
    7868      : 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();
    8173
    82       SolutionCreator = new RandomBinaryVectorCreator();
    83       RegisterParameterEvents();
    84       DiscoverOperators();
     74      RegisterEventHandlers();
    8575    }
    8676
    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      };
    9782    }
    9883
    9984    #region Operator Discovery
    10085    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
     86
    10187    static BinaryVectorEncoding() {
    10288      encodingSpecificOperatorTypes = new List<Type>() {
     
    169155    }
    170156    #endregion
     157
     158    public event PropertyChangedEventHandler PropertyChanged;
     159    private void OnPropertyChanged(string property) {
     160      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
     161    }
    171162  }
    172163}
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorMultiObjectiveProblem.cs

    r17522 r17544  
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Core;
     28using HeuristicLab.Data;
    2829using HeuristicLab.Optimization;
     30using HeuristicLab.Parameters;
    2931
    3032namespace HeuristicLab.Encodings.BinaryVectorEncoding {
     
    3335    [Storable] protected IResultParameter<ParetoFrontScatterPlot<BinaryVector>> BestResultParameter { get; private set; }
    3436    public IResultDefinition<ParetoFrontScatterPlot<BinaryVector>> BestResult { get { return BestResultParameter; } }
     37    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
     38    public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
    3539
    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; }
    3943    }
    4044
     
    4953      : base(original, cloner) {
    5054      BestResultParameter = cloner.Clone(original.BestResultParameter);
     55      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
    5156      RegisterEventHandlers();
    5257    }
     
    5661      EncodingParameter.ReadOnly = true;
    5762      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));
    5864
    5965      Operators.Add(new HammingSimilarityCalculator());
     
    8692
    8793    private void RegisterEventHandlers() {
    88       Encoding.LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
     94      DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
    8995    }
    9096
    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() { }
    92102  }
    93103}
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorProblem.cs

    r17522 r17544  
    2121#endregion
    2222
    23 using System;
    2423using System.Linq;
    2524using HEAL.Attic;
     
    2726using HeuristicLab.Common;
    2827using HeuristicLab.Core;
     28using HeuristicLab.Data;
    2929using HeuristicLab.Optimization;
    3030using HeuristicLab.Optimization.Operators;
     31using HeuristicLab.Parameters;
    3132
    3233namespace HeuristicLab.Encodings.BinaryVectorEncoding {
     
    3435  public abstract class BinaryVectorProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
    3536    [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; }
    3739
    38     public int Length {
     40    public int Dimension {
    3941      get { return Encoding.Length; }
    4042      set { Encoding.Length = value; }
     
    5153      : base(original, cloner) {
    5254      BestResultParameter = cloner.Clone(original.BestResultParameter);
     55      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
    5356      RegisterEventHandlers();
    5457    }
     
    5861      EncodingParameter.ReadOnly = true;
    5962      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));
    6064
    6165      Operators.Add(new HammingSimilarityCalculator());
     
    7478    }
    7579
    76     protected override void OnEncodingChanged() {
    77       base.OnEncodingChanged();
    78       Parameterize();
    79     }
    80 
    8180    private void Parameterize() {
    8281      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
     
    8786
    8887    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      };
    9092    }
    9193
    92     protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
     94    protected virtual void DimensionOnChanged() { }
    9395  }
    9496}
Note: See TracChangeset for help on using the changeset viewer.