我想调用OPL CPLEX来解决用Excel VBA编写的程序中的问题。我想创建一个OPL模型,并将.mod和.dat文件加载到该模型中。在互联网上搜索了很长时间后,我想出了IBM网站上发布的以下几行脚本。
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文件,如下所示:
Public Declare Function AccessCPLEX Lib "C:\ILOG\CPLEX_Studio1261\opl\lib\oplall.dll" Alias "oplall" [([arglist])] [As type]
我没有足够的经验来处理这样的声明函数,所以我非常困惑。函数的类型是什么(应用程序、对象、长整型、字符串等)是吗?如何在VBA中引入变量类型,包括Oplfactory、OplModelSource、OplModelDefinition等?
任何帮助都将不胜感激。
谢谢,
发布于 2016-02-23 00:16:39
你可以看一看
https://github.com/AlexFleischerParis/howtowithopl/blob/master/oil.mod
在那里,您将找到一个示例,说明如何从VBA中简单地调用oplrun
关键部分是调用外部exe oplrun.exe。
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
结束子对象
问候
https://stackoverflow.com/questions/35420442
复制相似问题