|
本帖最后由 lee1892 于 2012-12-1 14:15 编辑
花了2天时间研究LindoAPI,总算模型搞定,给我来这么个消息:
"License is too small for the given problem."
我吐血啊。。。
查了一下人家的API价格
变量2000->$195,变量8000->$395,变量32000->$695,变量无限->$995
法师这个要设置60000个方案,贵死。。。6000+RMB啊
API快是快,实在是不值得买啊,俺还是用那个OLE的办法吧,跳对话框烦就烦点吧。。。
找LindoAPI Crack中。。。
调用LindoAPI的办法,结合上面那个CLASS文件的,要编译成DLL用。。。
可惜了。。。
- Public Sub Calculate()
- Dim i As Long, j As Long
- '----------------------------
- ' 获得检查证书
- '----------------------------
- Dim pEnv As Long, nErr As Long
- Dim LicenseKey As String * LS_MAX_ERROR_MESSAGE_LENGTH
- nErr = LSloadLicenseString(strLicensePath, LicenseKey)
- Call CheckErr(pEnv, nErr)
- '----------------------------
- ' 创建Lindo环境
- '----------------------------
- pEnv = LScreateEnv(nErr, LicenseKey)
- If nErr > 0 Then
- Err.Raise Number:=1000, Description:="创建Lindo环境失败"
- Exit Sub
- End If
- '----------------------------
- ' 计算切割方案
- '----------------------------
- Call GeneratePatterns
- '----------------------------
- ' 创建计算模型实例
- '----------------------------
- Dim pMod As Long
- pMod = LScreateModel(pEnv, nErr)
- Call CheckErr(pEnv, nErr)
- '----------------------------
- ' 构造计算模型
- '----------------------------
- '变量数量,即所获得的方案数量
- Dim nVars As Long
- nVars = lngPlansCount
- '约束数量,等于产品种类个数
- Dim nRows As Long
- nRows = lngProdCount
- '目标方向,最小
- Dim nDir As Long
- nDir = LS_MIN
- '目标常数,A constant value to be added to the objective value.
- Dim dObjConst As Double
- dObjConst = 0
- '目标参数,即方案原材料长度数组
- Dim dObjCoef() As Double
- ReDim dObjCoef(0 To nVars)
- For i = 0 To nVars - 1
- dObjCoef(i) = arrLngAllRaws(i + 1, 1)
- Next
- '约束值数组,即产品数量数组
- Dim dB() As Double
- ReDim dB(0 To nRows)
- For i = 0 To nRows - 1
- dB(i) = arrLngProdReq(i + 1)
- Next
- '约束条件
- Dim cConTypes As String
- For i = 0 To nRows - 1
- cConTypes = cConTypes & Chr(LS_CONTYPE_LE) '"L"
- Next
- '约束非0参数个数,即全部方案中非0数值的个数
- Dim nNZ As Long
- nNZ = 0
- '约束非0参数数组
- Dim dA() As Double
- ReDim dA(0 To nVars * nRows)
- '约束非0参数的列起始编号数组,个数为方案个数+1
- Dim nBegCol() As Long
- ReDim nBegCol(0 To nVars + 1)
- '约束非0参数的行编号数组
- Dim nRowX() As Long
- ReDim nRowX(0 To nVars * nRows)
- Dim dMaxBound As Double '变量最大值
- dMaxBound = 0
- Dim bNewRow As Boolean
- For i = 1 To nVars '列为方案
- bNewRow = True
- For j = 1 To nRows '行为产品种类
- If arrLngAllPlans(i, j) > 0 Then
- dA(nNZ) = arrLngAllPlans(i, j)
- If bNewRow Then
- nBegCol(i - 1) = nNZ
- bNewRow = False
- End If
- nRowX(nNZ) = j - 1
- nNZ = nNZ + 1
- If dMaxBound < arrLngProdReq(j) / arrLngAllPlans(i, j) Then
- dMaxBound = arrLngProdReq(j) / arrLngAllPlans(i, j)
- End If
- End If
- Next
- Next
- ReDim Preserve dA(nNZ)
- nBegCol(nVars) = nNZ
- ReDim Preserve nRowX(nNZ)
- '变量左边界数组
- Dim dLower() As Double
- ReDim dLower(0 To nVars)
- For i = 0 To nVars - 1
- dLower(i) = 0
- Next
- '变量右边界数组
- Dim dUpper() As Double
- ReDim dUpper(0 To nVars)
- For i = 0 To nVars - 1
- dUpper(i) = dMaxBound
- Next
- '----------------------------
- ' 输入到计算模型
- '----------------------------
- nErr = LSloadLPData(pMod, nRows, nVars, nDir, _
- dObjConst, dObjCoef(0), dB(0), _
- cConTypes, nNZ, nBegCol(0), ByVal 0, _
- dA(0), nRowX(0), dLower(0), dUpper(0))
- Call CheckErr(pEnv, nErr)
- '----------------------------
- ' 设置变量类型
- '----------------------------
- '变量类型
- Dim cVarType As String
- For i = 0 To lngPlansCount
- cVarType = cVarType & Chr(LS_VARTYPE_INT) '"I"
- Next
- nErr = LSloadVarType(pMod, cVarType)
- Call CheckErr(pEnv, nErr)
- '----------------------------
- ' 模型计算
- '----------------------------
- nErr = LSsolveMIP(pMod, ByVal 0)
- Call CheckErr(pEnv, nErr)
- 'Dim nSolStat As Long
- 'nErr = LSoptimize(pMod, LS_METHOD_PSIMPLEX, nSolStat)
- 'Call CheckErr(pEnv, nErr)
- '----------------------------
- ' 获取计算结果
- '----------------------------
- Dim dObj As Long
- nErr = LSgetInfo(pMod, LS_DINFO_POBJ, dObj)
- Call CheckErr(pEnv, nErr)
- Dim dX() As Double
- ReDim dX(0 To lngPlansCount)
- nErr = LSgetPrimalSolution(pMod, dX(0))
- Call CheckErr(pEnv, nErr)
- '********************************************
- '* 变量太多,免费证书不支持,后面不写了。。。*
- '********************************************
- '----------------------------
- ' 删除Lindo环境
- '----------------------------
- Call LSdeleteEnv(pEnv)
- End Sub
复制代码
|
|