julia编写格子网络上的囚徒困境

我是做复杂网络的囚徒困境方向的,最近刚从fortran转过来,可是一个很简单的模型一直得不到正确的结果,就是格子网络上的weak囚徒困境,策略同步更新,之前用fortran的时候当收益矩阵为[1 0;1 0]的时候,对应的合作水平是0.55左右,可是我用julia计算的结果是1全合作状态。实在找不到问题,有没有相关方向的大佬,贴上完整代码

main.jl

include("squareLattice.jl")
include("squareLatticeNeighbor.jl")
include("constant.jl")
using DelimitedFiles
using Statistics
using Random
using Distributed
using SharedArrays
using GR
Random.seed!()

function evolutionGame()
#   addprocs(4-nprocs())
#   println("Running ",nprocs()," processes")
   lattice = squareLattice(len)
   neighbor = squareLatticeNeighbor(lattice)
#   fccList=Array{Float64,2}[]

   for b in 1.0:0.002:1.0
      println(b)
      payoffMatrix=[1 0;b 0]
      fc=SharedArray{Float64}((xzTime,fullStep),init=A->(A=zeros(xzTime,fullStep)))
#      fc=zeros(Float64, nodeNumber)
#       @sync @distributed for xz=1:xzTime
      for xz=1:xzTime
         nodeStrategy = zeros(Int, nodeNumber)
         nodeStrategyMid = zeros(Int, nodeNumber)
         nodeStrategy[1:nodeNumber] = rand([1,2], nodeNumber)
         fc[xz,1]=count(nodeStrategy.==1)/nodeNumber
         for t in 2:fullStep
            payoff=Float64[]
             for k in 1:nodeNumber
                pay=0
                for nei in 1:4
                   pay += payoffMatrix[nodeStrategy[k],nodeStrategy[neighbor[k,nei]]]
                end
                push!(payoff,pay)
             end
             for k in 1:nodeNumber
                nei=rand((1,2,3,4))
                pLearn = 1/(1+exp((payoff[k]-payoff[neighbor[k,nei]])/kai))
                zLearn = rand()
                if zLearn < pLearn
                   nodeStrategyMid[k] = nodeStrategy[neighbor[k,nei]]
                else
                   nodeStrategyMid[k] = nodeStrategy[k]
                end
             end
            nodeStrategy=nodeStrategyMid
            fc[xz,t]=count(nodeStrategy.==1)/nodeNumber
         end
      end
      file=open("test1.txt","w")
      fcc=mean(fc,dims=1)
      writedlm(file,fcc,"\r\t")
      close(file)
      fccMean=0
      for i in evolute_step+1:fullStep
         fccMean +=fcc[i]
      end
      fccMean /= mean_step
       writedlm(file,[b fccMean])
       println([b fccMean])
  end


end

squareLattice.jl

include("constant.jl")
function squareLattice(length)
    lattice = zeros(Int,length+2,length+2)
    for i in 2:length+1
        for j in 2:length+1
            lattice[i,j] = (i-2)*length+(j-1)
        end
    end
    for i in 2:length+1
        lattice[1,i] = lattice[length+1,i]
        lattice[length+2,i] = lattice[2,i]
    end
    for i in 2:length+1
        lattice[i,1] = lattice[i,length+1]
        lattice[i,length+2] = lattice[i,2]
    end
    return lattice
end

squareLatticeNeighbor.jl

include("constant.jl")
function squareLatticeNeighbor(lattice)
    neighbor = zeros(Int,nodeNumber,5)
    for i in 2:len+1
        for j in 2:len+1
            k = lattice[i,j]
            neighbor[k,1] = lattice[i-1,j]
            neighbor[k,2] = lattice[i+1,j]
            neighbor[k,3] = lattice[i,j-1]
            neighbor[k,4] = lattice[i,j+1]
            neighbor[k,5] = lattice[i,j]
        end
    end
    return neighbor
end

constant.jl

len = 100
nodeNumber = len ^ 2
fullStep = 1000
kai=0.1
evolute_step = 900
mean_step = fullStep - evolute_step
xzTime=1

代码上黑色的是注释掉的,不知道咋回事变成粗体了 :joy:

需要格式化代码,不然默认是 markdown

```julia
# 注意上面是 **反引号** ,是键盘上的 ~ 键
# code

```

复制粘贴就行了,效果见下

# 注意上面是 **反引号** ,是键盘上的 ~ 键
# code

哦哦,明白明白,超级感谢

请问我的代码的随机数的产生会不会有问题,我实在找不到别的逻辑问题,一直怀疑随机数的产生的问题

问题找到了,深复制浅复制的问题

1 个赞