Example usage for org.apache.commons.math.optimization GoalType MINIMIZE

List of usage examples for org.apache.commons.math.optimization GoalType MINIMIZE

Introduction

In this page you can find the example usage for org.apache.commons.math.optimization GoalType MINIMIZE.

Prototype

GoalType MINIMIZE

To view the source code for org.apache.commons.math.optimization GoalType MINIMIZE.

Click Source Link

Document

Minimization goal.

Usage

From source file:rb.app.RBnSCMCSystem.java

public double get_SCM_LB() {
    //return 0.01;

    double min_J_obj = 0.;
    double[] min_Jlocal_obj = new double[n_mubar];

    // Sort the indices of mu_bar based on distance from current_parameters
    List<Integer> sortedmubarIndices = getSorted_CJ_Indices(mu_bar);
    int icount = 0;
    //while ((min_J_obj<=0) && (icount < sortedmubarIndices.size())){
    while ((min_J_obj <= 0) && (icount < sortedmubarIndices.size())) {
        int imubar = sortedmubarIndices.get(icount);

        // First, declare the constraints
        Collection constraints = new ArrayList();

        // Add bounding box constraints for the get_Q_a() variables
        for (int q = 0; q < get_Q_a(); q++) {
            double[] index = new double[get_Q_a() * 2];
            index[q] = 1.;//  ww  w. j  a  va 2 s.  c o m

            constraints.add(new LinearConstraint(index, Relationship.GEQ, B_min[q] / beta_bar[imubar]));
            constraints.add(new LinearConstraint(index, Relationship.LEQ, B_max[q] / beta_bar[imubar]));

            index[q] = 0.;
            index[q + get_Q_a()] = 1.;

            constraints.add(new LinearConstraint(index, Relationship.GEQ, B_min[q] / beta_bar[imubar]));
            constraints.add(new LinearConstraint(index, Relationship.LEQ, B_max[q] / beta_bar[imubar]));
        }

        // Save the current_parameters since we'll change them in the loop below
        save_current_parameters();

        // Add the constraint rows
        if (n_muhat[imubar] > 0) {
            for (int imuhat = 0; imuhat < n_muhat[imubar]; imuhat++) {
                current_parameters = mu_hat[imubar].get(imuhat);

                double[] constraint_row = new double[get_Q_a() * 2];
                for (int q = 0; q < get_Q_a(); q++) {
                    Complex theta_q_a = complex_eval_theta_q_a(q);
                    constraint_row[q] = theta_q_a.getReal() * beta_bar[imubar];
                    constraint_row[q + get_Q_a()] = theta_q_a.getImaginary() * beta_bar[imubar];
                }

                constraints
                        .add(new LinearConstraint(constraint_row, Relationship.GEQ, beta_hat[imubar][imuhat]));
            }
        }

        // Now load the original parameters back into current_parameters
        // in order to set the coefficients of the objective function
        reload_current_parameters();

        // Create objective function object
        double[] objectiveFn = new double[get_Q_a() * 2];
        for (int q = 0; q < get_Q_a(); q++) {
            Complex theta_q_a = complex_eval_theta_q_a(q);
            objectiveFn[q] = theta_q_a.getReal() * beta_bar[imubar];
            objectiveFn[q + get_Q_a()] = theta_q_a.getImaginary() * beta_bar[imubar];
        }
        LinearObjectiveFunction f = new LinearObjectiveFunction(objectiveFn, 0.);

        try {
            SimplexSolver solver = new SimplexSolver(1e-6);
            RealPointValuePair opt_pair = solver.optimize(f, constraints, GoalType.MINIMIZE, false);
            min_Jlocal_obj[icount] = opt_pair.getValue();
        } catch (OptimizationException e) {
            Log.e("RBSCMSYSTEM_TAG", "Optimal solution not found");
            e.printStackTrace();
        } catch (Exception e) {
            Log.e("RBSCMSYSTEM_TAG", "Exception occurred during SCM_LB calculation");
            e.printStackTrace();
        }

        min_J_obj = min_J_obj > min_Jlocal_obj[icount] ? min_J_obj : min_Jlocal_obj[icount];
        icount++;
    }
    return min_J_obj;
}

