1 /*
2 * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.numerical.optimization;
17
18 import com.irurueta.numerical.LockedException;
19 import com.irurueta.numerical.MultiDimensionFunctionEvaluatorListener;
20 import com.irurueta.numerical.NotAvailableException;
21
22 /**
23 * Abstract class to search for minima on multidimensional classes.
24 * A multidimensional class is one having several input parameters (usually
25 * provided as an array of values), and returning a single scalar value.
26 */
27 public abstract class MultiOptimizer extends Optimizer {
28 /**
29 * Listener to evaluate a multidimensional function.
30 */
31 protected MultiDimensionFunctionEvaluatorListener listener;
32
33 /**
34 * Minimum that was estimated.
35 */
36 protected double[] xmin;
37
38 /**
39 * Function value at estimated minimum.
40 */
41 protected double fmin;
42
43 /**
44 * Boolean indicating whether a minimum has already been found or not.
45 */
46 protected boolean resultAvailable;
47
48 /**
49 * Empty constructor.
50 */
51 protected MultiOptimizer() {
52 super();
53 listener = null;
54 xmin = null;
55 fmin = 0.0;
56 }
57
58 /**
59 * Constructor.
60 *
61 * @param listener Listener to evaluate a multidimensional function.
62 */
63 protected MultiOptimizer(final MultiDimensionFunctionEvaluatorListener listener) {
64 super();
65 this.listener = listener;
66 xmin = null;
67 fmin = 0.0;
68 }
69
70 /**
71 * Returns listener to evaluate a multidimensional function
72 *
73 * @return Listener to evaluate a multidimensional function.
74 * @throws NotAvailableException Raised if listener has not yet been
75 * provided and is not available for retrieval.
76 */
77 public MultiDimensionFunctionEvaluatorListener getListener() throws NotAvailableException {
78 if (!isListenerAvailable()) {
79 throw new NotAvailableException();
80 }
81 return listener;
82 }
83
84 /**
85 * Sets listener to evaluate a multidimensional function.
86 *
87 * @param listener Listener to evaluate a multidimensional function.
88 * @throws LockedException Raised if this instance is locked.
89 */
90 public void setListener(final MultiDimensionFunctionEvaluatorListener listener) throws LockedException {
91 if (isLocked()) {
92 throw new LockedException();
93 }
94 this.listener = listener;
95 }
96
97 /**
98 * Returns boolean indicating whether listener has been provided and is
99 * available for retrieval.
100 *
101 * @return True if available, false otherwise.
102 */
103 public boolean isListenerAvailable() {
104 return listener != null;
105 }
106
107 /**
108 * Returns boolean indicating whether this instance is ready to start the
109 * estimation of a minimum.
110 * Because this class is abstract, this method is meant to be overridden,
111 * otherwise false will always be returned.
112 *
113 * @return Boolean indicating whether this instance is ready.
114 */
115 @Override
116 public boolean isReady() {
117 return false;
118 }
119
120 /**
121 * Returns boolean indicating whether a minimum has been estimated and is
122 * available for retrieval.
123 *
124 * @return True if result is available, false otherwise.
125 */
126 public boolean isResultAvailable() {
127 return resultAvailable;
128 }
129
130 /**
131 * Returns minimum point that was found.
132 *
133 * @return Minimum point
134 * @throws NotAvailableException Raised if a minimum is not yet available
135 * for retrieval.
136 */
137 public double[] getResult() throws NotAvailableException {
138 if (!isResultAvailable()) {
139 throw new NotAvailableException();
140 }
141 return xmin;
142 }
143
144 /**
145 * Returns function evaluation at estimated minimum point.
146 *
147 * @return Function evaluation at minimum.
148 * @throws NotAvailableException Raised if a minimum is not yet available
149 * for retrieval.
150 */
151 public double getEvaluationAtResult() throws NotAvailableException {
152 if (!isResultAvailable()) {
153 throw new NotAvailableException();
154 }
155 return fmin;
156 }
157 }