1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package agents.anac.y2019.harddealer.math3.optim.univariate;
|
---|
18 |
|
---|
19 | import agents.anac.y2019.harddealer.math3.analysis.UnivariateFunction;
|
---|
20 | import agents.anac.y2019.harddealer.math3.optim.BaseOptimizer;
|
---|
21 | import agents.anac.y2019.harddealer.math3.optim.OptimizationData;
|
---|
22 | import agents.anac.y2019.harddealer.math3.optim.nonlinear.scalar.GoalType;
|
---|
23 | import agents.anac.y2019.harddealer.math3.optim.ConvergenceChecker;
|
---|
24 | import agents.anac.y2019.harddealer.math3.exception.TooManyEvaluationsException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Base class for a univariate scalar function optimizer.
|
---|
28 | *
|
---|
29 | * @since 3.1
|
---|
30 | */
|
---|
31 | public abstract class UnivariateOptimizer
|
---|
32 | extends BaseOptimizer<UnivariatePointValuePair> {
|
---|
33 | /** Objective function. */
|
---|
34 | private UnivariateFunction function;
|
---|
35 | /** Type of optimization. */
|
---|
36 | private GoalType goal;
|
---|
37 | /** Initial guess. */
|
---|
38 | private double start;
|
---|
39 | /** Lower bound. */
|
---|
40 | private double min;
|
---|
41 | /** Upper bound. */
|
---|
42 | private double max;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * @param checker Convergence checker.
|
---|
46 | */
|
---|
47 | protected UnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
|
---|
48 | super(checker);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * {@inheritDoc}
|
---|
53 | *
|
---|
54 | * @param optData Optimization data. In addition to those documented in
|
---|
55 | * {@link BaseOptimizer#parseOptimizationData(OptimizationData[])
|
---|
56 | * BaseOptimizer}, this method will register the following data:
|
---|
57 | * <ul>
|
---|
58 | * <li>{@link GoalType}</li>
|
---|
59 | * <li>{@link SearchInterval}</li>
|
---|
60 | * <li>{@link UnivariateObjectiveFunction}</li>
|
---|
61 | * </ul>
|
---|
62 | * @return {@inheritDoc}
|
---|
63 | * @throws TooManyEvaluationsException if the maximal number of
|
---|
64 | * evaluations is exceeded.
|
---|
65 | */
|
---|
66 | @Override
|
---|
67 | public UnivariatePointValuePair optimize(OptimizationData... optData)
|
---|
68 | throws TooManyEvaluationsException {
|
---|
69 | // Perform computation.
|
---|
70 | return super.optimize(optData);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * @return the optimization type.
|
---|
75 | */
|
---|
76 | public GoalType getGoalType() {
|
---|
77 | return goal;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Scans the list of (required and optional) optimization data that
|
---|
82 | * characterize the problem.
|
---|
83 | *
|
---|
84 | * @param optData Optimization data.
|
---|
85 | * The following data will be looked for:
|
---|
86 | * <ul>
|
---|
87 | * <li>{@link GoalType}</li>
|
---|
88 | * <li>{@link SearchInterval}</li>
|
---|
89 | * <li>{@link UnivariateObjectiveFunction}</li>
|
---|
90 | * </ul>
|
---|
91 | */
|
---|
92 | @Override
|
---|
93 | protected void parseOptimizationData(OptimizationData... optData) {
|
---|
94 | // Allow base class to register its own data.
|
---|
95 | super.parseOptimizationData(optData);
|
---|
96 |
|
---|
97 | // The existing values (as set by the previous call) are reused if
|
---|
98 | // not provided in the argument list.
|
---|
99 | for (OptimizationData data : optData) {
|
---|
100 | if (data instanceof SearchInterval) {
|
---|
101 | final SearchInterval interval = (SearchInterval) data;
|
---|
102 | min = interval.getMin();
|
---|
103 | max = interval.getMax();
|
---|
104 | start = interval.getStartValue();
|
---|
105 | continue;
|
---|
106 | }
|
---|
107 | if (data instanceof UnivariateObjectiveFunction) {
|
---|
108 | function = ((UnivariateObjectiveFunction) data).getObjectiveFunction();
|
---|
109 | continue;
|
---|
110 | }
|
---|
111 | if (data instanceof GoalType) {
|
---|
112 | goal = (GoalType) data;
|
---|
113 | continue;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * @return the initial guess.
|
---|
120 | */
|
---|
121 | public double getStartValue() {
|
---|
122 | return start;
|
---|
123 | }
|
---|
124 | /**
|
---|
125 | * @return the lower bounds.
|
---|
126 | */
|
---|
127 | public double getMin() {
|
---|
128 | return min;
|
---|
129 | }
|
---|
130 | /**
|
---|
131 | * @return the upper bounds.
|
---|
132 | */
|
---|
133 | public double getMax() {
|
---|
134 | return max;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Computes the objective function value.
|
---|
139 | * This method <em>must</em> be called by subclasses to enforce the
|
---|
140 | * evaluation counter limit.
|
---|
141 | *
|
---|
142 | * @param x Point at which the objective function must be evaluated.
|
---|
143 | * @return the objective function value at the specified point.
|
---|
144 | * @throws TooManyEvaluationsException if the maximal number of
|
---|
145 | * evaluations is exceeded.
|
---|
146 | */
|
---|
147 | protected double computeObjectiveValue(double x) {
|
---|
148 | super.incrementEvaluationCount();
|
---|
149 | return function.value(x);
|
---|
150 | }
|
---|
151 | }
|
---|