COPT from Java with ojAlgo
COPT (Cardinal Optimizer) is a high-performance commercial solver from Cardinal Operations, known for its results in Hans Mittelmann's public benchmarks and for GPU-accelerated algorithms for very large models. Through ojAlgo it solves LP, QP and QCQP models and their mixed-integer versions, convex and non-convex. ojAlgo is an open source, pure Java library for mathematical optimisation, with a modelling API, ExpressionsBasedModel. The integration module ojalgo-copt lets models built with ojAlgo be solved by COPT — whether you already use ojAlgo and want COPT's solving power, or are looking for a convenient way to use COPT from Java.
Already using ojAlgo: upgrade to COPT
Switching to COPT takes two dependencies and one line of code. You need COPT installed and licensed. Then:
-
Add two dependencies:
ojalgo-copt, and COPT's own Java package,copt.jar, which comes with the COPT installation (details below). Keep your existing ojAlgo dependency.<dependency> <groupId>org.ojalgo</groupId> <artifactId>ojalgo-copt</artifactId> <version>1.2.0</version> </dependency> -
Register COPT once, at start-up:
ExpressionsBasedModel.addIntegration(SolverCOPT.INTEGRATION);
That is all. Your model code stays exactly as it is, and COPT now solves your models.
To try COPT on one model first, leave out the registration and call model.maximise(SolverCOPT.INTEGRATION) (or minimise) on that model.
New to ojAlgo: COPT from Java
ojAlgo is made for the Java ecosystem:
- One Maven dependency, pure Java, no further dependencies, MIT licensed. It works with Maven, Gradle and every other JVM build tool, and from Kotlin, Scala and any other JVM language.
- Models are plain Java objects. No modelling language, no model files, no code generation: variables and constraints are created in code, with your IDE's completion and refactoring, from your application's own data.
Set up. You need Java 11 or later, and COPT installed and licensed as described in COPT's installation guide; ojalgo-copt is built and tested with COPT 8.0.7. Add ojalgo-copt, which brings ojAlgo with it, and COPT's Java package, described below:
<dependency>
<groupId>org.ojalgo</groupId>
<artifactId>ojalgo-copt</artifactId>
<version>1.2.0</version>
</dependency>
Write a model. A furniture maker decides how many chairs and tables to make. Each variable has bounds, each expression with a limit is a constraint, and the objective sets each variable's contribution (here the profit per unit). The table line has a fixed cost, modelled with a binary variable.
import org.ojalgo.optimisation.ExpressionsBasedModel;
import org.ojalgo.optimisation.Optimisation;
import org.ojalgo.optimisation.Variable;
import org.ojalgo.optimisation.solver.copt.SolverCOPT;
public class ProductionPlan {
public static void main(final String[] args) {
// Register COPT once; from then on it solves the models
ExpressionsBasedModel.addIntegration(SolverCOPT.INTEGRATION);
ExpressionsBasedModel model = new ExpressionsBasedModel();
// Whole units only
Variable chairs = model.newIntegerVariable("Chairs").lower(0);
Variable tables = model.newIntegerVariable("Tables").lower(0);
// Making any tables means running the table line, at a fixed cost of 900
Variable tableLine = model.newBinaryVariable("TableLine");
model.newConstraint("MachineHours").upper(400).set(chairs, 2).set(tables, 5);
model.newConstraint("Wood").upper(450).set(chairs, 4).set(tables, 3);
model.newConstraint("LineCapacity").upper(0).set(tables, 1).set(tableLine, -60);
// The profit per unit, and the table line's fixed cost
model.getObjective().set(chairs, 45).set(tables, 80).set(tableLine, -900);
Optimisation.Result result = model.maximise();
System.out.println(result.getState() + " " + result.doubleValue());
System.out.println("Chairs: " + chairs.getValue().toPlainString());
System.out.println("Tables: " + tables.getValue().toPlainString());
System.out.println("TableLine: " + tableLine.getValue().toPlainString());
}
}
Run it with COPT's native libraries on the library path (-Djava.library.path=$COPT_HOME/lib). The result is OPTIMAL with a profit of 6475: 75 chairs and 50 tables, with the table line running.
Models can also be read from MPS or LP files with ExpressionsBasedModel.parse(file). The Optimisation Cookbook has complete models for common problem shapes — assignment, scheduling, bin packing, blending and more.
COPT's Java package
The COPT Java API, copt.jar, is part of the COPT installation and is found in $COPT_HOME/lib. It is not on Maven Central, and ojalgo-copt does not bring it with it. Install it in your local Maven repository (here with the COPT version ojalgo-copt is built with; use your own):
mvn install:install-file -Dfile=$COPT_HOME/lib/copt.jar \
-DgroupId=copt -DartifactId=copt -Dversion=8.0.7 -Dpackaging=jar
and add it to your project:
<dependency>
<groupId>copt</groupId>
<artifactId>copt</artifactId>
<version>8.0.7</version>
</dependency>
At run time the JVM must find COPT's native libraries, which are in the same directory: -Djava.library.path=$COPT_HOME/lib.
ojalgo-copt brings the ojAlgo version it was built with. ojAlgo is released more often, and a version of ojAlgo you declare yourself takes precedence:
<dependency>
<groupId>org.ojalgo</groupId>
<artifactId>ojalgo</artifactId>
<version>58.0.0</version>
</dependency>
Parameters
ojalgo-copt passes these model options on to COPT. COPT logging is switched off.
ojAlgo option (model.options) |
COPT parameter |
|---|---|
time_suffice / time_abort (the shorter one) |
TimeLimit |
parallelism(n) |
Threads |
feasibility, when changed from the default |
FeasTol, IntTol |
| the integer strategy's gap tolerance, when changed | RelGap, AbsGap |
Any other COPT parameter is set with a configurator: extend SolverCOPT.Configurator, override configure, and register it.
import copt.*;
import org.ojalgo.optimisation.Optimisation;
import org.ojalgo.optimisation.solver.copt.SolverCOPT;
public class MyCOPTConfigurator extends SolverCOPT.Configurator {
@Override
protected void configure(final copt.Model model, final Optimisation.Options options) {
super.configure(model, options);
try {
model.setIntParam(IntParam.Logging, 1);
model.setDblParam(DblParam.RelGap, 1e-6);
} catch (CoptException cause) {
throw new RuntimeException(cause);
}
}
}
ExpressionsBasedModel.setConfigurator(new MyCOPTConfigurator());
More
- Mathematical optimisation with ojAlgo
- Optimisation Cookbook: complete models for common problem shapes
- ojalgo-copt on Maven Central
- Questions and support: Support & Community