运筹优化学习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.
相关文章
|
IDE Linux 开发工具
从旧服务器迁移svn到另一台新服务器中(linux系统)|遇到诸多坑,已解决
从旧服务器迁移svn到另一台新服务器中(linux系统)|遇到诸多坑,已解决
|
人工智能 边缘计算 安全
阿里云 ESA「函数和Pages」云栖发布:代码即部署,边缘原生落地
阿里云边缘安全加速(ESA)发布全新“函数和Pages”平台,国内首个实现站点托管与边缘函数原生打通,支持全球3200+节点极速部署、企业级安全防护及自动化发布流程。通过GitHub一键集成,结合AI编程助手,实现从代码到全球上线的分钟级交付,定义边缘原生与AI原生的下一代开发体验。
1054 0
阿里云 ESA「函数和Pages」云栖发布:代码即部署,边缘原生落地
|
11月前
|
人工智能 小程序 机器人
2026年阿里云无影云电脑一键部署 Moltbot(原 Clawdbot)指南 | 无影云电脑个人版
Moltbot(原Clawdbot)作为一款可理解自然语言并执行各类任务的AI助理,在日常办公与个人事务处理中实用性显著。对于追求便捷部署的用户而言,阿里云无影云电脑推出的专属部署方案,凭借预装镜像、简化配置等特性,规避了本地部署的诸多麻烦。以下为无影云电脑个人版一键部署Moltbot的详细流程,同时涵盖多消息通道配置及相关优化设置,助力不同需求的用户快速启用该AI助理。
1130 5
|
11月前
|
边缘计算 缓存 运维
边缘不是云的缩小版:K3s、KubeEdge 在受限网络下的真实部署经验
边缘不是云的缩小版:K3s、KubeEdge 在受限网络下的真实部署经验
549 4
|
11月前
|
安全 Linux API
JEB Pro 5.35 发布 - Android 反编译器
JEB Pro 5.35 (macOS, Linux, Windows) - 逆向工程平台
646 0
JEB Pro 5.35 发布 - Android 反编译器
|
编解码
【2025更新】视频压缩神器!视频体积瞬间缩小80%,可以指定大小压缩、批量压缩,超级良心免费使用!
Moo0视频压缩器是一款免费、高效的视频压缩工具,支持AVI、MP4等多种格式。可按文件大小、比例或屏幕尺寸智能压缩,兼顾画质与效率,操作简便,批量处理更省心,是2025年必备的视频压缩神器!
1594 2
【2025更新】视频压缩神器!视频体积瞬间缩小80%,可以指定大小压缩、批量压缩,超级良心免费使用!
|
XML 数据格式
美团抢单辅助器app,美团众包抢单辅助脚本,骑手自动抢高价单插件
这是一段关于美团骑手抢单辅助脚本的介绍。使用该脚本可设置最高与最低价格、延迟时间等参数,通过自动化检测和抢单功能帮助骑手提高收入。
|
人工智能 算法
从RLHF到DPO再到TDPO,大模型对齐算法已经是token-level
【7月更文挑战第1天】在AI领域的语言模型对齐研究中,新提出的TDPO算法实现了Token-level的直接优化。不同于以往在答案级别评估的方法,TDPO利用前向KL散度和Bradley-Terry模型,直接在生成过程的Token层面上调整对齐,提高微调精度和多样性。实验显示,TDPO优于DPO和RLHF,在某些任务上表现出色,但也面临计算资源需求高、处理复杂任务时局限性等问题,需要进一步验证和改进。[论文链接](https://arxiv.org/abs/2404.11999)
903 8
|
Cloud Native 关系型数据库 MySQL
华鼎冷链科技 × 阿里云瑶池数据库,打造全链路协同的智慧冷链新标杆
从 PolarDB 的高性能数据库服务到 AnalyticDB 的强大数据分析,阿里云提供的丰富产品矩阵为华鼎冷链科技构建了全面的解决方案,推动华鼎冷链科技从成本中心向效率中心转型。
第十五章 动态规划——最优二叉搜索树
 1、前言:   接着学习动态规划方法,最优二叉查找树问题。二叉查找树参考http://www.cnblogs.com/Anker/archive/2013/01/28/2880581.html。如果在二叉树中查找元素不考虑概率及查找不成功的情况下,可以采用红黑树或者平衡二叉树来搜索,这样可以在O(lgn)时间内完成。
2851 0