MOSEK from Java with ojAlgo

MOSEK is a high-performance commercial solver, best known for its interior-point method: it is particularly strong on large continuous convex models — LP, QP and quadratically constrained (QCQP) — and on conic optimisation. Through ojAlgo it solves LP, QP and convex QCQP models and their mixed-integer versions. ojAlgo is an open source, pure Java library for mathematical optimisation, with a modelling API, ExpressionsBasedModel. The integration module ojalgo-mosek lets models built with ojAlgo be solved by MOSEK — whether you already use ojAlgo and want MOSEK's solving power, or are looking for a convenient way to use MOSEK from Java.

Already using ojAlgo: upgrade to MOSEK

Switching to MOSEK takes one dependency and one line of code. You need a MOSEK licence. Then:

  1. Add the dependency. ojalgo-mosek brings MOSEK's own Java package from Maven Central, native libraries included, so there is nothing else to install. Keep your existing ojAlgo dependency.

    <dependency>
        <groupId>org.ojalgo</groupId>
        <artifactId>ojalgo-mosek</artifactId>
        <version>4.0.0</version>
    </dependency>
  2. Register MOSEK once, at start-up:

    ExpressionsBasedModel.addIntegration(SolverMosek.INTEGRATION);

That is all. Your model code stays exactly as it is, and MOSEK now solves your models.

To try MOSEK on one model first, leave out the registration and call model.maximise(SolverMosek.INTEGRATION) (or minimise) on that model.

New to ojAlgo: MOSEK 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 17 or later and a MOSEK licence; MOSEK looks for mosek.lic in ~/mosek/, or wherever the MOSEKLM_LICENSE_FILE environment variable points. Add ojalgo-mosek. It brings both ojAlgo and MOSEK's Java package with it, and MOSEK's package includes the native libraries for Linux (x86-64 and ARM64), Windows (x86-64) and macOS (ARM64), so nothing needs installing or configuring:

<dependency>
    <groupId>org.ojalgo</groupId>
    <artifactId>ojalgo-mosek</artifactId>
    <version>4.0.0</version>
</dependency>

Write a model. A furniture maker decides how many chairs and tables to make. Each variable has bounds and a weight, its contribution to the objective (here the profit per unit); each expression with a limit is a constraint. 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.mosek.SolverMosek;

public class ProductionPlan {

    public static void main(final String[] args) {

        // Register MOSEK once; from then on it solves the models
        ExpressionsBasedModel.addIntegration(SolverMosek.INTEGRATION);

        ExpressionsBasedModel model = new ExpressionsBasedModel();

        // Whole units only; the weight is the profit per unit
        Variable chairs = model.newVariable("Chairs").lower(0).integer().weight(45);
        Variable tables = model.newVariable("Tables").lower(0).integer().weight(80);
        // Making any tables means running the table line, at a fixed cost of 900
        Variable tableLine = model.newVariable("TableLine").binary().weight(-900);

        model.newExpression("MachineHours").upper(400).set(chairs, 2).set(tables, 5);
        model.newExpression("Wood").upper(450).set(chairs, 4).set(tables, 3);
        model.newExpression("LineCapacity").upper(0).set(tables, 1).set(tableLine, -60);

        Optimisation.Result result = model.maximise();

        System.out.println(result.getState() + " " + result.getValue());
        System.out.println("Chairs: " + chairs.getValue().toPlainString());
        System.out.println("Tables: " + tables.getValue().toPlainString());
        System.out.println("TableLine: " + tableLine.getValue().toPlainString());
    }
}

Run it. No library path is needed. The result is OPTIMAL with a profit of 6475: 75 chairs and 50 tables, with the table line running. On recent JDKs, loading MOSEK's native library prints a warning about restricted methods; the JVM argument --enable-native-access=ALL-UNNAMED silences it.

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.

Versions

ojalgo-mosek is built and tested with MOSEK 11.2.6, and brings that version with it. It also 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>57.6.2</version>
</dependency>

Parameters

ojalgo-mosek passes these model options on to MOSEK. MOSEK's log is shown with model.options.debug(SolverMosek.class).

ojAlgo option (model.options) MOSEK parameter
time_suffice / time_abort (the shorter one) optimizer_max_time
parallelism(n) num_threads
feasibility, when changed from the default basis_tol_x, mio_tol_feas, mio_tol_abs_relax_int
the integer strategy's gap tolerance, when changed mio_tol_rel_gap, mio_tol_abs_gap

Any other MOSEK parameter is set with a configurator: extend SolverMosek.Configurator, override newTask, and register it.

import com.mosek.mosek.*;
import org.ojalgo.optimisation.Optimisation;
import org.ojalgo.optimisation.solver.mosek.SolverMosek;

public class MyMosekConfigurator extends SolverMosek.Configurator {

    @Override
    protected Task newTask(final Env environment, final int nbConstraints, final int nbVariables, final Optimisation.Options options) {
        Task task = super.newTask(environment, nbConstraints, nbVariables, options);
        task.putdouparam(dparam.mio_tol_rel_gap, 1e-6);
        return task;
    }
}

ExpressionsBasedModel.setConfigurator(new MyMosekConfigurator());

More