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

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

Introduction

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

Prototype

public double getValue() 

Source Link

Document

Get the value of the constraint (right 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 ww  w .  ja v a 2  s  .  co  m*/
        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: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  w  ww  . ja  v a 2s. c o  m*/
            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);
}