| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 440 人关注过本帖
标题:求助时间约束条件的代码编写
取消只看楼主 加入收藏
筱超
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-11-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
求助时间约束条件的代码编写
节约 算 法 又称C-W 算法,是由Clarke和Wright于1964年首次提出的。它的基本思想是首先把各点单独与源点0(车场)相连,构成1条仅含一个点的线路。总费用为两倍的从原点到各点的距离的费用 。然后计算将点i和j连接在一条线路上费用的“节约值”: S(i,j)=c0i+ ci0+ c0j+ cj0-(c0i+ cij+ cj0)= c0i+ c0j-cij
S(i,j) 越大,说明把i和i连接在一起时总路程减少越多。构造线路时,根据S(i,j) 从大到小的顺序进行,实现时可在表上操作,具体步骤如下:
Step1: 计算节约值S(i,j) ,并按从大到小顺序排列成表格形式;
Step2:考察表格中最大元素S(i,j) 对应的点i和点j,检查是否满足下列条件:
(1)若 点 i 和点j均不在己构成的线路上,则可连接点i和点j,得到线路段0->i->j->0,转步骤Step3;
(2)若 点 i 或点j在已构成的线路上,但不是线路的内点(即不与源点0直接相连),则可以连接,连接后得到线路段 ,转步骤Step3;
(3)若 点 i 和点j位于己构成的不同线路上,且均不是内点,则连接后的得到线路段 ,转步骤Step3;
(4)若 点 i和 点j位于已构成的同一条线路上,则不能再进行连接,转步骤Step3;
Step3:划去第i行和第j列,即i点不能再到其他点,而j点也不能由其他点到达;
Step4:若所有元素均被划去,则己得到完整线路,算法终止;否则,在没被划去的元素中选择最大元素,转步骤Step2。

其中的车场0的坐标为(25,90)
其余的客户坐标分别为:(56,72),(10,107),(6,125),(27,79),(18,115)就是在这几个中间进行距离的优化,优化原则是上面所说的c-w算法。不是作业,要给出各部的实现思想也可以。能够给全源码,那当然是感谢咯!

注意,c-w算法不是最优算法,但相当好。
我自己对这个算法的理解不一定和上面给出的一样。请你认真运行检查我下面的代码:

Option Explicit

Private Type point
    x As Double
    y As Double
End Type

Private Type save
    i As Long
    j As Long
    s As Double
End Type

Private points() As point, cost() As Double, saving() As save, n As Long, m As Long
Private trip() As String
'http://www.
'C-W Saving Algorithm

Private Sub Command1_Click()
Dim i As Long, j As Long, inti As Long, intj As Long, pi As Long, pj As Long, str() As String
ReDim saving((n * (n - 1) / 2)) As save

For i = 1 To n - 1
For j = i + 1 To n
    m = m + 1
    saving(m).i = i
    saving(m).j = j
    saving(m).s = cost(0, i) + cost(0, j) - cost(i, j)
Next j
Next i

For i = 1 To m - 1                'sort saving list
For j = i + 1 To m
If saving(j).s > saving(i).s Then
    saving(0) = saving(i)
    saving(i) = saving(j)
    saving(j) = saving(0)
End If
Next j
Next i

Print "Saving:"
For i = 1 To m
    Print saving(i).i, saving(i).j, Round(saving(i).s, 2)
Next i

ReDim trip(1 To m)

j = 0
For i = 1 To m
If saving(i).s < 0 Then Exit For

inti = InTrip(saving(i).i & "", pi)
intj = InTrip(saving(i).j & "", pj)
                             'Create or join trips:
If inti = 0 And intj = 0 Then
    j = j + 1
    trip(j) = saving(i).i & "-" & saving(i).j
ElseIf inti > 0 And intj = 0 Then
    If pi = 1 Then
    trip(inti) = saving(i).j & "-" & trip(inti)
    ElseIf pi = 2 Then
    trip(inti) = trip(inti) & "-" & saving(i).j
    End If
ElseIf inti = 0 And intj > 0 Then
    If pj = 1 Then
    trip(intj) = saving(i).i & "-" & trip(intj)
    ElseIf pj = 2 Then
    trip(intj) = trip(intj) & "-" & saving(i).i
    End If
ElseIf inti <> intj Then 'now inti>0 and intj>0. i and j in different trips. Only join end to end

    If pi = 1 And pj = 1 Then
    str = Split(trip(inti), "-")
    For j = 0 To UBound(str)
    trip(intj) = str(j) & "-" & trip(intj)
    Next j
    trip(inti) = ""
    ElseIf pi = 1 And pj = 2 Then
    trip(intj) = trip(intj) & "-" & trip(inti)
    trip(inti) = ""
    ElseIf pi = 2 And pj = 1 Then
    trip(intj) = trip(inti) & "-" & trip(intj)
    trip(inti) = ""
    ElseIf pi = 2 And pj = 2 Then
    str = Split(trip(inti), "-")
    For j = UBound(str) To 0 Step -1
    trip(intj) = trip(intj) & "-" & str(j)
    Next j
    trip(inti) = ""
    End If

End If

Next i

Output

End Sub

Private Function InTrip(node As String, position As Long) As Long
Dim i As Long

For i = 1 To m
If trip(i) <> "" Then
If Left(trip(i), Len(node) + 1) = node & "-" Then   'head of the trip
    position = 1
    InTrip = i
    Exit For
ElseIf Right(trip(i), Len(node) + 1) = "-" & node Then   'tail of the trip
    position = 2
    InTrip = i
    Exit For
ElseIf InStr(trip(i), "-" & node & "-") Then           'middle of the trip
    position = 3    'will caurse mistake without this
    InTrip = i
    Exit For
End If
End If
Next i

End Function

Private Sub Output()
Dim i As Long

Print "Trip:"
For i = 1 To m
If trip(i) <> "" Then
Print "0-" & trip(i) & "-0"
End If
Next i

End Sub

Private Sub Form_Load()
Dim i As Long, j As Long
n = 6  '5
ReDim points(n), cost(0 To n - 1, 1 To n)
Me.AutoRedraw = True

points(0).x = 47   '25
points(0).y = 11   '90
points(1).x = 28   '56
points(1).y = 6    '72
points(2).x = 8    '10
points(2).y = 2    '107
points(3).x = 77   '6
points(3).y = 9    '125
points(4).x = 74   '27
points(4).y = 33   '79
points(5).x = 52   '18
points(5).y = 33   '115
points(6).x = 35
points(6).y = 38

Print "Cost:"
Print "",
For j = 1 To n
Print j,
Next j
Print

For i = 0 To n - 1
Print i,
For j = 1 To i
Print "",
Next j
For j = i + 1 To n                     'cost(i,j) is the distance(cost) between i and j
cost(i, j) = Sqr((points(i).x - points(j).x) ^ 2 + (points(i).y - points(j).y) ^ 2)
Print Round(cost(i, j), 2),
Next j
Print
Next i

End Sub
搜索更多相关主题的帖子: 具体步骤 费用 
2012-11-01 15:58
筱超
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-11-1
收藏
得分:0 
其中的车场0的坐标为(25,90)
其余的客户坐标分别为:(56,72),(10,107),(6,125),(27,79),(18,115)就是在这几个中间进行距离的优化,优化原则是上面所说的c-w算法。不是作业,要给出各部的实现思想也可以。能够给全源码,那当然是感谢咯!


加上一个时间约束使得零部件运货车从一点到另一个点的时间在t    ~t'内
2012-11-01 16:05
快速回复:求助时间约束条件的代码编写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016563 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved