标准语法

@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.

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

  1. Python+cplex运筹优化学习笔记(三)-营养膳食选择

    Python+cplex运筹优化学习笔记(三)-营养膳食选择 前言 首先呢,说明一下,本文只是自己在学习过程中运用到的例子,然后规整总结一下,随便写写自己所做的一些笔记.小白学习,有不对的地方还望大家 ...

  2. 运筹优化学习21:Java调用Cplex实现求解Cuting Stock Porblem的列生成算法详解

    目录 1 CSP问题与模型 1.1 问题描述 1.2 模型构建 2 列生成方法理论 2.1 引子 2.2 单纯形法到列生成 2.3 subproblem 2.3.1 对偶理论 2.3.2 影子价格 2 ...

  3. 运筹优化学习24:Gurobi902注册、下载、安装教程【保姆级手把手教学】

    目录 1 Gurobi的注册与下载 1.1 注册 1.2 下载 2 获取权限文件 3 安装后查看自己的grbgetkey 介绍Gurobi的下载及安装全部流程: 注意安装过程中,请保持网络环境通畅,特 ...

  4. 运筹优化学习02:Lingo求解带容量约束的车辆路径问题(CVRP)

    目录 1 基础知识储备 1.1 LINGO 具有9种逻辑运算符 1.2 lingo的窗口状态解析 1.3 @wrap函数解析 1.3.1 官方解释 1.3.2 示例代码及解释 2 CVRP问题描述与模 ...

  5. 运筹优化学习15:求解线性规划的单纯形法【手把手计算,够你应付考试了,看不懂算我输】

    目录 1 理论部分 1.1 单纯形表的要素含义解释 1.2 计算步骤 2 计算示例 2.1 初始单纯形表 2.2 第二次变换 2.3 第三次变换 2.4 第四次变换 3 参考文档 本博主研究了一天没有 ...

  6. 运筹优化学习01:Lingo入门与错误列表分析

    目录 1 Lingo编程基础 1.1基本思路 1.2 建模思路 1.3 注意事项 2 整数规划模型求解示例(不使用集合语言) 2.1 问题模型 2.2 lingo源代码: 2.3 结果展示 2.4 小 ...

  7. 运筹优化学习17:马尔科夫决策规划--例题及Matlab、Lingo和Cplex的建模实现

    目录 1 胡运权<运筹学>211页题目及理论分析 1.1 题目介绍 1.2 Matlab实现 1.3 使用Lingo求解 1.4 使用Cplex建模及求解 2 刘克<马尔科夫决策过程 ...

  8. 前端性能优化学习 07 图片优化

    图片优化 前端大部分的工作都围绕在 JavaScript 和 CSS 上,考虑如何更快地下载文件,如何提供给用户复杂而优雅的交互,如何高效合理地应用有限的处理和传输资源等,这些是用户感知的全部吗? 当 ...

  9. 运筹优化工具:Lingo、CPLEX、Gurobi的基础用法

    Lingo 1.1 编程要点 1.2 Lingo的几个常用命令 1.3 运用Lingo进行简单的线性规划建模 1.3 运用Lingo进行运输+选址问题建模求解 2.1 CPLEX求解背包问题 2.2 ...

  10. 运筹优化学习04:禁忌搜索算法的历史

    禁忌搜索算法的提出与学者们孜孜不倦的研究精确算法的努力一道发展起来的,启发式算法和精确算法同时汲取了人工智能和运筹学的成果精华:作为回馈,一些设计巧妙的算法也反过来推动了人工智能和运筹学的领域相关问题 ...

最新文章

  1. vue2.0组件生命周期探讨
  2. redis强一致性_分布式架构一定要有Redis吗?Redis的常见问题我都帮你解答了
  3. 【学习笔记】APP测试基本流程及测试要点
  4. HTML解析没有详情页url,爬虫找不到详情页URL的参数
  5. PAIP.MYSQL SLEEP 连接太多解决
  6. 计算机创业计划书800字大全,创业计划书范文800字
  7. 自媒体全套教程+全套工具(带教程)+原创实操教程
  8. Linux中MISC驱动简介及其简单使用
  9. 烙铁使用注意事项及元器件件焊接要点
  10. java网络文章博客抓取系统_java 后端博客系统文章系统——No5
  11. 自制美容面膜 简单实用方便让你大换肤 - 健康程序员,至尚生活!
  12. huggingface.transformers任务简介
  13. canu三代组装软件安装
  14. c语言生成excel文件简书,通过xlwings生成Excel文件
  15. Centos桌面版无法打开Chrome浏览器
  16. 计算机软考科目众多 我们该如何选择考试科目?
  17. post_thumbnail_html,WordPress 常用函数 / get_the_post_thumbnail
  18. 调研分析-全球与中国冲刺计时系统市场现状及未来发展趋势
  19. 第7章-JavaScript基础
  20. 什么是 Smurf 攻击?

热门文章

  1. QQ防诈骗记录查询-网络交易信誉分查询认证源码
  2. stm32g474教程_杜洋老师:STM32教程,STM32视频教程
  3. wamp5 mysql 启动不了_wamp5安装问题之mysql无法启动
  4. win10系统瑞星杀毒的卸载
  5. 眼保健操(UPWND公益版)保护眼睛
  6. latex脚注标号混合数字和特殊字符
  7. EMD(Exploiting Modification Direction)
  8. Markdown 图片居中并添加标题
  9. seraph_256 写给自己的编程小事
  10. 安卓MediaCodec编码aac