LP, QP and MILP Examples#

This section contains examples of how to use the cuOpt linear programming, quadratic programming and mixed integer linear programming Python API.

Note

The examples in this section are not exhaustive. They are provided to help you get started with the cuOpt linear programming, quadratic programming and mixed integer linear programming Python API. For more examples, please refer to the cuopt-examples GitHub repository.

Simple Linear Programming Example#

simple_lp_example.py

 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 = 10.0
y = 0.0
Objective value = 10.0

Simple Quadratic Programming Example#

simple_qp_example.py

 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

Mixed Integer Linear Programming Example#

simple_milp_example.py

 1# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 2# SPDX-License-Identifier: Apache-2.0
 3
 4"""
 5Mixed Integer Linear Programming Example
 6
 7This example demonstrates how to:
 8- Create a mixed integer programming problem
 9- Add integer variables with bounds
10- Add constraints with integer variables
11- Solve a MIP problem
12
13Problem:
14    Maximize: 5*x + 3*y
15    Subject to:
16        2*x + 4*y >= 230
17        3*x + 2*y <= 190
18        10 <= y <= 50
19        x, y are integers
20
21Expected Output:
22    Optimal solution found in 0.00 seconds
23    x = 36.0
24    y = 41.0
25    Objective value = 303.0
26"""
27
28from cuopt.linear_programming.problem import Problem, INTEGER, MAXIMIZE
29from cuopt.linear_programming.solver_settings import SolverSettings
30
31
32def main():
33    """Run the simple MIP example."""
34    # Create a new MIP problem
35    problem = Problem("Simple MIP")
36
37    # Add integer variables with bounds
38    x = problem.addVariable(vtype=INTEGER, name="V_x")
39    y = problem.addVariable(lb=10, ub=50, vtype=INTEGER, name="V_y")
40
41    # Add constraints
42    problem.addConstraint(2 * x + 4 * y >= 230, name="C1")
43    problem.addConstraint(3 * x + 2 * y <= 190, name="C2")
44
45    # Set objective function
46    problem.setObjective(5 * x + 3 * 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 and results
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.00 seconds
x = 36.0
y = 40.99999999999999
Objective value = 303.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 = 99.99999999999999
Objective value = 399.99999999999994

Working with Quadratic objective matrix#

qp_matrix_example.py

 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#

solution_example.py

 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 Incumbent Solutions#

Incumbent solutions are intermediate feasible solutions found during the MIP solving process. They represent the best integer-feasible solution discovered so far and can be accessed through callback functions.

Note

Incumbent solutions are only available for Mixed Integer Programming (MIP) problems, not for pure Linear Programming (LP) problems.

incumbent_solutions_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 Incumbent Solutions Example
  6
  7This example demonstrates:
  8- Using callbacks to receive intermediate solutions during MIP solving
  9- Tracking solution progress as the solver improves the solution
 10- Accessing incumbent (best so far) solutions before final optimum
 11- Custom callback class implementation
 12
 13Incumbent solutions are intermediate feasible solutions found during the MIP
 14solving process. They represent the best integer-feasible solution discovered
 15so far.
 16
 17Note:
 18    Incumbent solutions are only available for Mixed Integer Programming (MIP)
 19    problems, not for pure Linear Programming (LP) problems.
 20
 21Problem:
 22    Maximize: 5*x + 3*y
 23    Subject to:
 24        2*x + 4*y >= 230
 25        3*x + 2*y <= 190
 26        x, y are integers
 27
 28Expected Output:
 29    Incumbent 1: [ 0. 58.], cost: 174.00
 30    Incumbent 2: [36. 41.], cost: 303.00
 31
 32    === Final Results ===
 33    Problem status: Optimal
 34    Solve time: 0.16 seconds
 35    Final solution: x=36.0, y=41.0
 36    Final objective value: 303.00
 37"""
 38
 39from cuopt.linear_programming.problem import Problem, INTEGER, MAXIMIZE
 40from cuopt.linear_programming.solver_settings import SolverSettings
 41from cuopt.linear_programming.solver.solver_parameters import CUOPT_TIME_LIMIT
 42from cuopt.linear_programming.internals import GetSolutionCallback
 43
 44
 45# Create a callback class to receive incumbent solutions
 46class IncumbentCallback(GetSolutionCallback):
 47    """Callback to receive and track incumbent solutions during solving."""
 48
 49    def __init__(self, user_data):
 50        super().__init__()
 51        self.solutions = []
 52        self.n_callbacks = 0
 53        self.user_data = user_data
 54
 55    def get_solution(self, solution, solution_cost, solution_bound, user_data):
 56        assert user_data is self.user_data
 57        """
 58        Called whenever the solver finds a new incumbent solution.
 59
 60        Parameters
 61        ----------
 62        solution : array-like
 63            The variable values of the incumbent solution
 64        solution_cost : array-like
 65            The objective value of the incumbent solution
 66        solution_bound : array-like
 67            The current best bound in user objective space
 68        """
 69        self.n_callbacks += 1
 70
 71        # Store the incumbent solution
 72        incumbent = {
 73            "solution": solution.tolist(),
 74            "cost": float(solution_cost[0]),
 75            "bound": float(solution_bound[0]),
 76            "iteration": self.n_callbacks,
 77            "user_data": user_data,
 78        }
 79        self.solutions.append(incumbent)
 80
 81        print(
 82            f"Incumbent {self.n_callbacks}: {incumbent['solution']}, "
 83            f"cost: {incumbent['cost']:.2f}"
 84        )
 85
 86
 87def main():
 88    """Run the incumbent solutions example."""
 89    # Create a more complex MIP problem that will generate multiple incumbents
 90    problem = Problem("Incumbent Example")
 91
 92    # Add integer variables
 93    x = problem.addVariable(vtype=INTEGER)
 94    y = problem.addVariable(vtype=INTEGER)
 95
 96    # Add constraints to create a problem that will generate multiple
 97    # incumbents
 98    problem.addConstraint(2 * x + 4 * y >= 230)
 99    problem.addConstraint(3 * x + 2 * y <= 190)
100
101    # Set objective to maximize
102    problem.setObjective(5 * x + 3 * y, sense=MAXIMIZE)
103
104    # Configure solver settings with callback
105    settings = SolverSettings()
106    # Set the incumbent callback
107    user_data = {"source": "incumbent_solutions_example"}
108    incumbent_callback = IncumbentCallback(user_data)
109    settings.set_mip_callback(incumbent_callback, user_data)
110    # Allow enough time to find multiple incumbents
111    settings.set_parameter(CUOPT_TIME_LIMIT, 30)
112
113    # Solve the problem
114    problem.solve(settings)
115
116    # Display final results
117    print("\n=== Final Results ===")
118    print(f"Problem status: {problem.Status.name}")
119    print(f"Solve time: {problem.SolveTime:.2f} seconds")
120    print(f"Final solution: x={x.getValue()}, y={y.getValue()}")
121    print(f"Final objective value: {problem.ObjValue:.2f}")
122
123    # Display all incumbents found
124    print(
125        f"\nTotal incumbent solutions found: "
126        f"{len(incumbent_callback.solutions)}"
127    )
128
129
130if __name__ == "__main__":
131    main()

The response is as follows:

Optimal solution found.
Incumbent 1: [ 0. 58.], cost: 174.00
Incumbent 2: [36. 41.], cost: 303.00
Generated fast solution in 0.158467 seconds with objective 303.000000
Consuming B&B solutions, solution queue size 2
Solution objective: 303.000000 , relative_mip_gap 0.000000 solution_bound 303.000000 presolve_time 0.043211 total_solve_time 0.160270 max constraint violation 0.000000 max int violation 0.000000 max var bounds violation 0.000000 nodes 4 simplex_iterations 3

=== Final Results ===
Problem status: Optimal
Solve time: 0.16 seconds
Final solution: x=36.0, y=40.99999999999999
Final objective value: 303.00

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.

pdlp_warmstart_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 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.000000000639382
y = 0.0
Objective value = 50.000000001278764