Changeset 14073 for branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/MultiObjectiveTestFunctionProblem.cs
 Timestamp:
 07/14/16 17:40:54 (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/MultiObjectiveTestFunctionProblem.cs
r14068 r14073 33 33 namespace HeuristicLab.Problems.MultiObjectiveTestFunctions { 34 34 [StorableClass] 35 [Creatable(CreatableAttribute.Categories.Problems, Priority = 95)] 36 [Item("Test Function (multiobjective)", "Test functions with real valued inputs and multiple objectives.")] 35 37 public class MultiObjectiveTestFunctionProblem : MultiObjectiveBasicProblem<RealVectorEncoding>, IProblemInstanceConsumer<MOTFData> { 36 38 37 39 #region Parameter Properties 38 39 /// <summary> 40 /// Whether an objective is to be maximized or minimized 41 /// </summary> 42 private IValueParameter<BoolArray> MaximizationParameter { 43 get { 44 return (IValueParameter<BoolArray>)Parameters["Maximization"]; 45 } 46 set { 47 Parameters["Maximization"].ActualValue = value; 48 } 49 } 50 51 /// <summary> 52 /// The dimensionality of the solution candidates 53 /// </summary> 54 private IFixedValueParameter<IntValue> ProblemSizeParameter { 40 public IValueParameter<BoolArray> MaximizationParameter { 41 get { return (IValueParameter<BoolArray>)Parameters["Maximization"]; } 42 } 43 public IFixedValueParameter<IntValue> ProblemSizeParameter { 55 44 get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; } 56 45 } 57 58 /// <summary> 59 /// The number of objectives that are to be optimized 60 /// </summary> 61 private IFixedValueParameter<IntValue> ObjectivesParameter { 46 public IFixedValueParameter<IntValue> ObjectivesParameter { 62 47 get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; } 63 48 } 64 65 /// <summary> 66 /// The bounds for the entries of the solution candidate 67 /// </summary> 68 private IValueParameter<DoubleMatrix> BoundsParameter { 49 public IValueParameter<DoubleMatrix> BoundsParameter { 69 50 get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; } 70 51 } 71 72 /// <summary>73 /// The testfunction74 /// </summary>75 52 public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter { 76 53 get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; } 77 54 } 78 79 /// <summary>80 /// The testfunction81 /// </summary>82 55 public IValueParameter<DoubleArray> ReferencePointParameter { 83 56 get { return (IValueParameter<DoubleArray>)Parameters["ReferencePoint"]; } 84 57 } 85 86 58 public IValueParameter<DoubleMatrix> BestKnownFrontParameter { 59 get { return (IValueParameter<DoubleMatrix>)Parameters["BestKnownFront"]; } 60 } 61 62 #endregion 63 64 #region Properties 65 public override bool[] Maximization { 87 66 get { 88 return (IValueParameter<DoubleMatrix>)Parameters["BestKnownFront"]; 89 } 90 } 91 92 #endregion 93 94 #region Properties 95 private IEnumerable<IMultiObjectiveTestFunctionAnalyzer> Analyzers { 96 get { return Operators.OfType<IMultiObjectiveTestFunctionAnalyzer>(); } 67 //necessary because of virtual member call in base ctor to this property 68 if (!Parameters.ContainsKey("TestFunction")) return new bool[2]; 69 return TestFunction.Maximization(Objectives); 70 } 97 71 } 98 72 … … 113 87 set { TestFunctionParameter.Value = value; } 114 88 } 89 public IEnumerable<double[]> BestKnownFront { 90 get { return Parameters.ContainsKey("BestKnownFront") ? TestFunction.OptimalParetoFront(Objectives) : null; } 91 } 115 92 #endregion 116 93 117 94 [StorableConstructor] 118 95 protected MultiObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { } 96 [StorableHook(HookType.AfterDeserialization)] 97 private void AfterDeserialization() { 98 RegisterEventHandlers(); 99 } 100 119 101 protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner) 120 102 : base(original, cloner) { 121 103 RegisterEventHandlers(); 122 104 } 105 public override IDeepCloneable Clone(Cloner cloner) { 106 return new MultiObjectiveTestFunctionProblem(this, cloner); 107 } 108 123 109 public MultiObjectiveTestFunctionProblem() 124 110 : base() { 125 Parameters.Remove("Maximization");126 Parameters.Add(new ValueParameter<BoolArray>("Maximization", "", new BoolArray(new bool[] { false, false })));127 111 Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2))); 128 112 Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2))); … … 135 119 BestKnownFrontParameter.Hidden = true; 136 120 121 UpdateParameterValues(); 137 122 InitializeOperators(); 138 123 RegisterEventHandlers(); 139 124 } 140 public override IDeepCloneable Clone(Cloner cloner) { 141 return new MultiObjectiveTestFunctionProblem(this, cloner);142 }143 [StorableHook(HookType.AfterDeserialization)]144 private void AfterDeserialization() {145 RegisterEventHandlers();146 } 125 126 private void RegisterEventHandlers() { 127 TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged; 128 ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged; 129 ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged; 130 } 131 147 132 148 133 public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { … … 150 135 if (results.ContainsKey("Pareto Front")) { 151 136 ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true; 152 }153 }154 155 public override bool[] Maximization {156 get {157 return Parameters.ContainsKey("TestFunction") ? TestFunction.Maximization(Objectives) : new bool[2];158 }159 }160 161 public IEnumerable<double[]> BestKnownFront {162 get {163 return Parameters.ContainsKey("BestKnownFront") ? TestFunction.OptimalParetoFront(Objectives) : null;164 137 } 165 138 } … … 178 151 } 179 152 180 public double[] Evaluate(RealVector individual , IRandom random) {153 public double[] Evaluate(RealVector individual) { 181 154 return TestFunction.Evaluate(individual, Objectives); 182 155 } 183 156 184 157 public override double[] Evaluate(Individual individual, IRandom random) { 185 return Evaluate(individual.RealVector() , random);158 return Evaluate(individual.RealVector()); 186 159 } 187 160 … … 190 163 } 191 164 192 private void RegisterEventHandlers() {193 TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;194 ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;195 ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;196 BoundsParameter.ValueChanged += BoundsParameterOnValueChanged;197 }198 199 165 #region Events 166 private void UpdateParameterValues() { 167 MaximizationParameter.ActualValue = (BoolArray)new BoolArray(Maximization).AsReadOnly(); 168 var front = BestKnownFront; 169 if (front != null) { BestKnownFrontParameter.Value = (DoubleMatrix)new DoubleMatrix(To2D(front.ToArray())).AsReadOnly(); } 170 171 } 172 200 173 protected override void OnEncodingChanged() { 201 174 base.OnEncodingChanged(); 202 Parameterize();175 UpdateParameterValues(); 203 176 ParameterizeAnalyzers(); 204 177 } 205 178 protected override void OnEvaluatorChanged() { 206 179 base.OnEvaluatorChanged(); 207 Parameterize();180 UpdateParameterValues(); 208 181 ParameterizeAnalyzers(); 209 182 } 210 183 211 184 private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) { 212 var problemSizeChange = ProblemSize < TestFunction.MinimumSolutionLength 213  ProblemSize > TestFunction.MaximumSolutionLength; 214 if (problemSizeChange) { 215 ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength)); 216 } 217 218 var solutionSizeChange = Objectives < TestFunction.MinimumObjectives 219  Objectives > TestFunction.MaximumObjectives; 220 if (solutionSizeChange) { 221 ProblemSize = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives)); 222 } 185 ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength)); 186 Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives)); 223 187 224 188 Bounds = (DoubleMatrix)new DoubleMatrix(TestFunction.Bounds(Objectives)).Clone(); 225 189 ParameterizeAnalyzers(); 226 Parameterize();190 UpdateParameterValues(); 227 191 OnReset(); 228 192 } 229 193 230 194 private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) { 231 if (ProblemSize < TestFunction.MinimumSolutionLength 232  ProblemSize > TestFunction.MaximumSolutionLength) 233 ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize)); 234 if (Objectives < TestFunction.MinimumObjectives 235  Objectives > TestFunction.MaximumObjectives) 236 Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives)); 237 Parameterize(); 195 ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize)); 196 UpdateParameterValues(); 238 197 } 239 198 240 199 private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) { 241 if (Objectives < TestFunction.MinimumObjectives 242  Objectives > TestFunction.MaximumObjectives) 243 Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives)); 244 if (ProblemSize < TestFunction.MinimumSolutionLength 245  ProblemSize > TestFunction.MaximumSolutionLength) 246 ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize)); 247 248 249 Parameterize(); 250 } 251 252 private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) { 253 Parameterize(); 254 } 200 Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives)); 201 UpdateParameterValues(); 202 } 203 255 204 #endregion 256 205 … … 264 213 Operators.Add(new ScatterPlotAnalyzer()); 265 214 Operators.Add(new NormalizedHypervolumeAnalyzer()); 266 ParameterizeAnalyzers(); 267 Parameterize(); 268 } 269 270 private void Parameterize() { 271 MaximizationParameter.ActualValue = new BoolArray(Maximization); 272 var front = BestKnownFront; 273 if (front != null) { BestKnownFrontParameter.ActualValue = new DoubleMatrix(To2D(front.ToArray<double[]>())); } 274 215 216 ParameterizeAnalyzers(); 217 } 218 219 private IEnumerable<IMultiObjectiveTestFunctionAnalyzer> Analyzers { 220 get { return Operators.OfType<IMultiObjectiveTestFunctionAnalyzer>(); } 275 221 } 276 222 … … 282 228 analyzer.BestKnownFrontParameter.ActualName = BestKnownFrontParameter.Name; 283 229 284 285 var front = BestKnownFront;286 if (front != null) { BestKnownFrontParameter.ActualValue = new DoubleMatrix(To2D(front.ToArray<double[]>())); }287 288 289 230 var hyperVolumeAnalyzer = analyzer as HypervolumeAnalyzer; 290 231 if (hyperVolumeAnalyzer != null) { … … 295 236 var normalizedHyperVolumeAnalyzer = analyzer as NormalizedHypervolumeAnalyzer; 296 237 if (normalizedHyperVolumeAnalyzer != null) { 297 normalizedHyperVolumeAnalyzer.OptimalFrontParameter. ActualValue = (DoubleMatrix)BestKnownFrontParameter.ActualValue;238 normalizedHyperVolumeAnalyzer.OptimalFrontParameter.Value = (DoubleMatrix)BestKnownFrontParameter.ActualValue; 298 239 } 299 240 … … 302 243 scatterPlotAnalyzer.IndividualsParameter.ActualName = Encoding.Name; 303 244 } 304 305 245 } 306 246 } … … 308 248 public static T[,] To2D<T>(T[][] source) { 309 249 try { 310 int FirstDim= source.Length;311 int SecondDim= source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular312 313 var result = new T[ FirstDim, SecondDim];314 for (int i = 0; i < FirstDim; ++i)315 for (int j = 0; j < SecondDim; ++j)250 int firstDimension = source.Length; 251 int secondDimension = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular 252 253 var result = new T[firstDimension, secondDimension]; 254 for (int i = 0; i < firstDimension; ++i) 255 for (int j = 0; j < secondDimension; ++j) 316 256 result[i, j] = source[i][j]; 317 257
Note: See TracChangeset
for help on using the changeset viewer.