Optimisation-as-a-Service

The main benefit with ojAlgo’s suite of mathematical optimisation solvers is that it’s open source pure Java. It allows to solve mathematical optimisation problems directly in the JVM – no dependencies, no license management, no native code libraries or service calls.

Another benefit is that if it, later, turns out the capabilities of a commercial native code solver are required; you can just plug it in. You do of course then need to install that software as well as pay for and manage the license, but you do not need to change any of the Java code.

Now Optimatika introduces another alternative – Optimisation-as-a-Service. It frees you from the burdon of setting up and maintaining a server running optimisation code. Everything required to use this service is already in ojAlgo (v52.0.0 and later).

Calling this service does not require dealing with JSON, XML or anything like that. Using the service is just a matter of configuring to use that “solver” rather than the usual ones.

The server running the service is packaged as a Docker image – easy to deploy anywhere. There is a test/demo service available. Below is example code that demonstrates how to use Optimatika’s Optimisation-as-a-Service. If you run the code the optimisation problem is solved by that test/demo service.

Example Code

OptimisationAsAService.java
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.ojalgo.OjAlgoUtils;
import org.ojalgo.netio.BasicLogger;

import se.optimatika.optimisation.service.client.OptClientV1;
import se.optimatika.optimisation.service.client.OptModel;
import se.optimatika.optimisation.service.client.OptResult;
import se.optimatika.optimisation.service.client.OptVariable;

/**
 * A program that shows how to use Optimatika's Optimisation-as-a-Service.
 *
 * @see https://www.ojalgo.org/2022/10/optimisation-as-a-service/
 */
public class OptimisationAsAService {

    public static void main(final String[] args) throws InterruptedException, ExecutionException {

        BasicLogger.debug();
        BasicLogger.debug(OptimisationAsAService.class);
        BasicLogger.debug(OjAlgoUtils.getTitle());
        BasicLogger.debug(OjAlgoUtils.getDate());
        BasicLogger.debug();

        /*
         * Create a client for the optimisation service.
         */

        OptClientV1 client = OptClientV1.newInstance("https://optimatika-boot-services-969062758986.europe-north1.run.app");
        // OptimisationClientV01 client = new OptimisationClientV01(URI.create("http://test-service.optimatika.se"));

        /*
         * Verify the service is up and running.
         */

        if (!client.isServiceAvailable()) {
            BasicLogger.error("Service is not available!");
            return;
        }

        BasicLogger.debug("Service environment: {}", client.getServiceEnvironment());

        /*
         * Build a simple model and solve it remotely.
         */

        OptModel model = client.newModel();

        OptVariable varA = model.newRealVariable("A").lower(0);
        OptVariable varB = model.newRealVariable("B").lower(0);

        model.newConstraint("SumIs2").set(varA, 1).set(varB, 1).level(2);

        model.objective().set(varA, 10).set(varB, -10);

        Future<OptResult> maximise = model.maximise();
        OptResult result = maximise.get();

        BasicLogger.debug("Maximised => feasible={}, value={}", result.isFeasible(), result.getValue());
        BasicLogger.debug("A={}, B={}", varA.doubleValue(), varB.doubleValue());

        Future<OptResult> minimise = model.minimise();
        result = minimise.get();

        BasicLogger.debug("Minimised => feasible={}, value={}", result.isFeasible(), result.getValue());
        BasicLogger.debug("A={}, B={}", varA.doubleValue(), varB.doubleValue());
    }

}

The service endpoint used in the example above is for test and demonstration purposes only. That means NOT for production use! It may be restricted or removed, at any time, without warning. At the time of writing this post it is fully functional and unrestricted (running on a low spec server). Most likely there will always be some kind of test service endpoint available, but that will be with limited capabilities. Optimisation-as-a-Service is a commercial product/service. If you’d like access to an unrestricted service instance for (private) production use, you should look at the Optimisation Service.