Convex Optimization Examples#
This section contains examples of how to use the cuOpt convex optimization Python API.
Note
The examples in this section are not exhaustive. They are provided to help you get started with the cuOpt convex optimization Python API. For more examples, please refer to the cuopt-examples GitHub repository.
Simple Linear Programming Example#
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Simple Linear Programming Example
6
7This example demonstrates how to:
8- Create a linear programming problem
9- Add continuous variables
10- Add linear constraints
11- Set an objective function
12- Solve the problem and retrieve results
13
14Problem:
15 Maximize: x + y
16 Subject to:
17 x + y <= 10
18 x - y >= 0
19 x, y >= 0
20
21Expected Output:
22 Optimal solution found in 0.01 seconds
23 x = 10.0
24 y = 0.0
25 Objective value = 10.0
26"""
27
28from cuopt.linear_programming.problem import Problem, CONTINUOUS, MAXIMIZE
29from cuopt.linear_programming.solver_settings import SolverSettings
30
31
32def main():
33 """Run the simple LP example."""
34 # Create a new problem
35 problem = Problem("Simple LP")
36
37 # Add variables
38 x = problem.addVariable(lb=0, vtype=CONTINUOUS, name="x")
39 y = problem.addVariable(lb=0, vtype=CONTINUOUS, name="y")
40
41 # Add constraints
42 problem.addConstraint(x + y <= 10, name="c1")
43 problem.addConstraint(x - y >= 0, name="c2")
44
45 # Set objective function
46 problem.setObjective(x + y, sense=MAXIMIZE)
47
48 # Configure solver settings
49 settings = SolverSettings()
50 settings.set_parameter("time_limit", 60)
51
52 # Solve the problem
53 problem.solve(settings)
54
55 # Check solution status
56 if problem.Status.name == "Optimal":
57 print(f"Optimal solution found in {problem.SolveTime:.2f} seconds")
58 print(f"x = {x.getValue()}")
59 print(f"y = {y.getValue()}")
60 print(f"Objective value = {problem.ObjValue}")
61 else:
62 print(f"Problem status: {problem.Status.name}")
63
64
65if __name__ == "__main__":
66 main()
The response is as follows:
Optimal solution found in 0.01 seconds
x = 5.0
y = 5.0
Objective value = 10.0
Simple Quadratic Programming Example#
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Simple Quadratic Programming Example
6====================================
7
8This example demonstrates how to formulate and solve a simple
9Quadratic Programming (QP) problem using the cuOpt Python API.
10
11Problem:
12 minimize x^2 + y^2
13 subject to x + y >= 1
14 0.75 * x + y <= 1
15 x, y >= 0
16
17This is a convex QP that minimizes the squared distance from the origin
18while satisfying other constraints.
19"""
20
21from cuopt.linear_programming.problem import (
22 MINIMIZE,
23 Problem,
24)
25
26
27def main():
28 # Create a new optimization problem
29 prob = Problem("Simple QP")
30
31 # Add variables with non-negative bounds
32 x = prob.addVariable(lb=0)
33 y = prob.addVariable(lb=0)
34
35 # Add constraint: x + y >= 1
36 prob.addConstraint(x + y >= 1)
37 prob.addConstraint(0.75 * x + y <= 1)
38
39 # Set quadratic objective: minimize x^2 + y^2
40 # Using Variable * Variable to create quadratic terms
41 quad_obj = x * x + y * y
42 prob.setObjective(quad_obj, sense=MINIMIZE)
43
44 # Solve the problem
45 prob.solve()
46
47 # Print results
48 print(f"Optimal solution found in {prob.SolveTime:.2f} seconds")
49 print(f"x = {x.Value}")
50 print(f"y = {y.Value}")
51 print(f"Objective value = {prob.ObjValue}")
52
53
54if __name__ == "__main__":
55 main()
The response is as follows:
Optimal solution found in 0.01 seconds
x = 0.5
y = 0.5
Objective value = 0.5
Second-Order Cone Programming Example#
This example minimizes x3 subject to x1 + x2 >= 2 and the second-order
cone ||(x1, x2)||_2 <= x3, expressed as the inequalities
x1^2 + x2^2 - x3^2 <= 0, x_3 >= 0. cuOpt detects the cone structure and solves with the
barrier method.
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Simple Second-Order Cone Programming (SOCP) Example
6===================================================
7
8This example demonstrates how to formulate and solve a Second-Order Cone
9Program (SOCP) using the cuOpt Python API.
10
11Problem:
12 minimize x3
13 subject to x1 + x2 >= 2
14 x1^2 + x2^2 <= x3^2 (i.e. ||(x1, x2)||_2 <= x3)
15 x3 >= 0
16
17The cone constraint is supplied as the quadratic inequality
18``x1^2 + x2^2 - x3^2 <= 0``. cuOpt detects the second-order cone structure and
19solves with the barrier method.
20
21Optimal solution: x1 = x2 = 1, x3 = sqrt(2) ~= 1.4142, objective ~= 1.4142.
22"""
23
24from cuopt.linear_programming.problem import (
25 MINIMIZE,
26 Problem,
27)
28
29
30def main():
31 prob = Problem("Simple SOCP")
32
33 # Cone tail variables can be free; the cone head must be non-negative.
34 x1 = prob.addVariable(lb=-float("inf"), name="x1")
35 x2 = prob.addVariable(lb=-float("inf"), name="x2")
36 x3 = prob.addVariable(lb=0, name="x3")
37
38 # Linear constraint
39 prob.addConstraint(x1 + x2 >= 2)
40
41 # Second-order cone ||(x1, x2)||_2 <= x3 as x1^2 + x2^2 - x3^2 <= 0
42 prob.addConstraint(x1 * x1 + x2 * x2 - x3 * x3 <= 0, name="soc")
43
44 prob.setObjective(x3, sense=MINIMIZE)
45
46 # cuOpt automatically selects the barrier method for quadratic constraints.
47 prob.solve()
48
49 print(f"Status: {prob.Status}")
50 print(f"x1 = {x1.Value}")
51 print(f"x2 = {x2.Value}")
52 print(f"x3 = {x3.Value}")
53 print(f"Objective value = {prob.ObjValue}")
54
55
56if __name__ == "__main__":
57 main()
The response is as follows:
Status: 1
x1 = 1.0
x2 = 1.0
x3 = 1.4142135623730951
Objective value = 1.4142135623730951
Second-Order Cone Programming with Rotated Second-Order Cones Example#
This example solves a rotated second-order cone x1^2 + x2^2 <= x3 * x4, x3 >= 0, x4 >= 0. For rotated cones, cuOpt expects a
symmetric quadratic matrix Q, so the cross term is supplied as the two equal
halves -0.5*x3*x4 and -0.5*x4*x3. It minimizes x3 + x4 subject to
x1 + x2 >= 2.
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Rotated Second-Order Cone Programming (SOCP) Example
6====================================================
7
8This example formulates and solves a rotated second-order cone problem with the
9cuOpt Python API.
10
11Problem:
12 minimize x3 + x4
13 subject to x1 + x2 >= 2
14 x1^2 + x2^2 <= x3 * x4 (rotated second-order cone)
15 x3 >= 0, x4 >= 0
16
17The rotated cone is supplied as the quadratic inequality
18``x1^2 + x2^2 - 0.5 * x3*x4 - 0.5 * x4*x3 <= 0``. cuOpt expects a symmetric quadratic matrix ``Q``,
19so the cross term is split into the two equal halves ``-0.5*x3*x4`` and
20``-0.5*x4*x3`` (i.e. ``Q[x3, x4] = Q[x4, x3] = -0.5``). cuOpt detects the
21second-order cone structure and solves with the barrier method.
22
23Optimal solution: x1 = x2 = 1, x3 = x4 = sqrt(2) ~= 1.4142, objective ~= 2.8284.
24"""
25
26from cuopt.linear_programming.problem import (
27 MINIMIZE,
28 Problem,
29)
30
31
32def main():
33 prob = Problem("Rotated SOCP")
34
35 # Cone tail variables can be free; the cone heads must be non-negative.
36 x1 = prob.addVariable(lb=-float("inf"), name="x1")
37 x2 = prob.addVariable(lb=-float("inf"), name="x2")
38 x3 = prob.addVariable(lb=0, name="x3")
39 x4 = prob.addVariable(lb=0, name="x4")
40
41 # Linear constraint
42 prob.addConstraint(x1 + x2 >= 2)
43
44 # Rotated cone x1^2 + x2^2 <= x3 * x4. The quadratic matrix must be
45 # symmetric, so the cross term is supplied as two equal halves.
46 prob.addConstraint(
47 x1 * x1 + x2 * x2 - 0.5 * x3 * x4 - 0.5 * x4 * x3 <= 0,
48 name="rotated_soc",
49 )
50
51 prob.setObjective(x3 + x4, sense=MINIMIZE)
52
53 # cuOpt automatically selects the barrier method for quadratic constraints.
54 prob.solve()
55
56 print(f"Status: {prob.Status}")
57 print(f"x1 = {x1.Value}")
58 print(f"x2 = {x2.Value}")
59 print(f"x3 = {x3.Value}")
60 print(f"x4 = {x4.Value}")
61 print(f"Objective value = {prob.ObjValue}")
62
63
64if __name__ == "__main__":
65 main()
The response is as follows:
Status: 1
x1 = 1.0
x2 = 1.0
x3 = 1.4142135623730951
x4 = 1.4142135623730951
Objective value = 2.8284271247461903
General Convex Quadratic Constraint Example#
This example uses a general convex quadratic constraint
2*x^2 + 2*x*y + 2*y^2 <= 6 (an ellipsoid). It minimizes x + y.
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5General Convex Quadratic Constraint Example
6===========================================
7
8This example demonstrates adding a general convex quadratic constraint
9with the cuOpt Python API.
10
11Problem:
12 minimize x + y
13 subject to x + y >= -5
14 2*x^2 + 2*x*y + 2*y^2 <= 6 (general convex quadratic)
15
16The quadratic constraint is an ellipsoid (a convex set). The
17linear objective is minimized on it at x = y = -1, objective -2.
18"""
19
20from cuopt.linear_programming.problem import (
21 MINIMIZE,
22 Problem,
23)
24
25
26def main():
27 prob = Problem("General Convex QC")
28
29 x = prob.addVariable(lb=-float("inf"), name="x")
30 y = prob.addVariable(lb=-float("inf"), name="y")
31
32 # A linear constraint
33 prob.addConstraint(x + y >= -5)
34
35 # General convex quadratic constraint 2*x^2 + 2*x*y + 2*y^2 <= 6.
36 prob.addConstraint(
37 2 * x * x + 2 * x * y + 2 * y * y <= 6, name="ellipsoid"
38 )
39
40 prob.setObjective(x + y, sense=MINIMIZE)
41
42 # cuOpt automatically selects the barrier method for problems with quadratic constraints.
43 prob.solve()
44
45 print(f"Status: {prob.Status}")
46 print(f"x = {x.Value}")
47 print(f"y = {y.Value}")
48 print(f"Objective value = {prob.ObjValue}")
49
50
51if __name__ == "__main__":
52 main()
The response is as follows:
Status: 1
x = -1.0
y = -1.0
Objective value = -2.0
Reading a Problem from an MPS File#
Problem.read loads a problem from an MPS, QPS, or LP file, dispatching on the
file extension. The same call also reads QUADOBJ quadratic objectives and
and QCMATRIX quadratic constraints. This example reads the
bundled sample.mps (a small LP) and solves it.
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Reading a Problem from an MPS File
6==================================
7
8This example loads an optimization problem from a file with ``Problem.read``
9and solves it. ``Problem.read`` dispatches on the file extension:
10
11* ``.mps`` / ``.qps`` (and ``.gz`` / ``.bz2`` variants) use the MPS reader
12* ``.lp`` (and compressed variants) use the LP reader.
13
14The bundled ``sample.mps`` is a small LP with optimal objective ``-0.36``.
15"""
16
17import os
18
19from cuopt.linear_programming.problem import Problem
20
21
22def main():
23 # Resolve the sample file next to this script so it runs from any directory.
24 mps_path = os.path.join(os.path.dirname(__file__), "sample.mps")
25
26 problem = Problem.read(mps_path)
27 problem.solve()
28
29 print(f"Status: {problem.Status}")
30 print(f"Number of variables: {problem.NumVariables}")
31 print(f"Objective value = {problem.ObjValue}")
32 for var in problem.getVariables():
33 print(f"{var.VariableName} = {var.Value}")
34
35
36if __name__ == "__main__":
37 main()
The sample MPS file:
1NAME good-1
2ROWS
3 N COST
4 L ROW1
5 L ROW2
6COLUMNS
7 VAR1 COST -0.2
8 VAR1 ROW1 3 ROW2 2.7
9 VAR2 COST 0.1
10 VAR2 ROW1 4 ROW2 10.1
11RHS
12 RHS1 ROW1 5.4 ROW2 4.9
13ENDATA
The response is as follows:
Status: 1
Number of variables: 2
Objective value = -0.36000000000000004
VAR1 = 1.8
VAR2 = 0.0
Advanced Example: Production Planning#
production_planning_example.py
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Production Planning Example
6
7This example demonstrates a real-world application of MIP:
8- Production planning with resource constraints
9- Multiple constraint types (machine time, labor, materials)
10- Profit maximization
11
12Problem:
13 A factory produces two products (A and B)
14 - Product A: $50 profit per unit
15 - Product B: $30 profit per unit
16
17 Resources:
18 - Machine time: 2 hrs/unit A, 1 hr/unit B, max 100 hrs
19 - Labor: 1 hr/unit A, 3 hrs/unit B, max 120 hrs
20 - Material: 4 units/unit A, 2 units/unit B, max 200 units
21
22 Constraints:
23 - Minimum 10 units of Product A
24 - Minimum 15 units of Product B
25
26Expected Output:
27 === Production Planning Solution ===
28 Status: Optimal
29 Solve time: 0.09 seconds
30 Product A production: 36.0 units
31 Product B production: 28.0 units
32 Total profit: $2640.00
33"""
34
35from cuopt.linear_programming.problem import Problem, INTEGER, MAXIMIZE
36from cuopt.linear_programming.solver_settings import SolverSettings
37
38
39def main():
40 """Run the production planning example."""
41 # Production planning problem
42 problem = Problem("Production Planning")
43
44 # Decision variables: production quantities
45 # x1 = units of product A
46 # x2 = units of product B
47 x1 = problem.addVariable(lb=10, vtype=INTEGER, name="Product_A")
48 x2 = problem.addVariable(lb=15, vtype=INTEGER, name="Product_B")
49
50 # Resource constraints
51 # Machine time: 2 hours per unit of A, 1 hour per unit of B, max 100 hours
52 problem.addConstraint(2 * x1 + x2 <= 100, name="Machine_Time")
53
54 # Labor: 1 hour per unit of A, 3 hours per unit of B, max 120 hours
55 problem.addConstraint(x1 + 3 * x2 <= 120, name="Labor_Hours")
56
57 # Material: 4 units per unit of A, 2 units per unit of B, max 200 units
58 problem.addConstraint(4 * x1 + 2 * x2 <= 200, name="Material")
59
60 # Objective: maximize profit
61 # Profit: $50 per unit of A, $30 per unit of B
62 problem.setObjective(50 * x1 + 30 * x2, sense=MAXIMIZE)
63
64 # Solve with time limit
65 settings = SolverSettings()
66 settings.set_parameter("time_limit", 30)
67 problem.solve(settings)
68
69 # Display results
70 if problem.Status.name == "Optimal":
71 print("=== Production Planning Solution ===")
72 print(f"Status: {problem.Status.name}")
73 print(f"Solve time: {problem.SolveTime:.2f} seconds")
74 print(f"Product A production: {x1.getValue()} units")
75 print(f"Product B production: {x2.getValue()} units")
76 print(f"Total profit: ${problem.ObjValue:.2f}")
77
78 else:
79 print(f"Problem not solved optimally. Status: {problem.Status.name}")
80
81
82if __name__ == "__main__":
83 main()
The response is as follows:
=== Production Planning Solution ===
Status: Optimal
Solve time: 0.09 seconds
Product A production: 36.0 units
Product B production: 28.000000000000004 units
Total profit: $2640.00
Working with Expressions and Constraints#
expressions_constraints_example.py
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Working with Expressions and Constraints Example
6
7This example demonstrates:
8- Creating complex linear expressions
9- Using expressions in constraints
10- Different constraint types (<=, >=, ==)
11- Building constraints from multiple variables
12
13Problem:
14 Maximize: x + 2*y + 3*z
15 Subject to:
16 2*x + 3*y - z <= 100 (Complex_Constraint_1)
17 x + y + z >= 20 (Complex_Constraint_2)
18 x + y == 50 (Equality_Constraint)
19 x <= 30 (Upper_Bound_X)
20 y >= 10 (Lower_Bound_Y)
21 z <= 100 (Upper_Bound_Z)
22
23Expected Output:
24 === Expression Example Results ===
25 x = 0.0
26 y = 50.0
27 z = 100.0
28 Objective value = 400.0
29"""
30
31from cuopt.linear_programming.problem import Problem, MAXIMIZE
32from cuopt.linear_programming.solver_settings import SolverSettings
33
34
35def main():
36 """Run the expressions and constraints example."""
37 problem = Problem("Expression Example")
38
39 # Create variables
40 x = problem.addVariable(lb=0, name="x")
41 y = problem.addVariable(lb=0, name="y")
42 z = problem.addVariable(lb=0, name="z")
43
44 # Create complex expressions
45 expr1 = 2 * x + 3 * y - z
46 expr2 = x + y + z
47
48 # Add constraints using expressions
49 problem.addConstraint(expr1 <= 100, name="Complex_Constraint_1")
50 problem.addConstraint(expr2 >= 20, name="Complex_Constraint_2")
51
52 # Add constraint with different senses
53 problem.addConstraint(x + y == 50, name="Equality_Constraint")
54 problem.addConstraint(1 * x <= 30, name="Upper_Bound_X")
55 problem.addConstraint(1 * y >= 10, name="Lower_Bound_Y")
56 problem.addConstraint(1 * z <= 100, name="Upper_Bound_Z")
57
58 # Set objective
59 problem.setObjective(x + 2 * y + 3 * z, sense=MAXIMIZE)
60
61 settings = SolverSettings()
62 settings.set_parameter("time_limit", 20)
63
64 problem.solve(settings)
65
66 if problem.Status.name == "Optimal":
67 print("=== Expression Example Results ===")
68 print(f"x = {x.getValue()}")
69 print(f"y = {y.getValue()}")
70 print(f"z = {z.getValue()}")
71 print(f"Objective value = {problem.ObjValue}")
72 else:
73 print(f"Problem status: {problem.Status.name}")
74
75
76if __name__ == "__main__":
77 main()
The response is as follows:
=== Expression Example Results ===
x = 0.0
y = 50.0
z = 100.0
Objective value = 400.0
Working with Quadratic Objective Matrix#
1# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Quadratic Programming Matrix Example
6====================================
7
8This example demonstrates how to formulate and solve a
9Quadratic Programming (QP) problem represented in a matrix format
10using the cuOpt Python API.
11
12Problem:
13 minimize 0.01 * p1^2 + 0.02 * p2^2 + 0.015 * p3^2 + 8 * p1 + 6 * p2 + 7 * p3
14 subject to p1 + p2 + p3 = 150
15 10 <= p1 <= 100
16 10 <= p2 <= 80
17 10 <= p3 <= 90
18
19This is a convex QP that minimizes the cost of power generation and dispatch
20while satisfying capacity and demand.
21"""
22
23from cuopt.linear_programming.problem import (
24 MINIMIZE,
25 Problem,
26 QuadraticExpression,
27)
28
29
30def main():
31 # Create a new optimization problem
32 prob = Problem("QP Power Dispatch")
33
34 # Add variables with lower and upper bounds
35 p1 = prob.addVariable(lb=10, ub=100)
36 p2 = prob.addVariable(lb=10, ub=80)
37 p3 = prob.addVariable(lb=10, ub=90)
38
39 # Add demand constraint: p1 + p2 + p3 = 150
40 prob.addConstraint(p1 + p2 + p3 == 150, name="demand")
41
42 # Create matrix for quadratic terms: 0.01 p1^2 + 0.02 p2^2 + 0.015 p3^2
43 matrix = [[0.01, 0.0, 0.0], [0.0, 0.02, 0.0], [0.0, 0.0, 0.015]]
44 quad_matrix = QuadraticExpression(matrix, prob.getVariables())
45
46 # Set objective using matrix representation
47 quad_obj = quad_matrix + 8 * p1 + 6 * p2 + 7 * p3
48 prob.setObjective(quad_obj, sense=MINIMIZE)
49
50 # Solve the problem
51 prob.solve()
52
53 # Print results
54 print(f"Optimal solution found in {prob.SolveTime:.2f} seconds")
55 print(f"p1 = {p1.Value}")
56 print(f"p2 = {p2.Value}")
57 print(f"p3 = {p3.Value}")
58 print(f"Minimized cost = {prob.ObjValue}")
59
60
61if __name__ == "__main__":
62 main()
The response is as follows:
Optimal solution found in 0.16 seconds
p1 = 30.770728122083014
p2 = 65.38350784293876
p3 = 53.84576403497824
Minimized cost = 1153.8461538953868
Inspecting the Problem Solution#
1# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Linear Programming Solution Example
6
7This example demonstrates how to:
8- Create a linear programming problem
9- Solve the problem and retrieve result details
10
11Problem:
12 Minimize: 3x + 2y + 5z
13 Subject to:
14 x + y + z = 4
15 2x + y + z = 5
16 x, y, z >= 0
17
18Expected Output:
19 Optimal solution found in 0.02 seconds
20 Objective: 9.0
21 x = 1.0, ReducedCost = 0.0
22 y = 3.0, ReducedCost = 0.0
23 z = 0.0, ReducedCost = 2.999999858578644
24 c1 DualValue = 1.0000000592359144
25 c2 DualValue = 1.0000000821854418
26"""
27
28from cuopt.linear_programming.problem import Problem, MINIMIZE
29
30
31def main():
32 """Run the simple LP example."""
33 problem = Problem("min_dual_rc")
34
35 # Add Variables
36 x = problem.addVariable(lb=0.0, name="x")
37 y = problem.addVariable(lb=0.0, name="y")
38 z = problem.addVariable(lb=0.0, name="z")
39
40 # Add Constraints (equalities)
41 problem.addConstraint(x + y + z == 4.0, name="c1")
42 problem.addConstraint(2.0 * x + y + z == 5.0, name="c2")
43
44 # Set Objective (minimize)
45 problem.setObjective(3.0 * x + 2.0 * y + 5.0 * z, sense=MINIMIZE)
46
47 # Solve
48 problem.solve()
49
50 # Check solution status
51 if problem.Status.name == "Optimal":
52 print(f"Optimal solution found in {problem.SolveTime:.2f} seconds")
53 # Get Primal
54 print("Objective:", problem.ObjValue)
55 for v in problem.getVariables():
56 print(
57 f"{v.VariableName} = {v.Value}, ReducedCost = {v.ReducedCost}"
58 )
59 # Get Duals
60 for c in problem.getConstraints():
61 print(f"{c.ConstraintName} DualValue = {c.DualValue}")
62 else:
63 print(f"Problem status: {problem.Status.name}")
64
65
66if __name__ == "__main__":
67 main()
The response is as follows:
Optimal solution found in 0.02 seconds
Objective: 9.0
x = 1.0, ReducedCost = 0.0
y = 3.0, ReducedCost = 0.0
z = 0.0, ReducedCost = 2.999999858578644
c1 DualValue = 1.0000000592359144
c2 DualValue = 1.0000000821854418
Working with PDLP Warmstart Data#
Warmstart data allows to restart PDLP with a previous solution context. This should be used when you solve a new problem which is similar to the previous one.
Note
Warmstart data is only available for Linear Programming (LP) problems, not for Mixed Integer Linear Programming (MILP) problems.
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Working with PDLP Warmstart Data Example
6
7This example demonstrates:
8- Using PDLP (Primal-Dual hybrid gradient for LP) solver method
9- Extracting warmstart data from a solved problem
10- Reusing warmstart data to solve a similar problem faster
11- Comparing solve times with and without warmstart
12
13Warmstart data allows restarting PDLP with a previous solution context.
14This should be used when you solve a new problem which is similar to the
15previous one.
16
17Note:
18 Warmstart data is only available for Linear Programming (LP) problems,
19 not for Mixed Integer Linear Programming (MILP) problems.
20
21Problem 1:
22 Maximize: 2*x + y
23 Subject to:
24 4*x + 10*y <= 130
25 8*x - 3*y >= 40
26 x, y >= 0
27
28Problem 2 (similar):
29 Maximize: 2*x + y
30 Subject to:
31 4*x + 10*y <= 100
32 8*x - 3*y >= 50
33 x, y >= 0
34
35Expected Output:
36 Optimal solution found in 0.01 seconds
37 x = 25.0
38 y = 0.0
39 Objective value = 50.0
40"""
41
42from cuopt.linear_programming.problem import Problem, CONTINUOUS, MAXIMIZE
43from cuopt.linear_programming.solver.solver_parameters import (
44 CUOPT_METHOD,
45 CUOPT_PDLP_SOLVER_MODE,
46)
47from cuopt.linear_programming.solver_settings import (
48 SolverSettings,
49 SolverMethod,
50 PDLPSolverMode,
51)
52
53
54def main():
55 """Run the PDLP warmstart example."""
56 print("=== Solving Problem 1 ===")
57
58 # Create a new problem
59 problem = Problem("Simple LP")
60
61 # Add variables
62 x = problem.addVariable(lb=0, vtype=CONTINUOUS, name="x")
63 y = problem.addVariable(lb=0, vtype=CONTINUOUS, name="y")
64
65 # Add constraints
66 problem.addConstraint(4 * x + 10 * y <= 130, name="c1")
67 problem.addConstraint(8 * x - 3 * y >= 40, name="c2")
68
69 # Set objective function
70 problem.setObjective(2 * x + y, sense=MAXIMIZE)
71
72 # Configure solver settings
73 settings = SolverSettings()
74 settings.set_parameter(CUOPT_METHOD, SolverMethod.PDLP)
75 settings.set_parameter(CUOPT_PDLP_SOLVER_MODE, PDLPSolverMode.Stable2)
76
77 # Solve the problem
78 problem.solve(settings)
79
80 print(f"Problem 1 solved in {problem.SolveTime:.4f} seconds")
81 print(f"x = {x.getValue()}, y = {y.getValue()}")
82 print(f"Objective value = {problem.ObjValue}")
83
84 # Get the warmstart data
85 warmstart_data = problem.getWarmstartData()
86 print(
87 f"\nWarmstart data extracted (primal solution size: "
88 f"{len(warmstart_data.current_primal_solution)})"
89 )
90
91 print("\n=== Solving Problem 2 with Warmstart ===")
92
93 # Create a new problem
94 new_problem = Problem("Warmstart LP")
95
96 # Add variables
97 x = new_problem.addVariable(lb=0, vtype=CONTINUOUS, name="x")
98 y = new_problem.addVariable(lb=0, vtype=CONTINUOUS, name="y")
99
100 # Add constraints (slightly different from problem 1)
101 new_problem.addConstraint(4 * x + 10 * y <= 100, name="c1")
102 new_problem.addConstraint(8 * x - 3 * y >= 50, name="c2")
103
104 # Set objective function
105 new_problem.setObjective(2 * x + y, sense=MAXIMIZE)
106
107 # Configure solver settings with warmstart data
108 settings.set_pdlp_warm_start_data(warmstart_data)
109
110 # Solve the problem
111 new_problem.solve(settings)
112
113 # Check solution status
114 if new_problem.Status.name == "Optimal":
115 print(f"Optimal solution found in {new_problem.SolveTime:.2f} seconds")
116 print(f"x = {x.getValue()}")
117 print(f"y = {y.getValue()}")
118 print(f"Objective value = {new_problem.ObjValue}")
119 else:
120 print(f"Problem status: {new_problem.Status.name}")
121
122
123if __name__ == "__main__":
124 main()
The response is as follows:
Optimal solution found in 0.01 seconds
x = 25.001763214569394
y = 0.0
Objective value = 50.00352642913879