教学课件:第七章-ILOG-OPL-界面.ppt
主讲人:雒兴刚东北大学系统工程研究所Email:Tel:83682292,优化软件与应用,第七章 OPL 界面,IDE 介绍,IDE 是什么:integrated development environment(IDE)for mathematical programming and combinatorial optimization applications.It is the graphical user interface(GUI)for the OPL modeling language and the ILOG Script for OPL scripting language.,第七章 OPL 界面,IDE 介绍,IDE能做什么:Create and modify model,data,and project files using the editing capabilities Execute a project Generate a compiled model Generate external data files or calculated data files to various formats Visualize OPL results in text or tabular form Visualize the state of variables at some point during the search for a solution Set GUI options and mathematical programming options Connect to a database or to a spreadsheet to read and write data Work with ILOG Script for OPL,the scripting language Profile the execution of a model Search for relaxations of variables and constraints,and for conflicts between constraints Debug scripts using the debug facilities,第七章 OPL 界面,IDE 介绍,IDE 环境处理三种文件:model files,data files,and project filesModels:保持OPL语句及ILOG脚本,后缀是.mod。也可以生成编译后的模型格式,后缀是.opl。Data:对于较大规模问题,可以分离数据之数据文件(.dat),数据文件中也可以说明外部数据源,如Excel文件或ODBC数据库源等。Projects:文本格式,XML表示。说明project关联的模型和数据文件。类似VC的工程文件。,第七章 OPL 界面,IDE 介绍,创建新文件:,第七章 OPL 界面,IDE 介绍,运行项目:如何检查语法:运行项目后,出错的语法会以红色标识。修改错误后,继续运行项目。,第七章 OPL 界面,IDE 介绍,查看运行结果:Solution TAB显示运行的结果。CPLEX statistics显示算法的具体运行参数。当然,可以通过Options设置显示的项。,第七章 OPL 界面,IDE 介绍,产生一个编译的opl文件:File-Generate Compiled Model File,第七章 OPL 界面,IDE 介绍,IDE文本编辑的特色:MDI approach;Syntax coloring;Multiple levels of Undo and Redo;Automatic indentation Blocks:as delimited by curly brackets;Bracket(or brace)matching;When typing,or),highlighted;Margin symbols the yellow arrow that indicates the current line the red box that indicates an error the red circle that indicates a breakpoint Reload prompt:If you modify a file with an external editor,you are prompted to reload Customization:options to customize the editor,第七章 OPL 界面,IDE 介绍,数据库支持:OPL通过ILOG DBLink 5.0 支持RDBMS;支持的数据库类型:OPL数据库支持通过函数DBconnection 来实现,该函数主要有两个参数:the database client you want to use:e.g.oracle81the connection string:e.g.scott/tigerilog“后面我们有一个详细的数据库连接的例子。,第七章 OPL 界面,IDE 介绍,为什么使用ILOG 脚本?Add preprocessing to prepare data for the model Control the flow while solving the model Set CPLEX parameters Add postprocessing to aggregate,transform,and format data(including results data)for display or for sending to another application,for example,a spreadsheet Solve repeated instances of the same model Feedback:Create algorithmic solutions where the output of one model instance is used as the input of a second model instance,第七章 OPL 界面,IDE 介绍,ILOG 脚本的入口点The main statement for a flow control script The execute statement for preprocessing and postprocessing scripts 没有独立的脚本文件;脚本文件总是和模型文件结合使用,第七章 OPL 界面,例:IDE Project,1、在IDE界面中,FileNewProject.,红色的星号表示是必选项。选择Location为C:Temp,在项目Name中键入MyProject;可以看到系统自动命名mod文件和dat文件为MyProject。用户也可以对其名称进行修改,或者点选其右面的图标,选择一个现有的文件添加到本项目中;注意一个项目中mod文件只有1个,dat文件可以有多个;点击“Create Project”,可以看到系统在C:Temp下创建了两个文件,Project窗口如右图。,第七章 OPL 界面,例:IDE Project,2、拷贝下面的代码到mod文件中,保存.,string Products=.;string Resources=.;tuple ProductData float demand;float insideCost;float outsideCost;float consumptionResources;ProductData productProducts=.;float capacityResources=.;dvar float+insideProducts;dvar float+outsideProducts;execute CPX_PARAM cplex.preind=0;cplex.simdisplay=2;minimize sum(p in Products)(productp.insideCost*insidep+productp.outsideCost*outsidep);subject to forall(r in Resources)sum(p in Products)productp.consumptionr*insidep=productp.demand;,第七章 OPL 界面,例:IDE Project,3、如图,选择“Add new data file”,选择和project文件相同的文件夹,输入文件名称MyProject.dat,保存.,第七章 OPL 界面,例:IDE Project,4、双击MyProject.dat,将下面的代码拷贝到右侧的编辑区,保存。运行文件,结果如图.,Products=kluski,capellini,fettucine;Resources=flour,eggs;product=#kluski:,capellini:,fettucine:#;capacity=20,40;,注意:项目可以有多个dat文件。一个dat中的变量定义可能参考另一个dat文件,IDE调取dat文件的顺序是从上到下。因此,可以通过点选dat文件右键菜单的move up来调整顺序。另外,从项目中移除一个文件也可以通过右键菜单来实现。,第七章 OPL 界面,例:IDE Project,另外,IDE也支持同时打开多个项目。例如,图中打开了4个项目,红色的、单选框选中的是当前项目。当前项目可以通过右键菜单中的“set as active project”来设定。,第七章 OPL 界面,例:IDE Project,5、在execute处设置一个断点,然后运行项目。,和高级语言的Debug按钮类似:,停止运行,暂停(当程序长时间运行时),继续运行,Step into,Step out,Step over,Run to cursor,第七章 OPL 界面,例:IDE Project,6、取消断点,继续运行项目。下面以这个例子的结果为例,介绍IDE的界面元素。,首先是Output窗口中的Console标签页:,如果编译出错,则显示语法错误;如果求解成功显示最优解状态,第七章 OPL 界面,例:IDE Project,Output窗口中的Solution标签页:,显示局优解partial solutions(如果有)和最优解。下面是另一个例子的结果。,第七章 OPL 界面,例:IDE Project,Output窗口中的Infeasibility标签页:因为本例有解,所以标签页为空白。下面是另一个例子的结果。,第七章 OPL 界面,例:IDE Project,Output窗口中的CPLEX Log标签页:,显示CPLEX求解过程中的日志信息,包括每个迭代过程的换基过程,第七章 OPL 界面,例:IDE Project,Output窗口中的CPLEX Statistics标签页:,显示CPLEX求解的各种统计信息,包括变量个数、约束个数、算法类型、迭代次数等等。,第七章 OPL 界面,例:IDE Project,Output窗口中的Profiler标签页:,右面是执行步骤,左面是计算时间和使用内存。通过分析这些信息,可以改进模型(缩短时间和减少内存),第七章 OPL 界面,例:IDE Project,Profiler标签页中,执行步骤主要分为以下两类:,左面的计算时间和使用内存的含义见下表:,第七章 OPL 界面,例:IDE Project,Output窗口中的Log标签页:,显示运行日志信息。,第七章 OPL 界面,例:IDE Project,模型窗口显示如图。,下面介绍IDE的模型窗口。7、关闭当前项目。,第七章 OPL 界面,例:IDE Project,可以显示模型的类型、常量、变量、变量的初始化数据。,8、重新调入刚才的项目,运行项目。,用鼠标点选Data或者Variables中的变量或常量,下面的Property和Value会显示相应的值;同时,右面的编辑区也会定位的模型的相关代码处。,第七章 OPL 界面,例:IDE Project,9、修改模型代码如下,然后重新运行项目。constraint ct1;constraint ct2;subject to ct1=forall(r in Resources)sum(p in Products)productp.consumptionr*insidep=productp.demand;,注意到前面的模型窗口中,Contraints没有详细的信息。这是因为mod文件中没有给约束命名。,第七章 OPL 界面,例:IDE Project,现在可以看到详细的约束信息(打开小加号):,另外,在模型窗口中,也可以双击元素,可以弹出窗口,显示详细信息。,第七章 OPL 界面,例:IDE Project,下面再介绍一点IDE的选项设置。10、如图方式选择;或者从菜单选择OptionsProject Options,第七章 OPL 界面,例:IDE Project,选择 OPL Language下的General,右面的Display Solution不选。重新运行项目,将只显示最优目标。,第七章 OPL 界面,IDE 界面详解,IDE主环境,脚本对象浏览器,菜单,主工具栏,执行工具栏,项目 浏览器,模型 浏览器,输出 窗口,编辑区域,状态显示,当前行和列,状态显示,第七章 OPL 界面,IDE 界面详解,菜单命令详解File菜单,第七章 OPL 界面,IDE 界面详解,File菜单,第七章 OPL 界面,IDE 界面详解,File菜单,第七章 OPL 界面,IDE 界面详解,Edit菜单,第七章 OPL 界面,IDE 界面详解,Edit菜单,第七章 OPL 界面,IDE 界面详解,Edit菜单,第七章 OPL 界面,IDE 界面详解,View菜单,第七章 OPL 界面,IDE 界面详解,Project菜单:其内容根据当前选定的对象而改变,第七章 OPL 界面,IDE 界面详解,Project菜单,第七章 OPL 界面,IDE 界面详解,Execution菜单,第七章 OPL 界面,IDE 界面详解,Execution菜单,第七章 OPL 界面,IDE 界面详解,Debug菜单,Options菜单,第七章 OPL 界面,IDE 界面详解,Window菜单,第七章 OPL 界面,IDE 界面详解,Help菜单,第七章 OPL 界面,IDE 界面详解,模型窗口的图标含义,第七章 OPL 界面,IDE 界面详解,状态图标的含义,第七章 OPL 界面,IDE 界面详解,数据类型图标的含义,