当前位置:网站首页>Lingo basic use
Lingo basic use
2022-07-23 20:28:00 【A-L-Kun】
List of articles
Lingo grammar
One 、 summary
1、 brief introduction
LINGO It is a particularly useful software for solving optimization problems , Can quickly solve linear programming 、 Nonlinear programming 、 Linear and nonlinear equations and so on , It is one of the indispensable tools for solving optimization problems in mathematical modeling
(1)LINGO The mathematical programming model of contains the objective function 、 The decision variables 、 Three elements of constraints ;
(2)LINGO In the program , Every sentence must end with a semicolon in English , A statement can be entered in several lines ;
(3)LINGO The comments of are in English ! Start , It must end with a semicolon in English ;
(4)LINGO Variables of are not case sensitive , Must start with a letter , Can contain numbers and underscores , No more than 32 Characters ;
(5)LINGO In the program , As long as the set is defined , The order of other statements is arbitrary ;
(6)LINGO In the function of “@” start ;
(7)LINGO The program defaults that all variables are nonnegative ;
(8)LINGO In the program "<“ or ”>" Number and "" or " " The number has the same function .
2、 File format
- suffix “lg4” Express lingo Format of the model file , Only lingo Software can be opened ;
- suffix “lng” Represents a model file in text format ;
- suffix “ldt” Express lingo Data files ;
- suffix “ltf” Express lingo Command script file ;
- suffix “lgr” Express lingo The report file .
3、 Window details

