运筹优化学习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.
相关文章
|
3月前
|
人工智能 算法 开发者
2025年高教社杯E题——AI 辅助智能体测全国大学生数学建模(思路、代码、论文)
2025年高教社杯E题——AI 辅助智能体测全国大学生数学建模(思路、代码、论文)
473 1
|
8月前
|
机器学习/深度学习 存储 缓存
加速LLM大模型推理,KV缓存技术详解与PyTorch实现
大型语言模型(LLM)的推理效率是AI领域的重要挑战。本文聚焦KV缓存技术,通过存储复用注意力机制中的Key和Value张量,减少冗余计算,显著提升推理效率。文章从理论到实践,详细解析KV缓存原理、实现与性能优势,并提供PyTorch代码示例。实验表明,该技术在长序列生成中可将推理时间降低近60%,为大模型优化提供了有效方案。
1565 15
加速LLM大模型推理,KV缓存技术详解与PyTorch实现
|
Python
turtle库的几个案例进阶,代码可直接运行(python经典编程案例)
该文章展示了使用Python的turtle库进行绘图的进阶案例,包括绘制彩色圆形和复杂图案的代码示例。
4239 7
turtle库的几个案例进阶,代码可直接运行(python经典编程案例)
|
SQL 运维 安全
【产品升级】Dataphin V4.2重大升级:上线敏捷版,打通数据资产管理和消费,开启数据价值放大新篇章
Dataphin 是阿里巴巴旗下的一个智能数据建设与治理平台,旨在帮助企业构建高效、可靠、安全的数据资产。在V4.2版本中,Dataphin敏捷版上线助力企业打造轻量版数据中台,打通数据资产管理和消费,陪伴企业迈入数据高价值应用新阶段。
2946 2
【产品升级】Dataphin V4.2重大升级:上线敏捷版,打通数据资产管理和消费,开启数据价值放大新篇章
|
知识图谱
举例解释Lingo的条件执行@if语句
举例解释Lingo的条件执行@if语句
585 0
|
前端开发 测试技术 UED
【专栏:HTML 与 CSS 实战项目篇】实现一个在线产品展示页面
【4月更文挑战第30天】本文介绍了使用HTML和CSS创建吸引人的在线产品展示页面的实战步骤,包括页面设计规划、HTML结构搭建、CSS样式设计、具体页面实现、交互效果添加、优化与提升。通过简洁布局、产品列表和详情页设计,实现易用且具吸引力的展示效果。优化图片和代码,提升性能,以助企业在数字时代脱颖而出。
1124 5
|
传感器 语音技术
STM32智能小车(循迹、跟随、避障、测速、蓝牙、wife、4g、语音识别)总结-1
STM32智能小车(循迹、跟随、避障、测速、蓝牙、wife、4g、语音识别)总结
STM32智能小车(循迹、跟随、避障、测速、蓝牙、wife、4g、语音识别)总结-1
|
存储 并行计算 物联网
现代操作系统的发展与未来趋势
在当今数字化时代,操作系统作为计算机的核心软件,不断迭代更新,以适应日益复杂的应用需求和硬件环境。本文将探讨现代操作系统的发展历程,分析其面临的挑战,并展望未来趋势。从单一核心到分布式系统,从云计算到物联网,操作系统正朝着更加智能、安全、高效的方向发展。
568 1
|
决策智能
运筹优化学习04:Lingo的sum函数和for函数的使用方法介绍
运筹优化学习04:Lingo的sum函数和for函数的使用方法介绍
运筹优化学习04:Lingo的sum函数和for函数的使用方法介绍