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

简介: 运筹优化学习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

image.png

@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.
相关文章
|
算法 安全 机器人
Python语言如何使用MindOpt建模并求解二次规划问题
MindOpt是一款高效的优化算法软件包,求解算法实现了线性规划(LP)、混合整数线性规划(MILP)、二次规划(QP),可以支持命令行、c、c++、java和python调用。接下来我们将发布一系列文章,讲述各个语言如何使用 MindOpt 来求解数学规划问题。
Python语言如何使用MindOpt建模并求解二次规划问题
|
5月前
|
自然语言处理 数据库
Lingo优化软件初步
Lingo优化软件初步
|
8月前
|
自然语言处理 算法
第4章 MATLAB编程基础——4.5 脚本与函数(2)
第4章 MATLAB编程基础——4.5 脚本与函数(2)
|
8月前
|
自然语言处理
第4章 MATLAB编程基础——4.5 脚本与函数(1)
第4章 MATLAB编程基础——4.5 脚本与函数(1)
|
9月前
|
存储 算法 调度
Matlab+Yalmip两阶段鲁棒优化通用编程指南
主要包含8大内容: ①.拿到一个复杂的两阶段鲁棒优化问题的分析步骤和方法。 ②.采用Yalmip工具箱中的uncertain函数和鲁棒优化模块求解两阶段鲁棒优化的子问题。 ③.Yalmip工具箱中的鲁棒优化模块和常规的求解思路有什么异同。 ④.使用KKT条件求解两阶段鲁棒优化的子问题。 ⑤.使用对偶变换求解两阶段鲁棒优化的子问题。 ⑥.采用Yalmip工具箱的内置函数,将线性约束写成紧凑矩阵形式的方法。 ⑦.矩阵形式的两阶段鲁棒优化问题,如何快速写出子问题内层优化的KKT条件。 ⑧.矩阵形式的两阶段鲁棒优化问题,如何快速写出子问题内层优化的对偶问题。
|
9月前
|
机器学习/深度学习 自然语言处理 算法
【机器学习实战】10分钟学会Python怎么用EM期望最大化进行参数估计(十五)
【机器学习实战】10分钟学会Python怎么用EM期望最大化进行参数估计(十五)
170 0
|
10月前
从0到1学习Yalmip工具箱(2)-决策变量进阶
从0到1学习Yalmip工具箱第二章,决策变量进阶学习
|
决策智能
运筹优化学习01:Lingo入门与错误列表分析(一)
运筹优化学习01:Lingo入门与错误列表分析
运筹优化学习01:Lingo入门与错误列表分析(一)
|
决策智能
运筹优化学习01:Lingo入门与错误列表分析(三)
运筹优化学习01:Lingo入门与错误列表分析
运筹优化学习01:Lingo入门与错误列表分析(三)
|
决策智能 Windows
运筹优化学习01:Lingo入门与错误列表分析(二)
运筹优化学习01:Lingo入门与错误列表分析
运筹优化学习01:Lingo入门与错误列表分析(二)