Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/20/14 15:12:27 (10 years ago)
Author:
mkommend
Message:

#2174: Adapted IEncoding and Encoding base class.

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

Legend:

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

    r11553 r11559  
    3535  [Item("BinaryEncoding", "Describes a binary vector encoding.")]
    3636  [StorableClass]
    37   public sealed class BinaryEncoding : Encoding {
     37  public sealed class BinaryEncoding : Encoding<IBinaryVectorCreator> {
    3838    #region Encoding Parameters
    3939    [Storable]
     
    5353    #endregion
    5454
    55     private List<IOperator> encodingOperators = new List<IOperator>();
    56     [Storable]
    57     public override IEnumerable<IOperator> Operators {
    58       get { return encodingOperators; }
    59       protected set { encodingOperators = new List<IOperator>(value); }
    60     }
    61 
    62     public override ISolutionCreator DefaultSolutionCreator {
    63       get { return Operators.OfType<RandomBinaryVectorCreator>().First(); }
    64     }
    65 
    6655    public int Length {
    6756      get { return LengthParameter.Value.Value; }
     
    8069      : base(original, cloner) {
    8170      lengthParameter = cloner.Clone(original.lengthParameter);
    82       encodingOperators = original.Operators.Select(cloner.Clone).ToList();
    8371      RegisterParameterEvents();
    8472    }
     
    123111    #endregion
    124112
    125     public void ConfigureOperators(IEnumerable<IOperator> operators) {
     113    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
    126114      ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
    127115      ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs

    r11553 r11559  
    3131  [Item("Encoding", "Base class for describing different encodings.")]
    3232  [StorableClass]
    33   public abstract class Encoding : NamedItem, IEncoding {
     33  public abstract class Encoding<T> : NamedItem, IEncoding
     34    where T : class,ISolutionCreator {
    3435    public override sealed bool CanChangeName {
    3536      get { return false; }
     
    4041    }
    4142
    42     public virtual IEnumerable<IOperator> Operators {
    43       get { return Enumerable.Empty<IOperator>(); }
    44       protected set { }
     43    protected List<IOperator> encodingOperators = new List<IOperator>();
     44    [Storable]
     45    public IEnumerable<IOperator> Operators {
     46      get { return encodingOperators; }
     47      protected set { encodingOperators = new List<IOperator>(value); }
    4548    }
    4649
    47     public virtual ISolutionCreator DefaultSolutionCreator {
     50
     51    ISolutionCreator IEncoding.DefaultSolutionCreator { get { return DefaultSolutionCreator; } }
     52    public virtual T DefaultSolutionCreator {
    4853      get { return null; }
     54    }
     55
     56
     57    ISolutionCreator IEncoding.SolutionCreator {
     58      get { return SolutionCreator; }
     59      set {
     60        if (!(value is T)) throw new ArgumentException("???");
     61        SolutionCreator = (T)value;
     62      }
     63    }
     64    private T solutionCreator;
     65    public T SolutionCreator {
     66      get {
     67        if (solutionCreator == null) return DefaultSolutionCreator;
     68        return solutionCreator;
     69      }
     70      set {
     71        if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
     72        if (solutionCreator == value) return;
     73        solutionCreator = value;
     74        OnSolutionCreatorChanged();
     75      }
    4976    }
    5077
    5178    [StorableConstructor]
    5279    protected Encoding(bool deserializing) : base(deserializing) { }
    53     protected Encoding(Encoding original, Cloner cloner)
    54       : base(original, cloner) { }
     80
     81    protected Encoding(Encoding<T> original, Cloner cloner)
     82      : base(original, cloner) {
     83      encodingOperators = original.Operators.Select(cloner.Clone).ToList();
     84      solutionCreator = cloner.Clone(original.solutionCreator);
     85    }
    5586    protected Encoding(string name) : base(name) { }
     87
     88
     89
     90    public void ConfigureOperator(IOperator @operator) {
     91      ConfigureOperators(new[] { @operator });
     92    }
     93    public virtual void ConfigureOperators(IEnumerable<IOperator> operators) {
     94
     95    }
     96
     97    protected virtual void OnSolutionCreatorChanged() {
     98      ConfigureOperator(SolutionCreator);
     99    }
     100
    56101
    57102    public event EventHandler ParameterConfigurationChanged;
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs

    r11484 r11559  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
     28using HeuristicLab.Encodings.IntegerVectorEncoding;
    2829using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2930
     
    3132  [Item("IntegerEncoding", "Describes an integer vector encoding.")]
    3233  [StorableClass]
    33   public class IntegerEncoding : Encoding {
     34  public class IntegerEncoding : Encoding<IIntegerVectorCreator> {
    3435    [Storable]
    3536    private IntValue length;
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs

    r11484 r11559  
    3030  [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
    3131  [StorableClass]
    32   public class MultiEncoding : Encoding {
     32  public class MultiEncoding : Encoding<MultiEncodingCreator> {
    3333    [Storable]
    34     public KeyedItemCollection<string, Encoding> Encodings { get; protected set; }
     34    public KeyedItemCollection<string, IEncoding> Encodings { get; protected set; }
    3535
    3636    [StorableConstructor]
     
    4242    public MultiEncoding()
    4343      : base("MultiEncoding") {
    44       Encodings = new NamedItemCollection<Encoding>();
     44      Encodings = new NamedItemCollection<IEncoding>();
    4545    }
    4646
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs

    r11484 r11559  
    2929  [Item("PermutationEncoding", "Describes a permutation encoding.")]
    3030  [StorableClass]
    31   public class PermutationEncoding : Encoding {
     31  public class PermutationEncoding : Encoding<IPermutationCreator> {
    3232    [Storable]
    3333    private IntValue length;
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs

    r11553 r11559  
    2121
    2222using System;
    23 using System.Collections;
    2423using System.Collections.Generic;
    2524using System.Linq;
    26 using System.Runtime.CompilerServices;
    27 using System.Text;
    2825using HeuristicLab.Common;
    2926using HeuristicLab.Core;
     
    3835  [Item("RealEncoding", "Describes a real vector encoding.")]
    3936  [StorableClass]
    40   public sealed class RealEncoding : Encoding {
     37  public sealed class RealEncoding : Encoding<IRealVectorCreator> {
    4138    #region Encoding Parameters
    4239    [Storable]
     
    6966    #endregion
    7067
    71     private List<IOperator> encodingOperators = new List<IOperator>();
    72     [Storable]
    73     public override IEnumerable<IOperator> Operators {
    74       get { return encodingOperators; }
    75       protected set { encodingOperators = new List<IOperator>(value); }
    76     }
    77 
    78     public override ISolutionCreator DefaultSolutionCreator {
    79       get { return Operators.OfType<UniformRandomRealVectorCreator>().First(); }
    80     }
    81 
    8268    public int Length {
    8369      get { return LengthParameter.Value.Value; }
     
    8874      set { BoundsParameter.Value = value; }
    8975    }
    90 
    91 
    9276
    9377    [StorableConstructor]
     
    10488      lengthParameter = cloner.Clone(original.lengthParameter);
    10589      boundsParameter = cloner.Clone(original.boundsParameter);
    106       encodingOperators = original.Operators.Select(cloner.Clone).ToList();
    10790      RegisterParameterEvents();
    10891    }
    109 
    110 
    11192
    11293    public RealEncoding(string name, int length, double min, double max)
     
    149130      ConfigureOperators(Operators);
    150131    }
     132
    151133    private void RegisterParameterEvents() {
    152134      RegisterLengthParameterEvents();
     
    166148    static RealEncoding() {
    167149      encodingSpecificOperatorTypes = new List<Type>() {
    168         typeof (IRealVectorOperator),
    169         typeof (IRealVectorCreator),
    170         typeof (IRealVectorCrossover),
    171         typeof (IRealVectorManipulator),
    172         typeof (IRealVectorStdDevStrategyParameterOperator),
    173         typeof (IRealVectorSwarmUpdater),
    174         typeof (IRealVectorParticleCreator),
    175         typeof (IRealVectorParticleUpdater),
    176         typeof (IRealVectorMultiNeighborhoodShakingOperator),
    177         typeof (IRealVectorBoundsChecker),
    178         typeof (IRealVectorMoveOperator),
    179         typeof (IRealVectorMoveGenerator)
    180       };
     150  typeof (IRealVectorOperator),
     151  typeof (IRealVectorCreator),
     152  typeof (IRealVectorCrossover),
     153  typeof (IRealVectorManipulator),
     154  typeof (IRealVectorStdDevStrategyParameterOperator),
     155  typeof (IRealVectorSwarmUpdater),
     156  typeof (IRealVectorParticleCreator),
     157  typeof (IRealVectorParticleUpdater),
     158  typeof (IRealVectorMultiNeighborhoodShakingOperator),
     159  typeof (IRealVectorBoundsChecker),
     160  typeof (IRealVectorMoveOperator),
     161  typeof (IRealVectorMoveGenerator)
     162  };
    181163    }
    182164    private void DiscoverOperators() {
    183       var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IRealVectorOperator));
    184       var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false);
     165      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
    185166      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
    186167      var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();
     
    188169      ConfigureOperators(newOperators);
    189170      encodingOperators.AddRange(newOperators);
     171
     172      foreach (var strategyVectorCreator in Operators.OfType<IRealVectorStdDevStrategyParameterCreator>())
     173        strategyVectorCreator.BoundsParameter.ValueChanged += strategyVectorCreator_BoundsParameter_ValueChanged;
    190174    }
    191175    #endregion
    192176
    193     public void ConfigureOperators(IEnumerable<IOperator> operators) {
     177
     178    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
     179      var boundsParameter = (IValueLookupParameter<DoubleMatrix>)sender;
     180      if (boundsParameter.Value == null) return;
     181      foreach (var strategyVectorManipulator in Operators.OfType<IRealVectorStdDevStrategyParameterManipulator>())
     182        strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)boundsParameter.Value.Clone();
     183    }
     184
     185    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
    194186      ConfigureCreators(operators.OfType<IRealVectorCreator>());
    195187      ConfigureCrossovers(operators.OfType<IRealVectorCrossover>());
     
    201193      ConfigureShakingOperators(operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>());
    202194      ConfigureBoundsCheckers(operators.OfType<IRealVectorBoundsChecker>());
     195      ConfigureMoveGenerators(operators.OfType<IRealVectorMoveGenerator>());
    203196      ConfigureMoveOperators(operators.OfType<IRealVectorMoveOperator>());
    204       ConfigureMoveGenerators(operators.OfType<IRealVectorMoveGenerator>());
     197      ConfigureAdditiveMoveOperator(operators.OfType<IAdditiveRealVectorMoveOperator>());
    205198    }
    206199
     
    236229    }
    237230    private void ConfigureStdDevStrategyParameterOperators(IEnumerable<IRealVectorStdDevStrategyParameterOperator> strategyOperators) {
    238       var bounds = (DoubleMatrix)Bounds.Clone();
    239       for (var i = 0; i < bounds.Rows; i++) {
    240         bounds[i, 1] = 0.1 * (bounds[i, 1] - bounds[i, 0]);
     231      var bounds = new DoubleMatrix(Bounds.Rows, Bounds.Columns);
     232      for (var i = 0; i < Bounds.Rows; i++) {
    241233        bounds[i, 0] = 0;
     234        bounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
    242235      }
    243236      foreach (var s in strategyOperators) {
     
    303296        moveGenerator.BoundsParameter.ActualName = BoundsParameter.Name;
    304297    }
     298
     299    private void ConfigureAdditiveMoveOperator(IEnumerable<IAdditiveRealVectorMoveOperator> additiveMoveOperators) {
     300      foreach (var additiveMoveOperator in additiveMoveOperators) {
     301        additiveMoveOperator.AdditiveMoveParameter.ActualName = Name + "_AdditiveMove";
     302      }
     303    }
    305304    #endregion
    306305  }
Note: See TracChangeset for help on using the changeset viewer.