您的位置:首页 > 编程语言 > Go语言

Lingo中命令脚本文件使用范例

2015-11-16 22:32 501 查看
我们在用lingo的时候有时候会遇到比较繁重的,重复性的工作,手动的单次运行显然耗时耗力,Lingo里的@for函数只能做一些简单的循环,这就要求我们应用脚本文件来简化这些重复性的工作。下面的英文部分是官方手册里给出的一个事例,这里总结下我觉得有用的2个点:

1.脚本文件类型:.ltf 不过脚本文件不会有文件的高亮显示,大家可以先用普通编辑器编辑再拷贝过来。
2.如何解决重复性工作:我们存储数据通常来说都有相对固定的格式。比如饭馆周一到周日每日需要的服务员数,每一项都存在Monday-Sunday的表格里。我们要做的就是巧妙的调用,替换和存储。
示例中把数据存储在ldt格式里了,同理我们也可以调用excel格式(xlsx)文件读取所需数据。替换过程主要用到一个ALTER命令,使用格式为:
 ALTER '新名字'旧名字’ 
这样所有旧名字的字段会被替换成新字段,程序就可以这么循环下去了。
存储数据时也比较有趣,事例中给的是存到text文本中,且给每个文本对应的命名,大家其实也可以用@OLE命令把所有的结果存到一个表里,这也不难。
大家如果看英文觉得麻烦就看看薛金星老师的教材,在第四章LINGO软件与外部文件接口那一章的命令脚本文件那一节。跟官方给的很相似。
示例链接:
http://www.lindo.com/doc/online_help/lingo15_0/a_command_script_example.htm
事例内容:

Once again, we will make use of the staff scheduling model introduced in Using
Sets to illustrate the use of a command script. Suppose, instead of one hot dog stand, our operations have expanded and we now have three hot dog stands: Pluto Dogs, Mars Dogs, and Saturn Dogs. Our staffing requirements at the three sites are:

Site

Mon

Tue

Wed

Thu

Fri

Sat

Sun

Pluto

20

16

13

16

19

14

12

Mars

10

12

10

11

14

16

8

Saturn

8

12

16

16

18

22

19

Running staffing models for all three sites is cumbersome(笨重的) and prone to error. We would like to automate the process by constructing a script file that runs all three staffing models automatically. To do this, we construct the following script file:

! Have LINGO echo input to the screen

SET ECHOIN 1

! Suppresses the standard solution report

SET TERSEO 1

! Begins input of a new model

MODEL:

SETS:

DAYS / MON TUE WED THU FRI SAT SUN/:

REQUIRED, START;

ENDSETS

DATA:

REQUIRED = @FILE( 'PLUTO.LDT');

@TEXT( 'PLUTO.TXT') = START;

ENDDATA

MIN = @SUM( DAYS( I): START( I));

@FOR( DAYS( J):

@SUM( DAYS( I) | I #LE# 5:

START( @WRAP( J - I + 1, 7)))

>= REQUIRED( J)

);

@FOR( DAYS: @GIN( START));

END

! Solve Pluto Dogs model

GO

! Alter model for Mars

ALTER ALL 'PLUTO'MARS'

! Solve Mars model

GO

! Alter model for Saturn

ALTER ALL 'MARS'SATURN'

! Solve Saturn model

GO

! Restore parameters

SET TERSEO 0

SET ECHOIN 0

Command Script: DOGS.LTF

We use two SET commands to set two of LINGO's parameters. First, we set ECHOIN to
1, which causes LINGO to echo all command script input to the screen. This can be useful when you are trying to debug a script file. Next, we set TERSEO to
1. This causes LINGO to go into terse output mode, which suppresses the default solution report each time we solve a model.

Next, we include the MODEL: command to put LINGO into model input mode. It is important here to remember the MODEL: statement is a command.
When LINGO encounters this command in a script file, it reads all subsequent text in the file as model text until it encounters the END command. This model then becomes the current model in memory.

The key feature to note in our model is the data section:

DATA:

REQUIRED = @FILE( 'PLUTO.LDT');

@TEXT( 'PLUTO.TXT') = START;

ENDDATA


We use the @FILE function to include the staffing requirements from an external file and we use the @TEXT function
to send the values of the START attribute to a file.

After the END statement, we have a GO command to solve the model for the Pluto stand. We then include an ALTER command
to change all occurrences of 'PLUTO' with 'MARS'. This command will change the data section to (changes in bold):

DATA:

REQUIRED = @FILE( 'MARS.LDT');

@TEXT( 'MARS.TXT') = START;

ENDDATA


 

Assuming we have the staffing requirements for the Mars stand in the file MARS.LDT, our model is then ready to run again. However, this time it will solve for the START values for the Mars hot dog stand. We include commands to do the same for the Saturn
location as well. Finally, we have two SET commands to restore the modified parameters.

You can run this command script by issuing the File|Take Commands command in Windows versions of LINGO, or you can use the TAKE command
in other versions. Once the command script has been executed, you will find the three solution files: PLUTO.TXT, MARS.TXT, and SATURN.TXT. These files will contain the optimal values for the START attribute for each of the three locations.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: