Changeset 10850


Ignore:
Timestamp:
05/14/14 14:46:00 (5 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
Files:
7 added
12 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/SimSharp/HeuristicLab.Problems.Programmable.Views/3.3/Plugin.cs.frame

    r10753 r10850  
    2525  [Plugin("HeuristicLab.Problems.Programmable.Views", "Views for programmable problem for defining custom representation and evaluation function.", "3.3.9.$WCREV$")]
    2626  [PluginFile("HeuristicLab.Problems.Programmable.Views-3.3.dll", PluginFileType.Assembly)]
     27  [PluginDependency("HeuristicLab.Common", "3.3")]
     28  [PluginDependency("HeuristicLab.Common.Resources", "3.3")]
     29  [PluginDependency("HeuristicLab.Core", "3.3")]
     30  [PluginDependency("HeuristicLab.Core.Views", "3.3")]
     31  [PluginDependency("HeuristicLab.Data.Views", "3.3")]
     32  [PluginDependency("HeuristicLab.MainForm", "3.3")]
     33  [PluginDependency("HeuristicLab.MainForm.WindowsForms", "3.3")]
     34  [PluginDependency("HeuristicLab.Optimization", "3.3")]
    2735  [PluginDependency("HeuristicLab.Problems.Programmable", "3.3")]
     36  [PluginDependency("HeuristicLab.Scripting", "3.3")]
     37  [PluginDependency("HeuristicLab.Scripting.Views", "3.3")]
    2838  public class HeuristicLabProblemsProgrammableViewsPlugin : PluginBase { }
    2939}
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Datastructures/ParameterVector.cs

    r10796 r10850  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    24 using HeuristicLab.Collections;
    25 using HeuristicLab.Common;
    26 using HeuristicLab.Core;
    27 using HeuristicLab.Data;
     23using System.Linq;
    2824using HeuristicLab.Encodings.BinaryVectorEncoding;
    2925using HeuristicLab.Encodings.IntegerVectorEncoding;
    3026using HeuristicLab.Encodings.PermutationEncoding;
    3127using HeuristicLab.Encodings.RealVectorEncoding;
    32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3328
    34 namespace HeuristicLab.Encodings.ParameterVectorEncoding {
    35   [Item("ParameterVector", "Encodes a range of parameters of different types.")]
    36   [StorableClass]
    37   public class ParameterVector : Item {
    38     private bool isFeasible;
    39     [Storable]
    40     public bool IsFeasible {
    41       get { return isFeasible; }
    42       set {
    43         if (isFeasible == value) return;
    44         isFeasible = value;
    45         OnIsFeasibleChanged();
    46       }
     29namespace HeuristicLab.Problems.Programmable {
     30  public class ParameterVector {
     31    protected Dictionary<string, BinaryVector> BinaryParameters;
     32    protected Dictionary<string, IntegerVector> IntegerParameters;
     33    protected Dictionary<string, RealVector> RealParameters;
     34    protected Dictionary<string, Permutation> PermutationParameters;
     35
     36    public ParameterVector(IEnumerable<KeyValuePair<string, BinaryVector>> binaryVectors = null,
     37      IEnumerable<KeyValuePair<string, IntegerVector>> integerVectors = null,
     38      IEnumerable<KeyValuePair<string, RealVector>> realVectors = null,
     39      IEnumerable<KeyValuePair<string, Permutation>> permutations = null) {
     40      if (binaryVectors != null) BinaryParameters = binaryVectors.ToDictionary(x => x.Key, x => x.Value);
     41      if (integerVectors != null) IntegerParameters = integerVectors.ToDictionary(x => x.Key, x => x.Value);
     42      if (realVectors != null) RealParameters = realVectors.ToDictionary(x => x.Key, x => x.Value);
     43      if (permutations != null) PermutationParameters = permutations.ToDictionary(x => x.Key, x => x.Value);
    4744    }
    4845
    49     [Storable]
    50     public BinaryVector BooleanParameters { get; set; }
    51     [Storable]
    52     public IntegerVector IntegerParameters { get; set; }
    53     [Storable]
    54     public RealVector RealParameters { get; set; }
    55     [Storable]
    56     public Permutation PermutationParameter { get; set; }
    57 
    58     [Storable]
    59     protected BidirectionalDictionary<string, int> BoolMap;
    60     [Storable]
    61     protected BidirectionalDictionary<string, int> IntMap;
    62     [Storable]
    63     protected BidirectionalDictionary<string, int> RealMap;
    64 
    65     [Storable]
    66     public IntMatrix IntegerBounds { get; set; }
    67     [Storable]
    68     public DoubleMatrix RealBounds { get; set; }
    69 
    70     [StorableConstructor]
    71     protected ParameterVector(bool deserializing) : base(deserializing) { }
    72     protected ParameterVector(ParameterVector original, Cloner cloner)
    73       : base(original, cloner) {
    74       IsFeasible = original.IsFeasible;
    75       if (original.BooleanParameters != null) {
    76         BooleanParameters = cloner.Clone(original.BooleanParameters);
    77         BoolMap = new BidirectionalDictionary<string, int>(original.BoolMap);
    78       }
    79       if (original.IntegerParameters != null) {
    80         IntegerParameters = cloner.Clone(original.IntegerParameters);
    81         IntMap = new BidirectionalDictionary<string, int>(original.IntMap);
    82         IntegerBounds = cloner.Clone(original.IntegerBounds);
    83       }
    84       if (original.RealParameters != null) {
    85         RealParameters = cloner.Clone(original.RealParameters);
    86         RealMap = new BidirectionalDictionary<string, int>(original.RealMap);
    87         RealBounds = cloner.Clone(original.RealBounds);
    88       }
    89       PermutationParameter = cloner.Clone(original.PermutationParameter);
    90     }
    91     public ParameterVector() {
    92 
     46    public BinaryVector Binary(string name) {
     47      return BinaryParameters[name];
    9348    }
    9449
    95     public override IDeepCloneable Clone(Cloner cloner) {
    96       return new ParameterVector(this, cloner);
     50    public bool Binary(string name, int index) {
     51      return BinaryParameters[name][index];
    9752    }
    9853
    99     public bool Boolean(string name) {
    100       return BooleanParameters[BoolMap.GetByFirst(name)];
     54    public IEnumerable<string> BinaryNames {
     55      get { return BinaryParameters.Keys; }
    10156    }
    10257
    103     public bool Boolean(int index) {
    104       return BooleanParameters[index];
     58    public IntegerVector Integer(string name) {
     59      return IntegerParameters[name];
    10560    }
    10661
    107     public IEnumerable<string> BooleanParameterNames {
    108       get {
    109         for (int i = 0; i < BooleanParameters.Length; i++)
    110           yield return BoolMap.GetBySecond(i);
    111       }
     62    public int Integer(string name, int index) {
     63      return IntegerParameters[name][index];
    11264    }
    11365
    114     public int Integer(string name) {
    115       return IntegerParameters[IntMap.GetByFirst(name)];
     66    public IEnumerable<string> IntegerNames {
     67      get { return IntegerParameters.Keys; }
    11668    }
    11769
    118     public int Integer(int index) {
    119       return IntegerParameters[index];
     70    public RealVector Real(string name) {
     71      return RealParameters[name];
    12072    }
    12173
    122     public IEnumerable<string> IntegerParameterNames {
    123       get {
    124         for (int i = 0; i < IntegerParameters.Length; i++)
    125           yield return IntMap.GetBySecond(i);
    126       }
     74    public double Real(string name, int index) {
     75      return RealParameters[name][index];
    12776    }
    12877
    129     public double Real(string name) {
    130       return RealParameters[RealMap.GetByFirst(name)];
     78    public IEnumerable<string> RealNames {
     79      get { return RealParameters.Keys; }
    13180    }
    13281
    133     public double Real(int index) {
    134       return RealParameters[index];
     82    public Permutation Permutation(string name) {
     83      return PermutationParameters[name];
    13584    }
    13685
    137     public IEnumerable<string> RealParameterNames {
    138       get {
    139         for (int i = 0; i < RealParameters.Length; i++)
    140           yield return RealMap.GetBySecond(i);
    141       }
     86    public IEnumerable<string> PermutationNames {
     87      get { return PermutationParameters.Keys; }
    14288    }
    143 
    144     public Permutation Permutation() {
    145       return PermutationParameter;
    146     }
    147 
    148     public event EventHandler IsFeasibleChanged;
    149     protected virtual void OnIsFeasibleChanged() {
    150       var handler = IsFeasibleChanged;
    151       if (handler != null) handler(this, EventArgs.Empty);
    152     }
    153 
    154     #region Internal Methods for the Builder
    155     internal void SetBooleanParameters(HashSet<string> bools = null) {
    156       if (bools == null) {
    157         BooleanParameters = null;
    158         BoolMap = null;
    159       } else {
    160         BooleanParameters = new BinaryVector(bools.Count);
    161         BoolMap = new BidirectionalDictionary<string, int>();
    162         foreach (var p in bools) {
    163           BoolMap.Add(p, BoolMap.Count);
    164         }
    165       }
    166     }
    167 
    168     internal void SetIntegerParameters(Dictionary<string, Tuple<int, int, int?>> ints = null) {
    169       if (ints == null) {
    170         IntegerParameters = null;
    171         IntMap = null;
    172         IntegerBounds = null;
    173       } else {
    174         IntegerParameters = new IntegerVector(ints.Count);
    175         IntMap = new BidirectionalDictionary<string, int>();
    176         IntegerBounds = new IntMatrix(ints.Count, 3);
    177         var i = 0;
    178         foreach (var p in ints) {
    179           IntegerBounds[i, 0] = p.Value.Item1;
    180           IntegerBounds[i, 1] = p.Value.Item2;
    181           IntegerBounds[i, 2] = p.Value.Item3 ?? 1;
    182           IntegerParameters[i] = p.Value.Item1; // default to min
    183           IntMap.Add(p.Key, i++);
    184         }
    185       }
    186     }
    187 
    188     internal void SetRealParameters(Dictionary<string, Tuple<double, double>> reals = null) {
    189       if (reals == null) {
    190         RealParameters = null;
    191         RealMap = null;
    192         RealBounds = null;
    193       } else {
    194         RealParameters = new RealVector(reals.Count);
    195         RealMap = new BidirectionalDictionary<string, int>();
    196         RealBounds = new DoubleMatrix(reals.Count, 2);
    197         var i = 0;
    198         foreach (var p in reals) {
    199           RealBounds[i, 0] = p.Value.Item1;
    200           RealBounds[i, 1] = p.Value.Item2;
    201           RealParameters[i] = p.Value.Item1; // default to min
    202           RealMap.Add(p.Key, i++);
    203         }
    204       }
    205     }
    206 
    207     internal void SetPermutationParameters(Tuple<PermutationTypes, int> perms = null) {
    208       if (perms == null) PermutationParameter = null;
    209       else PermutationParameter = new Permutation(perms.Item1, perms.Item2);
    210     }
    211     #endregion
    21289  }
    21390}
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj

    r10754 r10850  
    136136  </ItemGroup>
    137137  <ItemGroup>
     138    <Compile Include="Datastructures\PermutationParameterConfiguration.cs" />
     139    <Compile Include="Datastructures\RealParameterConfiguration.cs" />
     140    <Compile Include="Datastructures\IntegerParameterConfiguration.cs" />
     141    <Compile Include="Datastructures\BinaryParameterConfiguration.cs" />
     142    <Compile Include="Datastructures\Configuration.cs" />
     143    <Compile Include="Datastructures\ParameterConfiguration.cs" />
     144    <Compile Include="Datastructures\ParameterVector.cs" />
    138145    <Compile Include="Interfaces\ISingleObjectiveProgrammableProblemEvaluator.cs" />
    139146    <Compile Include="Operators\ParameterVectorManipulator.cs" />
     
    151158    <None Include="Properties\AssemblyInfo.cs.frame" />
    152159    <Compile Include="Properties\AssemblyInfo.cs" />
    153   </ItemGroup>
    154   <ItemGroup>
    155     <ProjectReference Include="..\..\HeuristicLab.Encodings.ParameterVector\HeuristicLab.Encodings.ParameterVectorEncoding-3.3.csproj">
    156       <Project>{ba2b2335-ef06-473f-b06d-210f356f8ebd}</Project>
    157       <Name>HeuristicLab.Encodings.ParameterVectorEncoding-3.3</Name>
    158     </ProjectReference>
    159160  </ItemGroup>
    160161  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • 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();
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Plugin.cs.frame

    r10753 r10850  
    2525  [Plugin("HeuristicLab.Problems.Programmable", "Programmable problem for defining custom representation and evaluation function.", "3.3.9.$WCREV$")]
    2626  [PluginFile("HeuristicLab.Problems.Programmable-3.3.dll", PluginFileType.Assembly)]
    27   [PluginDependency("HeuristicLab.Encodings.ParameterVectorEncoding", "3.3")]
     27  [PluginDependency("HeuristicLab.Analysis", "3.3")]
     28  [PluginDependency("HeuristicLab.Collections", "3.3")]
     29  [PluginDependency("HeuristicLab.Common", "3.3")]
     30  [PluginDependency("HeuristicLab.Common.Resources", "3.3")]
     31  [PluginDependency("HeuristicLab.Core", "3.3")]
     32  [PluginDependency("HeuristicLab.Data", "3.3")]
     33  [PluginDependency("HeuristicLab.Encodings.BinaryVectorEncoding", "3.3")]
     34  [PluginDependency("HeuristicLab.Encodings.IntegerVectorEncoding", "3.3")]
     35  [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")]
     36  [PluginDependency("HeuristicLab.Encodings.PermutationEncoding", "3.3")]
     37  [PluginDependency("HeuristicLab.Operators", "3.3")]
     38  [PluginDependency("HeuristicLab.Optimization", "3.3")]
     39  [PluginDependency("HeuristicLab.Parameters", "3.3")]
     40  [PluginDependency("HeuristicLab.Persistence", "3.3")]
     41  [PluginDependency("HeuristicLab.Problems.Instances", "3.3")]
     42  [PluginDependency("HeuristicLab.Scripting", "3.3")]
    2843  public class HeuristicLabProblemsProgrammablePlugin : PluginBase { }
    2944}
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/ProblemBase.cs

    r10754 r10850  
    2020#endregion
    2121
    22 using HeuristicLab.Encodings.ParameterVectorEncoding;
    23 
    2422namespace HeuristicLab.Problems.Programmable {
    2523  public abstract class ProblemBase {
    26     public abstract ParameterVector GetParametersToOptimize();
     24    public abstract Configuration GetConfiguration();
    2725  }
    2826}
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemBase.cs

    r10754 r10850  
    2121
    2222using HeuristicLab.Core;
    23 using HeuristicLab.Encodings.ParameterVectorEncoding;
    2423
    2524namespace HeuristicLab.Problems.Programmable {
    2625  public abstract class SingleObjectiveProblemBase : ProblemBase {
    27     public abstract bool IsMaximizationProblem { get; }
    2826    public abstract double Evaluate(IRandom random, ParameterVector vector);
    2927  }
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r10754 r10850  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Drawing;
    2425using System.Linq;
     
    2930using HeuristicLab.Encodings.BinaryVectorEncoding;
    3031using HeuristicLab.Encodings.IntegerVectorEncoding;
    31 using HeuristicLab.Encodings.ParameterVectorEncoding;
    3232using HeuristicLab.Encodings.PermutationEncoding;
    3333using HeuristicLab.Encodings.RealVectorEncoding;
     
    3535using HeuristicLab.Parameters;
    3636using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     37using HeuristicLab.PluginInfrastructure;
    3738
    3839namespace HeuristicLab.Problems.Programmable {
     
    4041  [Creatable("Problems")]
    4142  [StorableClass]
    42   public sealed class SingleObjectiveProgrammableProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveProgrammableProblemEvaluator, IParameterVectorCreator>, IStorableContent {
     43  public class SingleObjectiveProgrammableProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveProgrammableProblemEvaluator, ISolutionCreator>, IParameterizedNamedItem, IStorableContent {
    4344    public string Filename { get; set; }
    4445
     
    4748    }
    4849
     50    public new ParameterCollection Parameters {
     51      get { return base.Parameters; }
     52    }
     53    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
     54      get { return Parameters; }
     55    }
     56
    4957    public IValueParameter<SingleObjectiveScript> ScriptParameter {
    5058      get { return (IValueParameter<SingleObjectiveScript>)Parameters["Script"]; }
    5159    }
    5260
    53     private IValueParameter<IntValue> BinaryVectorLengthParameter {
    54       get { return (IValueParameter<IntValue>)Parameters["BinaryVectorLength"]; }
    55     }
    56 
    57     private IValueParameter<IntValue> IntegerVectorLengthParameter {
    58       get { return (IValueParameter<IntValue>)Parameters["IntegerVectorLength"]; }
    59     }
    60 
    61     private IValueParameter<IntMatrix> IntegerVectorBoundsParameter {
    62       get { return (IValueParameter<IntMatrix>)Parameters["IntegerVectorBounds"]; }
    63     }
    64 
    65     private IValueParameter<IntValue> RealVectorLengthParameter {
    66       get { return (IValueParameter<IntValue>)Parameters["RealVectorLength"]; }
    67     }
    68 
    69     private IValueParameter<DoubleMatrix> RealVectorBoundsParameter {
    70       get { return (IValueParameter<DoubleMatrix>)Parameters["RealVectorBounds"]; }
    71     }
    72 
    73     private IValueParameter<IntValue> PermutationLengthParameter {
    74       get { return (IValueParameter<IntValue>)Parameters["PermutationLength"]; }
    75     }
    76 
    77     private IValueParameter<PermutationType> PermutationTypeParameter {
    78       get { return (IValueParameter<PermutationType>)Parameters["PermutationType"]; }
     61    public IValueParameter<Configuration> ConfigurationParameter {
     62      get { return (IValueParameter<Configuration>)Parameters["Configuration"]; }
    7963    }
    8064
    8165    [Storable]
    82     private ParameterVectorCrossover Crossover { get; set; }
    83     [Storable]
    84     private ParameterVectorManipulator Manipulator { get; set; }
     66    protected List<IParameter> DynamicConfigurationParameters;
    8567
    8668    [StorableConstructor]
    87     private SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
    88 
    89     private SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
     69    protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
     70
     71    protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
    9072      : base(original, cloner) {
    91       Crossover = cloner.Clone(original.Crossover);
    92       Manipulator = cloner.Clone(original.Manipulator);
     73      DynamicConfigurationParameters = original.DynamicConfigurationParameters.Select(cloner.Clone).ToList();
    9374      RegisterEventHandlers();
    9475    }
     
    9677      : base(new SingleObjectiveEvaluator(), new ParameterVectorCreater()) {
    9778      Parameters.Add(new ValueParameter<SingleObjectiveScript>("Script", "Defines the problem.", new SingleObjectiveScript() { Name = ItemName }));
    98       Parameters.Add(new OptionalValueParameter<IntValue>("BinaryVectorLength", "The length of the binary vector."));
    99       Parameters.Add(new OptionalValueParameter<IntValue>("IntegerVectorLength", "The length of the integer vector."));
    100       Parameters.Add(new OptionalValueParameter<IntMatrix>("IntegerVectorBounds", "The bounds of the integer vector."));
    101       Parameters.Add(new OptionalValueParameter<IntValue>("RealVectorLength", "The length of the real vector."));
    102       Parameters.Add(new OptionalValueParameter<DoubleMatrix>("RealVectorBounds", "The bounds of the real vector."));
    103       Parameters.Add(new OptionalValueParameter<IntValue>("PermutationLength", "The length of the permutation."));
    104       Parameters.Add(new OptionalValueParameter<PermutationType>("PermutationType", "The type of the permutation."));
    105 
    106       Crossover = new ParameterVectorCrossover();
    107       Manipulator = new ParameterVectorManipulator();
     79      Parameters.Add(new ValueParameter<Configuration>("Configuration", "Describes which parameters exist, what they're called, what type they are and their bounds if any."));
     80
     81      DynamicConfigurationParameters = new List<IParameter>();
    10882
    10983      Operators.Add(new BestScopeSolutionAnalyzer());
    11084      Operators.Add(Evaluator);
    11185      Operators.Add(SolutionCreator);
    112       Operators.Add(Crossover);
    113       Operators.Add(Manipulator);
    11486
    11587      RegisterEventHandlers();
     
    12799    private void RegisterEventHandlers() {
    128100      ScriptParameter.ValueChanged += ScriptParameterOnValueChanged;
    129       Crossover.BeforeExecutionOperators.CollectionReset += CrossoverBeforeExecutionOperatorsOnChanged;
    130       Crossover.BeforeExecutionOperators.ItemsAdded += CrossoverBeforeExecutionOperatorsOnChanged;
    131       Crossover.BeforeExecutionOperators.ItemsReplaced += CrossoverBeforeExecutionOperatorsOnChanged;
    132       Manipulator.BeforeExecutionOperators.CollectionReset += ManipulatroBeforeExecutionOperatorsOnChanged;
    133       Manipulator.BeforeExecutionOperators.ItemsAdded += ManipulatroBeforeExecutionOperatorsOnChanged;
    134       Manipulator.BeforeExecutionOperators.ItemsReplaced += ManipulatroBeforeExecutionOperatorsOnChanged;
    135101      RegisterScriptInstanceChanges();
    136102    }
     
    138104    private void ScriptParameterOnValueChanged(object sender, EventArgs eventArgs) {
    139105      RegisterScriptInstanceChanges();
    140     }
    141 
    142     private void CrossoverBeforeExecutionOperatorsOnChanged(object sender, EventArgs e) {
    143       ParameterizeCrossover();
    144     }
    145 
    146     private void ManipulatroBeforeExecutionOperatorsOnChanged(object sender, EventArgs e) {
    147       ParameterizeManipulator();
    148106    }
    149107
     
    165123    }
    166124
    167     private void ScriptOnInstanceChanged(object sender, EventArgs eventArgs) {
     125    protected virtual void ScriptOnInstanceChanged(object sender, EventArgs eventArgs) {
    168126
    169127      var instance = ScriptParameter.Value.Instance;
    170128      if (instance == null) return;
    171129
    172       Maximization.Value = instance.IsMaximizationProblem;
    173       var vector = instance.GetParametersToOptimize();
    174       BinaryVectorLengthParameter.Value = vector.BooleanParameters != null ? new IntValue(vector.BooleanParameters.Length) : null;
    175       IntegerVectorLengthParameter.Value = vector.IntegerParameters != null ? new IntValue(vector.IntegerParameters.Length) : null;
    176       IntegerVectorBoundsParameter.Value = vector.IntegerBounds;
    177       RealVectorLengthParameter.Value = vector.RealParameters != null ? new IntValue(vector.RealParameters.Length) : null;
    178       RealVectorBoundsParameter.Value = vector.RealBounds;
    179       PermutationLengthParameter.Value = vector.PermutationParameter != null ? new IntValue(vector.PermutationParameter.Length) : null;
    180       PermutationTypeParameter.Value = vector.PermutationParameter != null ? new PermutationType(vector.PermutationParameter.PermutationType) : null;
    181 
    182       ((IBinaryVectorCreator)SolutionCreator).LengthParameter.ActualName = BinaryVectorLengthParameter.Name;
    183       ((IIntegerVectorCreator)SolutionCreator).LengthParameter.ActualName = IntegerVectorLengthParameter.Name;
    184       ((IIntegerVectorCreator)SolutionCreator).BoundsParameter.ActualName = IntegerVectorBoundsParameter.Name;
    185       ((IRealVectorCreator)SolutionCreator).LengthParameter.ActualName = RealVectorLengthParameter.Name;
    186       ((IRealVectorCreator)SolutionCreator).BoundsParameter.ActualName = RealVectorBoundsParameter.Name;
    187       ((IPermutationCreator)SolutionCreator).LengthParameter.ActualName = PermutationLengthParameter.Name;
    188       ((IPermutationCreator)SolutionCreator).PermutationTypeParameter.Value = new PermutationType(vector.PermutationParameter != null ? vector.PermutationParameter.PermutationType : PermutationTypes.Absolute);
    189 
    190       if (vector.BooleanParameters != null) {
    191         if (!Crossover.BeforeExecutionOperators.Any(x => x is IBinaryVectorCrossover))
    192           Crossover.BeforeExecutionOperators.Add(new Encodings.BinaryVectorEncoding.SinglePointCrossover());
    193         if (!Manipulator.BeforeExecutionOperators.Any(x => x is IBinaryVectorManipulator))
    194           Manipulator.BeforeExecutionOperators.Add(new Encodings.BinaryVectorEncoding.SinglePositionBitflipManipulator());
    195       } else {
    196         Crossover.BeforeExecutionOperators.RemoveAll(x => x is IBinaryVectorCrossover);
    197         Manipulator.BeforeExecutionOperators.RemoveAll(x => x is IBinaryVectorManipulator);
     130      var configuration = instance.GetConfiguration();
     131      ConfigurationParameter.Value = configuration;
     132      Maximization.Value = configuration.Maximization;
     133
     134      foreach (var param in DynamicConfigurationParameters)
     135        if (Parameters.Contains(param)) Parameters.Remove(param);
     136      DynamicConfigurationParameters.Clear();
     137
     138      var solutionCreators = new List<ISolutionCreator>();
     139      foreach (var parameters in configuration.Parameters) {
     140        var binConfig = parameters.Value as BinaryParameterConfiguration;
     141        if (binConfig != null) {
     142          var p = new ValueParameter<IntValue>(parameters.Key + "Length", binConfig.Length);
     143          DynamicConfigurationParameters.Add(p);
     144
     145          var creator = new RandomBinaryVectorCreator();
     146          creator.BinaryVectorParameter.ActualName = parameters.Key;
     147          creator.LengthParameter.ActualName = p.Name;
     148          solutionCreators.Add(creator);
     149        }
     150        var intConfig = parameters.Value as IntegerParameterConfiguration;
     151        if (intConfig != null) {
     152          var l = new ValueParameter<IntValue>(parameters.Key + "Length", intConfig.Length);
     153          var b = new ValueParameter<IntMatrix>(parameters.Key + "Bounds", intConfig.Bounds);
     154          DynamicConfigurationParameters.Add(l);
     155          DynamicConfigurationParameters.Add(b);
     156
     157          var creator = new UniformRandomIntegerVectorCreator();
     158          creator.IntegerVectorParameter.ActualName = parameters.Key;
     159          creator.LengthParameter.ActualName = l.Name;
     160          creator.BoundsParameter.ActualName = b.Name;
     161          solutionCreators.Add(creator);
     162        }
     163        var realConfig = parameters.Value as RealParameterConfiguration;
     164        if (realConfig != null) {
     165          var l = new ValueParameter<IntValue>(parameters.Key + "Length", realConfig.Length);
     166          var b = new ValueParameter<DoubleMatrix>(parameters.Key + "Bounds", realConfig.Bounds);
     167          DynamicConfigurationParameters.Add(l);
     168          DynamicConfigurationParameters.Add(b);
     169
     170          var creator = new UniformRandomRealVectorCreator();
     171          creator.RealVectorParameter.ActualName = parameters.Key;
     172          creator.LengthParameter.ActualName = l.Name;
     173          creator.BoundsParameter.ActualName = b.Name;
     174          solutionCreators.Add(creator);
     175        }
     176        var permConfig = parameters.Value as PermutationParameterConfiguration;
     177        if (permConfig != null) {
     178          var l = new ValueParameter<IntValue>(parameters.Key + "Length", permConfig.Length);
     179          DynamicConfigurationParameters.Add(l);
     180
     181          var creator = new RandomPermutationCreator();
     182          creator.PermutationParameter.ActualName = parameters.Key;
     183          creator.LengthParameter.ActualName = l.Name;
     184          creator.PermutationTypeParameter.Value = permConfig.Type;
     185          solutionCreators.Add(creator);
     186        }
    198187      }
    199188
    200       if (vector.IntegerParameters != null) {
    201         if (!Crossover.BeforeExecutionOperators.Any(x => x is IIntegerVectorCrossover))
    202           Crossover.BeforeExecutionOperators.Add(new Encodings.IntegerVectorEncoding.SinglePointCrossover());
    203         if (!Manipulator.BeforeExecutionOperators.Any(x => x is IIntegerVectorManipulator))
    204           Manipulator.BeforeExecutionOperators.Add(new Encodings.IntegerVectorEncoding.UniformOnePositionManipulator());
    205       } else {
    206         Crossover.BeforeExecutionOperators.RemoveAll(x => x is IIntegerVectorCrossover);
    207         Manipulator.BeforeExecutionOperators.RemoveAll(x => x is IIntegerVectorManipulator);
     189      foreach (var param in DynamicConfigurationParameters) {
     190        param.Hidden = true;
     191        Parameters.Add(param);
    208192      }
    209193
    210       if (vector.RealParameters != null) {
    211         if (!Crossover.BeforeExecutionOperators.Any(x => x is IRealVectorCrossover))
    212           Crossover.BeforeExecutionOperators.Add(new Encodings.RealVectorEncoding.SinglePointCrossover());
    213         if (!Manipulator.BeforeExecutionOperators.Any(x => x is IRealVectorManipulator))
    214           Manipulator.BeforeExecutionOperators.Add(new Encodings.RealVectorEncoding.BreederGeneticAlgorithmManipulator());
    215       } else {
    216         Crossover.BeforeExecutionOperators.RemoveAll(x => x is IRealVectorCrossover);
    217         Manipulator.BeforeExecutionOperators.RemoveAll(x => x is IRealVectorManipulator);
    218       }
    219 
    220       if (vector.PermutationParameter != null) {
    221         if (!Crossover.BeforeExecutionOperators.Any(x => x is IPermutationCrossover))
    222           Crossover.BeforeExecutionOperators.Add(new Encodings.PermutationEncoding.OrderCrossover2());
    223         if (!Manipulator.BeforeExecutionOperators.Any(x => x is IPermutationManipulator))
    224           Manipulator.BeforeExecutionOperators.Add(new Encodings.PermutationEncoding.Swap2Manipulator());
    225       } else {
    226         Crossover.BeforeExecutionOperators.RemoveAll(x => x is IPermutationCrossover);
    227         Manipulator.BeforeExecutionOperators.RemoveAll(x => x is IPermutationManipulator);
    228       }
    229 
    230       ParameterizeCrossover();
    231       ParameterizeManipulator();
    232     }
    233 
    234     private void ParameterizeCrossover() {
    235       Crossover.ChildParameter.ActualName = SolutionCreator.ParameterVectorParameter.ActualName;
    236       Crossover.ParentsParameter.ActualName = SolutionCreator.ParameterVectorParameter.ActualName;
    237 
    238       foreach (var xo in Crossover.BeforeExecutionOperators.OfType<IBinaryVectorCrossover>()) {
    239         xo.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    240         xo.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    241       }
    242       foreach (var xo in Crossover.BeforeExecutionOperators.OfType<IIntegerVectorCrossover>()) {
    243         xo.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
    244         xo.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
    245         var bxo = xo as IBoundedIntegerVectorOperator;
    246         if (bxo != null) bxo.BoundsParameter.ActualName = IntegerVectorBoundsParameter.Name;
    247       }
    248       foreach (var xo in Crossover.BeforeExecutionOperators.OfType<IRealVectorCrossover>()) {
    249         xo.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
    250         xo.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
    251         xo.BoundsParameter.ActualName = RealVectorBoundsParameter.Name;
    252       }
    253       foreach (var xo in Crossover.BeforeExecutionOperators.OfType<IPermutationCrossover>()) {
    254         xo.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    255         xo.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    256       }
    257     }
    258 
    259     private void ParameterizeManipulator() {
    260       foreach (var ma in Manipulator.BeforeExecutionOperators.OfType<IBinaryVectorManipulator>()) {
    261         ma.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    262       }
    263       foreach (var ma in Manipulator.BeforeExecutionOperators.OfType<IIntegerVectorManipulator>()) {
    264         ma.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
    265         var bma = ma as IBoundedIntegerVectorOperator;
    266         if (bma != null) bma.BoundsParameter.ActualName = IntegerVectorBoundsParameter.Name;
    267       }
    268       foreach (var ma in Manipulator.BeforeExecutionOperators.OfType<IRealVectorManipulator>()) {
    269         ma.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
    270         ma.BoundsParameter.ActualName = RealVectorBoundsParameter.Name;
    271       }
    272       foreach (var ma in Manipulator.BeforeExecutionOperators.OfType<IPermutationManipulator>()) {
    273         ma.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     194      // use specialized creators if the configuration consists only of a single parameter type
     195      // this allows to use specialized algorithms such as CMA-ES
     196      if (solutionCreators.Count == 1) { // single-vector encoding
     197        Operators.RemoveAll(x => x is ICrossover);
     198        Operators.RemoveAll(x => x is IManipulator);
     199        var newCreator = solutionCreators.Single();
     200        var binCreator = newCreator as IBinaryVectorCreator;
     201        if (binCreator != null) {
     202          // do not replace a binary vector creator that was manually set
     203          if (!(SolutionCreator is IBinaryVectorCreator)
     204              || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != binCreator.BinaryVectorParameter.ActualName) {
     205            Operators.Remove(SolutionCreator);
     206            SolutionCreator = newCreator;
     207            Operators.Add(SolutionCreator);
     208          }
     209          var crossovers = ApplicationManager.Manager.GetInstances<IBinaryVectorCrossover>().ToList();
     210          var manipulators = ApplicationManager.Manager.GetInstances<IBinaryVectorManipulator>().ToList();
     211          foreach (var xo in crossovers) {
     212            xo.ChildParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
     213            xo.ParentsParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
     214          }
     215          foreach (var m in manipulators)
     216            m.BinaryVectorParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
     217          Operators.AddRange(crossovers);
     218          Operators.AddRange(manipulators);
     219        }
     220        var intCreator = newCreator as IIntegerVectorCreator;
     221        if (intCreator != null) {
     222          // do not replace an integer vector creator that was manually set
     223          if (!(SolutionCreator is IIntegerVectorCreator)
     224              || ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != intCreator.IntegerVectorParameter.ActualName) {
     225            Operators.Remove(SolutionCreator);
     226            SolutionCreator = newCreator;
     227            Operators.Add(SolutionCreator);
     228          }
     229          var crossovers = ApplicationManager.Manager.GetInstances<IIntegerVectorCrossover>().ToList();
     230          var manipulators = ApplicationManager.Manager.GetInstances<IIntegerVectorManipulator>().ToList();
     231          foreach (var xo in crossovers) {
     232            xo.ChildParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
     233            xo.ParentsParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
     234          }
     235          foreach (var m in manipulators)
     236            m.IntegerVectorParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
     237          foreach (var bo in crossovers.OfType<IBoundedIntegerVectorOperator>().Concat(manipulators.OfType<IBoundedIntegerVectorOperator>()).ToList()) {
     238            bo.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName;
     239          }
     240          Operators.AddRange(crossovers);
     241          Operators.AddRange(manipulators);
     242        }
     243        var realCreator = newCreator as IRealVectorCreator;
     244        if (realCreator != null) {
     245          // do not replace a real vector creator that was manually set
     246          if (!(SolutionCreator is IRealVectorCreator)
     247              || ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realCreator.RealVectorParameter.ActualName) {
     248            Operators.Remove(SolutionCreator);
     249            SolutionCreator = newCreator;
     250            Operators.Add(SolutionCreator);
     251          }
     252          var crossovers = ApplicationManager.Manager.GetInstances<IRealVectorCrossover>().ToList();
     253          var manipulators = ApplicationManager.Manager.GetInstances<IRealVectorManipulator>().ToList();
     254          foreach (var xo in crossovers) {
     255            xo.ChildParameter.ActualName = realCreator.RealVectorParameter.ActualName;
     256            xo.ParentsParameter.ActualName = realCreator.RealVectorParameter.ActualName;
     257            xo.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
     258          }
     259          foreach (var m in manipulators) {
     260            m.RealVectorParameter.ActualName = realCreator.RealVectorParameter.ActualName;
     261            m.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
     262          }
     263          Operators.AddRange(crossovers);
     264          Operators.AddRange(manipulators);
     265        }
     266        var permCreator = newCreator as IPermutationCreator;
     267        if (permCreator != null) {
     268          // do not replace a permutation creator that was manually set
     269          if (!(SolutionCreator is IPermutationCreator)
     270              || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permCreator.PermutationParameter.ActualName) {
     271            Operators.Remove(SolutionCreator);
     272            SolutionCreator = newCreator;
     273            Operators.Add(SolutionCreator);
     274          }
     275          var crossovers = ApplicationManager.Manager.GetInstances<IPermutationCrossover>().ToList();
     276          var manipulators = ApplicationManager.Manager.GetInstances<IPermutationManipulator>().ToList();
     277          foreach (var xo in crossovers) {
     278            xo.ChildParameter.ActualName = permCreator.PermutationParameter.ActualName;
     279            xo.ParentsParameter.ActualName = permCreator.PermutationParameter.ActualName;
     280          }
     281          foreach (var m in manipulators)
     282            m.PermutationParameter.ActualName = permCreator.PermutationParameter.ActualName;
     283          Operators.AddRange(crossovers);
     284          Operators.AddRange(manipulators);
     285        }
     286      } else { // multi-vector encoding
     287        var oldCreator = SolutionCreator as ParameterVectorCreater;
     288        var newCreator = new ParameterVectorCreater();
     289        var newBinParams = new HashSet<string>(solutionCreators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName));
     290        if (oldCreator != null) { // we want to reuse the old creator
     291          var oldParams = new HashSet<string>(oldCreator
     292            .BeforeExecutionOperators.OfType<IBinaryVectorCreator>()
     293            .Select(x => x.BinaryVectorParameter.ActualName));
     294          foreach (var toAdd in newBinParams.Except(oldParams)) {
     295            var paramName = toAdd;
     296            oldCreator.BeforeExecutionOperators.Add(solutionCreators.OfType<IBinaryVectorCreator>().Single(x => x.BinaryVectorParameter.ActualName == paramName));
     297          }
     298          foreach (var toRemove in oldParams.Except(newBinParams)) {
     299            var paramName = toRemove;
     300            oldCreator.BeforeExecutionOperators.RemoveAll(
     301              op => op is IBinaryVectorCreator && ((IBinaryVectorCreator)op).BinaryVectorParameter.ActualName == paramName);
     302          }
     303        } else { // we will use the new creator
     304          foreach (var binCreator in solutionCreators.OfType<IBinaryVectorCreator>()) {
     305            newCreator.BeforeExecutionOperators.Add(binCreator);
     306          }
     307        }
     308
     309        var newIntParams = new HashSet<string>(solutionCreators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName));
     310        if (oldCreator != null) { // we want to reuse the old creator
     311          var oldParams = new HashSet<string>(oldCreator
     312            .BeforeExecutionOperators.OfType<IIntegerVectorCreator>()
     313            .Select(x => x.IntegerVectorParameter.ActualName));
     314          foreach (var toAdd in newIntParams.Except(oldParams)) {
     315            var paramName = toAdd;
     316            oldCreator.BeforeExecutionOperators.Add(solutionCreators.OfType<IIntegerVectorCreator>().Single(x => x.IntegerVectorParameter.ActualName == paramName));
     317          }
     318          foreach (var toRemove in oldParams.Except(newIntParams)) {
     319            var paramName = toRemove;
     320            oldCreator.BeforeExecutionOperators.RemoveAll(
     321              op => op is IIntegerVectorCreator && ((IIntegerVectorCreator)op).IntegerVectorParameter.ActualName == paramName);
     322          }
     323        } else { // we will use the new creator
     324          foreach (var intCreator in solutionCreators.OfType<IIntegerVectorCreator>()) {
     325            newCreator.BeforeExecutionOperators.Add(intCreator);
     326          }
     327        }
     328
     329        var newRealParams = new HashSet<string>(solutionCreators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName));
     330        if (oldCreator != null) { // we want to reuse the old creator
     331          var oldParams = new HashSet<string>(oldCreator
     332            .BeforeExecutionOperators.OfType<IRealVectorCreator>()
     333            .Select(x => x.RealVectorParameter.ActualName));
     334          foreach (var toAdd in newRealParams.Except(oldParams)) {
     335            var paramName = toAdd;
     336            oldCreator.BeforeExecutionOperators.Add(solutionCreators.OfType<IRealVectorCreator>().Single(x => x.RealVectorParameter.ActualName == paramName));
     337          }
     338          foreach (var toRemove in oldParams.Except(newRealParams)) {
     339            var paramName = toRemove;
     340            oldCreator.BeforeExecutionOperators.RemoveAll(
     341              op => op is IRealVectorCreator && ((IRealVectorCreator)op).RealVectorParameter.ActualName == paramName);
     342          }
     343        } else { // we will use the new creator
     344          foreach (var realCreator in solutionCreators.OfType<IRealVectorCreator>()) {
     345            newCreator.BeforeExecutionOperators.Add(realCreator);
     346          }
     347        }
     348
     349        var newPermParams = new HashSet<string>(solutionCreators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName));
     350        if (oldCreator != null) { // we want to reuse the old creator
     351          var oldParams = new HashSet<string>(oldCreator
     352            .BeforeExecutionOperators.OfType<IPermutationCreator>()
     353            .Select(x => x.PermutationParameter.ActualName));
     354          foreach (var toAdd in newPermParams.Except(oldParams)) {
     355            var paramName = toAdd;
     356            oldCreator.BeforeExecutionOperators.Add(solutionCreators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == paramName));
     357          }
     358          foreach (var toRemove in oldParams.Except(newPermParams)) {
     359            var paramName = toRemove;
     360            oldCreator.BeforeExecutionOperators.RemoveAll(
     361              op => op is IPermutationCreator && ((IPermutationCreator)op).PermutationParameter.ActualName == paramName);
     362          }
     363          // we also have to sync the permutation type (in case this changes, as it is a value parameter)
     364          foreach (var intersect in newPermParams.Intersect(oldParams)) {
     365            var paramName = intersect;
     366            var oldPermCreator = oldCreator.BeforeExecutionOperators.OfType<IPermutationCreator>()
     367              .Single(x => x.PermutationParameter.ActualName == paramName);
     368            var newPermCreator = solutionCreators.OfType<IPermutationCreator>()
     369                .Single(x => x.PermutationParameter.ActualName == paramName);
     370            oldPermCreator.PermutationTypeParameter.Value = newPermCreator.PermutationTypeParameter.Value;
     371          }
     372        } else { // we will use the new creator
     373          foreach (var permCreator in solutionCreators.OfType<IPermutationCreator>()) {
     374            newCreator.BeforeExecutionOperators.Add(permCreator);
     375          }
     376        }
    274377      }
    275378    }
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveScript.cs

    r10754 r10850  
    5353using HeuristicLab.Core;
    5454using HeuristicLab.Data;
    55 using HeuristicLab.Encodings.ParameterVectorEncoding;
    5655using HeuristicLab.Encodings.PermutationEncoding;
     56using HeuristicLab.Problems.Programmable;
    5757
    5858public class MyProblem : HeuristicLab.Problems.Programmable.SingleObjectiveProblemBase {
     
    6060    // initialize private fields
    6161  }
    62   public override bool IsMaximizationProblem { get { return false; } }
    63   public override ParameterVector GetParametersToOptimize() {
    64     return new ParameterVectorBuilder()
    65       // .AddBoolean(""Parameter"")
    66       // .AddInteger(""Parameter"", min: 5, max: 15, step: 3)
    67       // .AddReal(""Parameter"", min: 0.0, max: 1.0)
    68       // .SetPermutation(type: PermutationTypes.Absolute, length: 10)
    69     .Build();
     62
     63  public override Configuration GetConfiguration() {
     64    return new Configuration()
     65      // .SetMaximization(true)
     66      // add an arbitrary number of uniquely named parameter vectors
     67      // .AddBinary(""p"", length: 5)
     68      // .AddInteger(""i"", length: 5, min: 2, max: 14, step: 4)
     69      // .AddReal(""r"", length: 5, min: -1.0, max: 1.0)
     70      // .AddPermutation(""P"", type: PermutationTypes.Absolute, length: 5)
     71    ;
    7072  }
    7173
    7274  public override double Evaluate(IRandom random, ParameterVector vector) {
    7375    var quality = 0.0;
    74     // quality = vector.Boolean(""Parameter"") ? vector.Real(""Parameter"") : vector.Integer(""Parameter"");
     76    // quality = vector.Real(""r"").Select(x => x * x).Sum();
    7577    return quality;
    7678  }
  • branches/SimSharp/ProgrammableProblem.sln

    r10753 r10850  
    77EndProject
    88Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Problems.Programmable-3.3", "HeuristicLab.Problems.Programmable\3.3\HeuristicLab.Problems.Programmable-3.3.csproj", "{EE07BFF8-B23D-41F5-8AD7-AC9598D7A2C9}"
    9 EndProject
    10 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Encodings.ParameterVectorEncoding-3.3", "HeuristicLab.Encodings.ParameterVector\HeuristicLab.Encodings.ParameterVectorEncoding-3.3.csproj", "{BA2B2335-EF06-473F-B06D-210F356F8EBD}"
    119EndProject
    1210Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Problems.Programmable.Views-3.3", "HeuristicLab.Problems.Programmable.Views\3.3\HeuristicLab.Problems.Programmable.Views-3.3.csproj", "{6F023B90-2091-40A9-8AC0-B0338DFF8E5F}"
     
    2624    {EE07BFF8-B23D-41F5-8AD7-AC9598D7A2C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
    2725    {EE07BFF8-B23D-41F5-8AD7-AC9598D7A2C9}.Release|Any CPU.Build.0 = Release|Any CPU
    28     {BA2B2335-EF06-473F-B06D-210F356F8EBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    29     {BA2B2335-EF06-473F-B06D-210F356F8EBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
    30     {BA2B2335-EF06-473F-B06D-210F356F8EBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
    31     {BA2B2335-EF06-473F-B06D-210F356F8EBD}.Release|Any CPU.Build.0 = Release|Any CPU
    3226    {6F023B90-2091-40A9-8AC0-B0338DFF8E5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    3327    {6F023B90-2091-40A9-8AC0-B0338DFF8E5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
Note: See TracChangeset for help on using the changeset viewer.