Convex Optimization Examples#
These examples show the Java modeling patterns for LP and QP problems. They
assume the Java module has been compiled as described in
Java Quickstart Guide and that the application can load libcuopt_jni.
Simple Linear Programming#
The high-level API uses fluent expressions and explicit comparison methods.
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5import com.nvidia.cuopt.mathematicaloptimization.*;
6
7public class SimpleLp {
8 public static void main(String[] args) throws Exception {
9 try (Problem problem = new Problem("simple-lp")) {
10 Variable x = problem.addVariable(
11 0.0, Double.POSITIVE_INFINITY, 1.0,
12 VariableType.CONTINUOUS, "x");
13 Variable y = problem.addVariable(
14 0.0, Double.POSITIVE_INFINITY, 1.0,
15 VariableType.CONTINUOUS, "y");
16
17 problem.addConstraint(
18 LinearExpression.of(x).plus(y).ge(10.0), "demand");
19 problem.setObjective(
20 LinearExpression.of(x).plus(y), ObjectiveSense.MINIMIZE);
21
22 try (SolverSettings settings = new SolverSettings()
23 .setSetting(CuOptConstants.CUOPT_METHOD, SolverMethod.PDLP.nativeValue());
24 Solution solution = problem.solve(settings)) {
25 System.out.println("Status: " + solution.getTerminationStatus());
26 System.out.println("x = " + x.getValue());
27 System.out.println("y = " + y.getValue());
28 System.out.println("Objective = " + solution.getPrimalObjective());
29 }
30 }
31 }
32}
Example Response:
Status: OPTIMAL
x = 0.0
y = 10.0
Objective = 10.0
Problem.solve populates the Variable and Constraint objects after
the solve. The solution object remains available for detailed native results
and statistics.
Simple Quadratic Programming#
Quadratic objectives combine quadratic, linear, and constant terms:
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5import com.nvidia.cuopt.mathematicaloptimization.*;
6
7public class SimpleQp {
8 public static void main(String[] args) throws Exception {
9 try (Problem problem = new Problem("simple-qp")) {
10 Variable x = problem.addVariable(0.0, 10.0, 0.0, VariableType.CONTINUOUS, "x");
11 Variable y = problem.addVariable(0.0, 10.0, 0.0, VariableType.CONTINUOUS, "y");
12
13 QuadraticExpression objective = QuadraticExpression
14 .of(x, x, 1.0)
15 .plus(y, y, 1.0)
16 .plus(LinearExpression.of(x).times(-1.0))
17 .plus(LinearExpression.of(y).times(-1.0));
18
19 problem.addConstraint(
20 LinearExpression.of(x).plus(y).eq(1.0), "sum");
21 problem.setObjective(objective, ObjectiveSense.MINIMIZE);
22
23 try (Solution solution = problem.solve()) {
24 System.out.println("x = " + x.getValue());
25 System.out.println("y = " + y.getValue());
26 System.out.println("Objective = " + solution.getPrimalObjective());
27 }
28 }
29 }
30}
Example Response:
x = 0.5
y = 0.5
Objective = -0.5
For QP solutions, getDualObjective is available when the solver returns it,
and variable and constraint values are read from the model through
Variable.getValue, Variable.getReducedCost, and
Constraint.getDualValue.
Quadratic Constraints#
Quadratic constraints can be added directly to a Problem. As of this
writing, cuOptCreateProblem requires at least one linear constraint row,
so a purely quadratically-constrained model needs a (possibly non-binding)
linear constraint too:
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5import com.nvidia.cuopt.mathematicaloptimization.*;
6
7public class QuadraticConstraint {
8 public static void main(String[] args) throws Exception {
9 try (Problem problem = new Problem("quadratic-constraint")) {
10 Variable x = problem.addVariable(0.0, 10.0, 1.0, VariableType.CONTINUOUS, "x");
11 Variable y = problem.addVariable(0.0, 10.0, 1.0, VariableType.CONTINUOUS, "y");
12
13 // cuOptCreateProblem currently requires at least one linear constraint row.
14 problem.addConstraint(LinearExpression.of(x).plus(y).le(15.0), "budget");
15
16 QuadraticExpression radius = QuadraticExpression
17 .of(x, x, 1.0)
18 .plus(y, y, 1.0);
19 problem.addConstraint(radius.le(4.0), "radius");
20 problem.setObjective(
21 LinearExpression.of(x).plus(y), ObjectiveSense.MAXIMIZE);
22
23 try (Solution solution = problem.solve()) {
24 System.out.println(solution.getTerminationStatus());
25 }
26 }
27 }
28}
Only LE and GE quadratic constraints are supported;
QuadraticExpression does not expose an eq method.
Reading and Writing MPS/QPS#
Problem exposes both extension-dispatch and direct MPS entry points:
MpsRoundtrip.java and
sample.mps
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5import com.nvidia.cuopt.mathematicaloptimization.*;
6
7public class MpsRoundtrip {
8 public static void main(String[] args) throws Exception {
9 try (Problem problem = Problem.read("sample.mps")) {
10 System.out.println("Variables: " + problem.getNumVariables());
11 problem.write("roundtrip.mps");
12 }
13 }
14}
Example Response:
Variables: 2
Fixed-format parsing is also available:
try (Problem fixed = Problem.read("fixed-format.mps", true)) {
// Use fixed-format parsing explicitly.
}
Parsing failures are reported as CuOptException with the cuOpt status code
available from getStatusCode.