SOS_type_1 definition in Java
Hello,
I want to use SOS_type_1 in my model in Java language. I defined variable "rank" as the below form:
// Definition variable rank[i][k]
GRBVar[][] rank = new GRBVar[item.length][item.length];
for (int i = 0; i < item.length; i++) {
for (int k = 0; k < item.length; k++) {
rank[i][k] = model.addVar(0, 1, 0, GRB.BINARY, "rank" + i + "." + k);
}
}
Please, let me know, how can I define this type of variable in arrays form?
Thanks in advance
Omidi. A
-
You should use the method GRBModel.addSOS(), see
http://www.gurobi.com/documentation/8.1/refman/java_grbmodel_addsos.html
If you want to define an SOS1 constraint on all the item.length^2 variables you have defined in the code snippet, you will have to copy them to a one-dimensional array first.
0 -
Dear Mr. Luce,
Thanks for your replay.
I try to copy the variable in the one-dimensional array but, when I run the code I have an error.
The code is:
// Definition variable rank[i][k]
GRBVar[][] rank = new GRBVar[item.length][item.length];
for (int i = 0; i < item.length; i++) {
for (int k = 0; k < item.length; k++) {
double[] weight = {1,2};
int v = i *+rank[0].length + k;
GRBVar[] test = new GRBVar[item.length*item.length];
test[v] = rank[i][k];
model.addSOS(test, weight, GRB.SOS_TYPE1);
}
}the error is: Error code: 10003. Invalid arguments.
Could you please, say that how can I fix it?
Regards
Omidi. A
0 -
The snippet above redefines the 'test' array within the loop, and hence tries to add SOS constraints based on an unpopulated array of variables. If you want to have an SOS constraint on all of the item.length^2 variables, it would look more like this:
// rank[i][k] needs to be fully populated already
GRBVar[] test = new GRBVar[item.length*item.length];
double[] weight = new double[item.length*item.length];
for (int i = 0; i < item.length; i++) {
for (int k = 0; k < item.length; k++) {
int v = i * item.length + k;
weight[v] = 1.0;
test[v] = rank[i][k];
}
}
model.addSOS(test, weight, GRB.SOS_TYPE1);0 -
Dear Mr Luce,
Thanks for your comment.
I was trying to model a scheduling problem. I have some constraints that I can write to them as an SOS constraint. As per some issues on the modelling language (such as GAMS), I should define the SOS variable first and then using of them on the constraints.
I have worked on the transfer my code on the GUROBI Java API. I have defined a variable in the below form:GRBVar[][] rank = new GRBVar[item.length][item.length];
for (int i = 0; i < item.length; i++) {
for (int k = 0; k < item.length; k++) {
rank[i][k] = model.addVar(0, 1, 0, GRB.BINARY, "rank" + i + "." +k);
}
}According to your previous comment, you mentioned that I have to define a one-dimensional array and copy binary variable on it. I try it as below:
// Definition variable rank[i][k]
GRBVar[][] rank = new GRBVar[item.length][item.length];
GRBVar[] test = new GRBVar[item.length * item.length];
double[] weight = new double[item.length * item.length];
for (int i = 0; i < item.length; i++) {
for (int k = 0; k < item.length; k++) {
rank[i][k] = model.addVar(0, 1, 0, GRB.BINARY, "rank" + i + "." +k);
int v = i * item.length + k;
weight[v] = 1.0;
test[v] = rank[i][k];
}
}
model.addSOS(test, weight, GRB.SOS_TYPE1);In the GUROBI log, it's mentioned that: Model has 1 SOS constraint.
When I have tried to define constraints I have some errors. I have defined it as below:
// 'every position gets a jobs'
for (int k = 0; k < item.length; k++) {
for (int i = 0; i < item.length; i++) {
GRBLinExpr rank_k = new GRBLinExpr();
for (int v = 0; v < (i * item.length + k); v++) {
rank_k.addTerm(1, test[v]);
}
model.addConstr(rank_k, GRB.EQUAL, 1, "assign_k" + k);
}
}The error is: Model is infeasible.
I was wondering if, you could advise me how can I define such constraint?Regards
Omidi. A0
Please sign in to leave a comment.
Comments
4 comments