SlackLinearProgramConverter implements LinearProgramConverter<E> { @Override public LinearProgram<E> convert(LinearProgram<E> original) { original = converter.convert(original); int slackness = 0; final List<LinearProgramConstraint<E>> constraints = new ArrayList<>(); for (LinearProgramConstraint<E> constraint : original.getConstraints()) { if (!constraint.isSlack()) { slackness ++; } } int index = 0; for (LinearProgramConstraint<E> constraint : original.getConstraints()) { final List<E> coefficients = new ArrayList<>(constraint.getCoefficients()); for (int i = 0; i < slackness; i++) { coefficients.add(NumberUtils.zero(type)); } if (!constraint.isSlack()) { coefficients.set(constraint.getCoefficients().size() + index, NumberUtils.one(type)); index ++; } final LinearProgramConstraint<E> substitute = new DefaultLinearProgramConstraint<>(type, coefficients, ConstraintType.EQUAL_TO, constraint.getValue(), constraint.getOffset()); constraints.add(substitute); } final List<E> objectiveCoefficients = new ArrayList<>(original.getObjective().getCoefficients()); for (int i = 0; i < slackness; i++) { objectiveCoefficients.add(NumberUtils.zero(type)); } final List<Integer> basicVariables = new ArrayList<>(); for (int i = original.variables(); i < original.variables() + slackness; i++) { basicVariables.add(i); } return new DefaultLinearProgram<>(constraints, new DefaultLinearFunction<>(type, objectiveCoefficients, original.getObjective().getOffset())); } SlackLinearProgramConverter(Class<E> type); @Override LinearProgram<E> convert(LinearProgram<E> original);  }
@Test public void testSlackness() throws Exception { final LinearProgram<Integer> program = program(); assertThat(program.isSlack(), is(false)); assertThat(program.getBasicVariables(), is(Matchers.<Integer>empty())); assertThat(program.variables(), is(3)); final LinearProgram<Integer> converted = converter.convert(program); assertThat(converted, is(notNullValue())); assertThat(converted.isSlack(), is(true)); assertThat(converted.getBasicVariables(), hasSize(3)); }
@Test public void testConversion() throws Exception { final LinearProgram<Integer> program = program(); final LinearProgram<Integer> converted = converter.convert(program); assertThat(converted.getObjective(), is(notNullValue())); assertThat(converted.getObjective().getCoefficients(), contains(2, -3, 3, 0, 0, 0)); assertThat(converted.getConstraints(), hasSize(3)); assertThat(converted.getConstraints().get(0), is(notNullValue())); assertThat(converted.getConstraints().get(0).isSlack(), is(true)); assertThat(converted.getConstraints().get(0).getConstraintType(), is(ConstraintType.EQUAL_TO)); assertThat(converted.getConstraints().get(0).getCoefficients(), contains(1, 1, -1, 1, 0, 0)); assertThat(converted.getConstraints().get(1), is(notNullValue())); assertThat(converted.getConstraints().get(1).isSlack(), is(true)); assertThat(converted.getConstraints().get(1).getConstraintType(), is(ConstraintType.EQUAL_TO)); assertThat(converted.getConstraints().get(1).getCoefficients(), contains(-1, -1, 1, 0, 1, 0)); assertThat(converted.getConstraints().get(2), is(notNullValue())); assertThat(converted.getConstraints().get(2).isSlack(), is(true)); assertThat(converted.getConstraints().get(2).getConstraintType(), is(ConstraintType.EQUAL_TO)); assertThat(converted.getConstraints().get(2).getCoefficients(), contains(1, -2, 2, 0, 0, 1)); }