I'm using CPLEX 12.5.0.0 via the C# API.
我通过c# API使用CPLEX 12.5.0.0。
Until now, I've never had a situation with an objective with a constant term - only constraints. With constraints, I have always been able to re-arrange the equation so the constant is always on one side, meaning each ILinearNumExpr
has no constant term on its own.
直到现在,我从来没有遇到过一个有固定期限的目标。在约束条件下,我总是能够重新排列方程,所以常数总是在一边,意味着每个ILinearNumExpr本身没有常数项。
Now I have a quadratic programming problem, with an objective of the following type:
现在我有一个二次规划问题,目标如下:
MAX Z =
c[1,2] * a[1] * a[2] - c[1,2] * (1 - a[1] * a[2]) +
c[1,3] * a[1] * a[3] - c[1,2] * (1 - a[1] * a[3]) +
c[2,3] * a[2] * a[3] - c[2,2] * (1 - a[2] * a[3])
c[,] is a constant, symmetric cost matrix. a[i] are binary variables.
c[,]是一个常数,对称的成本矩阵。[我]是二进制变量。
So looking at the left halves of the 3 lines above, having both a[i] and a[j] together will contribute c[i,j] to the objective value. This is what is currently implemented, tested, and working.
因此,看一下上面这3行的左半部分,有a[i]和[j]一起,将贡献c[i,j]到目标值。这是当前实现、测试和工作的内容。
I want to modify the objective so that, if a[i] and a[j] are not both equal to 1, rather than not contributing c[i,j] to the objective value, it will subtract it.
我想要修改目标,如果a[I]和[j]不等于1,而不是不贡献c[I,j]到目标值,它会减去它。
Now, I've looked up the CPLEX documentation (the authors of which are apparently allergic to providing clear explanations or examples), and there appears to be an ILinearNumExpr.Constant
property that allows me to set a constant for a given expression.
现在,我已经查阅了CPLEX文档(这些文档的作者显然对提供清晰的解释或示例过敏),而且似乎有一个ILinearNumExpr。常数性质,允许我为给定的表达式设定一个常数。
When I tried to modify my code with IQuadNumExpr
, I noticed it doesn't have that .Constant
property.
当我尝试用IQuadNumExpr修改我的代码时,我注意到它没有那个。常数属性。
Is there any way to add constant terms to a quadratic objective function in CPLEX?
是否有办法将常数项添加到CPLEX的二次目标函数中?
1 个解决方案
#1
2
To answer your specific question, to add a constant to a quadratic objective function, you can use the .Sum method of the cplex object. For example
为了回答你的具体问题,要将一个常数加到二次目标函数中,你可以使用cplex对象的. sum方法。例如
cplex.AddMaximize(cplex.sum(quadExpr, cplex.Constant(10));
makes the objective function quadExpr + 10
.
使目标函数为4 + 10。
Now, two comments on the rest of your post.
现在,对你剩下的文章有两个评论。
First, any linear transformation on the objective function will have no effect on your solution. So, if you are maximizing either
首先,任何对目标函数的线性变换都不会对你的解有影响。所以,如果你想最大化。
quadExpr
or
或
m * quadExpr + c
are equivalent for any (nonzero) constant m and constant c.
等于任何(非零)常数m和常数c。
Next, Since the variables in your quadratic expression are binary, then you can almost always do better by formulating a mixed-integer linear model. To do this, you create an additional set of linear variables, say b[i][j] that will be 1 only if both x[i] and a[j] are both 1. You can enforce the property of b[][] by adding the constraints
其次,由于二次表达式中的变量是二进制的,那么通过构造一个混合整数的线性模型,几乎总是可以做得更好。要做到这一点,你需要创建另一组线性变量,比如b[i][j],只有当x[i]和a[j]都是1时,才会是1。您可以通过添加约束来执行b[][]的属性。
b[i][j] <= x[i]
b[i][j] <= x[j]
If you are maximizing, and c[i][j] >= 0, then you don't need to explicitly enforce the converse, but if that's not the case, you can add
如果您正在最大化,并且c[i][j] >= 0,那么您不需要显式地执行相反的操作,但是如果情况不是这样,您可以添加。
x[i] + x[j] <= 1 + b[i][j]
#1
2
To answer your specific question, to add a constant to a quadratic objective function, you can use the .Sum method of the cplex object. For example
为了回答你的具体问题,要将一个常数加到二次目标函数中,你可以使用cplex对象的. sum方法。例如
cplex.AddMaximize(cplex.sum(quadExpr, cplex.Constant(10));
makes the objective function quadExpr + 10
.
使目标函数为4 + 10。
Now, two comments on the rest of your post.
现在,对你剩下的文章有两个评论。
First, any linear transformation on the objective function will have no effect on your solution. So, if you are maximizing either
首先,任何对目标函数的线性变换都不会对你的解有影响。所以,如果你想最大化。
quadExpr
or
或
m * quadExpr + c
are equivalent for any (nonzero) constant m and constant c.
等于任何(非零)常数m和常数c。
Next, Since the variables in your quadratic expression are binary, then you can almost always do better by formulating a mixed-integer linear model. To do this, you create an additional set of linear variables, say b[i][j] that will be 1 only if both x[i] and a[j] are both 1. You can enforce the property of b[][] by adding the constraints
其次,由于二次表达式中的变量是二进制的,那么通过构造一个混合整数的线性模型,几乎总是可以做得更好。要做到这一点,你需要创建另一组线性变量,比如b[i][j],只有当x[i]和a[j]都是1时,才会是1。您可以通过添加约束来执行b[][]的属性。
b[i][j] <= x[i]
b[i][j] <= x[j]
If you are maximizing, and c[i][j] >= 0, then you don't need to explicitly enforce the converse, but if that's not the case, you can add
如果您正在最大化,并且c[i][j] >= 0,那么您不需要显式地执行相反的操作,但是如果情况不是这样,您可以添加。
x[i] + x[j] <= 1 + b[i][j]