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 | } //TODO shady
|
---|
24 | }
|
---|
25 |
|
---|
26 | #region Parameter Properties
|
---|
27 | private IFixedValueParameter<IntValue> ProblemSizeParameter {
|
---|
28 | get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
|
---|
29 | }
|
---|
30 | private IFixedValueParameter<IntValue> SolutionSizeParameter {
|
---|
31 | get { return (IFixedValueParameter<IntValue>)Parameters["SolutionSize"]; }
|
---|
32 | }
|
---|
33 | private IValueParameter<DoubleMatrix> BoundsParameter {
|
---|
34 | get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
35 | }
|
---|
36 | public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
|
---|
37 | get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
|
---|
38 | }
|
---|
39 | public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
|
---|
40 | get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
|
---|
41 | }
|
---|
42 | #endregion
|
---|
43 |
|
---|
44 | #region Properties
|
---|
45 | public int ProblemSize {
|
---|
46 | get { return ProblemSizeParameter.Value.Value; }
|
---|
47 | set { ProblemSizeParameter.Value.Value = value; }
|
---|
48 | }
|
---|
49 | public int SolutionSize {
|
---|
50 | get { return SolutionSizeParameter.Value.Value; }
|
---|
51 | set { SolutionSizeParameter.Value.Value = value; }
|
---|
52 | }
|
---|
53 | public DoubleMatrix Bounds {
|
---|
54 | get { return BoundsParameter.Value; }
|
---|
55 | set { BoundsParameter.Value = value; }
|
---|
56 | }
|
---|
57 | public IMultiObjectiveTestFunction TestFunction {
|
---|
58 | get { return TestFunctionParameter.Value; }
|
---|
59 | set { TestFunctionParameter.Value = value; }
|
---|
60 | }
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 | [StorableConstructor]
|
---|
66 | private MultiObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
|
---|
67 | private MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner)
|
---|
68 | : base(original, cloner) {
|
---|
69 | RegisterEventHandlers();
|
---|
70 | }
|
---|
71 | public MultiObjectiveTestFunctionProblem()
|
---|
72 | : base() { //former: base(new RealVectorEncoding("Point"))
|
---|
73 | Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
|
---|
74 | Parameters.Add(new FixedValueParameter<IntValue>("SolutionSize", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
|
---|
75 | 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[,] { { -100, 100 } })));
|
---|
76 | Parameters.Add(new ValueParameter<IMultiObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Fonseca()));
|
---|
77 |
|
---|
78 | Encoding.LengthParameter = ProblemSizeParameter;
|
---|
79 | Encoding.BoundsParameter = BoundsParameter;
|
---|
80 | //BestKnownQuality = TestFunction.BestKnownQuality;
|
---|
81 |
|
---|
82 | InitializeOperators();
|
---|
83 | RegisterEventHandlers();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
87 | return new MultiObjectiveTestFunctionProblem(this, cloner);
|
---|
88 | }
|
---|
89 |
|
---|
90 | [StorableHook(HookType.AfterDeserialization)]
|
---|
91 | private void AfterDeserialization() {
|
---|
92 | RegisterEventHandlers();
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void RegisterEventHandlers() {
|
---|
96 | //Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
97 | TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
|
---|
98 | ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
|
---|
99 | BoundsParameter.ValueChanged += BoundsParameterOnValueChanged;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public double[] Evaluate(RealVector vector, IRandom random) {
|
---|
103 | return TestFunction.Evaluate(vector);
|
---|
104 | }
|
---|
105 | public sealed override double[] Evaluate(Individual individual, IRandom random) {
|
---|
106 | return Evaluate(individual.RealVector(), random);
|
---|
107 | }
|
---|
108 |
|
---|
109 | #region Events
|
---|
110 | protected override void OnEncodingChanged() {
|
---|
111 | base.OnEncodingChanged();
|
---|
112 | Parameterize();
|
---|
113 | }
|
---|
114 | protected override void OnEvaluatorChanged() {
|
---|
115 | base.OnEvaluatorChanged();
|
---|
116 | // Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
117 | Parameterize();
|
---|
118 | }
|
---|
119 | //private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
120 | // Parameterize();
|
---|
121 | //}
|
---|
122 |
|
---|
123 |
|
---|
124 | private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
125 | var problemSizeChange = ProblemSize < TestFunction.MinimumProblemSize
|
---|
126 | || ProblemSize > TestFunction.MaximumProblemSize;
|
---|
127 | if (problemSizeChange) {
|
---|
128 | ProblemSize = Math.Max(TestFunction.MinimumProblemSize, Math.Min(ProblemSize, TestFunction.MaximumProblemSize));
|
---|
129 | }
|
---|
130 |
|
---|
131 | var solutionSizeChange = SolutionSize < TestFunction.MinimumSolutionSize
|
---|
132 | || SolutionSize > TestFunction.MaximumSolutionSize;
|
---|
133 | if (solutionSizeChange) {
|
---|
134 | ProblemSize = Math.Max(TestFunction.MinimumSolutionSize, Math.Min(SolutionSize, TestFunction.MaximumSolutionSize));
|
---|
135 | }
|
---|
136 |
|
---|
137 | Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
|
---|
138 | OnReset();
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
142 | if (ProblemSize < TestFunction.MinimumProblemSize
|
---|
143 | || ProblemSize > TestFunction.MaximumProblemSize)
|
---|
144 | ProblemSize = Math.Min(TestFunction.MaximumProblemSize, Math.Max(TestFunction.MinimumProblemSize, ProblemSize));
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void SolutionSizeOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
148 | if (SolutionSize < TestFunction.MinimumSolutionSize
|
---|
149 | || ProblemSize > TestFunction.MaximumSolutionSize)
|
---|
150 | ProblemSize = Math.Min(TestFunction.MaximumSolutionSize, Math.Max(TestFunction.MinimumSolutionSize, SolutionSize));
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
154 | Parameterize();
|
---|
155 | }
|
---|
156 | #endregion
|
---|
157 |
|
---|
158 | #region Helpers
|
---|
159 | private void InitializeOperators() {
|
---|
160 | //empty for now
|
---|
161 | Parameterize();
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void Parameterize() {
|
---|
165 | //empty for now
|
---|
166 | }
|
---|
167 | #endregion
|
---|
168 |
|
---|
169 |
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | |
---|