001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math.estimation;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    /**
024     * Simple implementation of the {@link EstimationProblem
025     * EstimationProblem} interface for boilerplate data handling.
026     * <p>This class <em>only</em> handles parameters and measurements
027     * storage and unbound parameters filtering. It does not compute
028     * anything by itself. It should either be used with measurements
029     * implementation that are smart enough to know about the
030     * various parameters in order to compute the partial derivatives
031     * appropriately. Since the problem-specific logic is mainly related to
032     * the various measurements models, the simplest way to use this class
033     * is by extending it and using one internal class extending
034     * {@link WeightedMeasurement WeightedMeasurement} for each measurement
035     * type. The instances of the internal classes would have access to the
036     * various parameters and their current estimate.</p>
037    
038     * @version $Revision: 670469 $ $Date: 2008-06-23 10:01:38 +0200 (lun, 23 jun 2008) $
039     * @since 1.2
040    
041     */
042    public class SimpleEstimationProblem implements EstimationProblem {
043    
044        /**
045         * Build an empty instance without parameters nor measurements.
046         */
047        public SimpleEstimationProblem() {
048            parameters   = new ArrayList<EstimatedParameter>();
049            measurements = new ArrayList<WeightedMeasurement>();
050        }
051    
052        /** 
053         * Get all the parameters of the problem.
054         * @return parameters
055         */
056        public EstimatedParameter[] getAllParameters() {
057            return (EstimatedParameter[]) parameters.toArray(new EstimatedParameter[parameters.size()]);
058        }
059    
060        /** 
061         * Get the unbound parameters of the problem.
062         * @return unbound parameters
063         */
064        public EstimatedParameter[] getUnboundParameters() {
065    
066            // filter the unbound parameters
067            List<EstimatedParameter> unbound = new ArrayList<EstimatedParameter>(parameters.size());
068            for (EstimatedParameter p : parameters) {
069                if (! p.isBound()) {
070                    unbound.add(p);
071                }
072            }
073    
074            // convert to an array
075            return (EstimatedParameter[]) unbound.toArray(new EstimatedParameter[unbound.size()]);
076            
077        }
078    
079        /** 
080         * Get the measurements of an estimation problem.
081         * @return measurements
082         */
083        public WeightedMeasurement[] getMeasurements() {
084            return (WeightedMeasurement[]) measurements.toArray(new WeightedMeasurement[measurements.size()]);
085        }
086    
087        /** Add a parameter to the problem.
088         * @param p parameter to add
089         */
090        protected void addParameter(EstimatedParameter p) {
091            parameters.add(p);
092        }
093    
094        /**
095         * Add a new measurement to the set.
096         * @param m measurement to add
097         */
098        protected void addMeasurement(WeightedMeasurement m) {
099            measurements.add(m);
100        }
101    
102        /** Estimated parameters. */
103        private final List<EstimatedParameter> parameters;
104    
105        /** Measurements. */
106        private final List<WeightedMeasurement> measurements;
107    
108    }