Using the Proto-API for Quadratic Programming (in Java)

330 views
Skip to first unread message

Frank Leañez

unread,
Apr 12, 2021, 11:50:55 AM4/12/21
to or-tools...@googlegroups.com
Dear all,
I have searched for days how to use the Proto API from Java, but I haven't managed to make not even 1 basic instance work.
I compiled the latest released or-tools version (8.2) on Window 10 64bits without (apparent) problems. It installed ortools in the local maven repository and I can call it from my projects. I replicated the linear test sample from https://developers.google.com/optimization/lp/glop without problems. But since we are interested in solving QP problems, I guessed that using Proto is the only way to do it. But I can't create a constraint or objective function using MPConstraintProto or MPQuadraticObjective. It is stopped with a NoClassDefFoundError exception.

My QP Code is very simple:
//Max: x . y
//s.t: x+7y<17.5
public static void testQP() {
    Loader.loadNativeLibraries();
    final MPVariableProto x = MPVariableProto.newBuilder().setName("x").setLowerBound(0.0).setUpperBound(100.0).build();
    final MPVariableProto y = MPVariableProto.newBuilder().setName("y").setLowerBound(0.0).setUpperBound(100.0).build();
    final MPConstraintProto c = MPConstraintProto.newBuilder().setLowerBound(Double.NEGATIVE_INFINITY).setUpperBound(17.5d).addVarIndex(0).addCoefficient(1.0d).addVarIndex(1).addCoefficient(7.0d).build();
    final MPQuadraticObjective quadraticObjective = MPQuadraticObjective.newBuilder().addCoefficient(1.0).addQvar1Index(0).addQvar2Index(1).build();
    MPModelProto.Builder mpModelProto = MPModelProto.newBuilder().addVariable(x).addVariable(y).addConstraint(c).setQuadraticObjective(quadraticObjective).setMaximize(true);
    final MPModelRequest mpModelRequest = MPModelRequest.newBuilder().setEnableInternalSolverOutput(true).setSolverType(MPModelRequest.SolverType.SCIP_MIXED_INTEGER_PROGRAMMING).setModel(mpModelProto).build();
    MPSolutionResponse response = MPSolver.solveWithProto(mpModelRequest);
    System.out.println("Status = " + response.getStatusStr());
}

But it never passes the MPConstraintProto build line (it builds MPVariableProto though). Any ideas or suggestions or other examples on how to create QP problems in Java?
Thanks a lot,
Frank

Exception First time it runs:
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: com.google.ortools.linearsolver.MPConstraintProto.emptyIntList()Lcom/google/protobuf/Internal$IntList;
at com.google.ortools.linearsolver.MPConstraintProto.<init>(MPConstraintProto.java:33)
at com.google.ortools.linearsolver.MPConstraintProto.<clinit>(MPConstraintProto.java:1418)
at org.deepedit.solver.TestTemp.testQP(TestTemp.java:90)

Exception Second time it runs:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Could not initialize class com.google.ortools.linearsolver.MPConstraintProto
at org.deepedit.solver.TestTemp.testQP(TestTemp.java:90)

This little model is inspired in the (only) or-tools QP example I could find here:
https://github.com/ogunheper/google-or-tools-sample/blob/master/src/test/java/com/ortools/OrToolsDemoApplicationTest.java

Virus-free. www.avast.com
Reply all
Reply to author
Forward
0 new messages