简体   繁体   English

如何在 Java 中克隆一个 cplex object?

[英]How to clone a cplex object in Java?

I am solving a problem in Java using the CPLEX library.我正在使用 CPLEX 库解决 Java 中的一个问题。 I use a class "Model" to create a CPLEX object, and add to it variables, objective function and multiple constraints.我使用 class“模型”来创建 CPLEX object,并向其添加变量、目标 function 和多个约束。 This is the basic model, and I call the constraints "basic constraints".这是基本的 model,我将约束称为“基本约束”。

I use an algorithm that basically adds a constraint to "Model" and solves it iteratively until no more relevant constraints can be added.我使用一种算法,该算法基本上将约束添加到“模型”并迭代求解,直到无法添加更多相关约束。 I call these constraints "imposed constraints".我将这些约束称为“强加约束”。

To create a copy of the same model (with both basic and imposed constraints), I use the code below.要创建相同 model 的副本(具有基本约束和强制约束),我使用下面的代码。

private Model duplicate(Model M) throws IloException {
        Model M2 = new Model(Q,k,dep,dLoc,N,cus,cLoc,D,lD,eps);     \\create cplex object, variables, basic constraints, objective function
        ArrayList<IloRange> constraints = M.getImposedConstraints(); \\list of imposed constraints
        IloCopyManager copymanager = new IloCopyManager(M.getCplex());
        Iterator iter = (Iterator) M.getCplex().rangeIterator();
        while (iter.hasNext()) {
            IloRange c = (IloRange) iter.next();
            M2.imposeConstraint((IloRange)c.makeCopy(copymanager));
        }
        return M2;
    }

However, the Model does not get copied properly.但是,无法正确复制 Model。 The imposed constraints are not the same (I think this is maybe because the variable references seem to change?), and so the result when solving models M and M2 is not the same.施加的约束不一样(我认为这可能是因为变量引用似乎发生了变化?),因此求解模型 M 和 M2 时的结果是不一样的。 There does not seem to be a problem with the basic constraints, but definitely with the imposed constraints.基本约束似乎没有问题,但施加的约束肯定有问题。 Why is this happening, and how do I fix it?为什么会发生这种情况,我该如何解决? Any help is greatly appreciated, thanks!非常感谢任何帮助,谢谢!

The problem probably is that in models M and M2 you have different instances of variables and IloConstraint.copy() cannot know which variable in M has to be mapped to which variable in M2 .问题可能是在模型MM2中,您有不同的变量实例,并且IloConstraint.copy()无法知道M中的哪个变量必须映射到M2中的哪个变量。 The result of copy() will still reference variables in M (and not in M2 ). copy()的结果仍将引用M中的变量(而不是M2中的变量)。

I don't see how you construct the basic constraints, but I guess you construct them from scratch in the Model constructor?我看不出你是如何构造基本约束的,但我猜你是在Model构造函数中从头开始构造它们的? That would explain why you get the correct variable references there.这可以解释为什么你在那里得到正确的变量引用。

To work around your problem don't store the imposed constraints as IloConstraint .要解决您的问题,请不要将施加的约束存储为IloConstraint Instead store them as a list of non-zero coefficients and the corresponding variable indices ( not the variable objects).而是将它们存储为非零系数列表和相应的变量索引不是变量对象)。 This way you can easily reconstruct the imposed constraints with the correct variable references when cloning the model.这样,您可以在克隆 model 时使用正确的变量引用轻松重建施加的约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM