commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1420684 [2/15] - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/exception/ main/java/org/apache/commons/math3/exception/util/ main/java/org/apache/commons/math3/fitting/ main/java/org/apache/commons/math3/optim/ main...
Date Wed, 12 Dec 2012 14:11:04 GMT
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseMultivariateOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseMultivariateOptimizer.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseMultivariateOptimizer.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseMultivariateOptimizer.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.optim.OptimizationData;
+import org.apache.commons.math3.optim.InitialGuess;
+import org.apache.commons.math3.optim.SimpleBounds;
+import org.apache.commons.math3.optim.ConvergenceChecker;
+import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.NumberIsTooSmallException;
+import org.apache.commons.math3.exception.NumberIsTooLargeException;
+
+/**
+ * Base class for implementing optimizers for multivariate functions.
+ * It contains the boiler-plate code for initial guess and bounds
+ * specifications.
+ * <em>It is not a "user" class.</em>
+ *
+ * @param <PAIR> Type of the point/value pair returned by the optimization
+ * algorithm.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public abstract class BaseMultivariateOptimizer<PAIR>
+    extends BaseOptimizer<PAIR> {
+    /** Initial guess. */
+    private double[] start;
+    /** Lower bounds. */
+    private double[] lowerBound;
+    /** Upper bounds. */
+    private double[] upperBound;
+
+    /**
+     * @param checker Convergence checker.
+     */
+    protected BaseMultivariateOptimizer(ConvergenceChecker<PAIR> checker) {
+        super(checker);
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @param optData Optimization data.
+     * The following data will be looked for:
+     * <ul>
+     *  <li>{@link MaxEval}</li>
+     *  <li>{@link InitialGuess}</li>
+     *  <li>{@link SimpleBounds}</li>
+     * </ul>
+     * @return {@inheritDoc}
+     */
+    @Override
+    public PAIR optimize(OptimizationData... optData) {
+        // Retrieve settings.
+        parseOptimizationData(optData);
+        // Check input consistency.
+        checkParameters();
+        // Perform optimization.
+        return super.optimize(optData);
+    }
+
+    /**
+     * Scans the list of (required and optional) optimization data that
+     * characterize the problem.
+     *
+     * @param optData Optimization data. The following data will be looked for:
+     * <ul>
+     *  <li>{@link InitialGuess}</li>
+     *  <li>{@link SimpleBounds}</li>
+     * </ul>
+     */
+    private void parseOptimizationData(OptimizationData... optData) {
+        // The existing values (as set by the previous call) are reused if
+        // not provided in the argument list.
+        for (OptimizationData data : optData) {
+            if (data instanceof InitialGuess) {
+                start = ((InitialGuess) data).getInitialGuess();
+                continue;
+            }
+            if (data instanceof SimpleBounds) {
+                final SimpleBounds bounds = (SimpleBounds) data;
+                lowerBound = bounds.getLower();
+                upperBound = bounds.getUpper();
+                continue;
+            }
+        }
+    }
+
+    /**
+     * Gets the initial guess.
+     *
+     * @return the initial guess, or {@code null} if not set.
+     */
+    public double[] getStartPoint() {
+        return start == null ? null : start.clone();
+    }
+    /**
+     * @return the lower bounds, or {@code null} if not set.
+     */
+    public double[] getLowerBound() {
+        return lowerBound == null ? null : lowerBound.clone();
+    }
+    /**
+     * @return the upper bounds, or {@code null} if not set.
+     */
+    public double[] getUpperBound() {
+        return upperBound == null ? null : upperBound.clone();
+    }
+
+    /**
+     * Check parameters consistency.
+     */
+    private void checkParameters() {
+        if (start != null) {
+            final int dim = start.length;
+            if (lowerBound != null) {
+                if (lowerBound.length != dim) {
+                    throw new DimensionMismatchException(lowerBound.length, dim);
+                }
+                for (int i = 0; i < dim; i++) {
+                    final double v = start[i];
+                    final double lo = lowerBound[i];
+                    if (v < lo) {
+                        throw new NumberIsTooSmallException(v, lo, true);
+                    }
+                }
+            }
+            if (upperBound != null) {
+                if (upperBound.length != dim) {
+                    throw new DimensionMismatchException(upperBound.length, dim);
+                }
+                for (int i = 0; i < dim; i++) {
+                    final double v = start[i];
+                    final double hi = upperBound[i];
+                    if (v > hi) {
+                        throw new NumberIsTooLargeException(v, hi, true);
+                    }
+                }
+            }
+        }
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseMultivariateOptimizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseOptimizer.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseOptimizer.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseOptimizer.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.util.Incrementor;
+import org.apache.commons.math3.optim.OptimizationData;
+import org.apache.commons.math3.optim.ConvergenceChecker;
+import org.apache.commons.math3.exception.TooManyEvaluationsException;
+import org.apache.commons.math3.exception.TooManyIterationsException;
+
+/**
+ * Base class for implementing optimizers.
+ * It contains the boiler-plate code for counting the number of evaluations
+ * of the objective function and the number of iterations of the algorithm,
+ * and storing the convergence checker.
+ * <em>It is not a "user" class.</em>
+ *
+ * @param <PAIR> Type of the point/value pair returned by the optimization
+ * algorithm.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public abstract class BaseOptimizer<PAIR> {
+    /** Evaluations counter. */
+    protected final Incrementor evaluations;
+    /** Iterations counter. */
+    protected final Incrementor iterations;
+    /** Convergence checker. */
+    private ConvergenceChecker<PAIR> checker;
+
+    /**
+     * @param checker Convergence checker.
+     */
+    protected BaseOptimizer(ConvergenceChecker<PAIR> checker) {
+        this.checker = checker;
+
+        evaluations = new Incrementor(0, new MaxEvalCallback());
+        iterations = new Incrementor(0, new MaxIterCallback());
+    }
+
+    /**
+     * Gets the maximal number of function evaluations.
+     *
+     * @return the maximal number of function evaluations.
+     */
+    public int getMaxEvaluations() {
+        return evaluations.getMaximalCount();
+    }
+
+    /**
+     * Gets the number of evaluations of the objective function.
+     * The number of evaluations corresponds to the last call to the
+     * {@code optimize} method. It is 0 if the method has not been
+     * called yet.
+     *
+     * @return the number of evaluations of the objective function.
+     */
+    public int getEvaluations() {
+        return evaluations.getCount();
+    }
+
+    /**
+     * Gets the maximal number of iterations.
+     *
+     * @return the maximal number of iterations.
+     */
+    public int getMaxIterations() {
+        return iterations.getMaximalCount();
+    }
+
+    /**
+     * Gets the number of iterations performed by the algorithm.
+     * The number iterations corresponds to the last call to the
+     * {@code optimize} method. It is 0 if the method has not been
+     * called yet.
+     *
+     * @return the number of evaluations of the objective function.
+     */
+    public int getIterations() {
+        return iterations.getCount();
+    }
+
+    /**
+     * Gets the convergence checker.
+     *
+     * @return the object used to check for convergence.
+     */
+    public ConvergenceChecker<PAIR> getConvergenceChecker() {
+        return checker;
+    }
+
+    /**
+     * Stores data and performs the optimization.
+     *
+     * @param optData Optimization data. The following data will be looked for:
+     * <ul>
+     *  <li>{@link MaxEval}</li>
+     *  <li>{@link MaxIter}</li>
+     * </ul>
+     * @return a point/value pair that satifies the convergence criteria.
+     * @throws TooManyEvaluationsException if the maximal number of
+     * evaluations is exceeded.
+     * @throws TooManyIterationsException if the maximal number of
+     * iterations is exceeded.
+     */
+    public PAIR optimize(OptimizationData... optData)
+        throws TooManyEvaluationsException,
+               TooManyIterationsException {
+        // Retrieve settings.
+        parseOptimizationData(optData);
+        // Reset counters.
+        evaluations.resetCount();
+        iterations.resetCount();
+        // Perform optimization.
+        return doOptimize();
+    }
+
+    /**
+     * Performs the bulk of the optimization algorithm.
+     *
+     * @return the point/value pair giving the optimal value of the
+     * objective function.
+     */
+    protected abstract PAIR doOptimize();
+
+    /**
+     * Increment the evaluation count.
+     *
+     * @throws TooManyEvaluationsException if the allowed evaluations
+     * have been exhausted.
+     */
+    protected void incrementEvaluationCount()
+        throws TooManyEvaluationsException {
+        evaluations.incrementCount();
+    }
+
+    /**
+     * Increment the iteration count.
+     *
+     * @throws TooManyIterationsException if the allowed iterations
+     * have been exhausted.
+     */
+    protected void incrementIterationCount()
+        throws TooManyIterationsException {
+        iterations.incrementCount();
+    }
+
+    /**
+     * Scans the list of (required and optional) optimization data that
+     * characterize the problem.
+     *
+     * @param optData Optimization data.
+     * The following data will be looked for:
+     * <ul>
+     *  <li>{@link MaxEval}</li>
+     *  <li>{@link MaxIter}</li>
+     * </ul>
+     */
+    private void parseOptimizationData(OptimizationData... optData) {
+        // The existing values (as set by the previous call) are reused if
+        // not provided in the argument list.
+        for (OptimizationData data : optData) {
+            if (data instanceof MaxEval) {
+                evaluations.setMaximalCount(((MaxEval) data).getMaxEval());
+                continue;
+            }
+            if (data instanceof MaxIter) {
+                iterations.setMaximalCount(((MaxIter) data).getMaxIter());
+                continue;
+            }
+        }
+    }
+
+    /**
+     * Defines the action to perform when reaching the maximum number
+     * of evaluations.
+     */
+    private static class MaxEvalCallback
+        implements  Incrementor.MaxCountExceededCallback {
+        /**
+         * {@inheritDoc}
+         * @throws TooManyEvaluationsException.
+         */
+        public void trigger(int max) {
+            throw new TooManyEvaluationsException(max);
+        }
+    }
+
+    /**
+     * Defines the action to perform when reaching the maximum number
+     * of evaluations.
+     */
+    private static class MaxIterCallback
+        implements Incrementor.MaxCountExceededCallback {
+        /**
+         * {@inheritDoc}
+         * @throws TooManyIterationsException.
+         */
+        public void trigger(int max) {
+            throw new TooManyIterationsException(max);
+        }
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/BaseOptimizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ConvergenceChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ConvergenceChecker.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ConvergenceChecker.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ConvergenceChecker.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optim;
+
+/**
+ * This interface specifies how to check if an optimization algorithm has
+ * converged.
+ * <br/>
+ * Deciding if convergence has been reached is a problem-dependent issue. The
+ * user should provide a class implementing this interface to allow the
+ * optimization algorithm to stop its search according to the problem at hand.
+ * <br/>
+ * For convenience, three implementations that fit simple needs are already
+ * provided: {@link SimpleValueChecker}, {@link SimpleVectorValueChecker} and
+ * {@link SimplePointChecker}. The first two consider that convergence is
+ * reached when the objective function value does not change much anymore, it
+ * does not use the point set at all.
+ * The third one considers that convergence is reached when the input point
+ * set does not change much anymore, it does not use objective function value
+ * at all.
+ *
+ * @param <PAIR> Type of the (point, objective value) pair.
+ *
+ * @see org.apache.commons.math3.optim.SimplePointChecker
+ * @see org.apache.commons.math3.optim.SimpleValueChecker
+ * @see org.apache.commons.math3.optim.SimpleVectorValueChecker
+ *
+ * @version $Id: ConvergenceChecker.java 1364392 2012-07-22 18:27:12Z tn $
+ * @since 3.0
+ */
+public interface ConvergenceChecker<PAIR> {
+    /**
+     * Check if the optimization algorithm has converged.
+     *
+     * @param iteration Current iteration.
+     * @param previous Best point in the previous iteration.
+     * @param current Best point in the current iteration.
+     * @return {@code true} if the algorithm is considered to have converged.
+     */
+    boolean converged(int iteration, PAIR previous, PAIR current);
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ConvergenceChecker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/GoalType.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/GoalType.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/GoalType.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/GoalType.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+/**
+ * Goal type for an optimization problem (minimization or maximization of
+ * a scalar function.
+ *
+ * @version $Id: GoalType.java 1364392 2012-07-22 18:27:12Z tn $
+ * @since 2.0
+ */
+public enum GoalType implements OptimizationData {
+    /** Maximization. */
+    MAXIMIZE,
+    /** Minimization. */
+    MINIMIZE
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/GoalType.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/InitialGuess.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/InitialGuess.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/InitialGuess.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/InitialGuess.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optim;
+
+/**
+ * Starting point (first guess) of the optimization procedure.
+ * <br/>
+ * Immutable class.
+ *
+ * @version $Id: InitialGuess.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 3.1
+ */
+public class InitialGuess implements OptimizationData {
+    /** Initial guess. */
+    private final double[] init;
+
+    /**
+     * @param startPoint Initial guess.
+     */
+    public InitialGuess(double[] startPoint) {
+        init = startPoint.clone();
+    }
+
+    /**
+     * Gets the initial guess.
+     *
+     * @return the initial guess.
+     */
+    public double[] getInitialGuess() {
+        return init.clone();
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/InitialGuess.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxEval.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxEval.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxEval.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxEval.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+
+/**
+ * Maximum number of evaluations of the function to be optimized.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public class MaxEval implements OptimizationData {
+    /** Allowed number of evalutations. */
+    private final int maxEval;
+
+    /**
+     * @param max Allowed number of evalutations.
+     * @throws NotStrictlyPositiveException if {@code max <= 0}.
+     */
+    public MaxEval(int max) {
+        if (max <= 0) {
+            throw new NotStrictlyPositiveException(max);
+        }
+
+        maxEval = max;
+    }
+
+    /**
+     * Gets the maximum number of evaluations.
+     *
+     * @return the allowed number of evaluations.
+     */
+    public int getMaxEval() {
+        return maxEval;
+    }
+
+    /**
+     * Factory method that creates instance of this class that represents
+     * a virtually unlimited number of evaluations.
+     *
+     * @return a new instance suitable for allowing {@link Integer#MAX_VALUE}
+     * evaluations.
+     */
+    public static MaxEval unlimited() {
+        return new MaxEval(Integer.MAX_VALUE);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxEval.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxIter.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxIter.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxIter.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxIter.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+
+/**
+ * Maximum number of iterations performed by an (iterative) algorithm.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public class MaxIter implements OptimizationData {
+    /** Allowed number of evalutations. */
+    private final int maxIter;
+
+    /**
+     * @param max Allowed number of iterations.
+     * @throws NotStrictlyPositiveException if {@code max <= 0}.
+     */
+    public MaxIter(int max) {
+        if (max <= 0) {
+            throw new NotStrictlyPositiveException(max);
+        }
+
+        maxIter = max;
+    }
+
+    /**
+     * Gets the maximum number of evaluations.
+     *
+     * @return the allowed number of evaluations.
+     */
+    public int getMaxIter() {
+        return maxIter;
+    }
+
+    /**
+     * Factory method that creates instance of this class that represents
+     * a virtually unlimited number of iterations.
+     *
+     * @return a new instance suitable for allowing {@link Integer#MAX_VALUE}
+     * evaluations.
+     */
+    public static MaxIter unlimited() {
+        return new MaxIter(Integer.MAX_VALUE);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/MaxIter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ObjectiveFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ObjectiveFunction.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ObjectiveFunction.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ObjectiveFunction.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.analysis.MultivariateFunction;
+
+/**
+ * Scalar function to be optimized.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public class ObjectiveFunction implements OptimizationData {
+    /** Function to be optimized. */
+    private final MultivariateFunction function;
+
+    /**
+     * @param f Function to be optimized.
+     */
+    public ObjectiveFunction(MultivariateFunction f) {
+        function = f;
+    }
+
+    /**
+     * Gets the function to be optimized.
+     *
+     * @return the objective function.
+     */
+    public MultivariateFunction getObjectiveFunction() {
+        return function;
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/ObjectiveFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/OptimizationData.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/OptimizationData.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/OptimizationData.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/OptimizationData.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+/**
+ * Marker interface.
+ * Implementations will provide functionality (optional or required) needed
+ * by the optimizers, and those will need to check the actual type of the
+ * arguments and perform the appropriate cast in order to access the data
+ * they need.
+ *
+ * @version $Id: OptimizationData.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 3.1
+ */
+public interface OptimizationData {}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/OptimizationData.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointValuePair.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointValuePair.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointValuePair.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointValuePair.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import java.io.Serializable;
+import org.apache.commons.math3.util.Pair;
+
+/**
+ * This class holds a point and the value of an objective function at
+ * that point.
+ *
+ * @see PointVectorValuePair
+ * @see org.apache.commons.math3.analysis.MultivariateFunction
+ * @version $Id: PointValuePair.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 3.0
+ */
+public class PointValuePair extends Pair<double[], Double> implements Serializable {
+    /** Serializable UID. */
+    private static final long serialVersionUID = 20120513L;
+
+    /**
+     * Builds a point/objective function value pair.
+     *
+     * @param point Point coordinates. This instance will store
+     * a copy of the array, not the array passed as argument.
+     * @param value Value of the objective function at the point.
+     */
+    public PointValuePair(final double[] point,
+                          final double value) {
+        this(point, value, true);
+    }
+
+    /**
+     * Builds a point/objective function value pair.
+     *
+     * @param point Point coordinates.
+     * @param value Value of the objective function at the point.
+     * @param copyArray if {@code true}, the input array will be copied,
+     * otherwise it will be referenced.
+     */
+    public PointValuePair(final double[] point,
+                          final double value,
+                          final boolean copyArray) {
+        super(copyArray ? ((point == null) ? null :
+                           point.clone()) :
+              point,
+              value);
+    }
+
+    /**
+     * Gets the point.
+     *
+     * @return a copy of the stored point.
+     */
+    public double[] getPoint() {
+        final double[] p = getKey();
+        return p == null ? null : p.clone();
+    }
+
+    /**
+     * Gets a reference to the point.
+     *
+     * @return a reference to the internal array storing the point.
+     */
+    public double[] getPointRef() {
+        return getKey();
+    }
+
+    /**
+     * Replace the instance with a data transfer object for serialization.
+     * @return data transfer object that will be serialized
+     */
+    private Object writeReplace() {
+        return new DataTransferObject(getKey(), getValue());
+    }
+
+    /** Internal class used only for serialization. */
+    private static class DataTransferObject implements Serializable {
+        /** Serializable UID. */
+        private static final long serialVersionUID = 20120513L;
+        /**
+         * Point coordinates.
+         * @Serial
+         */
+        private final double[] point;
+        /**
+         * Value of the objective function at the point.
+         * @Serial
+         */
+        private final double value;
+
+        /** Simple constructor.
+         * @param point Point coordinates.
+         * @param value Value of the objective function at the point.
+         */
+        public DataTransferObject(final double[] point, final double value) {
+            this.point = point.clone();
+            this.value = value;
+        }
+
+        /** Replace the deserialized data transfer object with a {@link PointValuePair}.
+         * @return replacement {@link PointValuePair}
+         */
+        private Object readResolve() {
+            return new PointValuePair(point, value, false);
+        }
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointValuePair.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointVectorValuePair.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointVectorValuePair.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointVectorValuePair.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointVectorValuePair.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import java.io.Serializable;
+import org.apache.commons.math3.util.Pair;
+
+/**
+ * This class holds a point and the vectorial value of an objective function at
+ * that point.
+ *
+ * @see PointValuePair
+ * @see org.apache.commons.math3.analysis.MultivariateVectorFunction
+ * @version $Id: PointVectorValuePair.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 3.0
+ */
+public class PointVectorValuePair extends Pair<double[], double[]> implements Serializable {
+    /** Serializable UID. */
+    private static final long serialVersionUID = 20120513L;
+
+    /**
+     * Builds a point/objective function value pair.
+     *
+     * @param point Point coordinates. This instance will store
+     * a copy of the array, not the array passed as argument.
+     * @param value Value of the objective function at the point.
+     */
+    public PointVectorValuePair(final double[] point,
+                                final double[] value) {
+        this(point, value, true);
+    }
+
+    /**
+     * Build a point/objective function value pair.
+     *
+     * @param point Point coordinates.
+     * @param value Value of the objective function at the point.
+     * @param copyArray if {@code true}, the input arrays will be copied,
+     * otherwise they will be referenced.
+     */
+    public PointVectorValuePair(final double[] point,
+                                final double[] value,
+                                final boolean copyArray) {
+        super(copyArray ?
+              ((point == null) ? null :
+               point.clone()) :
+              point,
+              copyArray ?
+              ((value == null) ? null :
+               value.clone()) :
+              value);
+    }
+
+    /**
+     * Gets the point.
+     *
+     * @return a copy of the stored point.
+     */
+    public double[] getPoint() {
+        final double[] p = getKey();
+        return p == null ? null : p.clone();
+    }
+
+    /**
+     * Gets a reference to the point.
+     *
+     * @return a reference to the internal array storing the point.
+     */
+    public double[] getPointRef() {
+        return getKey();
+    }
+
+    /**
+     * Gets the value of the objective function.
+     *
+     * @return a copy of the stored value of the objective function.
+     */
+    @Override
+    public double[] getValue() {
+        final double[] v = super.getValue();
+        return v == null ? null : v.clone();
+    }
+
+    /**
+     * Gets a reference to the value of the objective function.
+     *
+     * @return a reference to the internal array storing the value of
+     * the objective function.
+     */
+    public double[] getValueRef() {
+        return super.getValue();
+    }
+
+    /**
+     * Replace the instance with a data transfer object for serialization.
+     * @return data transfer object that will be serialized
+     */
+    private Object writeReplace() {
+        return new DataTransferObject(getKey(), getValue());
+    }
+
+    /** Internal class used only for serialization. */
+    private static class DataTransferObject implements Serializable {
+        /** Serializable UID. */
+        private static final long serialVersionUID = 20120513L;
+        /**
+         * Point coordinates.
+         * @Serial
+         */
+        private final double[] point;
+        /**
+         * Value of the objective function at the point.
+         * @Serial
+         */
+        private final double[] value;
+
+        /** Simple constructor.
+         * @param point Point coordinates.
+         * @param value Value of the objective function at the point.
+         */
+        public DataTransferObject(final double[] point, final double[] value) {
+            this.point = point.clone();
+            this.value = value.clone();
+        }
+
+        /** Replace the deserialized data transfer object with a {@link PointValuePair}.
+         * @return replacement {@link PointValuePair}
+         */
+        private Object readResolve() {
+            return new PointVectorValuePair(point, value, false);
+        }
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/PointVectorValuePair.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleBounds.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleBounds.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleBounds.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleBounds.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import java.util.Arrays;
+
+/**
+ * Simple optimization constraints: lower and upper bounds.
+ * The valid range of the parameters is an interval that can be infinite
+ * (in one or both directions).
+ * <br/>
+ * Immutable class.
+ *
+ * @version $Id: SimpleBounds.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 3.1
+ */
+public class SimpleBounds implements OptimizationData {
+    /** Lower bounds. */
+    private final double[] lower;
+    /** Upper bounds. */
+    private final double[] upper;
+
+    /**
+     * @param lB Lower bounds.
+     * @param uB Upper bounds.
+     */
+    public SimpleBounds(double[] lB,
+                        double[] uB) {
+        lower = lB.clone();
+        upper = uB.clone();
+    }
+
+    /**
+     * Gets the lower bounds.
+     *
+     * @return the lower bounds.
+     */
+    public double[] getLower() {
+        return lower.clone();
+    }
+    /**
+     * Gets the upper bounds.
+     *
+     * @return the upper bounds.
+     */
+    public double[] getUpper() {
+        return upper.clone();
+    }
+
+    /**
+     * Factory method that creates instance of this class that represents
+     * unbounded ranges.
+     *
+     * @param dim Number of parameters.
+     * @return a new instance suitable for passing to an optimizer that
+     * requires bounds specification.
+     */
+    public static SimpleBounds unbounded(int dim) {
+        final double[] lB = new double[dim];
+        Arrays.fill(lB, Double.NEGATIVE_INFINITY);
+        final double[] uB = new double[dim];
+        Arrays.fill(uB, Double.POSITIVE_INFINITY);
+
+        return new SimpleBounds(lB, uB);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleBounds.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimplePointChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimplePointChecker.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimplePointChecker.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimplePointChecker.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.Pair;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+
+/**
+ * Simple implementation of the {@link ConvergenceChecker} interface using
+ * only point coordinates.
+ *
+ * Convergence is considered to have been reached if either the relative
+ * difference between each point coordinate are smaller than a threshold
+ * or if either the absolute difference between the point coordinates are
+ * smaller than another threshold.
+ * <br/>
+ * The {@link #converged(int,Pair,Pair) converged} method will also return
+ * {@code true} if the number of iterations has been set (see
+ * {@link #SimplePointChecker(double,double,int) this constructor}).
+ *
+ * @param <PAIR> Type of the (point, value) pair.
+ * The type of the "value" part of the pair (not used by this class).
+ *
+ * @version $Id: SimplePointChecker.java 1413127 2012-11-24 04:37:30Z psteitz $
+ * @since 3.0
+ */
+public class SimplePointChecker<PAIR extends Pair<double[], ? extends Object>>
+    extends AbstractConvergenceChecker<PAIR> {
+    /**
+     * If {@link #maxIterationCount} is set to this value, the number of
+     * iterations will never cause {@link #converged(int, Pair, Pair)}
+     * to return {@code true}.
+     */
+    private static final int ITERATION_CHECK_DISABLED = -1;
+    /**
+     * Number of iterations after which the
+     * {@link #converged(int, Pair, Pair)} method
+     * will return true (unless the check is disabled).
+     */
+    private final int maxIterationCount;
+
+    /**
+     * Build an instance with specified thresholds.
+     * In order to perform only relative checks, the absolute tolerance
+     * must be set to a negative value. In order to perform only absolute
+     * checks, the relative tolerance must be set to a negative value.
+     *
+     * @param relativeThreshold relative tolerance threshold
+     * @param absoluteThreshold absolute tolerance threshold
+     */
+    public SimplePointChecker(final double relativeThreshold,
+                              final double absoluteThreshold) {
+        super(relativeThreshold, absoluteThreshold);
+        maxIterationCount = ITERATION_CHECK_DISABLED;
+    }
+
+    /**
+     * Builds an instance with specified thresholds.
+     * In order to perform only relative checks, the absolute tolerance
+     * must be set to a negative value. In order to perform only absolute
+     * checks, the relative tolerance must be set to a negative value.
+     *
+     * @param relativeThreshold Relative tolerance threshold.
+     * @param absoluteThreshold Absolute tolerance threshold.
+     * @param maxIter Maximum iteration count.
+     * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
+     *
+     * @since 3.1
+     */
+    public SimplePointChecker(final double relativeThreshold,
+                              final double absoluteThreshold,
+                              final int maxIter) {
+        super(relativeThreshold, absoluteThreshold);
+
+        if (maxIter <= 0) {
+            throw new NotStrictlyPositiveException(maxIter);
+        }
+        maxIterationCount = maxIter;
+    }
+
+    /**
+     * Check if the optimization algorithm has converged considering the
+     * last two points.
+     * This method may be called several times from the same algorithm
+     * iteration with different points. This can be detected by checking the
+     * iteration number at each call if needed. Each time this method is
+     * called, the previous and current point correspond to points with the
+     * same role at each iteration, so they can be compared. As an example,
+     * simplex-based algorithms call this method for all points of the simplex,
+     * not only for the best or worst ones.
+     *
+     * @param iteration Index of current iteration
+     * @param previous Best point in the previous iteration.
+     * @param current Best point in the current iteration.
+     * @return {@code true} if the arguments satify the convergence criterion.
+     */
+    @Override
+    public boolean converged(final int iteration,
+                             final PAIR previous,
+                             final PAIR current) {
+        if (maxIterationCount != ITERATION_CHECK_DISABLED) {
+            if (iteration >= maxIterationCount) {
+                return true;
+            }
+        }
+
+        final double[] p = previous.getKey();
+        final double[] c = current.getKey();
+        for (int i = 0; i < p.length; ++i) {
+            final double pi = p[i];
+            final double ci = c[i];
+            final double difference = FastMath.abs(pi - ci);
+            final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci));
+            if (difference > size * getRelativeThreshold() &&
+                difference > getAbsoluteThreshold()) {
+                return false;
+            }
+        }
+        return true;
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimplePointChecker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleValueChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleValueChecker.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleValueChecker.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleValueChecker.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+
+/**
+ * Simple implementation of the {@link ConvergenceChecker} interface using
+ * only objective function values.
+ *
+ * Convergence is considered to have been reached if either the relative
+ * difference between the objective function values is smaller than a
+ * threshold or if either the absolute difference between the objective
+ * function values is smaller than another threshold.
+ * <br/>
+ * The {@link #converged(int,PointValuePair,PointValuePair) converged}
+ * method will also return {@code true} if the number of iterations has been set
+ * (see {@link #SimpleValueChecker(double,double,int) this constructor}).
+ *
+ * @version $Id: SimpleValueChecker.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 3.0
+ */
+public class SimpleValueChecker
+    extends AbstractConvergenceChecker<PointValuePair> {
+    /**
+     * If {@link #maxIterationCount} is set to this value, the number of
+     * iterations will never cause
+     * {@link #converged(int,PointValuePair,PointValuePair)}
+     * to return {@code true}.
+     */
+    private static final int ITERATION_CHECK_DISABLED = -1;
+    /**
+     * Number of iterations after which the
+     * {@link #converged(int,PointValuePair,PointValuePair)} method
+     * will return true (unless the check is disabled).
+     */
+    private final int maxIterationCount;
+
+    /** Build an instance with specified thresholds.
+     *
+     * In order to perform only relative checks, the absolute tolerance
+     * must be set to a negative value. In order to perform only absolute
+     * checks, the relative tolerance must be set to a negative value.
+     *
+     * @param relativeThreshold relative tolerance threshold
+     * @param absoluteThreshold absolute tolerance threshold
+     */
+    public SimpleValueChecker(final double relativeThreshold,
+                              final double absoluteThreshold) {
+        super(relativeThreshold, absoluteThreshold);
+        maxIterationCount = ITERATION_CHECK_DISABLED;
+    }
+
+    /**
+     * Builds an instance with specified thresholds.
+     *
+     * In order to perform only relative checks, the absolute tolerance
+     * must be set to a negative value. In order to perform only absolute
+     * checks, the relative tolerance must be set to a negative value.
+     *
+     * @param relativeThreshold relative tolerance threshold
+     * @param absoluteThreshold absolute tolerance threshold
+     * @param maxIter Maximum iteration count.
+     * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
+     *
+     * @since 3.1
+     */
+    public SimpleValueChecker(final double relativeThreshold,
+                              final double absoluteThreshold,
+                              final int maxIter) {
+        super(relativeThreshold, absoluteThreshold);
+
+        if (maxIter <= 0) {
+            throw new NotStrictlyPositiveException(maxIter);
+        }
+        maxIterationCount = maxIter;
+    }
+
+    /**
+     * Check if the optimization algorithm has converged considering the
+     * last two points.
+     * This method may be called several time from the same algorithm
+     * iteration with different points. This can be detected by checking the
+     * iteration number at each call if needed. Each time this method is
+     * called, the previous and current point correspond to points with the
+     * same role at each iteration, so they can be compared. As an example,
+     * simplex-based algorithms call this method for all points of the simplex,
+     * not only for the best or worst ones.
+     *
+     * @param iteration Index of current iteration
+     * @param previous Best point in the previous iteration.
+     * @param current Best point in the current iteration.
+     * @return {@code true} if the algorithm has converged.
+     */
+    @Override
+    public boolean converged(final int iteration,
+                             final PointValuePair previous,
+                             final PointValuePair current) {
+        if (maxIterationCount != ITERATION_CHECK_DISABLED) {
+            if (iteration >= maxIterationCount) {
+                return true;
+            }
+        }
+
+        final double p = previous.getValue();
+        final double c = current.getValue();
+        final double difference = FastMath.abs(p - c);
+        final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
+        return difference <= size * getRelativeThreshold() ||
+            difference <= getAbsoluteThreshold();
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleValueChecker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleVectorValueChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleVectorValueChecker.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleVectorValueChecker.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleVectorValueChecker.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+
+/**
+ * Simple implementation of the {@link ConvergenceChecker} interface using
+ * only objective function values.
+ *
+ * Convergence is considered to have been reached if either the relative
+ * difference between the objective function values is smaller than a
+ * threshold or if either the absolute difference between the objective
+ * function values is smaller than another threshold for all vectors elements.
+ * <br/>
+ * The {@link #converged(int,PointVectorValuePair,PointVectorValuePair) converged}
+ * method will also return {@code true} if the number of iterations has been set
+ * (see {@link #SimpleVectorValueChecker(double,double,int) this constructor}).
+ *
+ * @version $Id: SimpleVectorValueChecker.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 3.0
+ */
+public class SimpleVectorValueChecker
+    extends AbstractConvergenceChecker<PointVectorValuePair> {
+    /**
+     * If {@link #maxIterationCount} is set to this value, the number of
+     * iterations will never cause
+     * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)}
+     * to return {@code true}.
+     */
+    private static final int ITERATION_CHECK_DISABLED = -1;
+    /**
+     * Number of iterations after which the
+     * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)} method
+     * will return true (unless the check is disabled).
+     */
+    private final int maxIterationCount;
+
+    /**
+     * Build an instance with specified thresholds.
+     *
+     * In order to perform only relative checks, the absolute tolerance
+     * must be set to a negative value. In order to perform only absolute
+     * checks, the relative tolerance must be set to a negative value.
+     *
+     * @param relativeThreshold relative tolerance threshold
+     * @param absoluteThreshold absolute tolerance threshold
+     */
+    public SimpleVectorValueChecker(final double relativeThreshold,
+                                    final double absoluteThreshold) {
+        super(relativeThreshold, absoluteThreshold);
+        maxIterationCount = ITERATION_CHECK_DISABLED;
+    }
+
+    /**
+     * Builds an instance with specified tolerance thresholds and
+     * iteration count.
+     *
+     * In order to perform only relative checks, the absolute tolerance
+     * must be set to a negative value. In order to perform only absolute
+     * checks, the relative tolerance must be set to a negative value.
+     *
+     * @param relativeThreshold Relative tolerance threshold.
+     * @param absoluteThreshold Absolute tolerance threshold.
+     * @param maxIter Maximum iteration count.
+     * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
+     *
+     * @since 3.1
+     */
+    public SimpleVectorValueChecker(final double relativeThreshold,
+                                    final double absoluteThreshold,
+                                    final int maxIter) {
+        super(relativeThreshold, absoluteThreshold);
+
+        if (maxIter <= 0) {
+            throw new NotStrictlyPositiveException(maxIter);
+        }
+        maxIterationCount = maxIter;
+    }
+
+    /**
+     * Check if the optimization algorithm has converged considering the
+     * last two points.
+     * This method may be called several times from the same algorithm
+     * iteration with different points. This can be detected by checking the
+     * iteration number at each call if needed. Each time this method is
+     * called, the previous and current point correspond to points with the
+     * same role at each iteration, so they can be compared. As an example,
+     * simplex-based algorithms call this method for all points of the simplex,
+     * not only for the best or worst ones.
+     *
+     * @param iteration Index of current iteration
+     * @param previous Best point in the previous iteration.
+     * @param current Best point in the current iteration.
+     * @return {@code true} if the arguments satify the convergence criterion.
+     */
+    @Override
+    public boolean converged(final int iteration,
+                             final PointVectorValuePair previous,
+                             final PointVectorValuePair current) {
+        if (maxIterationCount != ITERATION_CHECK_DISABLED) {
+            if (iteration >= maxIterationCount) {
+                return true;
+            }
+        }
+
+        final double[] p = previous.getValueRef();
+        final double[] c = current.getValueRef();
+        for (int i = 0; i < p.length; ++i) {
+            final double pi         = p[i];
+            final double ci         = c[i];
+            final double difference = FastMath.abs(pi - ci);
+            final double size       = FastMath.max(FastMath.abs(pi), FastMath.abs(ci));
+            if (difference > size * getRelativeThreshold() &&
+                difference > getAbsoluteThreshold()) {
+                return false;
+            }
+        }
+        return true;
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/SimpleVectorValueChecker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraint.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraint.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraint.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraint.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,229 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim.linear;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import org.apache.commons.math3.linear.MatrixUtils;
+import org.apache.commons.math3.linear.RealVector;
+import org.apache.commons.math3.linear.ArrayRealVector;
+
+/**
+ * A linear constraint for a linear optimization problem.
+ * <p>
+ * A linear constraint has one of the forms:
+ * <ul>
+ *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
+ *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
+ *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
+ *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
+ *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+ *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
+ *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+ *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
+ *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+ * </ul>
+ * The c<sub>i</sub>, l<sub>i</sub> or r<sub>i</sub> are the coefficients of the constraints, the x<sub>i</sub>
+ * are the coordinates of the current point and v is the value of the constraint.
+ * </p>
+ *
+ * @version $Id: LinearConstraint.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 2.0
+ */
+public class LinearConstraint implements Serializable {
+    /** Serializable version identifier. */
+    private static final long serialVersionUID = -764632794033034092L;
+    /** Coefficients of the constraint (left hand side). */
+    private final transient RealVector coefficients;
+    /** Relationship between left and right hand sides (=, &lt;=, >=). */
+    private final Relationship relationship;
+    /** Value of the constraint (right hand side). */
+    private final double value;
+
+    /**
+     * Build a constraint involving a single linear equation.
+     * <p>
+     * A linear constraint with a single linear equation has one of the forms:
+     * <ul>
+     *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
+     *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
+     *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
+     * </ul>
+     * </p>
+     * @param coefficients The coefficients of the constraint (left hand side)
+     * @param relationship The type of (in)equality used in the constraint
+     * @param value The value of the constraint (right hand side)
+     */
+    public LinearConstraint(final double[] coefficients,
+                            final Relationship relationship,
+                            final double value) {
+        this(new ArrayRealVector(coefficients), relationship, value);
+    }
+
+    /**
+     * Build a constraint involving a single linear equation.
+     * <p>
+     * A linear constraint with a single linear equation has one of the forms:
+     * <ul>
+     *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
+     *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
+     *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
+     * </ul>
+     * </p>
+     * @param coefficients The coefficients of the constraint (left hand side)
+     * @param relationship The type of (in)equality used in the constraint
+     * @param value The value of the constraint (right hand side)
+     */
+    public LinearConstraint(final RealVector coefficients,
+                            final Relationship relationship,
+                            final double value) {
+        this.coefficients = coefficients;
+        this.relationship = relationship;
+        this.value        = value;
+    }
+
+    /**
+     * Build a constraint involving two linear equations.
+     * <p>
+     * A linear constraint with two linear equation has one of the forms:
+     * <ul>
+     *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
+     *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+     *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
+     *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+     *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
+     *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+     * </ul>
+     * </p>
+     * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint
+     * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint
+     * @param relationship The type of (in)equality used in the constraint
+     * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint
+     * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint
+     */
+    public LinearConstraint(final double[] lhsCoefficients, final double lhsConstant,
+                            final Relationship relationship,
+                            final double[] rhsCoefficients, final double rhsConstant) {
+        double[] sub = new double[lhsCoefficients.length];
+        for (int i = 0; i < sub.length; ++i) {
+            sub[i] = lhsCoefficients[i] - rhsCoefficients[i];
+        }
+        this.coefficients = new ArrayRealVector(sub, false);
+        this.relationship = relationship;
+        this.value        = rhsConstant - lhsConstant;
+    }
+
+    /**
+     * Build a constraint involving two linear equations.
+     * <p>
+     * A linear constraint with two linear equation has one of the forms:
+     * <ul>
+     *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
+     *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+     *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
+     *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+     *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
+     *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
+     * </ul>
+     * </p>
+     * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint
+     * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint
+     * @param relationship The type of (in)equality used in the constraint
+     * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint
+     * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint
+     */
+    public LinearConstraint(final RealVector lhsCoefficients, final double lhsConstant,
+                            final Relationship relationship,
+                            final RealVector rhsCoefficients, final double rhsConstant) {
+        this.coefficients = lhsCoefficients.subtract(rhsCoefficients);
+        this.relationship = relationship;
+        this.value        = rhsConstant - lhsConstant;
+    }
+
+    /**
+     * Gets the coefficients of the constraint (left hand side).
+     *
+     * @return the coefficients of the constraint (left hand side).
+     */
+    public RealVector getCoefficients() {
+        return coefficients;
+    }
+
+    /**
+     * Gets the relationship between left and right hand sides.
+     *
+     * @return the relationship between left and right hand sides.
+     */
+    public Relationship getRelationship() {
+        return relationship;
+    }
+
+    /**
+     * Gets the value of the constraint (right hand side).
+     *
+     * @return the value of the constraint (right hand side).
+     */
+    public double getValue() {
+        return value;
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+        if (other instanceof LinearConstraint) {
+            LinearConstraint rhs = (LinearConstraint) other;
+            return relationship == rhs.relationship &&
+                value == rhs.value &&
+                coefficients.equals(rhs.coefficients);
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return relationship.hashCode() ^
+            Double.valueOf(value).hashCode() ^
+            coefficients.hashCode();
+    }
+
+    /**
+     * Serialize the instance.
+     * @param oos stream where object should be written
+     * @throws IOException if object cannot be written to stream
+     */
+    private void writeObject(ObjectOutputStream oos)
+        throws IOException {
+        oos.defaultWriteObject();
+        MatrixUtils.serializeRealVector(coefficients, oos);
+    }
+
+    /**
+     * Deserialize the instance.
+     * @param ois stream from which the object should be read
+     * @throws ClassNotFoundException if a class in the stream cannot be found
+     * @throws IOException if object cannot be read from the stream
+     */
+    private void readObject(ObjectInputStream ois)
+      throws ClassNotFoundException, IOException {
+        ois.defaultReadObject();
+        MatrixUtils.deserializeRealVector(this, "coefficients", ois);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim.linear;
+
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collection;
+import java.util.Collections;
+import org.apache.commons.math3.optim.OptimizationData;
+
+/**
+ * Class that represents a set of {@link LinearConstraint linear constraints}.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public class LinearConstraintSet implements OptimizationData {
+    /** Set of constraints. */
+    private final Set<LinearConstraint> linearConstraints
+        = new HashSet<LinearConstraint>();
+
+    /**
+     * Creates a set containing the given constraints.
+     *
+     * @param constraints Constraints.
+     */
+    public LinearConstraintSet(LinearConstraint... constraints) {
+        for (LinearConstraint c : constraints) {
+            linearConstraints.add(c);
+        }
+    }
+
+    /**
+     * Creates a set containing the given constraints.
+     *
+     * @param constraints Constraints.
+     */
+    public LinearConstraintSet(Collection<LinearConstraint> constraints) {
+        linearConstraints.addAll(constraints);
+    }
+
+    /**
+     * Gets the set of linear constraints.
+     *
+     * @return the constraints.
+     */
+    public Collection<LinearConstraint> getConstraints() {
+        return Collections.unmodifiableSet(linearConstraints);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearObjectiveFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearObjectiveFunction.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearObjectiveFunction.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearObjectiveFunction.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim.linear;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import org.apache.commons.math3.analysis.MultivariateFunction;
+import org.apache.commons.math3.linear.MatrixUtils;
+import org.apache.commons.math3.linear.RealVector;
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.commons.math3.optim.OptimizationData;
+
+/**
+ * An objective function for a linear optimization problem.
+ * <p>
+ * A linear objective function has one the form:
+ * <pre>
+ * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
+ * </pre>
+ * The c<sub>i</sub> and d are the coefficients of the equation,
+ * the x<sub>i</sub> are the coordinates of the current point.
+ * </p>
+ *
+ * @version $Id: LinearObjectiveFunction.java 1416643 2012-12-03 19:37:14Z tn $
+ * @since 2.0
+ */
+public class LinearObjectiveFunction
+    implements MultivariateFunction,
+               OptimizationData,
+               Serializable {
+    /** Serializable version identifier. */
+    private static final long serialVersionUID = -4531815507568396090L;
+    /** Coefficients of the linear equation (c<sub>i</sub>). */
+    private final transient RealVector coefficients;
+    /** Constant term of the linear equation. */
+    private final double constantTerm;
+
+    /**
+     * @param coefficients Coefficients for the linear equation being optimized.
+     * @param constantTerm Constant term of the linear equation.
+     */
+    public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
+        this(new ArrayRealVector(coefficients), constantTerm);
+    }
+
+    /**
+     * @param coefficients Coefficients for the linear equation being optimized.
+     * @param constantTerm Constant term of the linear equation.
+     */
+    public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
+        this.coefficients = coefficients;
+        this.constantTerm = constantTerm;
+    }
+
+    /**
+     * Gets the coefficients of the linear equation being optimized.
+     *
+     * @return coefficients of the linear equation being optimized.
+     */
+    public RealVector getCoefficients() {
+        return coefficients;
+    }
+
+    /**
+     * Gets the constant of the linear equation being optimized.
+     *
+     * @return constant of the linear equation being optimized.
+     */
+    public double getConstantTerm() {
+        return constantTerm;
+    }
+
+    /**
+     * Computes the value of the linear equation at the current point.
+     *
+     * @param point Point at which linear equation must be evaluated.
+     * @return the value of the linear equation at the current point.
+     */
+    public double value(final double[] point) {
+        return value(new ArrayRealVector(point, false));
+    }
+
+    /**
+     * Computes the value of the linear equation at the current point.
+     *
+     * @param point Point at which linear equation must be evaluated.
+     * @return the value of the linear equation at the current point.
+     */
+    public double value(final RealVector point) {
+        return coefficients.dotProduct(point) + constantTerm;
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+        if (other instanceof LinearObjectiveFunction) {
+            LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
+          return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
+        }
+
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
+    }
+
+    /**
+     * Serialize the instance.
+     * @param oos stream where object should be written
+     * @throws IOException if object cannot be written to stream
+     */
+    private void writeObject(ObjectOutputStream oos)
+        throws IOException {
+        oos.defaultWriteObject();
+        MatrixUtils.serializeRealVector(coefficients, oos);
+    }
+
+    /**
+     * Deserialize the instance.
+     * @param ois stream from which the object should be read
+     * @throws ClassNotFoundException if a class in the stream cannot be found
+     * @throws IOException if object cannot be read from the stream
+     */
+    private void readObject(ObjectInputStream ois)
+      throws ClassNotFoundException, IOException {
+        ois.defaultReadObject();
+        MatrixUtils.deserializeRealVector(this, "coefficients", ois);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearObjectiveFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message