Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/14/14 14:46:00 (10 years ago)
Author:
abeham
Message:

#2174: Major refactoring:

  • ParameterVector is only a temporary datastructure that is constructed for evaluation purposes only
  • Multiple permutations are allowed
  • Special support for single-vector encodings (to apply specialized algorithms such as PSO or CMA-ES)
  • General support for multi-vector encoding
Location:
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorCreater.cs

    r10753 r10850  
    2020#endregion
    2121
    22 using System;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
    25 using HeuristicLab.Data;
    26 using HeuristicLab.Encodings.BinaryVectorEncoding;
    27 using HeuristicLab.Encodings.IntegerVectorEncoding;
    28 using HeuristicLab.Encodings.ParameterVectorEncoding;
    29 using HeuristicLab.Encodings.PermutationEncoding;
    30 using HeuristicLab.Encodings.RealVectorEncoding;
    3124using HeuristicLab.Operators;
    3225using HeuristicLab.Optimization;
    33 using HeuristicLab.Parameters;
    3426using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3527
    3628namespace HeuristicLab.Problems.Programmable {
    37   [Item("ParameterVectorCreater", "Creates a parameter vector as received from a SimOptRunner.")]
     29  [Item("ParameterVectorCreater", "Contains solution creators that together create a multi-vector encoding.")]
    3830  [StorableClass]
    39   public class ParameterVectorCreater : SingleSuccessorOperator, IParameterVectorCreator, IStochasticOperator {
    40 
    41     public ILookupParameter<IRandom> RandomParameter {
    42       get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    43     }
    44 
    45     public ILookupParameter<ProgrammableProblemScript> ScriptParameter {
    46       get { return (ILookupParameter<ProgrammableProblemScript>)Parameters["Script"]; }
    47     }
    48 
    49     public ILookupParameter<ParameterVector> ParameterVectorParameter {
    50       get { return (ILookupParameter<ParameterVector>)Parameters["ParameterVector"]; }
    51     }
    52 
    53     #region Interface Implementations of Encoding-specific Creators
    54     ILookupParameter<BinaryVector> IBinaryVectorCreator.BinaryVectorParameter {
    55       get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
    56     }
    57     IValueLookupParameter<IntValue> IBinaryVectorCreator.LengthParameter {
    58       get { return (IValueLookupParameter<IntValue>)Parameters["BinaryVectorLength"]; }
    59     }
    60 
    61     ILookupParameter<IntegerVector> IIntegerVectorCreator.IntegerVectorParameter {
    62       get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
    63     }
    64 
    65     IValueLookupParameter<IntMatrix> IBoundedIntegerVectorOperator.BoundsParameter {
    66       get { return (IValueLookupParameter<IntMatrix>)Parameters["IntegerVectorBounds"]; }
    67     }
    68 
    69     IValueLookupParameter<IntValue> IIntegerVectorCreator.LengthParameter {
    70       get { return (IValueLookupParameter<IntValue>)Parameters["IntegerVectorLength"]; }
    71     }
    72 
    73     ILookupParameter<RealVector> IRealVectorCreator.RealVectorParameter {
    74       get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
    75     }
    76 
    77     IValueLookupParameter<DoubleMatrix> IRealVectorCreator.BoundsParameter {
    78       get { return (IValueLookupParameter<DoubleMatrix>)Parameters["RealVectorBounds"]; }
    79     }
    80 
    81     IValueLookupParameter<IntValue> IRealVectorCreator.LengthParameter {
    82       get { return (IValueLookupParameter<IntValue>)Parameters["RealVectorLength"]; }
    83     }
    84 
    85     ILookupParameter<Permutation> IPermutationCreator.PermutationParameter {
    86       get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
    87     }
    88 
    89     IValueParameter<PermutationType> IPermutationCreator.PermutationTypeParameter {
    90       get { return (IValueParameter<PermutationType>)Parameters["PermutationType"]; }
    91     }
    92 
    93     IValueLookupParameter<IntValue> IPermutationCreator.LengthParameter {
    94       get { return (IValueLookupParameter<IntValue>)Parameters["PermutationLength"]; }
    95     }
    96     #endregion
     31  public class ParameterVectorCreater : InstrumentedOperator, ISolutionCreator {
    9732
    9833    [StorableConstructor]
    9934    protected ParameterVectorCreater(bool deserializing) : base(deserializing) { }
    10035    protected ParameterVectorCreater(ParameterVectorCreater original, Cloner cloner) : base(original, cloner) { }
    101     public ParameterVectorCreater() {
    102       Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    103       Parameters.Add(new LookupParameter<ProgrammableProblemScript>("Script", "The script that will execute the evaluation function and define the parameter vector."));
    104       Parameters.Add(new LookupParameter<ParameterVector>("ParameterVector", "The vector that holds the parameters."));
    105       Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The binary vector that links into the parameter vector."));
    106       Parameters.Add(new ValueLookupParameter<IntValue>("BinaryVectorLength", "The length of the binary vector."));
    107       Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The integer vector that links into the parameter vector."));
    108       Parameters.Add(new ValueLookupParameter<IntValue>("IntegerVectorLength", "The length of the integer vector."));
    109       Parameters.Add(new ValueLookupParameter<IntMatrix>("IntegerVectorBounds", "The bounds of the integer vector."));
    110       Parameters.Add(new LookupParameter<RealVector>("RealVector", "The real vector that links into the parameter vector."));
    111       Parameters.Add(new ValueLookupParameter<IntValue>("RealVectorLength", "The length of the real vector."));
    112       Parameters.Add(new ValueLookupParameter<DoubleMatrix>("RealVectorBounds", "The bounds of the real vector."));
    113       Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation that links into the parameter vector."));
    114       Parameters.Add(new ValueParameter<PermutationType>("PermutationType", "The type of the permutation."));
    115       Parameters.Add(new ValueLookupParameter<IntValue>("PermutationLength", "The length of the permutation."));
    116     }
     36    public ParameterVectorCreater() { }
    11737
    11838    public override IDeepCloneable Clone(Cloner cloner) {
    11939      return new ParameterVectorCreater(this, cloner);
    12040    }
    121 
    122     public override IOperation Apply() {
    123       var random = RandomParameter.ActualValue;
    124       var runner = ScriptParameter.ActualValue;
    125       if (runner.Instance == null) throw new InvalidOperationException("Script instance is null, maybe the code doesn't compile.");
    126 
    127       var vector = runner.Instance.GetParametersToOptimize();
    128       ParameterVectorParameter.ActualValue = vector;
    129 
    130       if (vector.BooleanParameters != null) {
    131         vector.BooleanParameters.Randomize(random);
    132         ((IBinaryVectorCreator)this).BinaryVectorParameter.ActualValue = vector.BooleanParameters;
    133       }
    134       if (vector.IntegerParameters != null) {
    135         vector.IntegerParameters.Randomize(random, vector.IntegerBounds);
    136         ((IIntegerVectorCreator)this).IntegerVectorParameter.ActualValue = vector.IntegerParameters;
    137       }
    138       if (vector.RealParameters != null) {
    139         vector.RealParameters.Randomize(random, vector.RealBounds);
    140         ((IRealVectorCreator)this).RealVectorParameter.ActualValue = vector.RealParameters;
    141       }
    142       if (vector.PermutationParameter != null) {
    143         vector.PermutationParameter.Randomize(random);
    144         ((IPermutationCreator)this).PermutationParameter.ActualValue = vector.PermutationParameter;
    145       }
    146       return base.Apply();
    147     }
    14841  }
    14942}
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorCrossover.cs

    r10753 r10850  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Encodings.BinaryVectorEncoding;
    25 using HeuristicLab.Encodings.IntegerVectorEncoding;
    26 using HeuristicLab.Encodings.ParameterVectorEncoding;
    27 using HeuristicLab.Encodings.PermutationEncoding;
    28 using HeuristicLab.Encodings.RealVectorEncoding;
    2924using HeuristicLab.Operators;
    3025using HeuristicLab.Optimization;
    31 using HeuristicLab.Parameters;
    3226using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3327
    3428namespace HeuristicLab.Problems.Programmable {
    35   [Item("ParameterVector Crossover", "Applies different crossovers to cross a parameter vector.")]
     29  [Item("ParameterVector Crossover", "Applies different crossovers to cross a multi vector encoding.")]
    3630  [StorableClass]
    3731  public class ParameterVectorCrossover : InstrumentedOperator, ICrossover {
    38 
    39     public ILookupParameter<ParameterVector> ChildParameter {
    40       get { return (ILookupParameter<ParameterVector>)Parameters["Child"]; }
    41     }
    42     public IScopeTreeLookupParameter<ParameterVector> ParentsParameter {
    43       get { return (IScopeTreeLookupParameter<ParameterVector>)Parameters["Parents"]; }
    44     }
    45 
    46     public ILookupParameter<BinaryVector> BinaryVectorParameter {
    47       get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
    48     }
    49 
    50     public ILookupParameter<IntegerVector> IntegerVectorParameter {
    51       get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
    52     }
    53 
    54     public ILookupParameter<RealVector> RealVectorParameter {
    55       get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
    56     }
    57 
    58     public ILookupParameter<Permutation> PermutationParameter {
    59       get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
    60     }
    6132
    6233    [StorableConstructor]
     
    6536      : base(original, cloner) { }
    6637    public ParameterVectorCrossover() {
    67       Parameters.Add(new LookupParameter<ParameterVector>("Child", "The parameter vector of the child.", "ParameterVector"));
    68       Parameters.Add(new ScopeTreeLookupParameter<ParameterVector>("Parents", "The parameter vector of the parents.", "ParameterVector"));
    69       Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The binary vector that links into the parameter vector."));
    70       Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The integer vector that links into the parameter vector."));
    71       Parameters.Add(new LookupParameter<RealVector>("RealVector", "The real vector that links into the parameter vector."));
    72       Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation that links into the parameter vector."));
    7338    }
    7439
     
    7641      return new ParameterVectorCrossover(this, cloner);
    7742    }
    78 
    79     public override IOperation InstrumentedApply() {
    80       var child = (ParameterVector)ParentsParameter.ActualValue[0].Clone();
    81       child.BooleanParameters = BinaryVectorParameter.ActualValue;
    82       child.IntegerParameters = IntegerVectorParameter.ActualValue;
    83       child.RealParameters = RealVectorParameter.ActualValue;
    84       child.PermutationParameter = PermutationParameter.ActualValue;
    85       ChildParameter.ActualValue = child;
    86       return base.InstrumentedApply();
    87     }
    8843  }
    8944}
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorManipulator.cs

    r10753 r10850  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Encodings.BinaryVectorEncoding;
    25 using HeuristicLab.Encodings.IntegerVectorEncoding;
    26 using HeuristicLab.Encodings.PermutationEncoding;
    27 using HeuristicLab.Encodings.RealVectorEncoding;
    2824using HeuristicLab.Operators;
    2925using HeuristicLab.Optimization;
    30 using HeuristicLab.Parameters;
    3126using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3227
    3328namespace HeuristicLab.Problems.Programmable {
    34   [Item("ParameterVector Manipulator", "Applies different manipulators to change a parameter vector.")]
     29  [Item("ParameterVector Manipulator", "Applies different manipulators to change a multi vector encoding.")]
    3530  [StorableClass]
    3631  public class ParameterVectorManipulator : InstrumentedOperator, IManipulator {
    37     public ILookupParameter<BinaryVector> BinaryVectorParameter {
    38       get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
    39     }
    40 
    41     public ILookupParameter<IntegerVector> IntegerVectorParameter {
    42       get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
    43     }
    44 
    45     public ILookupParameter<RealVector> RealVectorParameter {
    46       get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
    47     }
    48 
    49     public ILookupParameter<Permutation> PermutationParameter {
    50       get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
    51     }
    5232
    5333    [StorableConstructor]
     
    5636      : base(original, cloner) { }
    5737    public ParameterVectorManipulator() {
    58       Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The binary vector that links into the parameter vector."));
    59       Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The integer vector that links into the parameter vector."));
    60       Parameters.Add(new LookupParameter<RealVector>("RealVector", "The real vector that links into the parameter vector."));
    61       Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation that links into the parameter vector."));
    6238    }
    6339
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveEvaluator.cs

    r10754 r10850  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    2526using HeuristicLab.Data;
    26 using HeuristicLab.Encodings.ParameterVectorEncoding;
     27using HeuristicLab.Encodings.BinaryVectorEncoding;
     28using HeuristicLab.Encodings.IntegerVectorEncoding;
     29using HeuristicLab.Encodings.PermutationEncoding;
     30using HeuristicLab.Encodings.RealVectorEncoding;
    2731using HeuristicLab.Operators;
    2832using HeuristicLab.Optimization;
     
    4347    }
    4448
    45     public ILookupParameter<ParameterVector> ParameterVectorParameter {
    46       get { return (ILookupParameter<ParameterVector>)Parameters["ParameterVector"]; }
     49    public ILookupParameter<Configuration> ConfigurationParameter {
     50      get { return (ILookupParameter<Configuration>)Parameters["Configuration"]; }
    4751    }
    4852
     
    5761      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    5862      Parameters.Add(new LookupParameter<SingleObjectiveScript>("Script", "The script that will execute the evaluation function and define the parameter vector."));
    59       Parameters.Add(new LookupParameter<ParameterVector>("ParameterVector", "The vector that holds the parameters."));
     63      Parameters.Add(new LookupParameter<Configuration>("Configuration", "An item that holds the problem's configuration."));
    6064      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
    6165    }
     
    6973      var runner = ScriptParameter.ActualValue;
    7074      if (runner.Instance == null) throw new InvalidOperationException("Script instance is null, maybe the code doesn't compile.");
    71       var vector = ParameterVectorParameter.ActualValue;
     75      var config = ConfigurationParameter.ActualValue;
     76      var binDict = new Dictionary<string, BinaryVector>();
     77      var intDict = new Dictionary<string, IntegerVector>();
     78      var realDict = new Dictionary<string, RealVector>();
     79      var permDict = new Dictionary<string, Permutation>();
     80      foreach (var param in config.Parameters) {
     81        var binConfig = param.Value as BinaryParameterConfiguration;
     82        if (binConfig != null) {
     83          binDict.Add(param.Key, (BinaryVector)ExecutionContext.Scope.Variables[param.Key].Value);
     84          continue;
     85        }
     86        var intConfig = param.Value as IntegerParameterConfiguration;
     87        if (intConfig != null) {
     88          intDict.Add(param.Key, (IntegerVector)ExecutionContext.Scope.Variables[param.Key].Value);
     89          continue;
     90        }
     91        var realConfig = param.Value as RealParameterConfiguration;
     92        if (realConfig != null) {
     93          realDict.Add(param.Key, (RealVector)ExecutionContext.Scope.Variables[param.Key].Value);
     94          continue;
     95        }
     96        var permConfig = param.Value as PermutationParameterConfiguration;
     97        if (permConfig != null) {
     98          permDict.Add(param.Key, (Permutation)ExecutionContext.Scope.Variables[param.Key].Value);
     99          continue;
     100        }
     101        throw new InvalidOperationException("Parameter " + param.Key + " not found.");
     102      }
     103      var vector = new ParameterVector(
     104        binaryVectors: binDict.Count > 0 ? binDict : null,
     105        integerVectors: intDict.Count > 0 ? intDict : null,
     106        realVectors: realDict.Count > 0 ? realDict : null,
     107        permutations: permDict.Count > 0 ? permDict : null);
    72108      QualityParameter.ActualValue = new DoubleValue(runner.Instance.Evaluate(random, vector));
    73109      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.