How can I efficiently add vector constraint in c++?
回答済みHi,
In my optimization problem I have constraint like this:
Ax==By
A and B are known coefficient matrix, x and y are unknow variables vector.
I'm now adding the constraints in elementwise like:
GRBLinExpra_sm_0_expr=bvar[0] / (2*dynamic_params.ds[0]);
GRBLinExprb_sm_0_expr=bvar[0] /2;
for (int j =0; j < nrdof; ++j) {
GRBLinExpr J_T_f = 0;
for (int i =0; i <dynamic_params.n_manip_obj_contacts; ++i) {
J_T_f += dynamic_params.J_M_sm[i][0](0, j) * M_f_x[i][0] +
dynamic_params.J_M_sm[i][0](1, j) *M_f_y[i][0] +
dynamic_params.J_M_sm[i][0](2, j) *M_f_z[i][0] +
dynamic_params.J_M_sm[i][0](5, j) *M_tau_z[i][0];
}
model.addConstr((dynamic_params.Mass_M_sm[0](j) * a_sm_0_expr +dynamic_params.Cor_M_sm[0](j) * b_sm_0_expr +
dynamic_params.Grvty_M_sm[0](j)) <=tau_ub(j) + J_T_f, "Force_Init_UB_"+std::to_string(j));
model.addConstr((dynamic_params.Mass_M_sm[0](j) * a_sm_0_expr +dynamic_params.Cor_M_sm[0](j) * b_sm_0_expr +
dynamic_params.Grvty_M_sm[0](j)) >=tau_lb(j) + J_T_f, "Force_Init_LB_"+std::to_string(j));
}
which is element-wise add the constraints.
I also check the method here:
https://support.gurobi.com/hc/en-us/articles/360039888911-How-do-I-efficiently-add-constraints-using-std-vector-in-C, and I modify my code in this:
https://support.gurobi.com/hc/en-us/articles/360039888911-How-do-I-efficiently-add-constraints-using-std-vector-in-C, and I modify my code in this:
for (int j =0; j < nrdof; ++j) {
GRBLinExpr J_T_f = 0;
// Use addTerms to combine terms for manipulative contacts
for (int i =0; i <dynamic_params.n_manip_obj_contacts; ++i) {
std::vector<double> coeffs = {
dynamic_params.J_M_sm[i][0](0, j),
dynamic_params.J_M_sm[i][0](1, j),
dynamic_params.J_M_sm[i][0](2, j),
dynamic_params.J_M_sm[i][0](5, j)
};
std::vector<GRBVar> M_f_terms = {M_f_x[i][0], M_f_y[i][0], M_f_z[i][0], M_tau_z[i][0]};
J_T_f.addTerms(coeffs.data(), M_f_terms.data(), M_f_terms.size());
}
// Add constraints using the constructed expression
model.addConstr(dynamic_params.Mass_M_sm[0](j) * a_sm_0_expr +dynamic_params.Cor_M_sm[0](j) * b_sm_0_expr +
dynamic_params.Grvty_M_sm[0](j) <=tau_ub(j) + J_T_f, "Force_Init_UB_"+std::to_string(j));
model.addConstr(dynamic_params.Mass_M_sm[0](j) * a_sm_0_expr +dynamic_params.Cor_M_sm[0](j) * b_sm_0_expr +
dynamic_params.Grvty_M_sm[0](j) >=tau_lb(j) + J_T_f, "Force_Init_LB_"+std::to_string(j));
}
but I think it is similar like we can only add one constraint for one element in a row of the vector at one times. Is there any more efficient way to build the constraint that vector==vector?
Thanks,
Yuhao
0
-
正式なコメント
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
As noted in the article,
Concerning adding multiple constraints in one call (documentation about addConstrs):
We recommend that you build your model one constraint at a time (using addConstr), since it introduces no significant overhead and we find that it produces simpler code.You can use GRBModel::addConstrs()
Cheers,
David0 -
Thanks David,
using GRBModel::addConstrs() is a little bit faster, but not much.
But thanks anyway.0
投稿コメントは受け付けていません。
コメント
3件のコメント