4、 Optimization model
Usually , An optimization model consists of the following three parts :
- Objective function : It is generally expressed as finding the maximum or minimum value of a mathematical expression
- The decision variables : The number of objective functions depends on which variables
- constraint condition : Attach some conditional restrictions to variables ( It is usually expressed by equality or inequality )
Be careful :Lingo By default, all decision variables are positive , Therefore, the non negative condition of the variable does not need to be entered
Two 、 Basic operators
1、 Logical operators
- #not# Negates the logical value of the operand ,#not# Is a unary operator
- #eq# If two operands are equal , Then for true; Otherwise flase
- #ne# If two operators are not equal , Then for true; Otherwise flase
- #gt# If the operator on the left is strictly greater than the operator on the right , Then for true; Otherwise flase
- #ge# If the operator on the left is greater than or equal to the operator on the right , Then for true; Otherwise flase
- #lt# If the operator on the left is strictly smaller than the operator on the right , Then for true; Otherwise flase
- #le# If the operator on the left is less than or equal to the operator on the right , Then for true; Otherwise flase
- #and# Only if both parameters are true when , The result is true; Otherwise flase
- #or# Only if both parameters are false when , The result is false; Otherwise true
2、 Comparison operator
The comparison between relational operators and logical operators is quite different , The former is the description that the relationship specified by the relational operator in the model is true , The latter only judges whether a relationship is satisfied
Lingo There are three operators in :=,>=、>, and <=、<,Lingo Strict less than and strict greater than relational operators are not supported in
A < B => A <= B
3、 Arithmetic operator
| Operator | describe |
|---|---|
| - | Take the opposite |
| ^ | chengfang |
| * | ride |
| / | except |
| + | Add |
| - | reduce |
Lingo The only unary arithmetic operator is the negation operator
The priority of the operator from high to low is : Take the opposite -》 reduce
The priority order can be changed by brackets
3、 ... and 、 Common function
1、 Mathematical functions
@abs(x) return x The absolute value of
@sin(x) return x The sine of ,x Use the radian system
@cos(x) return x Cosine of
@tan(x) return x The tangent of
@exp(x) Return constant e Of x Power
@log(x) return x The natural logarithm of
@lgm(x) return x Of gamma The natural logarithm of a function
@sign(x) If x<0 return -1; otherwise , return 1
@floor(x) return x The integral part of . When x>=0 when , Returns no more than x Maximum integer for ; When x<0 when , Return no less than x Maximum integer for .
@smax(x1,x2,…,xn) return x1,x2,…,xn Maximum of
@smin(x1,x2,…,xn) return x1,x2,…,xn Minimum of
2、 Defining function
@bin(x) Limit x by 0 or 1 — be used for 0-1 planning
@bnd(L,x,U) Limit L≤x≤U
@free(x) Cancel the variable x The default lower bound of is 0 The limitation of , namely x You can take any real number
@gin(x) Limit x Is an integer by default ,LINGO Specify that the variable is nonnegative , In other words, the lower bound is 0, The upper bound for +∞[email protected] The default lower bound is cancelled 0 The limitation of , So that the variable can also take a negative value [email protected] Used to set the upper and lower bounds of a variable , It can also cancel the default lower bound of 0 Constraints .
3、 Set cyclic function
@function(setname[(set_index_list)[|conditional_qualifier]]:
expression_list);
@function Corresponding to one of the four set cyclic functions listed below ;setname Is the set to traverse ;set_index_list Is a set index list ;conditional_qualifier It is used to limit the range of set cyclic functions , When the set loop function traverses each member of the set ,LINGO All right conditional_qualifier Evaluate , If it turns out to be true , Execute @function operation , Otherwise, skip , Continue with next cycle .expression_list Is a list of expressions that are applied to each set member , When using @for Function time ,expression_list Can contain multiple expressions , Separated by commas . These expressions will be added to the model as constraints . When using the remaining three set loop functions , expression_list There can only be one expression . If omitted set_index_list , So in expression_list All attributes referenced in are of type setname Set .
@for
This function is used to generate constraints on set members . Scalars based on modeling languages require explicit input of each constraint [email protected] Function allows you to enter only one constraint , then LINGO Automatically generate constraints for each set member .@sum
This function returns the sum of an expression that traverses the specified set member .
@min and @max
Returns the minimum or maximum value of an expression of a specified set member .
4、 Auxiliary function
@if(logical_condition,true_result,false_result)
@if Function will evaluate a logical expression logical_condition, If it is true to return true_ result, Otherwise return to false_result.
@warn(’text’,logical_condition), If the logical condition logical_condition It's true , Then a content is generated as ’text’ Information box
Other functions can be used to query documents
Four 、 model
1、 Assembly section
This part should be based on “SETS:” Start , With “ENDSETS” end , The function is to define the necessary set variables (SET) And its elements (member, The meaning is similar to the subscript of an array ) And attribute (attribute, The meaning is similar to array ).
To define a primitive set , It must be stated in detail :
- The name of the set
- Optional , Set member
- Optional , Set member properties
Define a primitive set , Use the following grammar :
setname[/member_list/][:attribute_list]
- The contents in brackets indicate optional
Several ways of listing members
- Show list set members : List all members , Comma " , " Or separated by spaces .
- Implicitly list set members :
setname/member1..memberN/[: attribute_list]; ! use ".." To omit

