Changeset 11543


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

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

Location:
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3
Files:
1 added
6 edited

Legend:

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

    r11484 r11543  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    2830  [Item("Encoding", "Base class for describing different encodings.")]
    2931  [StorableClass]
    30   public abstract class Encoding : NamedItem {
    31     public override bool CanChangeName {
     32  public abstract class Encoding : NamedItem, IEncoding {
     33    public override sealed bool CanChangeName {
    3234      get { return false; }
    3335    }
     
    3941    protected Encoding(string name) : base(name) { }
    4042
     43    public virtual IEnumerable<IValueParameter> EncodingParameters {
     44      get { return Enumerable.Empty<IValueParameter>(); }
     45    }
    4146
    42     public event EventHandler ParameterConfigurationChanged;
     47    //public event EventHandler ParameterConfigurationChanged;
    4348    protected virtual void OnParameterConfigurationChanged() {
    44       var handler = ParameterConfigurationChanged;
    45       if (handler != null) handler(this, EventArgs.Empty);
     49    //  var handler = ParameterConfigurationChanged;
     50    //  if (handler != null) handler(this, EventArgs.Empty);
    4651    }
    4752  }
  • 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}                           
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj

    r11484 r11543  
    139139    <Compile Include="Encodings\Individual.cs" />
    140140    <Compile Include="Helper.cs" />
     141    <Compile Include="Interfaces\IEncoding.cs" />
    141142    <Compile Include="Interfaces\ISingleObjectiveMoveOperator.cs" />
    142143    <Compile Include="Interfaces\IMultiObjectiveProblemDefinition.cs" />
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r11484 r11543  
    140140      if (definition == null) return;
    141141
    142       Encoding encoding;
    143       try {
    144         encoding = definition.Encoding;
    145       } catch { return; }
     142      Encoding encoding = definition.Encoding;
    146143      EncodingParameter.Value = encoding;
    147144      Maximization = new BoolArray(definition.Maximization);
     
    220217      var realEnc = encoding as RealEncoding;
    221218      if (realEnc != null) {
    222         var l = new ValueParameter<IntValue>(realEnc.Name + "Length", realEnc.Length);
    223         var b = new ValueParameter<DoubleMatrix>(realEnc.Name + "Bounds", realEnc.Bounds);
    224         DynamicEncodingParameters.Add(l);
    225         DynamicEncodingParameters.Add(b);
    226 
    227         var creator = new UniformRandomRealVectorCreator();
    228         creator.RealVectorParameter.ActualName = realEnc.Name;
    229         creator.LengthParameter.ActualName = l.Name;
    230         creator.BoundsParameter.ActualName = b.Name;
    231         return creator;
     219        DynamicEncodingParameters.AddRange(realEnc.EncodingParameters);
     220        return realEnc.SolutionCreator;
    232221      }
    233222      #endregion
     
    458447          .Union(ApplicationManager.Manager.GetInstances<IRealVectorStdDevStrategyParameterOperator>(), new TypeEqualityComparer<IRealVectorStdDevStrategyParameterOperator>())
    459448          .ToList();
    460         var problemSize = ((RealEncoding)encoding).Length.Value;
     449        var problemSize = ((RealEncoding)encoding).Length;
    461450        var bounds = (DoubleMatrix)((RealEncoding)encoding).Bounds.Clone();
    462451        for (var i = 0; i < bounds.Rows; i++) {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/ProblemDefinition.cs

    r11484 r11543  
    3535      get { return encoding; }
    3636      protected set {
     37        if (value == null) throw new ArgumentNullException("The encoding must not be null.");
    3738        if (encoding == value) return;
    3839        encoding = value;
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r11484 r11543  
    143143      if (definition == null) return;
    144144
    145       Encoding encoding;
    146       try {
    147         encoding = definition.Encoding;
    148       } catch { return; }
     145      Encoding encoding = definition.Encoding;
     146
    149147      EncodingParameter.Value = encoding;
    150148      Maximization.Value = definition.IsMaximizationProblem;
     
    225223      var realEnc = encoding as RealEncoding;
    226224      if (realEnc != null) {
    227         var l = new ValueParameter<IntValue>(realEnc.Name + "Length", realEnc.Length);
    228         var b = new ValueParameter<DoubleMatrix>(realEnc.Name + "Bounds", realEnc.Bounds);
    229         DynamicEncodingParameters.Add(l);
    230         DynamicEncodingParameters.Add(b);
    231 
    232         var creator = new UniformRandomRealVectorCreator();
    233         creator.RealVectorParameter.ActualName = realEnc.Name;
    234         creator.LengthParameter.ActualName = l.Name;
    235         creator.BoundsParameter.ActualName = b.Name;
    236         return creator;
     225        DynamicEncodingParameters.AddRange(realEnc.EncodingParameters);       
     226        return realEnc.SolutionCreator;
    237227      }
    238228      #endregion
     
    463453          .Union(ApplicationManager.Manager.GetInstances<IRealVectorStdDevStrategyParameterOperator>(), new TypeEqualityComparer<IRealVectorStdDevStrategyParameterOperator>())
    464454          .ToList();
    465         var problemSize = ((RealEncoding)encoding).Length.Value;
     455        var problemSize = ((RealEncoding)encoding).Length;
    466456        var bounds = (DoubleMatrix)((RealEncoding)encoding).Bounds.Clone();
    467457        for (var i = 0; i < bounds.Rows; i++) {
Note: See TracChangeset for help on using the changeset viewer.