From source file:rb.app.RBSCMSystem.java

/**
* @return the SCM lower bound for the current parameters.
*//*www  .  j  av a  2 s .  co m*/
public double get_SCM_LB() {
    double min_J_obj = 0.;

    try {

        // First, declare the constraints
        Collection constraints = new ArrayList();

        // Add bounding box constraints for the get_Q_a() variables
        for (int q = 0; q < get_Q_a(); q++) {
            double[] index = new double[get_Q_a()];
            index[q] = 1.;

            constraints.add(new LinearConstraint(index, Relationship.GEQ, B_min[q]));
            constraints.add(new LinearConstraint(index, Relationship.LEQ, B_max[q]));
        }

        // Sort the indices of C_J based on distance from current_parameters
        List<Integer> sortedIndices = getSorted_CJ_Indices();

        // Save the current_parameters since we'll change them in the loop below
        save_current_parameters();

        // Add the constraint rows
        int n_rows = Math.min(SCM_M, C_J.size());
        int count = 1;

        if (n_rows > 0) {
            for (Iterator it = sortedIndices.iterator(); it.hasNext();) {
                Integer mu_index = (Integer) it.next();

                get_current_parameters_from_C_J(mu_index);
                // current_parameters = C_J.get(mu_index);

                double[] constraint_row = new double[get_Q_a()];
                for (int q = 0; q < get_Q_a(); q++) {
                    constraint_row[q] = eval_theta_q_a(q);
                }

                constraints.add(
                        new LinearConstraint(constraint_row, Relationship.GEQ, C_J_stability_vector[mu_index]));

                if (count >= n_rows)
                    break;

                count++;

            }
        }

        // Now load the original parameters back into current_parameters
        // in order to set the coefficients of the objective function
        reload_current_parameters();

        // Create objective function object
        double[] objectiveFn = new double[get_Q_a()];
        for (int q = 0; q < get_Q_a(); q++) {
            objectiveFn[q] = eval_theta_q_a(q);
        }
        LinearObjectiveFunction f = new LinearObjectiveFunction(objectiveFn, 0.);

        SimplexSolver solver = new SimplexSolver();
        RealPointValuePair opt_pair = solver.optimize(f, constraints, GoalType.MINIMIZE, false);
        min_J_obj = opt_pair.getValue();
    } catch (OptimizationException e) {
        Log.e("DEBUG_TAG", "Optimal solution not found");
        e.printStackTrace();
    } catch (Exception e) {
        Log.e("DEBUG_TAG", "Exception occurred during SCM_LB calculation");
        e.printStackTrace();
    }

    Log.d(DEBUG_TAG, "SCM val = " + min_J_obj);
    return min_J_obj;
}

From source file:uk.ac.diamond.scisoft.analysis.optimize.ApacheConjugateGradient.java

@Override
public void optimize(IDataset[] coords, IDataset data, final IFunction function) throws Exception {

    // pull out appropriate data from the inputs in a way that they can be used
    final int numCoords = coords.length;
    final DoubleDataset[] newCoords = new DoubleDataset[numCoords];
    for (int i = 0; i < numCoords; i++) {
        newCoords[i] = (DoubleDataset) DatasetUtils.convertToAbstractDataset(coords[i])
                .cast(AbstractDataset.FLOAT64);
    }//from   w  ww  . jav  a 2 s  . c  o  m

    final DoubleDataset values = (DoubleDataset) DatasetUtils.convertToAbstractDataset(data)
            .cast(AbstractDataset.FLOAT64);

    NonLinearConjugateGradientOptimizer cg = new NonLinearConjugateGradientOptimizer(
            ConjugateGradientFormula.POLAK_RIBIERE);

    //FIXME - this does not work very well for many cases.
    DifferentiableMultivariateRealFunction f = new DifferentiableMultivariateRealFunction() {

        @Override
        public double value(double[] arg0) throws FunctionEvaluationException, IllegalArgumentException {
            function.setParameterValues(arg0);
            return function.residual(true, values, newCoords);

        }

        @Override
        public MultivariateRealFunction partialDerivative(final int parameter) {

            MultivariateRealFunction result = new MultivariateRealFunction() {

                @Override
                public double value(double[] parameters)
                        throws FunctionEvaluationException, IllegalArgumentException {
                    double step = 0.1;
                    parameters[parameter] -= step;
                    function.setParameterValues(parameters);
                    double min = function.residual(true, values, newCoords);
                    parameters[parameter] += 2.0 * step;
                    function.setParameterValues(parameters);
                    double max = function.residual(true, values, newCoords);
                    return -((max - min) / (2.0 * step));
                }
            };

            return result;
        }

        @Override
        public MultivariateVectorialFunction gradient() {

            MultivariateVectorialFunction result = new MultivariateVectorialFunction() {

                @Override
                public double[] value(double[] parameters)
                        throws FunctionEvaluationException, IllegalArgumentException {
                    double[] result = new double[parameters.length];
                    for (int i = 0; i < parameters.length; i++) {
                        result[i] = partialDerivative(i).value(parameters);
                    }
                    return result;
                }
            };
            return result;
        }
    };

    double[] start = function.getParameterValues();

    RealPointValuePair result = cg.optimize(f, GoalType.MINIMIZE, start);
    function.setParameterValues(result.getPoint());
}

