Example usage for org.apache.commons.math.optimization.linear LinearConstraint getCoefficients

List of usage examples for org.apache.commons.math.optimization.linear LinearConstraint getCoefficients

Introduction

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

Prototype

public RealVector getCoefficients() 

Source Link

Document

Get the coefficients of the constraint (left hand side).

Usage

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

public static String constraintToString(LinearConstraint c) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < c.getCoefficients().getData().length; i++) {
        if (i > 0) {
            sb.append(" + ");
        }/*from www.  j av  a 2 s. com*/
        sb.append(c.getCoefficients().getData()[i]);
        sb.append("*x");
        sb.append(i);
    }
    sb.append(" ");
    sb.append(c.getRelationship().toString());
    sb.append(" ");
    sb.append(c.getValue());
    return sb.toString();
}

From source file:com.polytech4A.cuttingstock.core.method.LinearResolutionMetodTest.java

@Test
public void testUpdateConstraints() throws NoSuchFieldException, IllegalAccessException {
    LinearResolutionMethod method = new LinearResolutionMethod(context);
    method.updateConstraints(solution);//from   w  w  w . j  av  a  2s  .  com
    // To avoid set attributes in public, we get the field function.
    Field field = method.getClass().getDeclaredField("constraints");
    field.setAccessible(true);
    ArrayList<LinearConstraint> constraints = (ArrayList<LinearConstraint>) field.get(method);
    // Test of value of each constraint's value.
    int i = 0;
    for (LinearConstraint c : constraints) {
        double[] coefficients = c.getCoefficients().getData();
        int j = 0;
        for (Pattern p : solution.getPatterns()) {
            Box b = p.getAmounts().get(i);
            assertEquals((double) p.getAmounts().get(i).getAmount(), coefficients[j]);
            j++;
        }
        i++;
        assertEquals(c.getRelationship(), Relationship.GEQ);
    }
}

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);
    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;//from ww w  .ja  v  a2 s.c om
            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);
}