Example usage for org.apache.commons.math.optimization.linear Relationship LEQ

List of usage examples for org.apache.commons.math.optimization.linear Relationship LEQ

Introduction

In this page you can find the example usage for org.apache.commons.math.optimization.linear Relationship LEQ.

Prototype

Relationship LEQ

To view the source code for org.apache.commons.math.optimization.linear Relationship LEQ.

Click Source Link

Document

Lesser than or equal relationship.

Usage

From source file:fi.smaa.libror.UTAGMSSolverTest.java

@Test
public void testBuildRORConstraints() {
    List<LinearConstraint> c = solver.buildRORConstraints();
    // constraint for the preferences
    LinearConstraint con1 = c.get(0);/* ww w  .  j  ava 2  s.  c o m*/
    assertArrayEquals(new double[] { 0.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, -1.0 },
            con1.getCoefficients().getData(), 0.001);
    assertEquals(Relationship.GEQ, con1.getRelationship());
    assertEquals(0.0, con1.getValue(), 0.0001);
    // constraints for the monotonicity
    int cIndex = 1;
    int cInIndex = 1;
    for (int i = 0; i < solver.getModel().getNrCriteria(); i++) {
        for (int j = 0; j < solver.getModel().getPerfMatrix().getLevels()[i].getDimension() - 1; j++) {
            LinearConstraint lc = c.get(cIndex);
            double[] vals = new double[8];
            vals[cInIndex - 1] = 1.0;
            vals[cInIndex] = -1.0;
            assertArrayEquals(vals, lc.getCoefficients().getData(), 0.0001);
            assertEquals(Relationship.LEQ, lc.getRelationship());
            cIndex++;
            cInIndex += 1;
        }
        cInIndex += 1;
    }
    // constraints for first level being 0
    int offset = 0;
    for (int i = 0; i < solver.getModel().getNrCriteria(); i++) {
        LinearConstraint lc = c.get(cIndex);
        assertEquals(Relationship.EQ, lc.getRelationship());
        assertEquals(0.0, lc.getValue(), 0.000001);
        double[] vals = new double[8];
        vals[offset] = 1.0;
        offset += solver.getModel().getPerfMatrix().getLevels()[i].getDimension();
        assertArrayEquals(vals, lc.getCoefficients().getData(), 0.00001);
        cIndex++;
    }
    // constraints for best levels summing to unity
    LinearConstraint lc = c.get(cIndex);
    cIndex++;
    assertEquals(Relationship.EQ, lc.getRelationship());
    assertEquals(1.0, lc.getValue(), 0.000001);

    double[] vals = new double[8];
    vals[1] = 1.0;
    vals[3] = 1.0;
    vals[6] = 1.0;
    assertArrayEquals(vals, lc.getCoefficients().getData(), 0.00001);
}

From source file:circdesigna.DesignSequenceConstraints.java

private void solveSimplex() {
    //Closest-To-Origin objective
    double[] ones = new double[Std.monomer.getNumMonomers()];
    for (int i = 0; i < ones.length; i++) {
        ones[i] = 1;/*from www.j  av a2s  . c  o  m*/
    }
    LinearObjectiveFunction f = new LinearObjectiveFunction(ones, 0);

    List<LinearConstraint> constraints = new ArrayList();
    for (Constraint d : maxConstituents) {
        if (d.constraintValue == -1) {
            continue;
        }
        double[] ei = new double[Std.monomer.getNumMonomers()];
        for (int i = 0; i < ei.length; i++) {
            if (d.regulates[i]) {
                ei[i] = 1;
            }
        }
        constraints.add(new LinearConstraint(ei, Relationship.LEQ, d.constraintValue));
    }
    for (Constraint d : minConstituents) {
        if (d.constraintValue == -1) {
            continue;
        }
        double[] ei = new double[Std.monomer.getNumMonomers()];
        for (int i = 0; i < ei.length; i++) {
            if (d.regulates[i]) {
                ei[i] = 1;
            }
        }
        constraints.add(new LinearConstraint(ei, Relationship.GEQ, d.constraintValue));
    }
    try {
        RealPointValuePair optimize = new SimplexSolver().optimize(f, constraints, GoalType.MINIMIZE, true);
        simplexSolution = optimize.getPoint();
        //System.out.println(Arrays.toString(simplexSolution));
    } catch (Throwable e) {
        throw new RuntimeException("Constraints are too strict: " + e.getMessage());
    }

}

From source file:fi.smaa.libror.UTAGMSSolver.java

private List<LinearConstraint> buildAllVariablesLessThan1Constraint() {
    List<LinearConstraint> con = new ArrayList<LinearConstraint>();
    for (int i = 0; i < getNrLPVariables(); i++) {
        double[] lhsVars = new double[getNrLPVariables()];
        lhsVars[i] = 1.0;/*w  w  w . java  2 s .  c om*/
        con.add(new LinearConstraint(lhsVars, Relationship.LEQ, 1.0));
    }
    return con;
}

From source file:fi.smaa.libror.UTAGMSSolver.java

private List<LinearConstraint> buildMonotonousConstraints(int critIndex) {
    List<LinearConstraint> constList = new ArrayList<LinearConstraint>();
    RealVector levels = model.getPerfMatrix().getLevels()[critIndex];
    for (int i = 0; i < levels.getDimension() - 1; i++) {
        double[] lhs = new double[getNrLPVariables()];
        double[] rhs = new double[getNrLPVariables()];
        lhs[getConstraintOffset(critIndex) + i] = 1.0;
        rhs[getConstraintOffset(critIndex) + i + 1] = 1.0;
        if (strictValueFunctions) {
            lhs[lhs.length - 1] = 1.0; // epsilon
        }//from  ww w .j a  v  a  2  s  .  c om
        constList.add(new LinearConstraint(lhs, 0.0, Relationship.LEQ, rhs, 0.0));
    }
    return constList;
}

From source file:org.rascalmpl.library.analysis.linearprogramming.LinearProgramming.java

private static Relationship convertConstraintType(IConstructor c) {
    if (c.getConstructorType() == ConstraintType_leq) {
        return Relationship.LEQ;
    } else if (c.getConstructorType() == ConstraintType_eq) {
        return Relationship.EQ;
    } else {//w w w  .  jav a  2s. com
        return Relationship.GEQ;
    }
}

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.;//from   www. j  av a 2  s.  c  om

            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.
*/// w ww  .j a  va2s . c  om
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;
}