Java JRE通过CPLEX LazyCallBack检测到致命错误
我在使用cplex的LazyCallback处理库存路径问题时遇到了一个问题Java JRE通过CPLEX LazyCallBack检测到致命错误,java,cplex,Java,Cplex,我在使用cplex的LazyCallback处理库存路径问题时遇到了一个问题 public class lazyConstraintCallback extends IloCplex.LazyConstraintCallback { private IloCplex cplex; private IloNumVar[][][][] x; private IloNumVar[][][] y; private int nbLocations; private int horizon; privat
public class lazyConstraintCallback extends IloCplex.LazyConstraintCallback {
private IloCplex cplex;
private IloNumVar[][][][] x;
private IloNumVar[][][] y;
private int nbLocations;
private int horizon;
private int nbVehicles;
private double[][] graph;
private boolean subtour;
public lazyConstraintCallback (IloCplex cplex, IloNumVar[][][][] x, IloNumVar[][][] y, int nbLocations, int horizon, int nbVehicles, double[][] graph, boolean subtour) {
this.cplex=cplex;
this.x=x;
this.y=y;
this.nbLocations=nbLocations;
this.horizon=horizon;
this.nbVehicles=nbVehicles;
this.graph=graph;
this.subtour=subtour;
}
protected void main() throws IloException {
this.graph= new double[this.nbLocations][this.nbLocations];
for(int t=0;t<this.horizon;t++) {
for(int k=0;k<this.nbVehicles;k++) {
double tourlength=1;
for(int i=1;i<this.nbLocations;i++) {
if(getValue(y[i][t][k])>0.1) {
tourlength++;
}
}
int[] subtour = null;
if(tourlength>5) {
for(int i=0;i<this.nbLocations;i++) {
for(int j=i+1;j<this.nbLocations;j++) {
graph[i][j]=getValue(x[i][j][t][k]);
}
}
subtour=this.getSubtours(graph, tourlength);
if(this.subtour) {
IloLinearNumExpr expr = this.cplex.linearNumExpr();
for (int i=0;i<subtour.length; i++) {
for (int j=i+1;j<subtour.length;j++) {
if (subtour[i] < subtour[j]) {
expr.addTerm(1, this.x[subtour[i]][subtour[j]][t][k]);
} else {
expr.addTerm(1, this.x[subtour[j]][subtour[i]][t][k]);
}
}
}
IloRange SEC = this.cplex.le(expr, subtour.length-1);
this.cplex.addLazyConstraint(SEC);
System.out.println(SEC);
}
}
}
}
}
公共类lazyConstraintCallback扩展了IloCplex.lazyConstraintCallback{
私人IloCplex-cplex;
私人IloNumVar[][]x;
私人IloNumVar[][]y;
私人场所;
私有int地平线;
私家车;
私有双[][]图;
私有布尔次目标;
公共lazyConstraintCallback(IloCplex cplex、IloNumVar[][]]x、IloNumVar[][]]y、int nbLocations、int horizon、int nbVehicles、双[][]图、布尔子图){
this.cplex=cplex;
这个.x=x;
这个。y=y;
此.nbLocations=nbLocations;
这个。地平线=地平线;
本标准中,nbVehicles=nbVehicles;
图=图;
this.subtour=subtour;
}
受保护的void main()引发异常{
this.graph=新的双精度[this.nbLocations][this.nbLocations];
对于(int t=0;t而言,问题在于这一行:
this.cplex.addLazyConstraint(SEC);
您在这里所做的是将惰性约束添加到modelsstatic惰性约束表中。由于这是模型的一部分,因此在优化过程中不允许对此进行修改。要从回调中添加惰性约束,请使用回调的add()
方法:
add(SEC);
非常感谢。这解决了问题。但是,我想问在这种情况下addLazyConstraint()方法有什么用?有两个选项可以添加惰性约束:1.在启动优化之前,可以添加所有惰性约束solve()
。这是使用addLazyConstraints()完成的
,如果惰性约束易于枚举且数量相对较小,则此功能非常有用。2.您可以使用回调动态分离惰性约束(就像您所做的那样)。这在存在大量惰性约束的情况下非常有用(特别是当惰性约束的数量为指数级时)。
add(SEC);