View Javadoc

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  
18  package org.apache.commons.math.estimation;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  /**
25   * Simple implementation of the {@link EstimationProblem
26   * EstimationProblem} interface for boilerplate data handling.
27   * <p>This class <em>only</em> handles parameters and measurements
28   * storage and unbound parameters filtering. It does not compute
29   * anything by itself. It should either be used with measurements
30   * implementation that are smart enough to know about the
31   * various parameters in order to compute the partial derivatives
32   * appropriately. Since the problem-specific logic is mainly related to
33   * the various measurements models, the simplest way to use this class
34   * is by extending it and using one internal class extending
35   * {@link WeightedMeasurement WeightedMeasurement} for each measurement
36   * type. The instances of the internal classes would have access to the
37   * various parameters and their current estimate.</p>
38  
39   * @version $Revision: 627989 $ $Date: 2008-02-15 03:04:02 -0700 (Fri, 15 Feb 2008) $
40   * @since 1.2
41  
42   */
43  public class SimpleEstimationProblem implements EstimationProblem {
44  
45      /**
46       * Build an empty instance without parameters nor measurements.
47       */
48      public SimpleEstimationProblem() {
49          parameters   = new ArrayList();
50          measurements = new ArrayList();
51      }
52  
53      /** 
54       * Get all the parameters of the problem.
55       * @return parameters
56       */
57      public EstimatedParameter[] getAllParameters() {
58          return (EstimatedParameter[]) parameters.toArray(new EstimatedParameter[parameters.size()]);
59      }
60  
61      /** 
62       * Get the unbound parameters of the problem.
63       * @return unbound parameters
64       */
65      public EstimatedParameter[] getUnboundParameters() {
66  
67          // filter the unbound parameters
68          List unbound = new ArrayList(parameters.size());
69          for (Iterator iterator = parameters.iterator(); iterator.hasNext();) {
70              EstimatedParameter p = (EstimatedParameter) iterator.next();
71              if (! p.isBound()) {
72                  unbound.add(p);
73              }
74          }
75  
76          // convert to an array
77          return (EstimatedParameter[]) unbound.toArray(new EstimatedParameter[unbound.size()]);
78          
79      }
80  
81      /** 
82       * Get the measurements of an estimation problem.
83       * @return measurements
84       */
85      public WeightedMeasurement[] getMeasurements() {
86          return (WeightedMeasurement[]) measurements.toArray(new WeightedMeasurement[measurements.size()]);
87      }
88  
89      /** Add a parameter to the problem.
90       * @param p parameter to add
91       */
92      protected void addParameter(EstimatedParameter p) {
93          parameters.add(p);
94      }
95  
96      /**
97       * Add a new measurement to the set.
98       * @param m measurement to add
99       */
100     protected void addMeasurement(WeightedMeasurement m) {
101         measurements.add(m);
102     }
103 
104     /** Estimated parameters. */
105     private final List parameters;
106 
107     /** Measurements. */
108     private final List measurements;
109 
110 }