首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在Excel VBA中使用OPL CPLEX?

在Excel VBA中使用OPL CPLEX?
EN

Stack Overflow用户
提问于 2016-02-16 06:41:08
回答 1查看 737关注 0票数 0

我想调用OPL CPLEX来解决用Excel VBA编写的程序中的问题。我想创建一个OPL模型,并将.mod和.dat文件加载到该模型中。在互联网上搜索了很长时间后,我想出了IBM网站上发布的以下几行脚本。

代码语言:javascript
代码运行次数:0
运行
复制
Sub CPLEXCallHere()

'To create the Concert environment
Dim oplF As Oplfactory

'To create the error handler
Dim errorHandler As OplErrorHandler

'To identify the model source
Dim modelSource As OplModelSource

'To identify the model definition
Dim def As OplModelDefinition

'To create the engine instance
Dim cp As cp

'To create the OPL model
Dim opl As OplModel

'Specifying a data source
Dim dataSource As OplDataSource

oplF = New Oplfactory   'concert environment
errorHandler = oplF.CreateOplErrorHandler() 'error handler

modelSource = oplF.CreateOplModelSource(DATADIR + "/SA_MP_R1.mod") 'model source
def = oplF.CreateOplModelDefinition(modelSource, settings) 'model definition
cp = oplF.CreateCP()    'engine instance

opl = oplF.CreateOplModel(def, CPLEX) 'OPL model

dataSource = oplF.CreateOplDataSource(DATADIR + "/1 SA_MP_R1_1.dat")     'specifying data source
opl.AddDataSource (dataSource)

'Generating the Concert model
opl.Generate

'Solving the model
If (CPLEX.Solve()) Then

End If

'Accessing the solution through OPL
 opl.PrintSolution (Console.Out)
End Sub

但是,为了能够使用上面的脚本,应该有一个函数被声明为OPL库或oplall.dll文件,如下所示:

代码语言:javascript
代码运行次数:0
运行
复制
Public Declare Function AccessCPLEX Lib "C:\ILOG\CPLEX_Studio1261\opl\lib\oplall.dll" Alias "oplall" [([arglist])] [As type]

我没有足够的经验来处理这样的声明函数,所以我非常困惑。函数的类型是什么(应用程序、对象、长整型、字符串等)是吗?如何在VBA中引入变量类型,包括Oplfactory、OplModelSource、OplModelDefinition等?

任何帮助都将不胜感激。

谢谢,

EN

回答 1

Stack Overflow用户

发布于 2016-02-23 00:16:39

你可以看一看

https://github.com/AlexFleischerParis/howtowithopl/blob/master/oil.mod

在那里,您将找到一个示例,说明如何从VBA中简单地调用oplrun

关键部分是调用外部exe oplrun.exe。

代码语言:javascript
代码运行次数:0
运行
复制
Private Sub Button1CallOPL_Click()
MsgBox "External call to OPL"

Dim strPath As String
strPath = "C:\ILOG\CPLEX_Studio127\opl\bin\x64_win64\oplrun.exe C:\poc\callOPLfromExcel\oil.mod C:\poc\callOPLfromExcel\excelbuttonoilSheet.dat"

Shell strPath

结束子对象

问候

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35420442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档