Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/12/14 17:52:15 (9 years ago)
Author:
mkommend
Message:

#2174: Refactoring of encodings to allow wiring of operators within the encoding instead of the problem (work in progress).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs

    r11484 r11543  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Text;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Data;
     29using HeuristicLab.Encodings.RealVectorEncoding;
     30using HeuristicLab.Optimization;
     31using HeuristicLab.Parameters;
    2832using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2933
     
    3135  [Item("RealEncoding", "Describes a real vector encoding.")]
    3236  [StorableClass]
    33   public class RealEncoding : Encoding {
     37  public sealed class RealEncoding : Encoding {
     38
    3439    [Storable]
    35     private IntValue length;
    36     public IntValue Length {
    37       get { return length; }
     40    private IFixedValueParameter<IntValue> lengthParameter;
     41    public IFixedValueParameter<IntValue> LengthParameter {
     42      get { return lengthParameter; }
    3843      set {
    39         if (length == value) return;
    40         length = value;
    41         OnParameterConfigurationChanged();
     44        if(value == null) throw new ArgumentNullException("Length parameter must not be null.");
     45        if (lengthParameter == value) return;
     46        lengthParameter = value;
     47        OnLengthParameterChanged();
    4248      }
    4349    }
    4450
    4551    [Storable]
    46     private DoubleMatrix bounds;
    47     public DoubleMatrix Bounds {
    48       get { return bounds; }
     52    private IValueParameter<DoubleMatrix> boundsParameter;
     53    public IValueParameter<DoubleMatrix> BoundsParameter {
     54      get { return boundsParameter; }
    4955      set {
    50         if (bounds == value) return;
    51         bounds = value;
    52         OnParameterConfigurationChanged();
     56        if (value == null) throw new ArgumentNullException("Bounds parameter must not be null.");
     57        if (boundsParameter == value) return;
     58        boundsParameter = value;
     59        OnBoundsParameterChanged();
    5360      }
    5461    }
    5562
     63    public override IEnumerable<IValueParameter> EncodingParameters {
     64      get { return base.EncodingParameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); }
     65    }
     66
     67    public int Length {
     68      get { return LengthParameter.Value.Value; }
     69      set { LengthParameter.Value.Value = value; }
     70    }
     71    public DoubleMatrix Bounds {
     72      get { return BoundsParameter.Value; }
     73      set { BoundsParameter.Value = value; }
     74    }
     75
     76    public ISolutionCreator SolutionCreator {
     77      get {
     78        var creator = new UniformRandomRealVectorCreator();
     79        creator.RealVectorParameter.ActualName = Name;
     80        creator.LengthParameter.ActualName = LengthParameter.Name;
     81        creator.BoundsParameter.ActualName = BoundsParameter.Name;
     82        return creator;
     83      }
     84    }
     85     
    5686    [StorableConstructor]
    57     protected RealEncoding(bool deserializing) : base(deserializing) { }
    58     protected RealEncoding(RealEncoding original, Cloner cloner)
     87    private RealEncoding(bool deserializing) : base(deserializing) { }
     88    private RealEncoding(RealEncoding original, Cloner cloner)
    5989      : base(original, cloner) {
    60       length = cloner.Clone(original.length);
    61       bounds = cloner.Clone(original.bounds);
     90      lengthParameter = cloner.Clone(original.lengthParameter);
     91      boundsParameter = cloner.Clone(original.boundsParameter);
    6292    }
    6393    public RealEncoding(string name, int length, double min, double max)
    6494      : base(name) {
    6595      if (min >= max) throw new ArgumentException("min must be less than max", "min");
    66       this.length = new IntValue(length);
    67       bounds = new DoubleMatrix(1, 2);
     96
     97      var bounds = new DoubleMatrix(1, 2);
    6898      bounds[0, 0] = min;
    6999      bounds[0, 1] = max;
     100
     101      lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
     102      boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds);
    70103    }
    71104    public RealEncoding(string name, int length, IList<double> min, IList<double> max)
     
    74107      if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min");
    75108      if (min.Zip(max, (mi, ma) => mi >= ma).Any(x => x)) throw new ArgumentException("min must be less than max in each dimension", "min");
    76       this.length = new IntValue(length);
    77       bounds = new DoubleMatrix(min.Count, 2);
     109
     110      var bounds = new DoubleMatrix(min.Count, 2);
    78111      for (int i = 0; i < min.Count; i++) {
    79112        bounds[i, 0] = min[i];
    80113        bounds[i, 1] = max[i];
    81114      }
    82     }
     115      lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
     116      boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds);
     117    } 
    83118
    84119    public override IDeepCloneable Clone(Cloner cloner) {
     
    86121    }
    87122
     123    private void OnLengthParameterChanged() {
     124       //TODO rewire parameters
     125    }
     126
     127    private void OnBoundsParameterChanged() {
     128      //TODO rewire parameters     
     129    }
     130
     131
    88132  }
    89 }
     133}                           
Note: See TracChangeset for help on using the changeset viewer.