我有一个侧面有l,w,h(自然数字)的盒子。我必须在约束条件下将此标准盒打包到BIN:最长BIN侧< = 150,最长BIN侧+ 2 *(其他2侧的总和)< = 300。基于拉格朗日乘数,在这些约束条件下,最大BIN体积和边长为100x50x50 = 250000。最佳BIN侧应接近100x50x50并且必须具有最大音量。现在下面的代码可以正常工作,但是对于一个小侧面的盒子,它需要更多的时间。例如,如果框边为1x1x1,则在上述约束下计算所有选项150x150x150。如果有人更好地了解如何改进此代码,请提供帮助。
`Sub Macro1()
l = Sheets("list").Range("a" & 2)'45
w = Sheets("list").Range("b" & 2)'20
h = Sheets("list").Range("c" & 2)'30
result = 150 / l
ll = Math.Round(150 / l, 0)
If result <> ll Then
ll = ll + 1
Else
End If
result = 150 / w
lw = Math.Round(150 / w, 0)
If result <> lw Then
lw = lw + 1
Else
End If
result = 150 / h
lh = Math.Round(150 / h, 0)
If result <> lh Then
lh = lh + 1
Else
End If
Dim londis() As Double
Dim shortdis() As Double
Dim options() As Double
ReDim options(lw * ll * lh)
ReDim longdis(lw * ll * lh)
ReDim shortdis(lw * ll * lh)
k = 6
s = 0
`
For i = 0 To lh
For j = 0 To lw
For n = 0 To ll
summa = i * h + j * w + n * l
If summa <= 150 Then
'Sheets("list").Range("a" & k) = summa
s = s + 1
options(s) = summa
'longdis(s) = 100 - summa
'shortdis(s) = 50 - summa
If summa > 100 Then
longdis(s) = 100 - summa
Else
longdis(s) = summa - 100
End If
If summa <= 50 Then
shortdis(s) = summa - 50
Else
shortdis(s) = 50 - summa
End If
k = k + 1
Else
GoTo 1
End If
Next n
1:
Next j
Next i
For i = 1 To s - 1
For j = i + 1 To s
If shortdis(i) < shortdis(j) Then
pTemp1 = options(i)
pTemp2 = longdis(i)
pTemp3 = shortdis(i)
options(i) = options(j)
shortdis(i) = shortdis(j)
longdis(i) = longdis(j)
options(j) = pTemp1
longdis(j) = pTemp2
shortdis(j) = pTemp3
Else
End If
Next j
Next i
t = 1
maxVol = 0
pT = 0
pL = 0
prodVol = l * w * h
maxPosUnit = Int(250000 / prodVol)
maxPosVol = maxPosUnit * prodVol
Do While t < s - 2
longside = 300 - 2 * (options(t) + options(t + 1))
For i = 1 To s
If options(i) <= longside Then
vol = options(i) * options(t) * options(t + 1)
If vol > maxVol Then
maxVol = vol
pT = t
pL = i
Else
End If
Else
End If
If options(i) = longside Then
Exit Do
End If
Next i
t = t + 1
Loop
Sheets("list").Range("d" & 2) = prodVol
Sheets("list").Range("e" & 2) = maxPosUnit
'Sheets("list").Range("f" & 2) = 250000 - maxPosVol
Sheets("list").Range("a" & 3) = options(pT)
Sheets("list").Range("b" & 3) = options(pT + 1)
Sheets("list").Range("c" & 3) = options(pL)
boxVol = options(pT) * options(pT + 1) * options(pL)
Sheets("list").Range("d" & 3) = boxVol
Sheets("list").Range("e" & 3) = Int(boxVol / prodVol)
Sheets("list").Range("f" & 3) = boxVol - Int(boxVol / prodVol) * prodVol
End Sub