How do one solve linear programming problems with ojAlgo?

241 views Asked by At

I am trying to learn how to solve a linear programming problem and I want to use ojAlgo LinearSolver.

  1. I solve the equality constraints using a SolverTask and get a single feasible point. Nice!
  2. Then I solve what I believe is an identical problem using the LinearSolver with equality constraints it fail, saying problem is INFEASIBLE.
  3. Then I add inequality constraints designed to not affect solution, it also say it is INFEASIBLE. But now I also get a larger Result vector than expected.

My questions:

  1. How should I reformulate my problem to solve it using LinearSolver.newGeneralBuilder() with only equality constraints, while getting same solution as for the SolverTask?
  2. How should I reformulate my problem to solve it using LinearSolver.newGeneralBuilder() with equality and inequality constraints, while getting same solution as for the SolverTask?
  3. What are those extra Result values when adding inequalities?

I use ojAlgo 49.2.1, and get same result in 50.0.1.

public void testLinearProgramSolver() throws RecoverableCondition {
  Primitive64Store g = FACTORY.column(new double[]{1, 1, 1});
  Primitive64Store Ae = FACTORY.rows(new double[][] {
           { 1,   1, 1},
           {0.55, 1, 0},
           { 0,   1, 1},
  });
  Primitive64Store ce = FACTORY.column(new double[]{-1.0, 0.050000000000000044, 0});
  Primitive64Store Ai = FACTORY.rows(new double[][] {
           {1, 0, 0},
           {0, 1, 0},
           {0, 0, 1},
  });
  Primitive64Store ci = FACTORY.column(new double[]{-2, -1, -1});

// Using SolverTask to solve Ae*x + ce = 0
 SolverTask<Double> solver = SolverTask.PRIMITIVE.make(Ae, ce);
 MatrixStore<Double> X = solver.solve(Ae, ce.negate());
 MatrixStore<Double> residual = Ae.multiply(X).add(ce);
 double norm = residual.norm();

//  Using LinearSolver with equalities
  LinearSolver.GeneralBuilder builderEqualities = LinearSolver.newGeneralBuilder();
  LinearSolver linearProgram = builderEqualities
           .objective(g)
           .equalities(Ae, ce.negate())
           .build();
  Optimisation.Result result = linearProgram.solve();
  Primitive64Store x2 = FACTORY.column(result.toRawCopy1D());
  int maxDim2 = x2.getMaxDim();

//  Using LinearSolver with inequalities
  LinearSolver.GeneralBuilder builderInequalities = LinearSolver.newGeneralBuilder();
  LinearSolver linearProgram1 = builderInequalities
           .objective(g)
           .equalities(Ae, ce.negate())
           .inequalities(Ai, ci.negate())
           .build();
  Optimisation.Result result1 = linearProgram1.solve();
  Primitive64Store x1 = FACTORY.column(result1.toRawCopy1D());
  int maxDim1 = x1.getMaxDim();

}
1

There are 1 answers

0
apete On

1/2) You typically don't use an LP solver to solve full rank equation systems. There should be many possible solutions, and you want to find the best one.

  1. Those would be the slack variables from the inequalities. (You get the results directly from the solver, and it doesn't know how it was built.)

Try things with the ExpressionsBasedModel first.