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.NotAvailableException; 20 import com.irurueta.numerical.SingleDimensionFunctionEvaluatorListener; 21 22 /** 23 * Abstract class to find minima on single dimension functions. 24 * Single dimension functions are functions having a single parameter and 25 * returning a single scalar value, such as f(x). 26 * <p> 27 * Subclasses of this class will implement specific methods to find function 28 * minima. 29 */ 30 public abstract class SingleOptimizer extends Optimizer { 31 32 /** 33 * Listener to evaluate single dimension functions. 34 */ 35 protected SingleDimensionFunctionEvaluatorListener listener; 36 37 /** 38 * Value where minimum has been found. 39 */ 40 protected double xmin; 41 42 /** 43 * Function evaluation at minimum that has been found. 44 */ 45 protected double fmin; 46 47 /** 48 * Boolean indicating whether a minimum has been found and is available for 49 * retrieval. 50 */ 51 protected boolean resultAvailable; 52 53 /** 54 * Empty constructor. 55 */ 56 protected SingleOptimizer() { 57 super(); 58 xmin = fmin = 0.0; 59 resultAvailable = false; 60 } 61 62 /** 63 * Constructor with listener. 64 * 65 * @param listener Listener to evaluate a single dimension function where 66 * minima is meant to be found. 67 */ 68 protected SingleOptimizer(final SingleDimensionFunctionEvaluatorListener listener) { 69 super(); 70 this.listener = listener; 71 xmin = fmin = 0.0; 72 resultAvailable = false; 73 } 74 75 /** 76 * Returns listener to evaluate a single dimension function. 77 * 78 * @return Listener to evaluate a single dimension function. 79 * @throws NotAvailableException Raised if a listener has not yet been 80 * provided. 81 */ 82 public SingleDimensionFunctionEvaluatorListener getListener() throws NotAvailableException { 83 if (!isListenerAvailable()) { 84 throw new NotAvailableException(); 85 } 86 87 return listener; 88 } 89 90 /** 91 * Sets listener. 92 * 93 * @param listener Listener to evaluate a single dimension function. 94 * @throws LockedException Raised if this instance is locked. 95 */ 96 public void setListener(final SingleDimensionFunctionEvaluatorListener listener) throws LockedException { 97 if (isLocked()) { 98 throw new LockedException(); 99 } 100 101 this.listener = listener; 102 } 103 104 /** 105 * Returns boolean indicating whether a listener has been provided and is 106 * available for retrieval. 107 * 108 * @return True if listener is available, false otherwise. 109 */ 110 public boolean isListenerAvailable() { 111 return listener != null; 112 } 113 114 /** 115 * Returns true if this instance is ready to start the minimum estimation, 116 * false otherwise. 117 * 118 * @return Boolean indicating whether this instance is ready to start the 119 * minimum estimation. 120 */ 121 @Override 122 public boolean isReady() { 123 return isListenerAvailable(); 124 } 125 126 /** 127 * Returns boolean indicating whether the estimated minimum is available for 128 * retrieval. 129 * 130 * @return True if result is available, false otherwise. 131 */ 132 public boolean isResultAvailable() { 133 return resultAvailable; 134 } 135 136 /** 137 * Returns value of the minimum that has been found. 138 * 139 * @return Value of the minimum that has been found. 140 * @throws NotAvailableException Raised if minimum is not yet available for 141 * retrieval. 142 */ 143 public double getResult() throws NotAvailableException { 144 if (!isResultAvailable()) { 145 throw new NotAvailableException(); 146 } 147 148 return xmin; 149 } 150 151 /** 152 * Returns function evaluation at minimum that has been found. 153 * 154 * @return Function evaluation at minimum that has been found. 155 * @throws NotAvailableException raised if result is not yet available for retrieval. 156 */ 157 public double getEvaluationAtResult() throws NotAvailableException { 158 if (!isResultAvailable()) { 159 throw new NotAvailableException(); 160 } 161 162 return fmin; 163 } 164 }