运筹优化学习07:Lingo的 @if 函数的使用方法

 

标准语法

@IF( logical_condition, true_result, false_result)

如果逻辑表达式为真,则返回真的结果,为假,返回假的结果;返回值可以为其他变量赋值

示例

constValue = if(pi > e, pi, e);
!返回值是pi

官方文档

@IF( logical_condition, true_result, false_result)

The @IF function evaluates logical_condition and, if true, returns true_result, otherwise it returns false_result.  For example, consider the following simple model that uses @IF to compute fixed production costs:

MIN = COST;

COST = XCOST + YCOST;

XCOST = @IF( X #GT# 0, 100, 0) + 2 * X;

YCOST = @IF( Y #GT# 0,  60, 0) + 3 * Y;

X + Y >= 30;

Model: IFCOST

We produce two products—X and Y.  We want to minimize total cost, subject to producing at least 30 total units of X and Y.  If we produce X, there is a fixed charge of 100 along with a variable cost of 2.  Similarly, for Y, these respective values are 60 and 3.  We use the @IF function to determine if either of the products are being produced in order to apply the relevant fixed cost.  This is accomplished by testing to see if their production levels are greater than 0.  If so, we return the fixed cost value, otherwise, we return zero.

Experienced modelers know that, without the benefit of an @IF function, modeling fixed costs requires invoking some "tricks" using binary integer variables.  The resulting models are not as intuitive as models constructed using @IF.  The caveat, however, is that the @IF function is not a linear function.  At best, the graph of an @IF function will be piecewise linear.  In our current example, the @IF functions are piecewise linear with a discontinuous break at the origin.  As we discuss in the chapter On Mathematical Modeling, it is always best to try and keep a model linear.  Barring this, it is best for all functions in a nonlinear model to be continuous.  Clearly, then, the @IF function is a problem in that it violates both these conditions.  Thus, models containing @IF functions may be tough to solve to global optimality.  Fortunately, LINGO has two options that can help overcome the difficult nature of models containing @IF functions—linearization and global optimization.

To illustrate the difficulty in solving models with discontinuous functions such as @IF, we will solve our example model with both linearization and global optimization disabled.  When we do this, we get the following solution:

Local optimal solution found at iteration:  42

Objective value:  160.0000

 

      Variable           Value

          COST        160.0000

         XCOST        160.0000

         YCOST        0.000000

             X        30.00000

             Y        0.000000

This solution involves producing only X at a total cost of 160.  Clearly, this is merely a locally optimal point, given that producing only Y and not X will result in a lower total cost of 150.  In order to find the globally optimal point we must resort to either the linearization or global optimization features in LINGO. 

Briefly, linearization seeks to reformulate a nonlinear model into a mathematically equivalent linear model.  This is desirable for two reasons.  First, and most important, linear models can always be solved to global optimality.  Secondly, linear models will tend to solve much faster than equivalent nonlinear models.  Unfortunately, linearization can’t always transform a model into an equivalent linear state, in which case, it may be of no benefit.  Fortunately, our sample model can be entirely linearized.  To enable the linearization option, run the LINGO|Options command and set the Linearization Degree to High on the General Solver tab.  

Global optimization breaks a model down into a series of smaller, local models.  Once this series of local models has been solved, a globally optimal solution can be determined.  To enable global optimization, run the LINGO|Options command, select the Global Solver tab, then click on the Global Solver checkbox.  Note that the global solver is an add-on option to LINGO. The global solver feature will not be enabled for some installations.  Run the Help|About LINGO command to determine if your installation has the global solver capability enabled.

Regardless, whether using the linearization option or the global solver, LINGO obtains the true, global solution:

Global optimal solution found at iteration:  6

Objective value:   150.0000

 

      Variable           Value  

          COST        150.0000  

         XCOST        0.000000

         YCOST        150.0000

             X        0.000000

             Y        30.00000

Note:Starting with release 9.0, the false branch of the @IF function may contain arithmetic errors without causing the solver to trigger an error.  This makes the @IF function useful in avoiding problems when the solver strays into areas where certain functions become undefined.  For instance, if your model involves division by a variable, you might use @IF as follows: @IF( X #GT# 1.E-10, 1/X, 1.E10).

@WARN( 'text', logical_condition)

This function displays the message ‘text’ if the logical_condition is met. This feature is useful for verifying the validity of a model's data. In the following example, if the user has entered a negative interest rate, the message "INVALID INTEREST RATE" is displayed:

! A model of a home mortgage;

 

DATA:

! Prompt the user for the interest

rate, years, and value of mortgage.

We will compute the monthly payment;

  YRATE  = ?;

  YEARS  = ?;

  LUMP   = ?;

ENDDATA

 

! Number of monthly payment;

  MONTHS = YEARS * 12;

 

! Monthly interest rate;

  ( 1 + MRATE) ^ 12  =  1 + YRATE;

 

! Solve next line for monthly payment;

  LUMP = PAYMENT * @FPA( MRATE, MONTHS);

 

! Warn them if interest rate is negative

  @WARN( 'INVALID INTEREST RATE',

   YRATE #LT# 0);

@USER( user_determined_arguments)

The user can supply this in an external DLL or object code file. For a detailed example on the use of @USER, see User Defined Functions.

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛引路

喜欢的读者,可以打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值