- The integrator is not included in the set definition , And in the subsequent data section to define
example :
! Common example method , Create a list set
SETS:
QUARTERS/1,2,3,4/:DEM,RP,OP,INV; ! Generate four attributes , The initialization values are [1 2 3 4]
factory /1..6/:a, b; ! Generate a 1 x 6 Matrix
! factory Type name called array ,a, b Variable names called arrays
plant /1..8/: c, d; ! Generate a 1 x 8 Matrix
Cooperation(factory,plant): e, f; ! Generate a 6 x 8 Matrix , If you exchange a position , be , Generate a 8 x 6 Matrix , You can also use link(factory, plant) 6 x 8
! Cooperation Big factories are made of factory and plant Two small factories merged , Producibility 6×8 Matrix
ENDSETS
! Initialization data
DATA:
a = 1, 2, 3, 4, 5, 6
ENDDATA:
The matrix to be assigned must be full , You can't give 6 The matrix of elements is only assigned 3 A numerical .
Lingo You can assign an integer to the matrix , You can also assign decimals .
2、 Target and constraint segments
This part actually defines the objective function 、 Constraints, etc , But there are no beginning and end marks for this part , So it's actually, among other things 4 Segments ( There are clear segment marks ) External lingo Model .
Functions are generally used here . for example :
MIN = @SUM(QUARTERS:400*RP+450*OP+20*INV); ! @sum For a summation function , Find 400*RP + 450*OP + 20*INV And , The first parameter passes in the type of the collection
@FOR(QUARTERS(I):RP(I)<40); ! Yes QUARTERS Traversal , Pass in the type name , Next is the operation , At the same time, it automatically traverses PR What's in it
@FOR(QUARTERS(I):I#GT#1:INV(I)=INV(I-1)+RP(I)+OP(I)-DEM(I););
INV(1) = 10+RP(1)+OP(1)-DEM(1);
5、 ... and 、 Case study
1、 Transportation and site selection
A company has 6 A construction site , The position coordinates are (ai, bi) ( Company : km ), Daily cement consumption di ( Company : Tons of )
i 1 2 3 4 5 6
a 1.25 8.75 0.5 5.75 3 7.25
b 1.25 0.75 4.75 5 6.5 7.75
d 3 5 4 7 6 11
existing 2 Stockyard , be located A (5, 1), B (2, 7), remember (xj,yj),j=1,2, / i=1~6 Daily reserves ej Each has 20 Tons of .
Suppose there is a straight road between the stockyard and the construction site , Make a daily supply plan , From A, B How many tons of cement are transported from the two stockyards to each construction site , Minimize the total ton kilometers .
Take the decision variable c_ij Express i The construction site is from j The amount of cement transported from the stockyard . Model ( Linear model ) by :
m a t r i x min ∑ j = 1 2 ∑ i = 1 6 c i j ( x j − a i ) 2 + ( y j − b i ) 2 s . t { ∑ j = 1 2 c i j = d i . i = 1 , 2 , . . . , 6 ∑ i = 1 6 c i j ≤ e j , j = 1 , 2 {matrix} \text{min} \sum_{j=1}^2{\sum_{i=1}^6{c_{ij}\sqrt{(x_j-a_i)^2+(y_j-b_i)^2}}}\\ s.t\left\{\begin{matrix} \sum_{j=1}^2 c_{ij}=d_i. i=1,2,...,6 \\ \sum_{i=1}^6 c_{ij} \le e_j,j=1,2 \end{matrix}\right. matrixminj=1∑2i=1∑6cij(xj−ai)2+(yj−bi)2s.t{ ∑j=12cij=di.i=1,2,...,6∑i=16cij≤ej,j=1,2 {matrix}
be , Solution available :
MODEL:
SETS:
demand/1..6/: a, b, d;
supply/1,2/:e, x, y;
link(demand, supply): c;
ENDSETS
DATA:
a=1.25 8.75 0.5 5.75 3 7.25;
b=1.25 0.75 4.75 5 6.5 7.75;
d=3 5 4 7 6 11;
x=5 2;
y=1 7;
e=20 20;
ENDDATA
[email protected](link(i, j):c(i, j)*@SQRT((a(i)-x(j))^2+(b(i)-y(j))^2)); ! express;
@FOR(demand(i):@SUM(supply(j):c(i,j))=d(i)); ! condition1;
@FOR(supply(j):@SUM(demand(i):c(i,j)) < e(j)); ! condition2;
END
2、 The best choice
A drilling crew will start from 10 Among the well locations available for selection 5 Drilling exploration , Minimize the total drilling cost . if 10 The code of the well location is s1,s2…,s10, Corresponding drilling costs c1,c2,…,c10 by 5,8,10,6,9,5,7,6,10,8. And the selection of well location should meet the following restrictions :
(1) Or select s1 and s7, Or choose to drill s9;
(2) I chose s3 or s4 You can't choose s5, Or vice versa ;
(3) stay s5,s6,s7,s8 You can only choose two at most .
Try to build an integer programming model of this problem , Determine the selected well location .
You can use truth table to establish constraint expression
take 0-1 Variable s_i, if s_i=1, Then it means that the i A well , if s_i=0, Then it means that the i A well . The mathematical model is established as follows :
m a t r i x min ∑ i = 1 10 s i c i s . t { ( s 1 + s 7 − 2 ) ( s 9 ) = 0 s 3 s 5 + s 4 s 5 = 0 s 5 + s 6 + s 7 + s 8 ≤ 2 ∑ i = 1 10 s i = 5 s i ∈ 0 , 1 ( i = 1 , 2 , . . . , 10 ) {matrix} \text{min} \sum_{i=1}^{10} {s_ic_i}\\ s.t \left\{\begin{matrix} (s_1+s_7 - 2)(s_9)=0 \\ s_3s_5+s_4s_5=0\\ s_5+s_6+s_7+s_8 \le 2\\ \sum_{i=1}^{10}s_i=5\\ s_i \in {0, 1}(i=1,2,...,10)\end{matrix}\right. matrixmini=1∑10sicis.t⎩⎨⎧(s1+s7−2)(s9)=0s3s5+s4s5=0s5+s6+s7+s8≤2∑i=110si=5si∈0,1(i=1,2,...,10) {matrix}
be , Solution available :
MODEL:
SETS:
var/1..10/:s,c; ! Create set
ENDSETS
DATA:
c=5 8 10 6 9 5 7 6 10 8; ! Assign a value to a set
ENDDATA
MIN = @SUM(var(i):s(i)*c(i)); ! Solution result
(s(1)+s(7)-2)*s(9) = 0; ! Conditions 1
s(3)*s(5)+s(4)*s(5)=0; ! Conditions 2
s(5)+s(6)+s(7)+s(8)<2; ! Conditions 3
@SUM(var(i):s(i))=5; ! Conditions 4
@FOR(var(i):@BIN(s(i))); ! Conditions 5, Use for Cycle pair s For each value of ,1 representative true
END
边栏推荐
- 【pdd面试】分析手机中的应用(相机)的活跃情况
- el-upload实现上传文件预览
- dokcer镜像理解
- [development experience] development project trample pit collection [continuous update]
- 梅科爾工作室-小熊派開發筆記2
- [Q] Error redirecting nmcli manual to TXT text
- scanf()和getchar()的用法讨论
- Solve the problem that the user clicks quickly and repeats the request within 1 second
- 今日睡眠质量记录81分
- D2Admin框架基本使用
猜你喜欢

The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap
![Relevant interfaces of [asp.net core] option mode](/img/2e/847e7541cfc49fd69794089dce2df2.jpg)
Relevant interfaces of [asp.net core] option mode

Task03 | return

Win11小组件怎么添加待办事项?Win11添加待办事项小组件的方法

2022DASCTF MAY

【pdd面试】分析手机中的应用(相机)的活跃情况

When using polymorphism, two ideas to judge whether it can be transformed downward

Uncover the working principle of solid state disk

OneFlow v0.8.0正式发布

Mysql的前世今生,Hello,Mysql
随机推荐
Atelier macoll - notes de développement de la secte de l'ours 2
如何在OneFlow中新增算子
shell脚本中$#、$*、[email protected]、$?、$0等含义一文搞懂
Mysql的前世今生,Hello,Mysql
121. 买卖股票的最佳时机
Solve the problem that the user clicks quickly and repeats the request within 1 second
2022 Shandong old age Expo, Shandong elderly care exhibition, China International elderly care service industry exhibition was held in September
ant接口用什么天线_电视后面有个接口写的是 标准ANT 75 欧输入,请问是什么意思,是天线吗?可不可以接闭路线啊?…「建议收藏」
13 ways of Excel automation to avoid repeating tasks in Microsoft Excel
[Q] Error redirecting nmcli manual to TXT text
Viewing the "Empathy" energy of iqoo 10 pro from 200W super flash charging
[Q]重定向nmcli的手册到txt文本出错
梅科爾工作室-小熊派開發筆記2
线性代数行列式计算方法之降阶法
2022DASCTF MAY
不用MQTT C库就能实现MQTT连接、订阅和发布
Mecol Studio - Little Bear Development Notes 3
Task03 notes 2
Discussion on the usage of scanf () and getchar ()
next数值型数据类型()出现输入错误后,下次依然能正常输入