To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

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
// Copy and paste again the QP example: --------------------------------------
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
double sol[3];
int ind[3];
double val[3];
int qrow[5];
int qcol[5];
double qval[5];
char vtype[3];
int optimstatus;
double objval;
// GRBenv *env = NULL;
// GRBmodel *model = NULL;
// int error = 0;
// double sol[3];
// int ind[3];
// double val[3];
// int qrow[5];
// int qcol[5];
// double qval[5];
// char vtype[3];
// int optimstatus;
// 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);
if (error) goto QUIT;
// /* Add variables */
/* 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,
NULL);
if (error) goto QUIT;
// /* Quadratic objective terms */
/* 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;
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;
// error = GRBaddqpterms(model, 5, qrow, qcol, qval);
// if (error) goto QUIT;
error = GRBaddqpterms(model, 5, qrow, qcol, qval);
if (error) goto QUIT;
// /* Linear objective term */
/* 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);
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;
// // val[0] = 0; val[1] = 0; val[2] = 0;
// ind[0] = 0; ind[1] = 1; ind[2] = 2;
// val[0] = 0; val[1] = 0; val[2] = 0;
// // error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 0, "c0");
// // if (error) goto QUIT;
// error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 0, "c0");
// if (error) goto QUIT;
// // /* Second constraint: x + y >= 1 */
// /* Second constraint: x + y >= 1 */
// // ind[0] = 0; ind[1] = 1;
// // val[0] = 1; val[1] = 1;
// ind[0] = 0; ind[1] = 1;
// val[0] = 1; val[1] = 1;
// // error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1");
// // 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;
/* Optimize model */
// /* Write model to 'qp.lp' */
error = GRBoptimize(model);
if (error) goto QUIT;
// error = GRBwrite(model, "qp.lp");
// if (error) goto QUIT;
/* Write model to 'qp.lp' */
// /* Capture solution information */
error = GRBwrite(model, "qp.lp");
if (error) goto QUIT;
// error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
// 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);
if (error) goto QUIT;
// error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
// if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
// printf("\nOptimization complete\n");
// if (optimstatus == GRB_OPTIMAL) {
// printf("Optimal objective: %.4e\n", objval);
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
if (error) goto QUIT;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
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");
}
// 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 */
......@@ -445,208 +449,210 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
/* Capture solution information */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) goto QUIT;
// error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
// if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
// error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
// if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
if (error) goto QUIT;
// error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
// if (error) goto QUIT;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval);
// printf("\nOptimization complete\n");
// if (optimstatus == GRB_OPTIMAL) {
// 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");
}
// 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");
// }
QUIT:
// QUIT:
/* Error reporting */
// /* Error reporting */
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
// if (error) {
// printf("ERROR: %s\n", GRBgeterrormsg(env));
// exit(1);
// }
/* Free model */
// /* Free model */
GRBfreemodel(model);
// GRBfreemodel(model);
/* Free environment */
// /* Free environment */
GRBfreeenv(env);
// GRBfreeenv(env);
// Trial with QP example: --------------------------------------------------------------------------
// int size_H = H.cols();
int size_H = H.cols();
// std::vector<int> q_row;
// std::vector<int> q_col;
// std::vector<double> q_val;
// std::vector<int> l_obj_ind;
// std::vector<double> l_obj_coeff;
std::vector<int> q_row;
std::vector<int> q_col;
std::vector<double> q_val;
std::vector<int> l_obj_ind;
std::vector<double> l_obj_coeff;
// for(int i = 0; i < size_H; i++)
// {
// for(int j = i; j < size_H; j++)
// {
// std::cout << "i,j -->" << i << "," << j << std::endl;
// if(H(i,j) != 0)
// {
// q_row.push_back(i);
// q_col.push_back(j);
// if(i == j)
// {
// q_val.push_back(H(i,j));
// std::cout << "H(i,j)" << H(i,j) << endl;
// }
// else
// {
// q_val.push_back(2*H(i,j));
// std::cout << "2H(i,j)" << 2*H(i,j) << endl;
// }
// }
// }
// }
for(int i = 0; i < size_H; i++)
{
for(int j = i; j < size_H; j++)
{
std::cout << "i,j -->" << i << "," << j << std::endl;
if(H(i,j) != 0)
{
q_row.push_back(i);
q_col.push_back(j);
if(i == j)
{
q_val.push_back(H(i,j));
std::cout << "H(i,j)" << H(i,j) << endl;
}
else
{
q_val.push_back(2*H(i,j));
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_q_coeffs = q_val.size();
int N_vars = N_u*N;
int N_q_coeffs = q_val.size();
// int N_vars = 2;
// int N_q_coeffs = 2;
// GRBenv *env = NULL;
// GRBmodel *model = NULL;
// int error = 0;
// double sol[N_vars];
// int ind[N_vars];
// double val[N_vars];
// // int* qrow = q_row.data();
// // int* qcol = q_col.data();
// // double* qval = q_val.data();
// int qrow[] = {0,1};
// int qcol[] = {0,1};
// double qval[] = {1,2};
// double c[] = {2,2};
// char vtype[N_vars];
// int optimstatus;
// double objval;
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
double sol[N_vars];
int ind[N_vars];
double val[N_vars];
int* qrow = q_row.data();
int* qcol = q_col.data();
double* qval = q_val.data();
// int qrow[] = {0,1};
// int qcol[] = {0,1};
// double qval[] = {1,2};
// double c[] = {2,2};
// char vtype[N_vars];
int optimstatus;
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");
// if (error) goto QUIT;
/* Create environment */
// /* Create an empty model */
error = GRBloadenv(&env, "qp.log");
if (error) goto QUIT;
// error = GRBnewmodel(env, &model, "qp", 0, NULL, NULL, NULL, NULL, NULL);
// if (error) goto QUIT;
/* Create an empty model */
// /* 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,
// NULL);
// if (error) goto QUIT;
/* Add variables */
// /* 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);
// if (error) goto QUIT;
/* Quadratic objective terms */
// /* Linear objective term */
// // for(int i = 0; i < l_obj_ind.size(); i++)
// // {
// // error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, l_obj_ind[i] , l_obj_coeff[i]);
// // std::cout << "l_obj_ind[i]" << std::endl;
// // std::cout << l_obj_ind[i] << std::endl;
// // std::cout << "l_obj_coeff[i]" << std::endl;
// // std::cout << l_obj_coeff[i] << std::endl;
// // 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;
error = GRBaddqpterms(model, N_q_coeffs, qrow, qcol, qval);
if (error) goto QUIT;
/* Linear objective term */
// for(int i = 0; i < l_obj_ind.size(); i++)
// {
// error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, l_obj_ind[i] , l_obj_coeff[i]);
// std::cout << "l_obj_ind[i]" << std::endl;
// std::cout << l_obj_ind[i] << std::endl;
// std::cout << "l_obj_coeff[i]" << std::endl;
// std::cout << l_obj_coeff[i] << std::endl;
// 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;
// // val[0] = 1; val[1] = 2; val[2] = 3;
// ind[0] = 0; ind[1] = 1; ind[2] = 2;
// val[0] = 1; val[1] = 2; val[2] = 3;
// // error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 4.0, "c0");
// // if (error) goto QUIT;
// error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 4.0, "c0");
// if (error) goto QUIT;
// /* Second constraint: x + y >= 1 */
/* Second constraint: x + y >= 1 */
// // ind[0] = 0; ind[1] = 1;
// // val[0] = 1; val[1] = 1;
// ind[0] = 0; ind[1] = 1;
// val[0] = 1; val[1] = 1;
// // error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1");
// // if (error) goto QUIT;
// error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1");
// if (error) goto QUIT;
// // Optimize model
// Optimize model
// error = GRBoptimize(model);
// if (error) goto QUIT;
error = GRBoptimize(model);
if (error) goto QUIT;
// /* Write model to 'qp.lp' */
/* Write model to 'qp.lp' */
// error = GRBwrite(model, "qp.lp");
// if (error) goto QUIT;
error = GRBwrite(model, "qp.lp");
if (error) goto QUIT;
// /* Capture solution information */
/* Capture solution information */
// error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
// if (error) goto QUIT;
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) goto QUIT;
// error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
// if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
// error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, N_vars, sol);
// if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, N_vars, sol);
if (error) goto QUIT;
// printf("\nOptimization complete\n");
// if (optimstatus == GRB_OPTIMAL) {
// printf("Optimal objective: %.4e\n", objval);
// for(int i = 0; i < N_vars; i++)
// {
// printf("sol[i] = %.4f\n", sol[i]);
// }
// } else if (optimstatus == GRB_INF_OR_UNBD) {
// printf("Model is infeasible or unbounded\n");
// } else {
// printf("Optimization was stopped early\n");
// }
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval);
for(int i = 0; i < N_vars; i++)
{
printf("sol[i] = %.4f\n", sol[i]);
}
} else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n");
} else {
printf("Optimization was stopped early\n");
}
// QUIT:
QUIT:
// /* Error reporting */
/* Error reporting */
// if (error) {
// printf("ERROR: %s\n", GRBgeterrormsg(env));
// exit(1);
// }
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
// /* Free model */
/* Free model */
// GRBfreemodel(model);
GRBfreemodel(model);
// /* Free environment */
/* Free environment */
// GRBfreeenv(env);
GRBfreeenv(env);
// Trial with Dense.c example: ----------------------------------------------------------------------
......
Markdown is supported
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