LP/QP C API Examples#
Example With Data#
This example demonstrates how to use the LP solver in C. More details on the API can be found in C API.
The example code is available at examples/cuopt-c/lp/simple_lp_example.c (download):
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5/*
6 * Simple LP C API Example
7 *
8 * This example demonstrates how to use the cuOpt C API for linear programming.
9 *
10 * Problem:
11 * Minimize: -0.2*x1 + 0.1*x2
12 * Subject to:
13 * 3.0*x1 + 4.0*x2 <= 5.4
14 * 2.7*x1 + 10.1*x2 <= 4.9
15 * x1, x2 >= 0
16 *
17 * Expected Output:
18 * Termination status: Optimal (1)
19 * Solve time: 0.000013 seconds
20 * Objective value: -0.360000
21 * x1 = 1.800000
22 * x2 = 0.000000
23 *
24 * Build:
25 * gcc -I $INCLUDE_PATH -L $LIBCUOPT_LIBRARY_PATH -o simple_lp_example simple_lp_example.c -lcuopt
26 *
27 * Run:
28 * ./simple_lp_example
29 */
30
31// Include the cuOpt linear programming solver header
32#include <cuopt/linear_programming/cuopt_c.h>
33#include <stdio.h>
34#include <stdlib.h>
35
36// Convert termination status to string
37const char* termination_status_to_string(cuopt_int_t termination_status)
38{
39 switch (termination_status) {
40 case CUOPT_TERMINATION_STATUS_OPTIMAL:
41 return "Optimal";
42 case CUOPT_TERMINATION_STATUS_INFEASIBLE:
43 return "Infeasible";
44 case CUOPT_TERMINATION_STATUS_UNBOUNDED:
45 return "Unbounded";
46 case CUOPT_TERMINATION_STATUS_ITERATION_LIMIT:
47 return "Iteration limit";
48 case CUOPT_TERMINATION_STATUS_TIME_LIMIT:
49 return "Time limit";
50 case CUOPT_TERMINATION_STATUS_NUMERICAL_ERROR:
51 return "Numerical error";
52 case CUOPT_TERMINATION_STATUS_PRIMAL_FEASIBLE:
53 return "Primal feasible";
54 case CUOPT_TERMINATION_STATUS_FEASIBLE_FOUND:
55 return "Feasible found";
56 default:
57 return "Unknown";
58 }
59}
60
61// Test simple LP problem
62cuopt_int_t test_simple_lp()
63{
64 cuOptOptimizationProblem problem = NULL;
65 cuOptSolverSettings settings = NULL;
66 cuOptSolution solution = NULL;
67
68 /* Solve the following LP:
69 minimize -0.2*x1 + 0.1*x2
70 subject to:
71 3.0*x1 + 4.0*x2 <= 5.4
72 2.7*x1 + 10.1*x2 <= 4.9
73 x1, x2 >= 0
74 */
75
76 cuopt_int_t num_variables = 2;
77 cuopt_int_t num_constraints = 2;
78 cuopt_int_t nnz = 4;
79
80 // CSR format constraint matrix
81 // https://docs.nvidia.com/nvpl/latest/sparse/storage_format/sparse_matrix.html#compressed-sparse-row-csr
82 // From the constraints:
83 // 3.0*x1 + 4.0*x2 <= 5.4
84 // 2.7*x1 + 10.1*x2 <= 4.9
85 cuopt_int_t row_offsets[] = {0, 2, 4};
86 cuopt_int_t column_indices[] = {0, 1, 0, 1};
87 cuopt_float_t values[] = {3.0, 4.0, 2.7, 10.1};
88
89 // Objective coefficients
90 // From the objective function: minimize -0.2*x1 + 0.1*x2
91 // -0.2 is the coefficient of x1
92 // 0.1 is the coefficient of x2
93 cuopt_float_t objective_coefficients[] = {-0.2, 0.1};
94
95 // Constraint bounds
96 // From the constraints:
97 // 3.0*x1 + 4.0*x2 <= 5.4
98 // 2.7*x1 + 10.1*x2 <= 4.9
99 cuopt_float_t constraint_upper_bounds[] = {5.4, 4.9};
100 cuopt_float_t constraint_lower_bounds[] = {-CUOPT_INFINITY, -CUOPT_INFINITY};
101
102 // Variable bounds
103 // From the constraints:
104 // x1, x2 >= 0
105 cuopt_float_t var_lower_bounds[] = {0.0, 0.0};
106 cuopt_float_t var_upper_bounds[] = {CUOPT_INFINITY, CUOPT_INFINITY};
107
108 // Variable types (continuous)
109 // From the constraints:
110 // x1, x2 >= 0
111 char variable_types[] = {CUOPT_CONTINUOUS, CUOPT_CONTINUOUS};
112
113 cuopt_int_t status;
114 cuopt_float_t time;
115 cuopt_int_t termination_status;
116 cuopt_float_t objective_value;
117
118 printf("Creating and solving simple LP problem...\n");
119
120 // Create the problem
121 status = cuOptCreateRangedProblem(num_constraints,
122 num_variables,
123 CUOPT_MINIMIZE,
124 0.0, // objective offset
125 objective_coefficients,
126 row_offsets,
127 column_indices,
128 values,
129 constraint_lower_bounds,
130 constraint_upper_bounds,
131 var_lower_bounds,
132 var_upper_bounds,
133 variable_types,
134 &problem);
135 if (status != CUOPT_SUCCESS) {
136 printf("Error creating problem: %d\n", status);
137 goto DONE;
138 }
139
140 // Create solver settings
141 status = cuOptCreateSolverSettings(&settings);
142 if (status != CUOPT_SUCCESS) {
143 printf("Error creating solver settings: %d\n", status);
144 goto DONE;
145 }
146
147 // Set solver parameters
148 status = cuOptSetFloatParameter(settings, CUOPT_ABSOLUTE_PRIMAL_TOLERANCE, 0.0001);
149 if (status != CUOPT_SUCCESS) {
150 printf("Error setting optimality tolerance: %d\n", status);
151 goto DONE;
152 }
153
154 // Solve the problem
155 status = cuOptSolve(problem, settings, &solution);
156 if (status != CUOPT_SUCCESS) {
157 printf("Error solving problem: %d\n", status);
158 goto DONE;
159 }
160
161 // Get solution information
162 status = cuOptGetSolveTime(solution, &time);
163 if (status != CUOPT_SUCCESS) {
164 printf("Error getting solve time: %d\n", status);
165 goto DONE;
166 }
167
168 status = cuOptGetTerminationStatus(solution, &termination_status);
169 if (status != CUOPT_SUCCESS) {
170 printf("Error getting termination status: %d\n", status);
171 goto DONE;
172 }
173
174 status = cuOptGetObjectiveValue(solution, &objective_value);
175 if (status != CUOPT_SUCCESS) {
176 printf("Error getting objective value: %d\n", status);
177 goto DONE;
178 }
179
180 // Print results
181 printf("\nResults:\n");
182 printf("--------\n");
183 printf("Termination status: %s (%d)\n",
184 termination_status_to_string(termination_status),
185 termination_status);
186 printf("Solve time: %f seconds\n", time);
187 printf("Objective value: %f\n", objective_value);
188
189 // Get and print solution variables
190 cuopt_float_t* solution_values = (cuopt_float_t*)malloc(num_variables * sizeof(cuopt_float_t));
191 if (solution_values == NULL) {
192 printf("Error allocating solution values\n");
193 goto DONE;
194 }
195 status = cuOptGetPrimalSolution(solution, solution_values);
196 if (status != CUOPT_SUCCESS) {
197 printf("Error getting solution values: %d\n", status);
198 free(solution_values);
199 goto DONE;
200 }
201
202 printf("\nPrimal Solution: Solution variables \n");
203 for (cuopt_int_t i = 0; i < num_variables; i++) {
204 printf("x%d = %f\n", i + 1, solution_values[i]);
205 }
206 free(solution_values);
207
208DONE:
209 cuOptDestroyProblem(&problem);
210 cuOptDestroySolverSettings(&settings);
211 cuOptDestroySolution(&solution);
212
213 return status;
214}
215
216int main()
217{
218 // Run the test
219 cuopt_int_t status = test_simple_lp();
220
221 if (status == CUOPT_SUCCESS) {
222 printf("\nTest completed successfully!\n");
223 return 0;
224 } else {
225 printf("\nTest failed with status: %d\n", status);
226 return 1;
227 }
228}
It is necessary to have the path for include and library dirs ready, if you know the paths, please add them to the path variables directly. Otherwise, run the following commands to find the path and assign it to the path variables. The following commands are for Linux and might fail in cases where the cuopt library is not installed or there are multiple cuopt libraries in the system.
If you have built it locally, libcuopt.so will be in the build directory cpp/build and include directoy would be cpp/include.
# Find the cuopt header file and assign to INCLUDE_PATH
INCLUDE_PATH=$(find / -name "cuopt_c.h" -path "*/linear_programming/*" -printf "%h\n" | sed 's/\/linear_programming//' 2>/dev/null)
# Find the libcuopt library and assign to LIBCUOPT_LIBRARY_PATH
LIBCUOPT_LIBRARY_PATH=$(find / -name "libcuopt.so" 2>/dev/null)
Build and run the example
# Build and run the example
gcc -I $INCLUDE_PATH -L $LIBCUOPT_LIBRARY_PATH -o simple_lp_example simple_lp_example.c -lcuopt
./simple_lp_example
You should see the following output:
Creating and solving simple LP problem...
Solving a problem with 2 constraints 2 variables (0 integers) and 4 nonzeros
Objective offset 0.000000 scaling_factor 1.000000
Running concurrent
Dual simplex finished in 0.00 seconds
Iter Primal Obj. Dual Obj. Gap Primal Res. Dual Res. Time
0 +0.00000000e+00 +0.00000000e+00 0.00e+00 0.00e+00 2.00e-01 0.011s
PDLP finished
Concurrent time: 0.013s
Solved with dual simplex
Status: Optimal Objective: -3.60000000e-01 Iterations: 1 Time: 0.013s
Results:
--------
Termination status: Optimal (1)
Solve time: 0.000013 seconds
Objective value: -0.360000
Primal Solution: Solution variables
x1 = 1.800000
x2 = 0.000000
Test completed successfully!
Example With MPS File#
This example demonstrates how to use the cuOpt linear programming solver in C to solve an MPS file.
The example code is available at examples/cuopt-c/lp/mps_file_example.c (download):
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5/*
6 * LP MPS File C API Example
7 *
8 * This example demonstrates how to solve LP problems from MPS files using the cuOpt C API.
9 *
10 * Problem (from MPS file):
11 * Minimize: -0.2*VAR1 + 0.1*VAR2
12 * Subject to:
13 * 3*VAR1 + 4*VAR2 <= 5.4
14 * 2.7*VAR1 + 10.1*VAR2 <= 4.9
15 * VAR1, VAR2 >= 0
16 *
17 * Expected Output:
18 * Number of variables: 2
19 * Termination status: Optimal (1)
20 * Solve time: 0.000014 seconds
21 * Objective value: -0.360000
22 * x1 = 1.800000
23 * x2 = 0.000000
24 *
25 * Build:
26 * gcc -I $INCLUDE_PATH -L $LIBCUOPT_LIBRARY_PATH -o lp_example_mps lp_example_mps.c -lcuopt
27 *
28 * Run:
29 * ./lp_example_mps sample.mps
30 */
31
32#include <cuopt/linear_programming/cuopt_c.h>
33#include <stdio.h>
34#include <stdlib.h>
35
36const char* termination_status_to_string(cuopt_int_t termination_status)
37{
38 switch (termination_status) {
39 case CUOPT_TERMINATION_STATUS_OPTIMAL:
40 return "Optimal";
41 case CUOPT_TERMINATION_STATUS_INFEASIBLE:
42 return "Infeasible";
43 case CUOPT_TERMINATION_STATUS_UNBOUNDED:
44 return "Unbounded";
45 case CUOPT_TERMINATION_STATUS_ITERATION_LIMIT:
46 return "Iteration limit";
47 case CUOPT_TERMINATION_STATUS_TIME_LIMIT:
48 return "Time limit";
49 case CUOPT_TERMINATION_STATUS_NUMERICAL_ERROR:
50 return "Numerical error";
51 case CUOPT_TERMINATION_STATUS_PRIMAL_FEASIBLE:
52 return "Primal feasible";
53 case CUOPT_TERMINATION_STATUS_FEASIBLE_FOUND:
54 return "Feasible found";
55 default:
56 return "Unknown";
57 }
58}
59
60cuopt_int_t solve_mps_file(const char* filename)
61{
62 cuOptOptimizationProblem problem = NULL;
63 cuOptSolverSettings settings = NULL;
64 cuOptSolution solution = NULL;
65 cuopt_int_t status;
66 cuopt_float_t time;
67 cuopt_int_t termination_status;
68 cuopt_float_t objective_value;
69 cuopt_int_t num_variables;
70 cuopt_float_t* solution_values = NULL;
71
72 printf("Reading and solving MPS file: %s\n", filename);
73
74 // Create the problem from MPS file
75 status = cuOptReadProblem(filename, &problem);
76 if (status != CUOPT_SUCCESS) {
77 printf("Error creating problem from MPS file: %d\n", status);
78 goto DONE;
79 }
80
81 // Get problem size
82 status = cuOptGetNumVariables(problem, &num_variables);
83 if (status != CUOPT_SUCCESS) {
84 printf("Error getting number of variables: %d\n", status);
85 goto DONE;
86 }
87
88 // Create solver settings
89 status = cuOptCreateSolverSettings(&settings);
90 if (status != CUOPT_SUCCESS) {
91 printf("Error creating solver settings: %d\n", status);
92 goto DONE;
93 }
94
95 // Set solver parameters
96 status = cuOptSetFloatParameter(settings, CUOPT_ABSOLUTE_PRIMAL_TOLERANCE, 0.0001);
97 if (status != CUOPT_SUCCESS) {
98 printf("Error setting optimality tolerance: %d\n", status);
99 goto DONE;
100 }
101
102 // Solve the problem
103 status = cuOptSolve(problem, settings, &solution);
104 if (status != CUOPT_SUCCESS) {
105 printf("Error solving problem: %d\n", status);
106 goto DONE;
107 }
108
109 // Get solution information
110 status = cuOptGetSolveTime(solution, &time);
111 if (status != CUOPT_SUCCESS) {
112 printf("Error getting solve time: %d\n", status);
113 goto DONE;
114 }
115
116 status = cuOptGetTerminationStatus(solution, &termination_status);
117 if (status != CUOPT_SUCCESS) {
118 printf("Error getting termination status: %d\n", status);
119 goto DONE;
120 }
121
122 status = cuOptGetObjectiveValue(solution, &objective_value);
123 if (status != CUOPT_SUCCESS) {
124 printf("Error getting objective value: %d\n", status);
125 goto DONE;
126 }
127
128 // Print results
129 printf("\nResults:\n");
130 printf("--------\n");
131 printf("Number of variables: %d\n", num_variables);
132 printf("Termination status: %s (%d)\n",
133 termination_status_to_string(termination_status),
134 termination_status);
135 printf("Solve time: %f seconds\n", time);
136 printf("Objective value: %f\n", objective_value);
137
138 // Get and print solution variables
139 solution_values = (cuopt_float_t*)malloc(num_variables * sizeof(cuopt_float_t));
140 status = cuOptGetPrimalSolution(solution, solution_values);
141 if (status != CUOPT_SUCCESS) {
142 printf("Error getting solution values: %d\n", status);
143 goto DONE;
144 }
145
146 printf("\nPrimal Solution: First 10 solution variables (or fewer if less exist):\n");
147 for (cuopt_int_t i = 0; i < (num_variables < 10 ? num_variables : 10); i++) {
148 printf("x%d = %f\n", i + 1, solution_values[i]);
149 }
150 if (num_variables > 10) {
151 printf("... (showing only first 10 of %d variables)\n", num_variables);
152 }
153
154DONE:
155 free(solution_values);
156 cuOptDestroyProblem(&problem);
157 cuOptDestroySolverSettings(&settings);
158 cuOptDestroySolution(&solution);
159
160 return status;
161}
162
163int main(int argc, char* argv[])
164{
165 if (argc != 2) {
166 printf("Usage: %s <mps_file_path>\n", argv[0]);
167 return 1;
168 }
169
170 // Run the solver
171 cuopt_int_t status = solve_mps_file(argv[1]);
172
173 if (status == CUOPT_SUCCESS) {
174 printf("\nSolver completed successfully!\n");
175 return 0;
176 } else {
177 printf("\nSolver failed with status: %d\n", status);
178 return 1;
179 }
180}
It is necessary to have the path for include and library dirs ready, if you know the paths, please add them to the path variables directly. Otherwise, run the following commands to find the path and assign it to the path variables. The following commands are for Linux and might fail in cases where the cuopt library is not installed or there are multiple cuopt libraries in the system.
If you have built it locally, libcuopt.so will be in the build directory cpp/build and include directoy would be cpp/include.
# Find the cuopt header file and assign to INCLUDE_PATH
INCLUDE_PATH=$(find / -name "cuopt_c.h" -path "*/linear_programming/*" -printf "%h\n" | sed 's/\/linear_programming//' 2>/dev/null)
# Find the libcuopt library and assign to LIBCUOPT_LIBRARY_PATH
LIBCUOPT_LIBRARY_PATH=$(find / -name "libcuopt.so" 2>/dev/null)
A sample MPS file (download sample.mps):
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
Build and run the example
# Build and run the example
gcc -I $INCLUDE_PATH -L $LIBCUOPT_LIBRARY_PATH -o mps_file_example mps_file_example.c -lcuopt
./mps_file_example sample.mps
You should see the following output:
Reading and solving MPS file: sample.mps
Solving a problem with 2 constraints 2 variables (0 integers) and 4 nonzeros
Objective offset 0.000000 scaling_factor 1.000000
Running concurrent
Dual simplex finished in 0.00 seconds
Iter Primal Obj. Dual Obj. Gap Primal Res. Dual Res. Time
0 +0.00000000e+00 +0.00000000e+00 0.00e+00 0.00e+00 2.00e-01 0.012s
PDLP finished
Concurrent time: 0.014s
Solved with dual simplex
Status: Optimal Objective: -3.60000000e-01 Iterations: 1 Time: 0.014s
Results:
--------
Number of variables: 2
Termination status: Optimal (1)
Solve time: 0.000014 seconds
Objective value: -0.360000
Primal Solution: First 10 solution variables (or fewer if less exist):
x1 = 1.800000
x2 = 0.000000
Solver completed successfully!
Simple Quadratic Programming Example#
This example demonstrates how to use the cuOpt C API for quadratic programming.
The example code is available at examples/cuopt-c/lp/simple_qp_example.c (download):
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights
3 * reserved. SPDX-License-Identifier: Apache-2.0
4 */
5/*
6 * Simple QP C API Example
7 *
8 * This example demonstrates how to use the cuOpt C API for quadratic programming.
9 *
10 * Problem:
11 * Minimize: x^2 + y^2
12 * Subject to:
13 * x + y >= 1
14 * x, y >= 0
15 *
16 *
17 * Build:
18 * gcc -I $INCLUDE_PATH -L $LIBCUOPT_LIBRARY_PATH -o simple_qp_example simple_qp_example.c -lcuopt
19 *
20 * Run:
21 * ./simple_qp_example
22 */
23
24// Include the cuOpt linear programming solver header
25#include <cuopt/linear_programming/cuopt_c.h>
26#include <stdio.h>
27#include <stdlib.h>
28
29// Convert termination status to string
30const char* termination_status_to_string(cuopt_int_t termination_status)
31{
32 switch (termination_status) {
33 case CUOPT_TERMINATION_STATUS_OPTIMAL:
34 return "Optimal";
35 case CUOPT_TERMINATION_STATUS_INFEASIBLE:
36 return "Infeasible";
37 case CUOPT_TERMINATION_STATUS_UNBOUNDED:
38 return "Unbounded";
39 case CUOPT_TERMINATION_STATUS_ITERATION_LIMIT:
40 return "Iteration limit";
41 case CUOPT_TERMINATION_STATUS_TIME_LIMIT:
42 return "Time limit";
43 case CUOPT_TERMINATION_STATUS_NUMERICAL_ERROR:
44 return "Numerical error";
45 case CUOPT_TERMINATION_STATUS_PRIMAL_FEASIBLE:
46 return "Primal feasible";
47 case CUOPT_TERMINATION_STATUS_FEASIBLE_FOUND:
48 return "Feasible found";
49 case CUOPT_TERMINATION_STATUS_UNBOUNDED_OR_INFEASIBLE:
50 return "Unbounded or infeasible";
51 default:
52 return "Unknown";
53 }
54}
55
56// Test simple QP problem
57cuopt_int_t test_simple_qp()
58{
59 cuOptOptimizationProblem problem = NULL;
60 cuOptSolverSettings settings = NULL;
61 cuOptSolution solution = NULL;
62
63 /* Solve the following QP:
64 minimize x^2 + y^2
65 subject to:
66 x + y >= 1
67 x, y >= 0
68 */
69
70 cuopt_int_t num_variables = 2;
71 cuopt_int_t num_constraints = 1;
72 cuopt_int_t nnz = 2;
73
74 // CSR format constraint matrix
75 // https://docs.nvidia.com/nvpl/latest/sparse/storage_format/sparse_matrix.html#compressed-sparse-row-csr
76 cuopt_int_t row_offsets[] = {0, 2};
77 cuopt_int_t column_indices[] = {0, 1};
78 cuopt_float_t values[] = {1.0, 1.0};
79
80 // Objective coefficients
81 // From the objective function: minimize x^2 + y^2
82 // 0 is the coefficient of the linear term on x
83 // 0 is the coefficient of the linear term on y
84 cuopt_float_t linear_objective_coefficients[] = {0.0, 0.0};
85
86 // Quadratic objective matrix
87 // From the objective function: minimize x^2 + y^2
88 // 1 is the coefficient of the quadratic term on x^2
89 // 1 is the coefficient of the quadratic term on y^2
90 cuopt_float_t quadratic_objective_matrix_values[] = {1.0, 1.0};
91 cuopt_int_t quadratic_objective_matrix_row_offsets[] = {0, 1, 2};
92 cuopt_int_t quadratic_objective_matrix_column_indices[] = {0, 1};
93
94 // Constraint bounds
95 // From the constraints:
96 // x + y >= 1
97 cuopt_float_t constraint_rhs[] = {1.0};
98 char constraint_sense[] = {CUOPT_GREATER_THAN};
99
100 // Variable bounds
101 // From the constraints:
102 // x1, x2 >= 0
103 cuopt_float_t var_lower_bounds[] = {0.0, 0.0};
104 cuopt_float_t var_upper_bounds[] = {CUOPT_INFINITY, CUOPT_INFINITY};
105
106 // Variable types (continuous)
107 // From the constraints:
108 // x1, x2 >= 0
109 char variable_types[] = {CUOPT_CONTINUOUS, CUOPT_CONTINUOUS};
110
111 cuopt_int_t status;
112 cuopt_float_t time;
113 cuopt_int_t termination_status;
114 cuopt_float_t objective_value;
115
116 printf("Creating and solving simple QP problem...\n");
117
118 // Create the problem
119 status = cuOptCreateQuadraticProblem(num_constraints,
120 num_variables,
121 CUOPT_MINIMIZE,
122 0.0, // objective offset
123 linear_objective_coefficients,
124 quadratic_objective_matrix_row_offsets,
125 quadratic_objective_matrix_column_indices,
126 quadratic_objective_matrix_values,
127 row_offsets,
128 column_indices,
129 values,
130 constraint_sense,
131 constraint_rhs,
132 var_lower_bounds,
133 var_upper_bounds,
134 &problem);
135 if (status != CUOPT_SUCCESS) {
136 printf("Error creating problem: %d\n", status);
137 goto DONE;
138 }
139
140 // Create solver settings
141 status = cuOptCreateSolverSettings(&settings);
142 if (status != CUOPT_SUCCESS) {
143 printf("Error creating solver settings: %d\n", status);
144 goto DONE;
145 }
146
147 // Solve the problem
148 status = cuOptSolve(problem, settings, &solution);
149 if (status != CUOPT_SUCCESS) {
150 printf("Error solving problem: %d\n", status);
151 goto DONE;
152 }
153
154 // Get solution information
155 status = cuOptGetSolveTime(solution, &time);
156 if (status != CUOPT_SUCCESS) {
157 printf("Error getting solve time: %d\n", status);
158 goto DONE;
159 }
160
161 status = cuOptGetTerminationStatus(solution, &termination_status);
162 if (status != CUOPT_SUCCESS) {
163 printf("Error getting termination status: %d\n", status);
164 goto DONE;
165 }
166
167 status = cuOptGetObjectiveValue(solution, &objective_value);
168 if (status != CUOPT_SUCCESS) {
169 printf("Error getting objective value: %d\n", status);
170 goto DONE;
171 }
172
173 // Print results
174 printf("\nResults:\n");
175 printf("--------\n");
176 printf("Termination status: %s (%d)\n",
177 termination_status_to_string(termination_status),
178 termination_status);
179 printf("Solve time: %f seconds\n", time);
180 printf("Objective value: %f\n", objective_value);
181
182 // Get and print solution variables
183 cuopt_float_t* solution_values = (cuopt_float_t*)malloc(num_variables * sizeof(cuopt_float_t));
184 if (solution_values == NULL) {
185 printf("Error allocating solution values\n");
186 goto DONE;
187 }
188 status = cuOptGetPrimalSolution(solution, solution_values);
189 if (status != CUOPT_SUCCESS) {
190 printf("Error getting solution values: %d\n", status);
191 free(solution_values);
192 goto DONE;
193 }
194
195 printf("\nPrimal Solution: Solution variables \n");
196 for (cuopt_int_t i = 0; i < num_variables; i++) {
197 printf("x%d = %f\n", i + 1, solution_values[i]);
198 }
199 free(solution_values);
200
201DONE:
202 cuOptDestroyProblem(&problem);
203 cuOptDestroySolverSettings(&settings);
204 cuOptDestroySolution(&solution);
205
206 return status;
207}
208
209int main()
210{
211 // Run the test
212 cuopt_int_t status = test_simple_qp();
213
214 if (status == CUOPT_SUCCESS) {
215 printf("\nTest completed successfully!\n");
216 return 0;
217 } else {
218 printf("\nTest failed with status: %d\n", status);
219 return 1;
220 }
221}
Build and run the example
# Build and run the example
gcc -I $INCLUDE_PATH -L $LIBCUOPT_LIBRARY_PATH -o simple_qp_example simple_qp_example.c -lcuopt
./simple_qp_example
You should see the following output:
Creating and solving simple QP problem...
Status: Optimal
Objective value: 0.500000
x = 0.500000
y = 0.500000
Test completed successfully!