每个约束条件,单独用一个循环添加,不要在一个循环中添加多种约束
否则隔几天看不懂代码了
今天写的这个不错,过很久回来看也应该能看懂
IloNumVar t=solver.numVar(0,Double.MAX_VALUE);//t
solver.addMinimize(solver.prod(1,t));
solver.setOut(new FileOutputStream("cplexlog.txt"));
/*(1) sum_j x_ij=1 for all i\ne 0
*(2) for any k, and i\ne 0, sum_j f_ijk=sum_j f_jik+[i=k]
*(3)for any k\ne 0, sum_j f_j0k=1
*(4) f_ijk<=x_ij
*(5)for any i\ne 0, sum_jk f_ijk- e_i*t/(Rx+Tx)<=Rx/(Rx+Tx)
*/
//constraint (1)
for(int i=1;i<graph.size();i++) {
IloLinearNumExpr ex=solver.linearNumExpr();
DEdge e=graph.get(i).edge;
while(e!=null) {
ex.addTerm(1,x[e.num]);
e=e.next;
}
solver.addEq(ex,1);
}
//constraint (2)
for(int k=1;k<graph.size();k++) {
for(int i=1;i<graph.size();i++) {
IloLinearNumExpr ex=solver.linearNumExpr();
DEdge e=graph.get(i).edge;
while(e!=null) {
ex.addTerm(1,f[e.num][k]);
e=e.next;
}
for(DEdge d:graph.get(i).inedges) {
ex.addTerm(-1,f[d.num][k]);
}
solver.addEq(ex,i==k?1:0);
}
}
//constraint (3)
for(int k=1;k<graph.size();k++) {
IloLinearNumExpr ex=solver.linearNumExpr();
for(DEdge e:graph.get(0).inedges) {
ex.addTerm(1,f[e.num][k]);
}
solver.addEq(ex,1);
}
//constraint (4)
for(int i=1;i<graph.size();i++) {
DEdge e=graph.get(i).edge;
while(e!=null) {
for(int k=1;k<graph.size();k++)
solver.addLe(f[e.num][k], x[e.num]);//
e=e.next;
}
}
//constraint (5)
for(int i=1;i<graph.size();i++) {
DEdge e=graph.get(i).edge;
IloLinearNumExpr ex=solver.linearNumExpr();
while(e!=null) {
for(int k=1;k<graph.size();k++) {
ex.addTerm(1,f[e.num][k]);
}
e=e.next;
}
ex.addTerm(-1*graph.get(i).E/(DNode.Rx+DNode.Tx), t);
solver.addLe(ex,DNode.Rx/(DNode.Rx+DNode.Tx));
}