From source file:uk.ac.diamond.scisoft.analysis.optimize.ApacheMultiDirectional.java

@Override
public void optimize(IDataset[] coords, IDataset data, final IFunction function) throws Exception {

    // Pull out the data which is required from the inputs
    final int numCoords = coords.length;
    final DoubleDataset[] newCoords = new DoubleDataset[numCoords];
    for (int i = 0; i < numCoords; i++) {
        newCoords[i] = (DoubleDataset) DatasetUtils.convertToAbstractDataset(coords[i])
                .cast(AbstractDataset.FLOAT64);
    }/* w w w.java  2  s. c o m*/

    final DoubleDataset values = (DoubleDataset) DatasetUtils.convertToAbstractDataset(data)
            .cast(AbstractDataset.FLOAT64);

    // create an instance of the fitter
    MultiDirectional md = new MultiDirectional();

    // provide the fitting function which wrappers all the normal fitting functionality
    MultivariateRealFunction f1 = new MultivariateRealFunction() {

        @Override
        public double value(double[] arg0) throws FunctionEvaluationException, IllegalArgumentException {
            function.setParameterValues(arg0);
            return function.residual(true, values, newCoords);
        }
    };

    double[] start = function.getParameterValues();

    // preform the optimisation
    RealPointValuePair result = md.optimize(f1, GoalType.MINIMIZE, start);

    // set the input functions parameters to be the result before finishing.
    function.setParameterValues(result.getPoint());

}

From source file:uk.ac.diamond.scisoft.analysis.optimize.ApacheNelderMead.java

@Override
public void optimize(IDataset[] coords, IDataset data, final IFunction function) throws Exception {

    // Pull out the data which is required from the inputs
    final int numCoords = coords.length;
    final DoubleDataset[] newCoords = new DoubleDataset[numCoords];
    for (int i = 0; i < numCoords; i++) {
        newCoords[i] = (DoubleDataset) DatasetUtils.convertToAbstractDataset(coords[i])
                .cast(AbstractDataset.FLOAT64);
    }/*  www  . ja  v  a2  s .  co  m*/

    final DoubleDataset values = (DoubleDataset) DatasetUtils.convertToAbstractDataset(data)
            .cast(AbstractDataset.FLOAT64);

    // create an instance of the fitter
    NelderMead nm = new NelderMead();

    // provide the fitting function which wrappers all the normal fitting functionality
    MultivariateRealFunction f1 = new MultivariateRealFunction() {

        @Override
        public double value(double[] arg0) throws FunctionEvaluationException, IllegalArgumentException {
            function.setParameterValues(arg0);
            return function.residual(true, values, newCoords);
        }
    };

    // preform the optimisation
    double[] start = function.getParameterValues();

    RealPointValuePair result = nm.optimize(f1, GoalType.MINIMIZE, start);

    // set the input functions parameters to be the result before finishing.
    function.setParameterValues(result.getPoint());

}