-
Notifications
You must be signed in to change notification settings - Fork 122
Add matrix interface to problem modeling #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2e8717a
update model to add QP matrix
Iroy30 42ab2fe
add test and update recipe
Iroy30 95560f5
Merge branch 'main' into add_QP_matrix
Iroy30 2a5cf28
add scipy to environments
Iroy30 c2b2032
Merge remote-tracking branch 'iroy30/add_QP_matrix'
Iroy30 254a5a9
update dependency
Iroy30 df85b24
style checks
Iroy30 9ddfca4
Merge branch 'main' into add_QP_matrix
Iroy30 806ec8c
update env
Iroy30 f16a2a9
Merge remote-tracking branch 'iroy30/add_QP_matrix'
Iroy30 a0b1483
Merge branch 'release/26.02' into add_QP_matrix
Iroy30 cdaab35
consolidate quad matrix API and add tests
Iroy30 d42db52
update scipy version
Iroy30 b52defd
Merge branch 'release/26.02' into add_QP_matrix
Iroy30 36df060
address reviews
Iroy30 e0f8c46
Merge remote-tracking branch 'origin/add_QP_matrix' into add_QP_matrix
Iroy30 61f7702
update docs
Iroy30 da78edf
address coderabbit reviews
Iroy30 2756f5b
address reviews and update copyright
Iroy30 955f640
address reviews and update copyright
Iroy30 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 2 additions & 15 deletions
17
docs/cuopt/source/cuopt-python/lp-qp-milp/examples/expressions_constraints_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 2 additions & 15 deletions
17
docs/cuopt/source/cuopt-python/lp-qp-milp/examples/incumbent_solutions_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 2 additions & 15 deletions
17
docs/cuopt/source/cuopt-python/lp-qp-milp/examples/production_planning_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
docs/cuopt/source/cuopt-python/lp-qp-milp/examples/qp_matrix_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """ | ||
| Quadratic Programming Matrix Example | ||
| ==================================== | ||
| .. note:: | ||
| The QP solver is currently in beta. | ||
| This example demonstrates how to formulate and solve a | ||
| Quadratic Programming (QP) problem represented in a matrix format | ||
| using the cuOpt Python API. | ||
| Problem: | ||
| minimize 0.01 * p1^2 + 0.02 * p2^2 + 0.015 * p3^2 + 8 * p1 + 6 * p2 + 7 * p3 | ||
| subject to p1 + p2 + p3 = 150 | ||
| 10 <= p1 <= 100 | ||
| 10 <= p2 <= 80 | ||
| 10 <= p3 <= 90 | ||
| This is a convex QP that minimizes the cost of power generation and dispatch | ||
| while satisfying capacity and demand. | ||
| """ | ||
|
|
||
| from cuopt.linear_programming.problem import ( | ||
| MINIMIZE, | ||
| Problem, | ||
| QuadraticExpression, | ||
| ) | ||
|
|
||
|
|
||
| def main(): | ||
| # Create a new optimization problem | ||
| prob = Problem("QP Power Dispatch") | ||
|
|
||
| # Add variables with lower and upper bounds | ||
| p1 = prob.addVariable(lb=10, ub=100) | ||
| p2 = prob.addVariable(lb=10, ub=80) | ||
| p3 = prob.addVariable(lb=10, ub=90) | ||
|
|
||
| # Add demand constraint: p1 + p2 + p3 = 150 | ||
| prob.addConstraint(p1 + p2 + p3 == 150, name="demand") | ||
|
|
||
| # Create matrix for quadratic terms: 0.01 p1^2 + 0.02 p2^2 + 0.015 p3^2 | ||
| matrix = [[0.01, 0.0, 0.0], [0.0, 0.02, 0.0], [0.0, 0.0, 0.015]] | ||
| quad_matrix = QuadraticExpression(matrix, prob.getVariables()) | ||
|
|
||
| # Set objective using matrix representation | ||
| quad_obj = quad_matrix + 8 * p1 + 6 * p2 + 7 * p3 | ||
| prob.setObjective(quad_obj, sense=MINIMIZE) | ||
|
|
||
| # Solve the problem | ||
| prob.solve() | ||
|
|
||
| # Print results | ||
| print(f"Optimal solution found in {prob.SolveTime:.2f} seconds") | ||
| print(f"p1 = {p1.Value}") | ||
| print(f"p2 = {p2.Value}") | ||
| print(f"p3 = {p3.Value}") | ||
| print(f"Minimized cost = {prob.ObjValue}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
17 changes: 2 additions & 15 deletions
17
docs/cuopt/source/cuopt-python/lp-qp-milp/examples/simple_lp_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 2 additions & 15 deletions
17
docs/cuopt/source/cuopt-python/lp-qp-milp/examples/simple_milp_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/cuopt/source/cuopt-python/lp-qp-milp/examples/simple_qp_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
docs/cuopt/source/cuopt-python/lp-qp-milp/examples/solution_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """ | ||
| Linear Programming Solution Example | ||
|
|
||
| This example demonstrates how to: | ||
| - Create a linear programming problem | ||
| - Solve the problem and retrieve result details | ||
|
|
||
| Problem: | ||
| Minimize: 3x + 2y + 5z | ||
| Subject to: | ||
| x + y + z = 4 | ||
| 2x + y + z = 5 | ||
| x, y, z >= 0 | ||
|
|
||
| Expected Output: | ||
| 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 | ||
| """ | ||
|
|
||
| from cuopt.linear_programming.problem import Problem, MINIMIZE | ||
|
|
||
|
|
||
| def main(): | ||
rgsl888prabhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Run the simple LP example.""" | ||
| problem = Problem("min_dual_rc") | ||
|
|
||
| # Add Variables | ||
| x = problem.addVariable(lb=0.0, name="x") | ||
| y = problem.addVariable(lb=0.0, name="y") | ||
| z = problem.addVariable(lb=0.0, name="z") | ||
|
|
||
| # Add Constraints (equalities) | ||
| problem.addConstraint(x + y + z == 4.0, name="c1") | ||
| problem.addConstraint(2.0 * x + y + z == 5.0, name="c2") | ||
|
|
||
| # Set Objective (minimize) | ||
| problem.setObjective(3.0 * x + 2.0 * y + 5.0 * z, sense=MINIMIZE) | ||
|
|
||
| # Solve | ||
| problem.solve() | ||
|
|
||
| # Check solution status | ||
| if problem.Status.name == "Optimal": | ||
| print(f"Optimal solution found in {problem.SolveTime:.2f} seconds") | ||
| # Get Primal | ||
| print("Objective:", problem.ObjValue) | ||
| for v in problem.getVariables(): | ||
| print( | ||
| f"{v.VariableName} = {v.Value}, ReducedCost = {v.ReducedCost}" | ||
| ) | ||
| # Get Duals | ||
| for c in problem.getConstraints(): | ||
| print(f"{c.ConstraintName} DualValue = {c.DualValue}") | ||
| else: | ||
| print(f"Problem status: {problem.Status.name}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated all examples copyright