现有7.4米钢材一批,现需焊接钢架,需要2.9米100根,2.1米100根,1.5米100根,问怎样切割用7.4米钢材最少?
1.先考虑1根的情况:
7.4米分别切割2.9,2.1,1.5最多能有几根?
总长 | 2.9 | 2.1 | 1.5 |
---|---|---|---|
7.4 | 2 | 3 | 4 |
但是当我们用7.4-2*2.9=1.6米 还可以切割1根1.5米余0.1米。
那么隐含条件就出现一个 余量必然小于1.5米。
那么在这个 余量>=0,同时余量<1.5的条件下有多少种组合方式?
用VBA进行穷举
Sub ListCuttingMethods()
Dim lengths() As Variant
lengths = Array(2.9, 2.1, 1.5) ' 定义可以切割的长度
Dim maxCount() As Integer
Dim totalLength As Double
totalLength = 7.4 ' 钢材总长度
ReDim Preserve maxCount(LBound(lengths) To UBound(lengths)) As Integer
Dim i As Integer, j As Integer, k As Integer
Dim remainingLength As Double
Dim method As String
' 计算每种长度的最大可切割数量
For i = LBound(lengths) To UBound(lengths)
maxCount(i) = Int(totalLength / lengths(i))
Next i
' 遍历所有可能的切割数量组合
For i = 0 To maxCount(0)
For j = 0 To maxCount(1)
For k = 0 To maxCount(2)
' 计算剩余长度
remainingLength = totalLength - (i * lengths(0) + j * lengths(1) + k * lengths(2))
' 检查剩余长度
If remainingLength < 1.5 And remainingLength >= 0 Then
m = m + 1
Cells(m + 1, 1) = i
Cells(m + 1, 2) = j
Cells(m + 1, 3) = k
method = "2.9米 x " & i & ", 2.1米 x " & j & ", 1.5米 x " & k
Debug.Print method ' 在Immediate窗口打印切割方法
End If
Next k
Next j
Next i
End Sub

得出8种方法,同时问题可以转化为用规划求解来处理最小值问题:怎样下料让余料最少?

=SUMPRODUCT($B$2:$B$9,C2:C9)
需要2.9,2.1,1.5各100根

求解目标值为:切割方式用量-切割后总量 的最小值

结果

就是说采用3种组合切割方式下料余量最少即:
X3:切2.1米2根,1.5米2根 共30根
X7:切2.9米1根,2.1米2根 共20根
X8:切2.9米2根,1.5米1根 共40根
为最优切割方案,用量为90根。
当然实际下料没有这么理想的状态,会多于90根的情况,但不应该多太多。
网友评论