I would like to model the following constraint set in gurobi-java.
\begin{equation} t^n_{ij} \geq t^n_{(i-1)j}+S_{(i-1)j} - (1 - \sum_{\substack{m \in \mathcal{M} \\ G_{ijm} = 1}}\sum_{k \in \mathcal{K}} x^n_{ijmk}) \times \Omega \hspace{1cm} \forall j \in \mathcal{J}; i \in \mathcal{I}_j/1; n \in \mathcal{N}_j \end{equation}
where $t^n_{ij} \in \mathbb{R}_{\geq0}$ and $x^n_{ijmk} \in \{0,1\}$ are decision variables. I have modeled this constraint set in gurobi-java as follows:
GRBVar[][][][][] x = new GRBVar[i_idx][j_idx][n_idx][m_idx][k_idx];
for (int j : j_set) {
for (int i : i_set[j]) {
for (int n : n_set[j]) {
for (int m : m_set) {
if (G[i][j][m] == true) {
for (int k : k_set) {
x[i][j][n][m][k] = model.addVar(0, 1, 1, GRB.BINARY,
i + "_" + j + "_" + n + "_" + m + "_" + k);
}
}
}
}
}
}
GRBVar[][][] t = new GRBVar[i_idx][j_idx][n_idx];
for (int j : j_set) {
for (int i : i_set[j]) {
for (int n : n_set[j]) {
t[i][j][n] = model.addVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS,
i + "_" + j + "_" + n);
}
}
}
GRBVar[][][] auxvar1 = new GRBVar[i_idx][j_idx][n_idx];
for (int j : j_set) {
for (int i : i_set[j]) {
for (int n : n_set[j]) {
auxvar1[i][j][n] = model.addVar(0, 1, 1, GRB.BINARY,
i + "_" + j + "_" + n);
GRBLinExpr lhs = new GRBLinExpr();
GRBLinExpr rhs = new GRBLinExpr();
lhs.addTerm(1, auxvar1[i][j][n]);
rhs.addConstant(1);
for (int m : m_set) {
if (G[i][j][m] == true) {
for (int k : k_set) {
rhs.addTerm(-1, x[i][j][n][m][k]);
}
}
}
model.addConstr(lhs, GRB.EQUAL, rhs, "auxiliary_" + i + "_" + j + "_" + n);
}
}
}
for (int j : j_set) {
for (int i = 1; i < i_set[j].length; i++) {
for (int n : n_set[j]) {
GRBLinExpr lhs = new GRBLinExpr();
GRBLinExpr rhs = new GRBLinExpr();
lhs.addTerm(1, t[i][j][n]);
rhs.addTerm(1, t[i-1][j][n]);
rhs.addConstant(S[j][i-1]);
rhs.addTerm(-Omega, auxvar1[i][j][n]);
model.addConstr(lhs, GRB.GREATER_EQUAL, rhs, "time_flow_" + i + "_" + j + "_" + n);
}
}
}
In comparison to docplex-python, it is much less readable and requires much more lines of code.
mdl.add_constraints(t[i, j ,n] >= t[i-1, j, n] + S[i-1, j] - (1 - mdl.sum(x[i, j, n, m, k] for m in m_set if G[i, j, m] == 1 for k in k_set) * Omega for j in j_set for i in i_set[j] if i > 0 for n in n_set[j]))
I am new to gurobi-java. Are there any better ways to model this constraint set.
Model.addVars
andModel.addConstrs
that are used to create variables and constraints in a batch $\endgroup$lhsExprs
,senses
,rhss
andnames
(you need the multiple loops in multiple lines). $\endgroup$addConstrs
as Java doesn't have nice list-comprehensions like Python. A few useaddVars
: e.g. GCPWL.java. $\endgroup$