我在装配线上有一组机器的路由序列。每条路线都必须经过整条线路(也就是说,如果你只运行第一台和第二台机器,你仍然要考虑从第二台到第二台机器的距离)。
我有六台不同的机器(720种可能的机器组合),线路上每个位置之间的距离固定。第一台和第二台机器之间的距离是100'第二和第三机器之间的距离是75'第三和第四是75'第四和第五是25'第五和第六是25& #39;
我有4种不同的产品必须在线上运行,并且每种产品都有固定的路由。
我的问题是,如何设置一个vba代码或解算器,让我可以运行所有可能的线路设置组合并确定此线路的最佳设置?任何机器都可以放在任何位置,只要它优化结果!
这四条产品路线是:
A - B - C - D - F
A - C - B - D – E - F
A - F - E - D - C - B - A - F
A - C - E - B - D – F
答案 0 :(得分:0)
运行所有可能的组合 - 如果你真的需要这样做 - 是Heap's algorithm之类的工作,虽然我更喜欢普通的改变方法:
Sub Evaluate(Lineup() As String)
' dummy evaluation, just output the permutation
Dim OffCell As Long
For OffCell = LBound(Lineup, 1) To UBound(Lineup, 1)
ActiveCell.Offset(0, OffCell).Value = Lineup(OffCell)
Next OffCell
ActiveCell.Offset(1, 0).Activate
End Sub
Sub AllPerms(Lineup() As String)
' Lineup is a 1-D array indexed at 1
Dim LSize As Long
Dim Shift() As Long
Dim Tot As Long
Dim Idx As Long
Dim Level As Long
Dim Change As Long
Dim Offset As Long
Dim TempStr As String
LSize = UBound(Lineup)
ReDim Shift(LSize)
'count of permutations, set initial changes
Tot = 1
For Idx = 2 To LSize
Tot = Tot * Idx
Shift(Idx) = 1 - Idx
Next Idx
Shift(1) = 2 ' end condition
' go through permutations
For Idx = 1 To Tot
' check this one
Call Evaluate(Lineup)
' switch for the next
Level = LSize
Offset = 0
Change = Abs(Shift(Level))
Do While Change = 0 Or Change = Level
If Change = 0 Then Shift(Level) = 1: Offset = Offset + 1
If Change = Level Then Shift(Level) = 1 - Level
Level = Level - 1
Change = Abs(Shift(Level))
Loop
Shift(Level) = Shift(Level) + 1
Change = Change + Offset
TempStr = Lineup(Change)
Lineup(Change) = Lineup(Change + 1)
Lineup(Change + 1) = TempStr
Next Idx
End Sub
Sub ABCDEF_case()
Dim LU(6) As String
LU(1) = "A"
LU(2) = "B"
LU(3) = "C"
LU(4) = "D"
LU(5) = "E"
LU(6) = "F"
Call AllPerms(LU)
End Sub