StandardLinearProgramConverter implements LinearProgramConverter<E> { @Override public LinearProgram<E> convert(LinearProgram<E> original) { Objects.requireNonNull(original); if (original.isSlack()) { throw new IllegalArgumentException("Input is not a standard form linear program"); } final List<LinearProgramConstraint<E>> constraints = new ArrayList<>(); for (LinearProgramConstraint<E> constraint : original.getConstraints()) { if (ConstraintType.GREATER_THAN_OR_EQUAL_TO.equals(constraint.getConstraintType())) { final List<E> coefficients = new ArrayList<>(); for (int i = 0; i < constraint.size(); i++) { coefficients.add(NumberUtils.negate(constraint.getCoefficient(i))); } constraints.add(new DefaultLinearProgramConstraint<>(type, coefficients, ConstraintType.LESS_THAN_OR_EQUAL_TO, NumberUtils.negate(constraint.getValue()), NumberUtils.negate(constraint.getOffset()))); } else { constraints.add(constraint); } } return new DefaultLinearProgram<>(constraints, original.getObjective()); } StandardLinearProgramConverter(Class<E> type); @Override LinearProgram<E> convert(LinearProgram<E> original);  }
@Test public void testThatSlacknessIsPreserved() throws Exception { final LinearProgram<Integer> program = createProgram(); assertThat(program, is(not(slack()))); final LinearProgram<Integer> converted = converter.convert(program); assertThat(converted, is(notNullValue())); assertThat(converted, is(not(slack()))); }
@Test public void testThatNumberOfVariablesIrPreserved() throws Exception { final LinearProgram<Integer> program = createProgram(); final LinearProgram<Integer> converted = converter.convert(program); assertThat(converted, is(notNullValue())); assertThat(converted, hasVariables(program.variables())); }
@Test public void testAllConstraintsAreLessThan() throws Exception { final LinearProgram<Integer> program = createProgram(); final LinearProgram<Integer> converted = converter.convert(program); assertThat(converted, is(notNullValue())); for (LinearProgramConstraint<Integer> constraint : converted.getConstraints()) { assertThat(constraint.getConstraintType(), is(ConstraintType.LESS_THAN_OR_EQUAL_TO)); } }
@Test public void testWhenASignIsInvertedVariablesAreNegated() throws Exception { final LinearProgram<Integer> program = LinearProgramBuilder.withObjective(1, 2, 3) .when(4, 5, 6).isGreaterThan(-4).maximize(); final LinearProgram<Integer> converted = converter.convert(program); assertThat(converted, is(notNullValue())); final LinearProgramConstraint<Integer> constraint = converted.getConstraints().iterator().next(); assertThat(constraint, is(notNullValue())); assertThat(constraint.getConstraintType(), is(ConstraintType.LESS_THAN_OR_EQUAL_TO)); assertThat(constraint.getValue(), is(4)); assertThat(constraint.getCoefficient(0), is(-4)); assertThat(constraint.getCoefficient(1), is(-5)); assertThat(constraint.getCoefficient(2), is(-6)); }