- Timestamp:
- 05/14/14 14:46:00 (10 years ago)
- Location:
- branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorCreater.cs
r10753 r10850 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();
Note: See TracChangeset
for help on using the changeset viewer.