View Javadoc
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.NotReadyException;
20  
21  /**
22   * Abstract class to find function minima. Implementations will take into account whether the function is single or
23   * multi-dimension, and will use different algorithms to find minima.
24   */
25  public abstract class Optimizer {
26  
27      /**
28       * Boolean indicating whether this instance is locked because computations are being done.
29       */
30      protected boolean locked;
31  
32      /**
33       * Listener to handle minimization events.
34       */
35      protected OnIterationCompletedListener iterationCompletedListener;
36  
37      /**
38       * Empty constructor.
39       */
40      protected Optimizer() {
41          locked = false;
42      }
43  
44      /**
45       * Gets listener to handle minimization events.
46       *
47       * @return listener to handle minimization events.
48       */
49      public OnIterationCompletedListener getOnIterationCompletedListener() {
50          return iterationCompletedListener;
51      }
52  
53      /**
54       * Sets listener to handle minimization events.
55       *
56       * @param iterationCompletedListener listener to handle minimization events.
57       * @throws LockedException Raised if this instance is locked, because estimation is being computed.
58       */
59      public void setOnIterationCompletedListener(final OnIterationCompletedListener iterationCompletedListener)
60              throws LockedException {
61          if (locked) {
62              throw new LockedException();
63          }
64          this.iterationCompletedListener = iterationCompletedListener;
65      }
66  
67      /**
68       * Returns boolean indicating whether this instance is locked. This instance will be locked while computations are being done. Attempting
69       * to change any parameter while this instance is locked will raise a LockedException.
70       *
71       * @return True if this instance is locked, false otherwise.
72       */
73      public boolean isLocked() {
74          return locked;
75      }
76  
77      /**
78       * This function estimates a function minimum. Implementations of this class will usually search a local minimum within a bracket of input
79       * values. Because this is an abstract class, this method is meant to be overridden, otherwise a NotReadyException will always be thrown.
80       *
81       * @throws LockedException       Raised if this instance is locked, because estimation is being computed.
82       * @throws NotReadyException     Raised if this instance is not ready, usually because listener has not yet been provided.
83       * @throws OptimizationException Raised if the algorithm failed because of lack of convergence or because function couldn't be
84       *                               evaluated.
85       */
86      public void minimize() throws LockedException, NotReadyException, OptimizationException {
87          throw new NotReadyException();
88      }
89  
90      /**
91       * Returns boolean indicating whether this instance is ready. Usually an instance will be ready once its listener has been provided.
92       * Because this is an abstract class, it will always return false;
93       *
94       * @return True if this instance is ready, false otherwise.
95       */
96      public boolean isReady() {
97          return false;
98      }
99  }