我需要你的帮助。对于我的论文,我需要使用Gurobi求解具有二次约束的混合整数二次问题(MIQP)。当我将问题写入文件时,实现很好,解决部分就是问题,因为它的最佳界限和目标是0 .......这是不可能的! 问题的定义:
maximize: \sum_{i \in A, j \in Q} c_ij*x_ij
\sum_{i \in A} c_ij*x_ij <= B_i
c_ij <= b_ij
x_ij, c_ij >=0
使用Java接口实现:
public class Gurobi_mod {
public static int m = 10; //number of items
public static int n = 5; //number of agents
public static double b_ij[][] = new double [n][m];
public static double B_i[] = new double [n];
public static void main(String[] args) throws IOException {
try {
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env);
GRBVar[][] xij = new GRBVar[n][m];
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
xij[i][j] =
model.addVar(0.0, 1.0, 1, GRB.BINARY, "x" + i + "," + j);
}
}
model.update();
GRBVar[][] cij = new GRBVar[n][m];
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cij[i][j] =
model.addVar(0.0, GRB.INFINITY, 1, GRB.CONTINUOUS, "c" + i + "," + j);
}
}
model.update();
double coeff = 1;
GRBQuadExpr linearobj = new GRBQuadExpr();
for (int i = 0; i < n; ++i){
GRBQuadExpr obj = new GRBQuadExpr();
for (int j = 0; j < m; ++j){
obj.addTerm(1, xij[i][j], cij[i][j]);
}
linearobj.multAdd (coeff, obj);//addTerm(coeff, var);add(obj);
}
model.setObjective(linearobj, GRB.MAXIMIZE);
model.update();
for (int i = 0; i < n; i++){
GRBQuadExpr thexpr1 = new GRBQuadExpr();
for (int j = 0; j < m; j++){
thexpr1.addTerm(1, cij[i][j], xij[i][j]);
}
model.addQConstr(thexpr1, GRB.LESS_EQUAL, B_i[i], "Budget"+ i);
}
model.update();
for (int j = 0; j < m; ++j){
GRBLinExpr thexpr = new GRBLinExpr();
for (int i = 0; i < n; ++i){
thexpr.addTerm(1, xij[i][j]);
}
model.addConstr(thexpr, GRB.LESS_EQUAL, 1, "Item"+j);
}
model.update();
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
GRBLinExpr thexprcij = new GRBLinExpr();
thexprcij.addTerm(1, cij [i][j]);
model.addConstr(thexprcij, GRB.LESS_EQUAL, b_ij[i][j], "Bid"+ i + j);
}
}
// Solve
model.optimize();
}catch (GRBException e){
System.out.println("Error code: " + e.getErrorCode() + ". " +
e.getMessage());
}
}
}
Gurobi可以解决这种混合整数二次问题,因为变量x_ij是BINARY而c_ij是连续的。如果我将c_ij设置为BINARY,我会得到一个看似合理的结果。这是否意味着问题不是凹陷最大化问题? (据我所知,Gurobi只能解决这种特殊的MIQP)。在此先感谢!!