简体   繁体   English

CPLEX 约束的逻辑值

[英]Logical value of CPLEX constraint

A C++ CPLEX model that I have is long, and I build constraints inside functions.我拥有的 C++ CPLEX model 很长,我在函数内部构建约束。 Say I have a function that returns a constraint:假设我有一个返回约束的 function:

IloConstraint f(IloInt i, IloInt j, IloNumVarArray x)
{
    IloConstraint constr;
    constr = (x[j]-x[i] >= 15)  && (x[j]-x[i] <= 20);
    return constr;
}

Is it possible to do pass a constant array instead of a variable array x , and to obtain a logical value of constraint, ie to do something like.是否有可能传递一个常量数组而不是变量数组x ,并获得约束的逻辑值,即做类似的事情。

IloNumArray a(env, 5, 1, 1, 1, 1, 1);
IloConstraint c = f(1,2,a);
cout<<c.logicalValue();

You can use IloAnd你可以使用 IloAnd

In CPLEX documentation you can read在 CPLEX 文档中,您可以阅读

For example, you may write:

 IloAnd and(env);
 and.add(constraint1);
 and.add(constraint2);
 and.add(constraint3);
 

Those lines are equivalent to :

 IloAnd and = constraint1 && constraint2 && constraint3;

I couldn't find a real solution, but I have some workaround.我找不到真正的解决方案,但我有一些解决方法。 Assuming we already have a constraint c , we create a new model with c as its constraint, and add new bounds for the variables.假设我们已经有一个约束c ,我们创建一个新的 model 以c作为它的约束,并为变量添加新的边界。

bool constraintLogicalValue(IloConstraint const& constr, IloNumVarArray const& x, IloNumArray const& a, unsigned int const n)
{        
    IloEnv env;
    IloModel model(env);
    IloCplex cplex_model(model);
    cplex_model.setOut(env.getNullStream());

    for(int i=0; i<n; ++i)
        model.add(x[i]==a[i]);

    model.add(constr);
    return(cplex_model.solve());
}

Now we can use it in the following way:现在我们可以通过以下方式使用它:

int n;
IloEnv env;
IloModel model(env);
IloNumVarArray x (env);
...
IloNumArray a(env);
for(int i=0; i<n; ++i)
    a.add(1);

IloConstraint c = f(1,2,x);
cout<<constraintLogicalValue(c, env, x, a, n);

EDIT: A problem could be an existing bounds for x , which are passed into constraintLogicalValue() .编辑:问题可能是x的现有边界,它被传递到constraintLogicalValue() If x[i]==a[i] is out of these bonds, for some i , the model is unfeasible and we obtain false as a result, event if constr is satisfied.如果x[i]==a[i]不在这些键中,对于某些imodel是不可行的,结果我们得到false ,如果满足 constr 则事件。

Sometimes this is what we want.有时这就是我们想要的。

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

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