运筹优化学习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.
相关文章
|
7天前
|
存储 算法 决策智能
《C 语言下模拟退火算法于组合优化的应用要点全解析》
组合优化问题是计算机科学与数学的交叉领域中的研究热点。模拟退火算法作为一种基于概率的随机搜索方法,通过模拟固体退火过程,能够在解空间中高效寻找全局最优或近似最优解。本文探讨了用C语言实现模拟退火算法的关键步骤,包括算法原理、数据结构设计、温度参数控制、邻域生成与搜索策略、接受准则、终止条件及性能评估与调优,旨在为解决组合优化问题提供有效途径。
49 11
|
7月前
|
算法 决策智能 Python
Python基于粒子群优化的投资组合优化研究
Python基于粒子群优化的投资组合优化研究
|
7月前
|
机器学习/深度学习 算法 vr&ar
【Python强化学习】动态规划法中策略迭代和值迭代求解冰湖问题实战(图文解释 附源码)
【Python强化学习】动态规划法中策略迭代和值迭代求解冰湖问题实战(图文解释 附源码)
140 0
|
7月前
|
存储 算法 编译器
第七章:函数
第七章:函数
69 0
|
自然语言处理 数据库
Lingo优化软件初步
Lingo优化软件初步
169 0
|
机器学习/深度学习 自然语言处理 算法
【机器学习实战】10分钟学会Python怎么用EM期望最大化进行参数估计(十五)
【机器学习实战】10分钟学会Python怎么用EM期望最大化进行参数估计(十五)
225 0
从0到1学习Yalmip工具箱(2)-决策变量进阶
从0到1学习Yalmip工具箱第二章,决策变量进阶学习
|
决策智能
运筹优化学习04:Lingo的sum函数和for函数的使用方法介绍
运筹优化学习04:Lingo的sum函数和for函数的使用方法介绍
运筹优化学习04:Lingo的sum函数和for函数的使用方法介绍
|
决策智能
运筹优化学习01:Lingo入门与错误列表分析(一)
运筹优化学习01:Lingo入门与错误列表分析
运筹优化学习01:Lingo入门与错误列表分析(一)
|
决策智能 Windows
运筹优化学习01:Lingo入门与错误列表分析(二)
运筹优化学习01:Lingo入门与错误列表分析
运筹优化学习01:Lingo入门与错误列表分析(二)
下一篇
DataWorks