planes = new IntVar[n];
tardiness = new IntVar[n];
earliness = new IntVar[n];
LLTs = new int[n];
int obj_ub = 0;
IntVar ZERO = VariableFactory.fixed(0, solver);
for (int i = 0; i < n; i++) {
planes[i] = VariableFactory.bounded("p_" + i, data[i][ELT], data[i][LLT], solver);
// earliness[i] = VariableFactory.bounded("a_" + i, 0, data[i][TT] - data[i][ELT], solver);
// tardiness[i] = VariableFactory.bounded("t_" + i, 0, data[i][LLT] - data[i][TT], solver);
obj_ub += Math.max(
(data[i][TT] - data[i][ELT]) * data[i][PCBT],
(data[i][LLT] - data[i][TT]) * data[i][PCAT]
);
earliness[i] = Max.var(ZERO, VariableFactory.offset(VariableFactory.minus(planes[i]), data[i][TT]));
tardiness[i] = Max.var(ZERO, VariableFactory.offset(planes[i], -data[i][TT]));
LLTs[i] = data[i][LLT];
}
List<BoolVar> booleans = new ArrayList<BoolVar>();
//disjunctive
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
BoolVar boolVar = VariableFactory.bool("b_" + i + "_" + j, solver);
booleans.add(boolVar);
Constraint c1 = precedence(planes[i], data[i][ST + j], planes[j]);
Constraint c2 = precedence(planes[j], data[j][ST + i], planes[i]);
Constraint cr = LogicalConstraintFactory.ifThenElse(boolVar, c1, c2);
solver.post(cr);
}
}
bVars = booleans.toArray(new BoolVar[booleans.size()]);
objective = VariableFactory.bounded("obj", 0, obj_ub, solver);
// build cost array
costLAT = new int[2 * n];
maxCost = new TObjectIntHashMap<IntVar>();
for (int i = 0; i < n; i++) {
costLAT[i] = data[i][PCBT];
costLAT[n + i] = data[i][PCAT];
maxCost.put(planes[i], Math.max(data[i][PCBT], data[i][PCAT]));
}
// solver.post(Sum.eq(ArrayUtils.append(earliness, tardiness), costLAT, objective, 1, solver));
IntVar obj_e = VariableFactory.bounded("obj_e", 0, obj_ub, solver);
solver.post(IntConstraintFactory.scalar(earliness, Arrays.copyOfRange(costLAT, 0, n), obj_e));
IntVar obj_t = VariableFactory.bounded("obj_t", 0, obj_ub, solver);
solver.post(IntConstraintFactory.scalar(tardiness, Arrays.copyOfRange(costLAT, n, 2 * n), obj_t));
solver.post(IntConstraintFactory.sum(new IntVar[]{obj_e, obj_t}, objective));
solver.post(IntConstraintFactory.alldifferent(planes, "BC"));
}