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 64c5ca9f authored by roangel's avatar roangel
Browse files

First draft of trying to shape the problem as gurobi input in C

parent 8af6f3dc
......@@ -306,6 +306,45 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
F = 2*(x0.transpose()*Sx.transpose()*Qbar*Su + g_tray_col.transpose()*Sg.transpose()*Qbar*Su - X_ref.transpose()*Qbar*Su - U_ref.transpose()*Rbar);
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;
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;
}
}
}
if(F(i) != 0)
{
l_obj_ind.push_back(i);
l_obj_coeff.push_back(F(i));
}
}
// Eigen::IOFormat CleanFmt(4, 0, ", ", "\n", "[", "]");
......@@ -315,17 +354,19 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
// std::cout << "F: " << std::endl;
// std::cout << F.format(CleanFmt) << std::endl;
int N_vars = N_u*N;
int N_q_coeffs = q_val.size();
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];
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();
char vtype[N_vars];
int optimstatus;
double objval;
......@@ -341,39 +382,42 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
/* Add variables */
error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
error = GRBaddvars(model, N_vars, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
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;
error = GRBaddqpterms(model, 5, qrow, qcol, qval);
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;
/* 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 */
......@@ -393,14 +437,16 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
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);
printf(" x=%.4f, y=%.4f, z=%.4f\n", sol[0], sol[1], sol[2]);
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 {
......@@ -408,45 +454,6 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
}
/* Modify variable types */
vtype[0] = GRB_INTEGER; vtype[1] = GRB_INTEGER; vtype[2] = GRB_INTEGER;
error = GRBsetcharattrarray(model, GRB_CHAR_ATTR_VTYPE, 0, 3, vtype);
if (error) goto QUIT;
/* Optimize model */
error = GRBoptimize(model);
if (error) goto QUIT;
/* Write model to 'qp2.lp' */
error = GRBwrite(model, "qp2.lp");
if (error) goto QUIT;
/* Capture solution information */
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 = 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");
}
QUIT:
/* Error reporting */
......
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