简体   繁体   English

约束规划,如何添加 x[i] <= (max(x[:i]) + 1)

[英]Constraint Programming, how to add x[i] <= (max(x[:i]) + 1)

I'm building a model using or-tools CP tools.我正在使用 or-tools CP 工具构建一个 model。 The values I want to find are placed in a vector X, and I want to add a constraint that says up to each position of X , the next position cannot have as a value something bigger than the maximum found until X[:i] + 1我想找到的值放在向量 X 中,我想添加一个约束,表示X的每个 position,下一个 position 的值不能大于X[:i] + 1之前找到的最大值X[:i] + 1

It would be something like this:它会是这样的:

X[i] <= (max(X[:i]) + 1)

Of course, I cannot add this as a linear constraint with a max() , and creating one extra feature for each value of X upper bound seems excessive and also I would need to minimize each one to make it the "max", otherwise those are just upper bounds that could be huge and not prune my search space (and I already have an objective function).当然,我不能将其添加为带有max()的线性约束,并且为 X 上限的每个值创建一个额外的特征似乎过多,而且我需要最小化每个特征以使其成为“最大”,否则那些只是上限可能很大并且不会修剪我的搜索空间(而且我已经有一个目标函数)。

I already have an objective function.我已经有一个目标 function。

I know that one trick to add for instance a min-max ( min(max(x[i]) ) problem is to create another variable that is an upper bound of each x and minimize that one. It would be sth like this:我知道添加最小 - 最大( min(max(x[i]) )问题的一个技巧是创建另一个变量,它是每个 x 的上限并最小化那个。它会是这样的:

model = cp_model.CpModel()

lb =0; ub=0
model.NewIntVar(z, lb, ub)

for i in domain(X):
    model.NewIntVar(X[i], lb, up)
    model.Add(X[i] <= z)

model.Minimize(z)

In case you don't want to program this you can use the method in or-tools:如果您不想对此进行编程,您可以使用 or-tools 中的方法:

model.AddMaxEquality(z, X)

Now I want to add a constraint that at each value of X sets an upper limit which is the maximum value found until the previous x.现在我想添加一个约束,在 X 的每个值处设置一个上限,该上限是在前一个 x 之前找到的最大值。 It would be something like this:它会是这样的:

X[i] <= max(X[:i]) + 1

I was thinking of replicating the previous idea but that would require creating a "z" for each x... not sure if that is the best approach and how much it will reduce my space of solutions.我正在考虑复制以前的想法,但这需要为每个 x 创建一个“z”……不确定这是否是最好的方法以及它会减少多少我的解决方案空间。 At the same time couldn't find a method in or-tools to do this.同时无法在 or-tools 中找到执行此操作的方法。

Any suggestions, please?有什么建议吗?

PS: I already have as an objective function min(z) like it is in the example presented. PS:我已经有一个目标 function min(z)就像它在给出的例子中一样。

Example:例子:

For instance, you can have as a result of the model: [0, 1, 2, 0, 2, 3]例如,您可以得到 model 的结果: [0, 1, 2, 0, 2, 3]

But you shouldn't have:但你不应该:

[0, 1, 1, 2, 4] Since the max until X[:3] is 2, so the ub of X[4] should be 2 + 1. [0, 1, 1, 2, 4]因为X[:3]之前的最大值是 2,所以X[4]的 ub 应该是 2 + 1。

Thanks!谢谢!

I have no specific hints except:我没有具体的提示,除了:

  • you need to experiment.你需要试验。 One modeling trick may work on one kind of model and not on the other一种建模技巧可能适用于一种 model 而不适用于另一种
  • make sure to use reuse the max variable at index i - 1. With X the array of variables and M the array of max, ie M[i] = max(X[0], .., X[i - 1])确保在索引 i - 1 处重复使用 max 变量X是变量数组, M是 max 数组,即M[i] = max(X[0], .., X[i - 1])
    M[i] = max(M[i - 1], X[i - 1])
    X[i] <= M[i] + 1

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

相关问题 如何在Julia中使用多个x,y,z值编写约束? - How do I write a constraint in Julia with multiple x, y, z values? 如何向 CPLEX 循环添加约束? - How do I add a constraint to the CPLEX loop for? 如何在 matlab 中生成随机数,条件是 |x(i) - x(i+1)|&gt;0.25, i = 1,…,N - How to generate random numbers in matlab subjecting it to condition |x(i) - x(i+1)|>0.25, i = 1,…,N 如何在 PuLP 中添加两个变量的交互作为约束 - How can i add interaction of two variables as a constraint in PuLP 如何使用范围不等式向 pyomo 添加约束 - How do I add constraint to pyomo with a range inequality Python 约束所有变量 x &gt;= 0 - Python constraint all variables x >= 0 如何仅在Aeq * X &lt;= Beq中定义负约束 - How to define a negative constraint only in Aeq * X <= Beq 对于给定的数字N,我如何找到(x和x的因子数)= N的x,ST乘积? - For a given number N, how do I find x, S.T product of (x and no. of factors to x) = N? 如何创建优化,其中对于 x 的值,我得到 y 使得 y 为最小值,x 为最大值 - How to create a optimization where for value of x I get y such that y is minimum and x is maximum 我怎样才能优化这个计算? (x^a + y^a +z^a)^(1/a) - How could I optimize this calculation ? (x^a + y^a +z^a)^(1/a)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM