GenConstrIndicator
AnsweredHi Guys,
the first post was supposed empty, so sorrow if I am repeating the question.
I need some help here. I am using C++/VS and my Gurobi version is 9.1, and I think I can not install an updated version.
a part of my Code is:
GRBLinExpr tON;
GRBLinExpr** timeON = new GRBLinExpr*[N_GEN];
for (gen = 0; gen < n; gen++) {
timeON[gen] = new GRBLinExpr[T];
for (t = 0; t < T; t++) {
int tmut = t + MUT[gen];
tON = (1-q[gen][t])+(1-q[gen][t-1]); // q is Gurobi binary variable;
// tON is a LinExpr that establishes if the q status has changed from 1 to 0 or 0 to 1;
timeON[gen][t] += q[gen][t]; // makes a sum of q;
//In case the q variable has a new status, it must stay there at least for a MUT period of time. Hence, constrained during time =t+MUt, then I tried to use Indicator constraint as follows:
GRBGenConstr addGenConstrIndicator(tON, 1.0, timeON[tmut],GRB_GREATER_EQUAL , MUT[gen]);
}
}
I had tried even to add a new object to "gurobi c++. h" for allowing two GRBLinExpr in Indicator constraint as:
GRBGenConstr addGenConstrIndicator(GRBLinExpr& expr1, int binval,
const GRBLinExpr& expr2, char sense, double rhs, std::string name = "");
I appreciated any Help and I thank you in advance!
-
Hi,
Please follow the arguments in the documentation: GRBModel::addGenConstrIndicator
Cheers,
David0 -
Hi David,
I had tried to follow the documentation examples but I am still getting the same error. Could you give an example of using C++ / VS? , previous settings:
for ( gen = 0; gen <N_GEN ; gen++) {
cdht[gen] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY);
}
GRBGenConstr* orCDHT = new GRBGenConstr;
GRBQuadExpr*cdexpr = new GRBQuadExpr[N_GEN];
orCDHT=&model.addGenConstrOr(*cdht, *q, N_GEN * T);
GRBGenConstr addGenConstrIndicator(*cdht,1.0,*cdexpr == 0);
In the snippet code is indicated that * from cdht is not correct.
ERROR MESSAGE (in error list) :
Error (active) E0289 804 no instance of constructor "GRBGenConstr::GRBGenConstr" matches the argument listThanks in advance!
0 -
The first argument in GRBModel::addGenConstrIndicator() is a single binary variable itself not a pointer to an array of variables.
You need to loop over \(\texttt{gen}\) and add a single indicator constraint for each of the binary variables.Cheers,
David0 -
Hi David!
Thanks for the answer !
The problem is that I need a constraint to generate a condition for another constraint and not a variable, so would be GenConstrIndicator( expr1== value, *binvar,1.0,len,"name");
The binvar is array ==> binvar[len]; If I understood correctly the syntax that you purpose is not acceptable as I need it.
Could you give an example? I found it ORConstraint but doesn't work as well, so I do not know what I am doing wrong.
ctr[gen]=model.addGenConstrOr(cdht[len], q[len], T, "cdht");
obj += value* cdht[len];Really thank you for your Help!
0 -
Hi Alessandra,
Could you please share the error that you are seeing?
So, you want \(\texttt{expr1 == value}\) implies that \(\texttt{binvar[len] == 1}\)?
Would \(\texttt{binvar[len] == 1}\) implies \(\texttt{expr1 == value}\) work?
Please note that the expression must a linear expression and not a quadratic one.// define coeff as vector of float
// define someothervar as vector of GRBVar
for (int gen = 0; gen < N_GEN; ++gen) { bvars[gen] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY);
GRBLinExpr myexpr; // You have to populate the linear expression
for (int term = 0; term < TERML; ++term) {
myexpr += coeff[term] * someothervar[term]
}
model.addConstrIndicator(bvars[gen], 1.0, myexpr == 0.0);
}Here you can find a more complete example genconstr_c++.cpp.
Cheers,
David0 -
Hallo David,
Thanks for your answer!
What I need is exactly what stays at your first question "expr1==value implies that binvar[len]=1", the vice versa would be a problem since I don't know to turn this binvar off, meaning 0 as value. I also need this binvar becomes 1 just once.
OR you may have a better idea of how I make once a variable that is ON , one function will be added to the cost function. After that, the same variable turns one again, as is not the first time another value will be added to the cost function.
for (int gen = 0; gen < N_GEN; ++gen) { bvars[gen] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY); GRBLinExpr myexpr; for (int term = 0; term < TERML; ++term) { myexpr += coeff[term] * someothervar[term] } model.addConstrIndicator(binvar, 1.0, obj=full_Cost*binvar); }Cheers,
Alessandra.0 -
Hi Alessandra,
What I need is exactly what stays at your first question "expr1==value implies that binvar[len]=1", the vice versa would be a problem since I don't know to turn this binvar off, meaning 0 as value. I also need this binvar becomes 1 just once.
If I'm not mistaken, you can write:
- \(\texttt{binvar[len] == 1}\) implies \(\texttt{expr == value}\)
- \(\texttt{binvar[len] == 0}\) implies \(\texttt{expr <= value - eps}\)
This way, we are sure that when the binary variable takes the value 1 (which will happen only once), the constraint is fulfilled, when it is 0, then the constraint is strictly less than the value (for a chosen small value \(\texttt{eps}\) e.g. 1e-3.
In code:
for (int gen = 0; gen < N_GEN; ++gen) { bvars[gen] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY); GRBLinExpr myexpr; // You have to populate the linear expression for (int term = 0; term < TERML; ++term) { myexpr += coeff[term] * someothervar[term] } model.addConstrIndicator(bvars[gen], 1.0, myexpr == value);
model.addConstrIndicator(bvars[gen], 0.0, myexpr <= value - 1e-3);
}(assuming a positive expression, otherwise things get a bit more complicated)
Cheers,
David0
Please sign in to leave a comment.
Comments
7 comments