Commit 49e69367 authored by roangel's avatar roangel
Browse files

Finally, first working version of GUROBI solving the opt problem as Matlab does

parent 55975c35
...@@ -334,96 +334,100 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M ...@@ -334,96 +334,100 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
// Copy and paste again the QP example: -------------------------------------- // Copy and paste again the QP example: --------------------------------------
GRBenv *env = NULL; // GRBenv *env = NULL;
GRBmodel *model = NULL; // GRBmodel *model = NULL;
int error = 0; // int error = 0;
double sol[3]; // double sol[3];
int ind[3]; // int ind[3];
double val[3]; // double val[3];
int qrow[5]; // int qrow[5];
int qcol[5]; // int qcol[5];
double qval[5]; // double qval[5];
char vtype[3]; // char vtype[3];
int optimstatus; // int optimstatus;
double objval; // double objval;
// double obj_1[] = {2.0, 0.0, 0.0};
// double lb[] = {-GRB_INFINITY, -GRB_INFINITY, -GRB_INFINITY};
// /* Create environment */
// error = GRBloadenv(&env, "qp.log");
// if (error) goto QUIT;
/* Create environment */ // /* Create an empty model */
error = GRBloadenv(&env, "qp.log");
if (error) goto QUIT;
/* Create an empty model */ // error = GRBnewmodel(env, &model, "qp", 3, obj_1, lb, NULL, NULL, NULL);
// if (error) goto QUIT;
error = GRBnewmodel(env, &model, "qp", 0, NULL, NULL, NULL, NULL, NULL); // /* Add variables */
if (error) goto QUIT;
/* Add variables */ // // error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
// // NULL);
// // if (error) goto QUIT;
error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // /* Quadratic objective terms */
NULL);
if (error) goto QUIT;
/* Quadratic objective terms */ // qrow[0] = 0; qrow[1] = 0; qrow[2] = 1; qrow[3] = 1; qrow[4] = 2;
// qcol[0] = 0; qcol[1] = 1; qcol[2] = 1; qcol[3] = 2; qcol[4] = 2;
// qval[0] = 1; qval[1] = 1; qval[2] = 1; qval[3] = 1; qval[4] = 1;
qrow[0] = 0; qrow[1] = 0; qrow[2] = 1; qrow[3] = 1; qrow[4] = 2; // error = GRBaddqpterms(model, 5, qrow, qcol, qval);
qcol[0] = 0; qcol[1] = 1; qcol[2] = 1; qcol[3] = 2; qcol[4] = 2; // if (error) goto QUIT;
qval[0] = 1; qval[1] = 1; qval[2] = 1; qval[3] = 1; qval[4] = 1;
error = GRBaddqpterms(model, 5, qrow, qcol, qval); // /* Linear objective term */
if (error) goto QUIT;
/* Linear objective term */ // // error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, 0, 2.0);
// // if (error) goto QUIT;
error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, 0, 2.0); // // // First constraint: x + 2 y + 3 z <= 4
if (error) goto QUIT;
// // First constraint: x + 2 y + 3 z <= 4 // // ind[0] = 0; ind[1] = 1; ind[2] = 2;
// // val[0] = 0; val[1] = 0; val[2] = 0;
// ind[0] = 0; ind[1] = 1; ind[2] = 2; // // error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 0, "c0");
// val[0] = 0; val[1] = 0; val[2] = 0; // // if (error) goto QUIT;
// error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 0, "c0"); // // /* Second constraint: x + y >= 1 */
// if (error) goto QUIT;
// /* Second constraint: x + y >= 1 */ // // ind[0] = 0; ind[1] = 1;
// // val[0] = 1; val[1] = 1;
// ind[0] = 0; ind[1] = 1; // // error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1");
// val[0] = 1; val[1] = 1; // // if (error) goto QUIT;
// error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1"); // /* Optimize model */
// error = GRBoptimize(model);
// if (error) goto QUIT; // if (error) goto QUIT;
/* Optimize model */ // /* Write model to 'qp.lp' */
error = GRBoptimize(model); // error = GRBwrite(model, "qp.lp");
if (error) goto QUIT; // if (error) goto QUIT;
/* Write model to 'qp.lp' */ // /* Capture solution information */
error = GRBwrite(model, "qp.lp"); // error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) goto QUIT; // if (error) goto QUIT;
/* Capture solution information */ // error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
// if (error) goto QUIT;
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); // error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
if (error) goto QUIT; // if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); // printf("\nOptimization complete\n");
if (error) goto QUIT; // if (optimstatus == GRB_OPTIMAL) {
// printf("Optimal objective: %.4e\n", objval);
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol); // printf(" x=%.4f, y=%.4f, z=%.4f\n", sol[0], sol[1], sol[2]);
if (error) goto QUIT; // } else if (optimstatus == GRB_INF_OR_UNBD) {
// printf("Model is infeasible or unbounded\n");
printf("\nOptimization complete\n"); // } else {
if (optimstatus == GRB_OPTIMAL) { // printf("Optimization was stopped early\n");
printf("Optimal objective: %.4e\n", objval); // }
printf(" x=%.4f, y=%.4f, z=%.4f\n", sol[0], sol[1], sol[2]);
} else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n");
} else {
printf("Optimization was stopped early\n");
}
/* Modify variable types */ /* Modify variable types */
...@@ -445,208 +449,210 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M ...@@ -445,208 +449,210 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
/* Capture solution information */ /* Capture solution information */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); // error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) goto QUIT; // if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); // error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT; // if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol); // error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
if (error) goto QUIT; // if (error) goto QUIT;
printf("\nOptimization complete\n"); // printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) { // if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval); // printf("Optimal objective: %.4e\n", objval);
printf(" x=%.4f, y=%.4f, z=%.4f\n", sol[0], sol[1], sol[2]); // printf(" x=%.4f, y=%.4f, z=%.4f\n", sol[0], sol[1], sol[2]);
} else if (optimstatus == GRB_INF_OR_UNBD) { // } else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n"); // printf("Model is infeasible or unbounded\n");
} else { // } else {
printf("Optimization was stopped early\n"); // printf("Optimization was stopped early\n");
} // }
QUIT: // QUIT:
/* Error reporting */ // /* Error reporting */
if (error) { // if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env)); // printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1); // exit(1);
} // }
/* Free model */ // /* Free model */
GRBfreemodel(model); // GRBfreemodel(model);
/* Free environment */ // /* Free environment */
GRBfreeenv(env); // GRBfreeenv(env);
// Trial with QP example: -------------------------------------------------------------------------- // Trial with QP example: --------------------------------------------------------------------------
// int size_H = H.cols(); int size_H = H.cols();
// std::vector<int> q_row; std::vector<int> q_row;
// std::vector<int> q_col; std::vector<int> q_col;
// std::vector<double> q_val; std::vector<double> q_val;
// std::vector<int> l_obj_ind; std::vector<int> l_obj_ind;
// std::vector<double> l_obj_coeff; std::vector<double> l_obj_coeff;
// for(int i = 0; i < size_H; i++) for(int i = 0; i < size_H; i++)
// { {
// for(int j = i; j < size_H; j++) for(int j = i; j < size_H; j++)
// { {
// std::cout << "i,j -->" << i << "," << j << std::endl; std::cout << "i,j -->" << i << "," << j << std::endl;
// if(H(i,j) != 0) if(H(i,j) != 0)
// { {
// q_row.push_back(i); q_row.push_back(i);
// q_col.push_back(j); q_col.push_back(j);
// if(i == j) if(i == j)
// { {
// q_val.push_back(H(i,j)); q_val.push_back(H(i,j));
// std::cout << "H(i,j)" << H(i,j) << endl; std::cout << "H(i,j)" << H(i,j) << endl;
// } }
// else else
// { {
// q_val.push_back(2*H(i,j)); q_val.push_back(2*H(i,j));
// std::cout << "2H(i,j)" << 2*H(i,j) << endl; std::cout << "2H(i,j)" << 2*H(i,j) << endl;
// } }
// } }
// } }
// } l_obj_coeff.push_back(F(i));
}
// // int N_vars = N_u*N; int N_vars = N_u*N;
// // int N_q_coeffs = q_val.size(); int N_q_coeffs = q_val.size();
// int N_vars = 2;
// int N_q_coeffs = 2;
// GRBenv *env = NULL; GRBenv *env = NULL;
// GRBmodel *model = NULL; GRBmodel *model = NULL;
// int error = 0; int error = 0;
// double sol[N_vars]; double sol[N_vars];
// int ind[N_vars]; int ind[N_vars];
// double val[N_vars]; double val[N_vars];
// // int* qrow = q_row.data(); int* qrow = q_row.data();
// // int* qcol = q_col.data(); int* qcol = q_col.data();
// // double* qval = q_val.data(); double* qval = q_val.data();
// int qrow[] = {0,1}; // int qrow[] = {0,1};
// int qcol[] = {0,1}; // int qcol[] = {0,1};
// double qval[] = {1,2}; // double qval[] = {1,2};
// double c[] = {2,2}; // double c[] = {2,2};
// char vtype[N_vars]; // char vtype[N_vars];
// int optimstatus; int optimstatus;
// double objval; double objval;
// /* Create environment */ std::vector<double> ub(N_vars, GRB_INFINITY);
std::vector<double> lb(N_vars, -GRB_INFINITY);
// error = GRBloadenv(&env, "qp.log"); /* Create environment */
// if (error) goto QUIT;
// /* Create an empty model */ error = GRBloadenv(&env, "qp.log");
if (error) goto QUIT;
// error = GRBnewmodel(env, &model, "qp", 0, NULL, NULL, NULL, NULL, NULL); /* Create an empty model */
// if (error) goto QUIT;
// /* Add variables */ error = GRBnewmodel(env, &model, "qp", N_vars, l_obj_coeff.data(),lb.data(), ub.data(), NULL, NULL);
if (error) goto QUIT;
// error = GRBaddvars(model, N_vars, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* Add variables */
// NULL);
// if (error) goto QUIT;
// /* Quadratic objective terms */ // error = GRBaddvars(model, N_vars, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
// NULL);
// if (error) goto QUIT;
// error = GRBaddqpterms(model, N_q_coeffs, qrow, qcol, qval); /* Quadratic objective terms */
// if (error) goto QUIT;
// /* Linear objective term */ error = GRBaddqpterms(model, N_q_coeffs, qrow, qcol, qval);
// // for(int i = 0; i < l_obj_ind.size(); i++) if (error) goto QUIT;
// // {
// // error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, l_obj_ind[i] , l_obj_coeff[i]); /* Linear objective term */
// // std::cout << "l_obj_ind[i]" << std::endl; // for(int i = 0; i < l_obj_ind.size(); i++)
// // std::cout << l_obj_ind[i] << std::endl; // {
// // std::cout << "l_obj_coeff[i]" << std::endl; // error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, l_obj_ind[i] , l_obj_coeff[i]);
// // std::cout << l_obj_coeff[i] << std::endl; // std::cout << "l_obj_ind[i]" << std::endl;
// // if (error) goto QUIT; // std::cout << l_obj_ind[i] << std::endl;
// // } // std::cout << "l_obj_coeff[i]" << std::endl;
// std::cout << l_obj_coeff[i] << std::endl;
// error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, 0, 2.0); // if (error) goto QUIT;
// if (error) goto QUIT; // }
// error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, 1, 2.0);
// if (error) goto QUIT; // error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, 0, 2.0);
// if (error) goto QUIT;
// error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, 1, 2.0);
// if (error) goto QUIT;
// // First constraint: x + 2 y + 3 z <= 4 // First constraint: x + 2 y + 3 z <= 4
// // ind[0] = 0; ind[1] = 1; ind[2] = 2; // ind[0] = 0; ind[1] = 1; ind[2] = 2;
// // val[0] = 1; val[1] = 2; val[2] = 3; // val[0] = 1; val[1] = 2; val[2] = 3;
// // error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 4.0, "c0"); // error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 4.0, "c0");
// // if (error) goto QUIT; // if (error) goto QUIT;
// /* Second constraint: x + y >= 1 */ /* Second constraint: x + y >= 1 */
// // ind[0] = 0; ind[1] = 1; // ind[0] = 0; ind[1] = 1;
// // val[0] = 1; val[1] = 1; // val[0] = 1; val[1] = 1;
// // error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1"); // error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1");
// // if (error) goto QUIT; // if (error) goto QUIT;
// // Optimize model // Optimize model
// error = GRBoptimize(model); error = GRBoptimize(model);
// if (error) goto QUIT; if (error) goto QUIT;
// /* Write model to 'qp.lp' */ /* Write model to 'qp.lp' */
// error = GRBwrite(model, "qp.lp"); error = GRBwrite(model, "qp.lp");
// if (error) goto QUIT; if (error) goto QUIT;
// /* Capture solution information */ /* Capture solution information */
// error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
// if (error) goto QUIT; if (error) goto QUIT;
// error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
// if (error) goto QUIT; if (error) goto QUIT;
// error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, N_vars, sol); error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, N_vars, sol);
// if (error) goto QUIT; if (error) goto QUIT;
// printf("\nOptimization complete\n"); printf("\nOptimization complete\n");
// if (optimstatus == GRB_OPTIMAL) { if (optimstatus == GRB_OPTIMAL) {
// printf("Optimal objective: %.4e\n", objval); printf("Optimal objective: %.4e\n", objval);
// for(int i = 0; i < N_vars; i++) for(int i = 0; i < N_vars; i++)
// { {
// printf("sol[i] = %.4f\n", sol[i]); printf("sol[i] = %.4f\n", sol[i]);
// } }
// } else if (optimstatus == GRB_INF_OR_UNBD) { } else if (optimstatus == GRB_INF_OR_UNBD) {
// printf("Model is infeasible or unbounded\n"); printf("Model is infeasible or unbounded\n");
// } else { } else {
// printf("Optimization was stopped early\n"); printf("Optimization was stopped early\n");
// } }
// QUIT: QUIT:
// /* Error reporting */ /* Error reporting */
// if (error) { if (error) {
// printf("ERROR: %s\n", GRBgeterrormsg(env)); printf("ERROR: %s\n", GRBgeterrormsg(env));
// exit(1); exit(1);
// } }
// /* Free model */ /* Free model */
// GRBfreemodel(model); GRBfreemodel(model);
// /* Free environment */ /* Free environment */
// GRBfreeenv(env); GRBfreeenv(env);
// Trial with Dense.c example: ---------------------------------------------------------------------- // Trial with Dense.c example: ----------------------------------------------------------------------
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment