Using OPL CPLEX in Excel VBA?

橙三吉。 提交于 2019-12-12 02:51:58

问题


I'd like to call OPL CPLEX and solve problems in a program coded on Excel VBA. I want to create an OPL model and, load .mod and .dat file into that model. After a long search process on the internet, I came up with some lines of script below released on IBM website.

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

However, to be able to use the script above, there should be a function to be declared the OPL library or oplall.dll file as follows:

Public Declare Function AccessCPLEX Lib "C:\ILOG\CPLEX_Studio1261\opl\lib\oplall.dll" Alias "oplall" [([arglist])] [As type]

I do not have enough experience for a declaration function like that so I am very confused. What would the type of the function (application, object, long, string etc.) be? How can I introduce variable types including Oplfactory, OplModelSource, OplModelDefinition, etc. into the VBA?

Any help would be a lot appreciated.

Thanks,


回答1:


you may have a look at

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014880109&ps=25

There you will find an example on how to simply call oplrun from VBA

The key part is to call external 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

End Sub

regards



来源:https://stackoverflow.com/questions/35420442/using-opl-cplex-in-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!