MicroCity的一点介绍-标号法、最小费用流



Lua语言

MicroCity的脚本用的是Lua语言,提到Lua大家想到的大多是游戏的制作,然而通过MicroCity这个软件,Lua语言可以辅助进行物流信息的处理。



一、Lua简单程序

用二维数组打印1~100孪生质数

s={}
k={}
d=0 --计算李生质数组数
for i=2,100 do --判断2~100的素数
    isPrime = true
    for a=2,i-1 do
        if i%a == θ then
            isprime = false
        end
    end
    if isPrime then
        b=i+2
            for c=2,b-1 do--判断+2是否为质数
                if b%c ==0 then
                    isPrime = false
                end
            end
            if isPrime then
                d=d+1 --组数, 共8组
                table. insert(s,i) -- 每组李生质数中较小的质数组成的数组
            end
    end
end
for e=1,d do
    k[e]={}
    for i=1,#s do
        k[e][1]=s[i]
        k[e][2]=s[1]+2
        print(k[e][1],"和",k[e][2],"为李生质数")
    end
    break 
end


二、物流信息的处理


1.标号法求最短路

标号法求解最短路的思想是运筹学中所学解决最短路问题的方法,从起点开始对各点临时标号,找到最短路后永久标号直至所有点检查完毕。

代码如下(示例):

local Links=GetControl("xian.shp")
local Points=GetControl("dian.shp")
local marked ={}     
local stpdis=  {}
local preid= {}   
local startid =24                                                      --设置起点
local endid=5                                                          --设置终点

for N=1,GetRecCount(Points) do                                          --GetRecCount:获得表格记录数量 
    stpdis[N] = 100000000                                               --初始化各点距离为正无穷
end

crtid = startid                                                         --设置当前节点
marked[crtid]=true                                                      --标记当前点(永久标号)
stpdis[crtid]=0                                                         --初始化当前点距离为零
preid[crtid]=-1                                                         --当前点前溯节点设为-1
while crtid ~= endid do                                                 --从当前点搜索直到终点
    for id = 1, GetRecCount(Links) do                                   --搜索所有链接
       local o = GetValue(Links, "O", id)                               --分别得到各条线的三个属性
       local d = GetValue(Links, "D", id)
       local dis = GetValue(Links, "DIS", id)
       if o == crtid then                                               --如果与当前节点邻接
           tmpid =d                                                     --并且另一端点的最短距离较大
           if stpdis[crtid] + dis < stpdis[tmpid] then       
              stpdis[tmpid] = stpdis[crtid] + dis        
              preid[tmpid] = crtid                                      --修改另一端点的最短距离和前溯节点
           end   
       elseif d==crtid then                                             --如果与当前节点邻接
           tmpid =o                                                     --并且另一端点的最短距离较大
           if stpdis[crtid] + dis < stpdis[tmpid] then       
              stpdis[tmpid] = stpdis[crtid] + dis        
              preid[tmpid] = crtid                                      --修改另一端点的最短距离和前溯节点
           end   
       end
       
    end
    
    crtid = endid                                                      --设置下一个当前节点
    for nodeid = 1,GetRecCount(Points) do                              --搜索所有节点
       if not marked[nodeid] and stpdis[nodeid] < stpdis[crtid] then   --找到未标记的有最小距离的节点
            crtid = nodeid                                             --重新设置当前节点
       end 
    end  
    marked[crtid] = true                                               --标记新的当前节点
end

local m={}                                                              --设置一个数组
c=endid                                                                 --将所有前溯节点存在数组里面
local i = 1
while c~=-1 do                                                          --当反推前溯节点回到-1时,结束循环
     m[i]=c
     c=preid[c]
     i=i+1
end
print("经过的ID为:")
while i~=1 do
    print("id:",m[i-1])                                                --正向输出所有点
    i=i-1
end

print("最短距离:",stpdis[endid])


2.最小费用流模型

代码如下:

lp = CreateLP()
SetobjFunction(lp, {2,2,1,3,1}, "min")--各边的费用
AddConstraint(lp,{1,1,0,0,0}, "=", 3)--起点的流出
AddConstraint(lp, {-1,0,1,1,0}, "=", 0)--流出等于流入
AddConstraint(lp, {0,-1,-1,0,1},"=", 0)
AddConstraint(lp, {0,0,0,-1,-1}, "=", -3)--终点的流入
AddConstraint(lp, {1,0,0,0,0}, "<=", 4)--各点的容量
AddConstraint(lp, {0,1,0,0,0}, "<=", 2)
AddConstraint(lp, {0,0,1,0,0}, "<=", 2)
AddConstraint(lp, {0,0,0,1,0}, "<=", 3)
AddConstraint(lp, {0,0,0,0,1}, "<=", 5)
for i=1,5 do
    local W = {}
    for j=1,5 do
        if i==j then
            w[j]=1
        else
            w[j] =0
        end
    end
    AddConstraint(lp, w, ">=", 0)
end
SolveLP(lp)
Print("最小费用为", Getobjective(1p))
Print("1-2流量是" , GetVariable(1p,1), ",",
"1-3流量是",GetVariable(lp,2),",",
"2-3流量是",GetVariable(lp,3),",",
"2-4流量是",GetVariable(lp,4),",",
"3-4流量是",GetVariable(lp,5))

具体最小费用流问题可以用线性规划求解,缺点代码繁琐,不方便适用于各类问题,比较适合小型最小费用流模型。



总结

可以在https://microcity.github.io上更新一下MicroCity,最近更新了能在地图上用尺子工具直接量出两点的真实距离,里面也有很多伪代码和函数介绍。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值