车辆路径问题VRPTW之四【节约里程法ClarkWright算法】

自从上次使用了最近邻算法后,运营成本降低了三分之一,你爹很高兴,让你继续努力,将家族企业发扬光大,早日进入世界五百强。
你也发现了算法的力量和乐趣。
研究了一下,发现还有一种算法叫做节约里程法。
赶快学习了一下。

function ClarkWright_Seq(mu)
    local savings = compute_savings(mu)
    local solution = getBackForth()
    local cIndex = 1
    while cIndex <= #solution do
        ::continue::
        for s,saving in ipairs(savings) do
            if solution[cIndex][1].id == saving.i then
                for j=cIndex+1,#solution do
                    if solution[j][#solution[j]].id == saving.j then 
                        if feasible(solution[j], solution[cIndex]) then
                            solution[j]:push_back_seq(solution[cIndex])
                            solution[cIndex], solution[j] = solution[j], solution[cIndex]
                            table.remove(savings, s)
                            table.remove(solution, j)
                            goto continue
                        else
                            break
                        end 
                    end 
                end
            elseif saving.j == solution[cIndex][#solution[cIndex]].id then
                for j=cIndex+1,#solution do
                    if solution[j][1].id == saving.i then 
                        if feasible(solution[cIndex], solution[j]) then
                            solution[cIndex]:push_back_seq(solution[j])
                            table.remove(savings, s)
                            table.remove(solution, j)
                            goto continue
                        else
                            break
                        end 
                    end 
                end
            end 
        end    
        cIndex = cIndex + 1
    end 
    return solution
end 

然后一跑
在这里插入图片描述
我靠,居然更差了,这两个糟老头子坏得很,居然比我最近插入还差。
垃圾!
不过,又看了下,发现还有一个版本,并行版本。秉着不抛弃不放弃的原则,你又继续搞下去。

function ClarkWright_Para(mu)
    local savings = compute_savings(mu)
    local solution = getBackForth()
    for s,saving in ipairs(savings) do
        for i, route1 in ipairs(solution) do 
            if route1[1].id == saving.i then
                for j,route2 in ipairs(solution) do
                    if i~=j then
                        if saving.j == route2[#route2].id then  
                            if feasible(route1, route2) then
                                route2:push_back_seq(route1)
                                table.remove(solution, i)
                            end 
                            goto continue
                        end 
                    end 
                end 
            end 
        end 
        ::continue::
    end 
    return solution
end

真香!!!

在这里插入图片描述
离世界五百强又近了一步

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值