车辆路径问题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

真香!!!

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

发布了11 篇原创文章 · 获赞 27 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sinat_41644416/article/details/97801174