如何在AMPL中实现决策变量 [英] How to implement decision variable in AMPL

查看:188
本文介绍了如何在AMPL中实现决策变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公式,我不知道如何在AMPL for CPLEX中实现它. 就这个: 在此处输入图片描述

I have an equation and i don't know how to implement it in AMPL for CPLEX. Here is it: enter image description here

谢谢您的帮助!

推荐答案

使用线性求解器(如CPLEX),通常的方法是将其转换为带有二进制变量的线性约束,以指示两种情况中的哪一种适用. /p>

With a linear solver (like CPLEX), the usual approach is to turn this into a linear constraint with a binary variable indicating which of the two cases applies.

param eps;
param beta;
param bignum = 1e5;
var z_s_1 binary;
# will have value 0 when z_v <= eps, else 1
var z_s = z_s_1*beta;
var z_v >= 0;

s.t. Define_z_s_1_a: z_s_1 * eps <= z_v;
# so if z_v < eps then z_s_1 must be 0 and hence z_s = 0
s.t. Define_z_s_1_b: z_s_1 * bignum >= z_v - eps;
# so if z_v > eps, then z_s_1 must be > 0 so must be 1, 
# and hence z_s = beta.

一些注意事项:

如果要在数学上精确,则当z_v精确等于eps时,z_s_1可以采用任一值,因此z_s可以为0或beta.但是,鉴于计算机算法的局限性,我们通常不需要因严格不平等和非严格不平等而失去睡眠.

If you want to be mathematically precise, when z_v equals eps exactly, z_s_1 could take either value, and hence z_s could be either 0 or beta. But given the limitations of computer arithmetic, we normally don't need to lose sleep about strict vs. non-strict inequalities.

这将阻止使用z_v > bignum+eps的解决方案,因此bignum将需要足够大以允许z_v的合理值.但是,如果将其设置得太大,则可能会遇到机器算法方面的问题.例如,考虑以下示例:

This will prevent solutions with z_v > bignum+eps, so bignum will need to be large enough to allow reasonable values of z_v. However, if you make it too large, you can run into problems with machine arithmetic. For instance, consider the following example:

option solver cplex;
param bignum = 1e6;
var x binary;

minimize of: x;

s.t. c1: bignum*x >= 1;
solve;
display x;

手动进行评估,因为x是二进制且bignum*x> = 1,我们可以看到x必须为1.但是,通过AMPL/CPLEX运行它,我得到了解决方案x=0 ,即使这违反了c1.

Evaluating this by hand, since x is binary and bignum*x >= 1, we can see that x must be 1. However, running this through AMPL/CPLEX, I instead get the solution x=0, even though this violates c1.

其原因是这些求解器通常对整数"变量具有较小的容差.因此,它接受x=1e-10的解作为足够接近"到整数.从x*bignum >= 1开始满足c1,但是随后将x舍入为零.如果您不期望的话,这可能会造成混淆!因此,根据z_v的最大可能值,找出可以完成此工作的bignum的最小值,然后仔细检查结果.您可能需要修改公差参数.

The reason for this is that these solvers generally allow a small tolerance for "integer" variables. Hence, it accepts a solution of x=1e-10 as "close enough" to integer. This satisfies c1 since x*bignum >= 1, but then it rounds x to zero. This can be confusing if you're not expecting it! So figure out what the smallest value is for bignum that will do the job, based on the largest plausible value for z_v, and check results carefully. You may need to tinker with the tolerance parameters.

AMPL确实还为使用CPLEX的逻辑约束提供支持但我对这些内容不太熟悉,所以我不确定他们是否可以为该问题提供替代解决方案.

AMPL does also offer some support for logical constraints with CPLEX but I'm less familiar with those, so I'm not sure if they offer an alternate solution to this problem.

这篇关于如何在AMPL中实现决策变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