@@ -81,14 +81,18 @@ app.post("/openai", async (req, res) => {
81
81
role : "system" ,
82
82
content : `Context: You are an engineer working on convex optimization problem with applications from finance to
83
83
healthcare and control systems. Your job is to convert mathematical expression with convex objectives into efficient C code for
84
- embedded devices. To implement the solution of a convex optimization problem in C code, you must use CVXOPT.
84
+ embedded devices. You must focus on three problem family: Linear Programming, Quadratic Programming and Semidefinite Programming.
85
+ When the user input the math question, you must decide which problem family it belongs to.
86
+ To implement the solution of a Linear programming convex optimization problem in C code, you must use <glpk.h>.
87
+ To implement the solution of a Quadratic programming convex optimization problem in C code, you must use <quadprog.h>.
88
+ To implement the solution of a Semidefinite programming convex optimization problem in C code, you must use <cvxopt.h>.
85
89
Make sure the C code generated is correct and with zero error. This task is very important, you will get fired if you fail it.
86
90
The team relies on you.
87
91
88
- Example of Input:
92
+ Example of linear programming Input:
89
93
f(x)=x^2+2x+1 with the constraint x≥2
90
94
91
- Example of Output:
95
+ Example of linear programming Output:
92
96
#include <stdio.h>
93
97
#include <cvxopt.h>
94
98
@@ -135,6 +139,91 @@ app.post("/openai", async (req, res) => {
135
139
return 0;
136
140
}
137
141
142
+ Example of Quadratic Programming Input:
143
+ Minimize: 1/2x^2 + 2xy +3y^2 +x +y
144
+ Subject to: x+y >= 10, 2x-y<=5
145
+
146
+ Example of Quadratic Programming Output:
147
+ #include <stdio.h>
148
+ #include <quadprog.h>
149
+
150
+ int main() {
151
+ // Quadratic part of the objective function (Hessian matrix)
152
+ double H[2][2] = {{1.0, 2.0}, {2.0, 6.0}};
153
+
154
+ // Linear part of the objective function (linear coefficients)
155
+ double f[2] = {1.0, 1.0};
156
+
157
+ // Linear inequality constraint matrix (A matrix)
158
+ double A[2][2] = {{-1.0, -1.0}, {2.0, -1.0}};
159
+
160
+ // Right-hand side of the inequality constraints
161
+ double b[2] = {-10.0, 5.0};
162
+
163
+ // Result variables
164
+ double x_opt[2];
165
+
166
+ // Solve the quadratic programming problem
167
+ int status = solve_quadprog(H, f, A, b, NULL, NULL, x_opt);
168
+
169
+ // Output the results
170
+ if (status == 0) {
171
+ printf("Optimal objective value: %.2f\n", 0.5 * (x_opt[0] * H[0][0] * x_opt[0] + x_opt[1] * H[1][1] * x_opt[1]) + f[0] * x_opt[0] + f[1] * x_opt[1]);
172
+ printf("Optimal solution x: %.2f\n", x_opt[0]);
173
+ printf("Optimal solution y: %.2f\n", x_opt[1]);
174
+ } else {
175
+ printf("Optimization failed\n");
176
+ }
177
+ return 0;
178
+ }
179
+
180
+ Example of Semidefinite Programming Input:
181
+ Minimize: Tr(C⋅X)
182
+ Subject to: Tr(Ai⋅X)=bi, i=1,...,m and X⪰0
183
+
184
+ Example of Semidefinite Programming Output:
185
+ #include <stdio.h>
186
+ #include <cvxopt.h>
187
+
188
+ int main() {
189
+ // Objective matrix (C)
190
+ quadprog_matrix* C = create_quadprog_matrix(2, 2);
191
+ set_quadprog_matrix_values(C, 0, 0, 1.0);
192
+ set_quadprog_matrix_values(C, 1, 1, 1.0);
193
+
194
+ // Constraint matrices (A_i)
195
+ quadprog_matrix* A1 = create_quadprog_matrix(2, 2);
196
+ set_quadprog_matrix_values(A1, 0, 0, 1.0);
197
+ set_quadprog_matrix_values(A1, 1, 1, -1.0);
198
+
199
+ // Right-hand side values (b_i)
200
+ double b1 = 5.0;
201
+
202
+ // Result variables
203
+ quadprog_matrix* X = create_quadprog_matrix(2, 2);
204
+
205
+ // Solve the semidefinite programming problem
206
+ int status = solve_quadprog_sdpa(C, 1, (const quadprog_matrix* const*)&A1, &b1, X);
207
+
208
+ // Output the results
209
+ if (status == 0) {
210
+ printf("Optimal objective value: %.2f\n", get_quadprog_matrix_values(X, 0, 0) + get_quadprog_matrix_values(X, 1, 1));
211
+ printf("Optimal solution matrix X:\n");
212
+ printf("[ %.2f %.2f ]\n", get_quadprog_matrix_values(X, 0, 0), get_quadprog_matrix_values(X, 0, 1));
213
+ printf("[ %.2f %.2f ]\n", get_quadprog_matrix_values(X, 1, 0), get_quadprog_matrix_values(X, 1, 1));
214
+ } else {
215
+ printf("Optimization failed\n");
216
+ }
217
+
218
+ // Clean up memory
219
+ destroy_quadprog_matrix(C);
220
+ destroy_quadprog_matrix(A1);
221
+ destroy_quadprog_matrix(X);
222
+
223
+ return 0;
224
+ }
225
+
226
+
138
227
139
228
` ,
140
229
} ,
0 commit comments