Changeset 10850 for branches/SimSharp
- Timestamp:
- 05/14/14 14:46:00 (11 years ago)
- 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 25 25 [Plugin("HeuristicLab.Problems.Programmable.Views", "Views for programmable problem for defining custom representation and evaluation function.", "3.3.9.$WCREV$")] 26 26 [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")] 27 35 [PluginDependency("HeuristicLab.Problems.Programmable", "3.3")] 36 [PluginDependency("HeuristicLab.Scripting", "3.3")] 37 [PluginDependency("HeuristicLab.Scripting.Views", "3.3")] 28 38 public class HeuristicLabProblemsProgrammableViewsPlugin : PluginBase { } 29 39 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Datastructures/ParameterVector.cs
r10796 r10850 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 using HeuristicLab.Collections; 25 using HeuristicLab.Common; 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 23 using System.Linq; 28 24 using HeuristicLab.Encodings.BinaryVectorEncoding; 29 25 using HeuristicLab.Encodings.IntegerVectorEncoding; 30 26 using HeuristicLab.Encodings.PermutationEncoding; 31 27 using HeuristicLab.Encodings.RealVectorEncoding; 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;33 28 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 } 29 namespace 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); 47 44 } 48 45 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]; 93 48 } 94 49 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]; 97 52 } 98 53 99 public bool Boolean(string name){100 return BooleanParameters[BoolMap.GetByFirst(name)];54 public IEnumerable<string> BinaryNames { 55 get { return BinaryParameters.Keys; } 101 56 } 102 57 103 public bool Boolean(int index) {104 return BooleanParameters[index];58 public IntegerVector Integer(string name) { 59 return IntegerParameters[name]; 105 60 } 106 61 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]; 112 64 } 113 65 114 public int Integer(string name){115 return IntegerParameters[IntMap.GetByFirst(name)];66 public IEnumerable<string> IntegerNames { 67 get { return IntegerParameters.Keys; } 116 68 } 117 69 118 public int Integer(int index) {119 return IntegerParameters[index];70 public RealVector Real(string name) { 71 return RealParameters[name]; 120 72 } 121 73 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]; 127 76 } 128 77 129 public double Real(string name){130 return RealParameters[RealMap.GetByFirst(name)];78 public IEnumerable<string> RealNames { 79 get { return RealParameters.Keys; } 131 80 } 132 81 133 public double Real(int index) {134 return RealParameters[index];82 public Permutation Permutation(string name) { 83 return PermutationParameters[name]; 135 84 } 136 85 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; } 142 88 } 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 Builder155 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 min183 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 min202 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 #endregion212 89 } 213 90 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj
r10754 r10850 136 136 </ItemGroup> 137 137 <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" /> 138 145 <Compile Include="Interfaces\ISingleObjectiveProgrammableProblemEvaluator.cs" /> 139 146 <Compile Include="Operators\ParameterVectorManipulator.cs" /> … … 151 158 <None Include="Properties\AssemblyInfo.cs.frame" /> 152 159 <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>159 160 </ItemGroup> 160 161 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorCreater.cs
r10753 r10850 20 20 #endregion 21 21 22 using System;23 22 using HeuristicLab.Common; 24 23 using 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;31 24 using HeuristicLab.Operators; 32 25 using HeuristicLab.Optimization; 33 using HeuristicLab.Parameters;34 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 35 27 36 28 namespace HeuristicLab.Problems.Programmable { 37 [Item("ParameterVectorCreater", "C reates a parameter vector as received from a SimOptRunner.")]29 [Item("ParameterVectorCreater", "Contains solution creators that together create a multi-vector encoding.")] 38 30 [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 { 97 32 98 33 [StorableConstructor] 99 34 protected ParameterVectorCreater(bool deserializing) : base(deserializing) { } 100 35 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() { } 117 37 118 38 public override IDeepCloneable Clone(Cloner cloner) { 119 39 return new ParameterVectorCreater(this, cloner); 120 40 } 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 }148 41 } 149 42 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorCrossover.cs
r10753 r10850 22 22 using HeuristicLab.Common; 23 23 using 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;29 24 using HeuristicLab.Operators; 30 25 using HeuristicLab.Optimization; 31 using HeuristicLab.Parameters;32 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 27 34 28 namespace 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.")] 36 30 [StorableClass] 37 31 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 }61 32 62 33 [StorableConstructor] … … 65 36 : base(original, cloner) { } 66 37 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."));73 38 } 74 39 … … 76 41 return new ParameterVectorCrossover(this, cloner); 77 42 } 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 }88 43 } 89 44 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorManipulator.cs
r10753 r10850 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Encodings.BinaryVectorEncoding;25 using HeuristicLab.Encodings.IntegerVectorEncoding;26 using HeuristicLab.Encodings.PermutationEncoding;27 using HeuristicLab.Encodings.RealVectorEncoding;28 24 using HeuristicLab.Operators; 29 25 using HeuristicLab.Optimization; 30 using HeuristicLab.Parameters;31 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 27 33 28 namespace 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.")] 35 30 [StorableClass] 36 31 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 }52 32 53 33 [StorableConstructor] … … 56 36 : base(original, cloner) { } 57 37 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."));62 38 } 63 39 -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveEvaluator.cs
r10754 r10850 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Data; 26 using HeuristicLab.Encodings.ParameterVectorEncoding; 27 using HeuristicLab.Encodings.BinaryVectorEncoding; 28 using HeuristicLab.Encodings.IntegerVectorEncoding; 29 using HeuristicLab.Encodings.PermutationEncoding; 30 using HeuristicLab.Encodings.RealVectorEncoding; 27 31 using HeuristicLab.Operators; 28 32 using HeuristicLab.Optimization; … … 43 47 } 44 48 45 public ILookupParameter< ParameterVector> ParameterVectorParameter {46 get { return (ILookupParameter< ParameterVector>)Parameters["ParameterVector"]; }49 public ILookupParameter<Configuration> ConfigurationParameter { 50 get { return (ILookupParameter<Configuration>)Parameters["Configuration"]; } 47 51 } 48 52 … … 57 61 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 58 62 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.")); 60 64 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 61 65 } … … 69 73 var runner = ScriptParameter.ActualValue; 70 74 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); 72 108 QualityParameter.ActualValue = new DoubleValue(runner.Instance.Evaluate(random, vector)); 73 109 return base.Apply(); -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Plugin.cs.frame
r10753 r10850 25 25 [Plugin("HeuristicLab.Problems.Programmable", "Programmable problem for defining custom representation and evaluation function.", "3.3.9.$WCREV$")] 26 26 [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")] 28 43 public class HeuristicLabProblemsProgrammablePlugin : PluginBase { } 29 44 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/ProblemBase.cs
r10754 r10850 20 20 #endregion 21 21 22 using HeuristicLab.Encodings.ParameterVectorEncoding;23 24 22 namespace HeuristicLab.Problems.Programmable { 25 23 public abstract class ProblemBase { 26 public abstract ParameterVector GetParametersToOptimize();24 public abstract Configuration GetConfiguration(); 27 25 } 28 26 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemBase.cs
r10754 r10850 21 21 22 22 using HeuristicLab.Core; 23 using HeuristicLab.Encodings.ParameterVectorEncoding;24 23 25 24 namespace HeuristicLab.Problems.Programmable { 26 25 public abstract class SingleObjectiveProblemBase : ProblemBase { 27 public abstract bool IsMaximizationProblem { get; }28 26 public abstract double Evaluate(IRandom random, ParameterVector vector); 29 27 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r10754 r10850 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Drawing; 24 25 using System.Linq; … … 29 30 using HeuristicLab.Encodings.BinaryVectorEncoding; 30 31 using HeuristicLab.Encodings.IntegerVectorEncoding; 31 using HeuristicLab.Encodings.ParameterVectorEncoding;32 32 using HeuristicLab.Encodings.PermutationEncoding; 33 33 using HeuristicLab.Encodings.RealVectorEncoding; … … 35 35 using HeuristicLab.Parameters; 36 36 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 37 using HeuristicLab.PluginInfrastructure; 37 38 38 39 namespace HeuristicLab.Problems.Programmable { … … 40 41 [Creatable("Problems")] 41 42 [StorableClass] 42 public sealed class SingleObjectiveProgrammableProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveProgrammableProblemEvaluator, IParameterVectorCreator>, IStorableContent {43 public class SingleObjectiveProgrammableProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveProgrammableProblemEvaluator, ISolutionCreator>, IParameterizedNamedItem, IStorableContent { 43 44 public string Filename { get; set; } 44 45 … … 47 48 } 48 49 50 public new ParameterCollection Parameters { 51 get { return base.Parameters; } 52 } 53 IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters { 54 get { return Parameters; } 55 } 56 49 57 public IValueParameter<SingleObjectiveScript> ScriptParameter { 50 58 get { return (IValueParameter<SingleObjectiveScript>)Parameters["Script"]; } 51 59 } 52 60 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"]; } 79 63 } 80 64 81 65 [Storable] 82 private ParameterVectorCrossover Crossover { get; set; } 83 [Storable] 84 private ParameterVectorManipulator Manipulator { get; set; } 66 protected List<IParameter> DynamicConfigurationParameters; 85 67 86 68 [StorableConstructor] 87 pr ivateSingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }88 89 pr ivateSingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)69 protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { } 70 71 protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner) 90 72 : base(original, cloner) { 91 Crossover = cloner.Clone(original.Crossover); 92 Manipulator = cloner.Clone(original.Manipulator); 73 DynamicConfigurationParameters = original.DynamicConfigurationParameters.Select(cloner.Clone).ToList(); 93 74 RegisterEventHandlers(); 94 75 } … … 96 77 : base(new SingleObjectiveEvaluator(), new ParameterVectorCreater()) { 97 78 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>(); 108 82 109 83 Operators.Add(new BestScopeSolutionAnalyzer()); 110 84 Operators.Add(Evaluator); 111 85 Operators.Add(SolutionCreator); 112 Operators.Add(Crossover);113 Operators.Add(Manipulator);114 86 115 87 RegisterEventHandlers(); … … 127 99 private void RegisterEventHandlers() { 128 100 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;135 101 RegisterScriptInstanceChanges(); 136 102 } … … 138 104 private void ScriptParameterOnValueChanged(object sender, EventArgs eventArgs) { 139 105 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();148 106 } 149 107 … … 165 123 } 166 124 167 pr ivatevoid ScriptOnInstanceChanged(object sender, EventArgs eventArgs) {125 protected virtual void ScriptOnInstanceChanged(object sender, EventArgs eventArgs) { 168 126 169 127 var instance = ScriptParameter.Value.Instance; 170 128 if (instance == null) return; 171 129 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 } 198 187 } 199 188 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); 208 192 } 209 193 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 } 274 377 } 275 378 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveScript.cs
r10754 r10850 53 53 using HeuristicLab.Core; 54 54 using HeuristicLab.Data; 55 using HeuristicLab.Encodings.ParameterVectorEncoding;56 55 using HeuristicLab.Encodings.PermutationEncoding; 56 using HeuristicLab.Problems.Programmable; 57 57 58 58 public class MyProblem : HeuristicLab.Problems.Programmable.SingleObjectiveProblemBase { … … 60 60 // initialize private fields 61 61 } 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 ; 70 72 } 71 73 72 74 public override double Evaluate(IRandom random, ParameterVector vector) { 73 75 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(); 75 77 return quality; 76 78 } -
branches/SimSharp/ProgrammableProblem.sln
r10753 r10850 7 7 EndProject 8 8 Project("{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 EndProject10 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}"11 9 EndProject 12 10 Project("{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}" … … 26 24 {EE07BFF8-B23D-41F5-8AD7-AC9598D7A2C9}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 25 {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 CPU29 {BA2B2335-EF06-473F-B06D-210F356F8EBD}.Debug|Any CPU.Build.0 = Debug|Any CPU30 {BA2B2335-EF06-473F-B06D-210F356F8EBD}.Release|Any CPU.ActiveCfg = Release|Any CPU31 {BA2B2335-EF06-473F-B06D-210F356F8EBD}.Release|Any CPU.Build.0 = Release|Any CPU32 26 {6F023B90-2091-40A9-8AC0-B0338DFF8E5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 27 {6F023B90-2091-40A9-8AC0-B0338DFF8E5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
Note: See TracChangeset
for help on using the changeset viewer.