1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
10 | using HeuristicLab.Optimization;
|
---|
11 | using HeuristicLab.Parameters;
|
---|
12 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
13 | using HeuristicLab.Problems.MultiObjectiveTestFunctions.Testfunctions;
|
---|
14 | using HeuristicLab.Problems.TestFunctions;
|
---|
15 |
|
---|
16 | namespace 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 | //TODO problemsize dominates solutionSize
|
---|
151 | SolutionSizeOnValueChanged(sender, eventArgs);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void SolutionSizeOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
155 | if (SolutionSize < TestFunction.MinimumSolutionSize
|
---|
156 | || SolutionSize > TestFunction.MaximumSolutionSize)
|
---|
157 | SolutionSize = Math.Min(TestFunction.MaximumSolutionSize, Math.Max(TestFunction.MinimumSolutionSize, SolutionSize));
|
---|
158 | TestFunction.ActualSolutionSize = SolutionSize;
|
---|
159 | OnReset();
|
---|
160 | }
|
---|
161 |
|
---|
162 | private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
163 | Parameterize();
|
---|
164 | }
|
---|
165 | #endregion
|
---|
166 |
|
---|
167 | #region Helpers
|
---|
168 | private void InitializeOperators() {
|
---|
169 | //empty for now
|
---|
170 | Parameterize();
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void Parameterize() {
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | #endregion
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | |
---|