Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/MultiObjectiveTestFunctionProblem.cs @ 13451

Last change on this file since 13451 was 13451, checked in by bwerth, 8 years ago

#1087 extensive testing; fixed minor bugs

File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Encodings.RealVectorEncoding;
10using HeuristicLab.Optimization;
11using HeuristicLab.Parameters;
12using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
13using HeuristicLab.Problems.MultiObjectiveTestFunctions.Testfunctions;
14using HeuristicLab.Problems.TestFunctions;
15
16namespace HeuristicLab.Problems.MultiObjectiveTestFunction {
17  [StorableClass]
18  public class MultiObjectiveTestFunctionProblem : MultiObjectiveBasicProblem<RealVectorEncoding> {
19
20    public override bool[] Maximization {
21      get {
22        return Parameters.ContainsKey("TestFunction") ? TestFunction.Maximization : new bool[2];
23      }
24    }
25
26    #region Parameter Properties
27
28
29    private IFixedValueParameter<IntValue> ProblemSizeParameter {
30      get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
31    }
32    private IFixedValueParameter<IntValue> SolutionSizeParameter {
33      get { return (IFixedValueParameter<IntValue>)Parameters["SolutionSize"]; }
34    }
35    private IValueParameter<DoubleMatrix> BoundsParameter {
36      get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
37    }
38    public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
39      get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
40    }
41    public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
42      get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
43    }
44    #endregion
45
46    #region Properties
47    public int ProblemSize {
48      get { return ProblemSizeParameter.Value.Value; }
49      set { ProblemSizeParameter.Value.Value = value; }
50    }
51    public int SolutionSize {
52      get { return SolutionSizeParameter.Value.Value; }
53      set { SolutionSizeParameter.Value.Value = value; }
54    }
55    public DoubleMatrix Bounds {
56      get { return BoundsParameter.Value; }
57      set { BoundsParameter.Value = value; }
58    }
59    public IMultiObjectiveTestFunction TestFunction {
60      get { return TestFunctionParameter.Value; }
61      set { TestFunctionParameter.Value = value; }
62    }
63    #endregion
64
65
66
67    [StorableConstructor]
68    private MultiObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
69    private MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner)
70      : base(original, cloner) {
71      RegisterEventHandlers();
72    }
73    public MultiObjectiveTestFunctionProblem()
74      : base() { //former: base(new RealVectorEncoding("Point"))
75      Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
76      Parameters.Add(new FixedValueParameter<IntValue>("SolutionSize", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
77      Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The bounds of the solution given as either one line for all variables or a line for each variable. The first column specifies lower bound, the second upper bound.", new DoubleMatrix(new double[,] { { -4, 4 } })));
78      Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
79
80      Encoding.LengthParameter = ProblemSizeParameter;
81      Encoding.BoundsParameter = BoundsParameter;
82      //BestKnownQuality = TestFunction.BestKnownQuality;
83
84      InitializeOperators();
85      RegisterEventHandlers();
86    }
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new MultiObjectiveTestFunctionProblem(this, cloner);
90    }
91
92    [StorableHook(HookType.AfterDeserialization)]
93    private void AfterDeserialization() {
94      RegisterEventHandlers();
95    }
96
97    private void RegisterEventHandlers() {
98      //Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
99      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
100      ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
101      SolutionSizeParameter.Value.ValueChanged += SolutionSizeOnValueChanged;
102      BoundsParameter.ValueChanged += BoundsParameterOnValueChanged;
103    }
104
105    public double[] Evaluate(RealVector individual, IRandom random) {
106      return TestFunction.Evaluate(individual);
107    }
108
109    public override double[] Evaluate(Individual individual, IRandom random) {
110      return Evaluate(individual.RealVector(), random);
111    }
112
113
114    #region Events
115    protected override void OnEncodingChanged() {
116      base.OnEncodingChanged();
117      Parameterize();
118    }
119    protected override void OnEvaluatorChanged() {
120      base.OnEvaluatorChanged();
121      // Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
122      Parameterize();
123    }
124
125    //private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
126    //  Parameterize();
127    //}
128
129
130    private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
131      var problemSizeChange = ProblemSize < TestFunction.MinimumProblemSize
132                              || ProblemSize > TestFunction.MaximumProblemSize;
133      if (problemSizeChange) {
134        ProblemSize = Math.Max(TestFunction.MinimumProblemSize, Math.Min(ProblemSize, TestFunction.MaximumProblemSize));
135      }
136
137      var solutionSizeChange = SolutionSize < TestFunction.MinimumSolutionSize
138                              || SolutionSize > TestFunction.MaximumSolutionSize;
139      if (solutionSizeChange) {
140        ProblemSize = Math.Max(TestFunction.MinimumSolutionSize, Math.Min(SolutionSize, TestFunction.MaximumSolutionSize));
141      }
142      Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
143      OnReset();
144    }
145
146    private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
147      if (ProblemSize < TestFunction.MinimumProblemSize
148        || ProblemSize > TestFunction.MaximumProblemSize)
149        ProblemSize = Math.Min(TestFunction.MaximumProblemSize, Math.Max(TestFunction.MinimumProblemSize, ProblemSize));
150      if (SolutionSize < TestFunction.MinimumSolutionSize
151        || SolutionSize > TestFunction.MaximumSolutionSize)
152        SolutionSize = Math.Min(TestFunction.MaximumSolutionSize, Math.Max(TestFunction.MinimumSolutionSize, SolutionSize));
153      TestFunction.ActualSolutionSize = SolutionSize;
154    }
155
156    private void SolutionSizeOnValueChanged(object sender, EventArgs eventArgs) {
157      if (SolutionSize < TestFunction.MinimumSolutionSize
158        || SolutionSize > TestFunction.MaximumSolutionSize)
159        SolutionSize = Math.Min(TestFunction.MaximumSolutionSize, Math.Max(TestFunction.MinimumSolutionSize, SolutionSize));
160      TestFunction.ActualSolutionSize = SolutionSize;
161      if (ProblemSize < TestFunction.MinimumProblemSize
162        || ProblemSize > TestFunction.MaximumProblemSize)
163        ProblemSize = Math.Min(TestFunction.MaximumProblemSize, Math.Max(TestFunction.MinimumProblemSize, ProblemSize));
164    }
165
166    private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) {
167      Parameterize();
168    }
169    #endregion
170
171    #region Helpers
172    private void InitializeOperators() {
173      //empty for now
174      Parameterize();
175    }
176
177    private void Parameterize() {
178    }
179
180
181    #endregion
182  }
183}
184
Note: See TracBrowser for help on using the repository browser.