在Excel VBA中使用OPL CPLEX? [英] Using OPL CPLEX in Excel VBA?

查看:129
本文介绍了在Excel VBA中使用OPL CPLEX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给OPL CPLEX打电话,并用Excel VBA编码的程序解决问题。我想创建一个OPL模型,并将.mod和.dat文件加载到该模型中。在Internet上进行了漫长的搜索过程之后,我想出了以下几行脚本在IBM网站上发布。

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

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

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]

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

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.

谢谢,

推荐答案

您可以看看

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

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

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

关键是调用外部exe oplrun.exe

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

结束子

致谢

这篇关于在Excel VBA中使用OPL CPLEX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