我想解决TSP,而hasSubtour为true,并为模型添加新约束,如下所示:
cplex.addMinimize(expr);
//System.out.println(cplex.getModel());
Integer nOfVariables = 0;
boolean hasSubtour = false;
do {
if( cplex.solve() ) {
if ( cplex.getStatus().equals(IloCplex.Status.Optimal) ) {
if(hasSubtour) {
for(int i = 0; i < n; i++) {
IloLinearNumExpr tempExpr = cplex.linearNumExpr();
for(int j = 0; j < n; j++) {
if( i != j ) {
if(cplex.getValue(x[i][j]) < 1) {
tempExpr.addTerm(1.0,x[j][i]);
}
}
}
cplex.addEq(tempExpr, 1);
}
}
}
} while(hasSubtour);
但是,当hasSubtour为true时,我收到错误
cplex.getValue(x [i] [j])<1命名
Exception in thread "main" ilog.cplex.CpxException: CPLEX Error 1217: No solution exists.
如何在不出现异常的情况下向模型添加新约束?