使用遗传算法第三方库GeneticAlgorithms报错:UndefVarError: AbstractRNG not defined

使用遗传算法第三方库GeneticAlgorithms报错:UndefVarError: AbstractRNG not defined。

[ Info: Precompiling GeneticAlgorithms [38a4a31c-f17b-5f22-8721-d9b4a0175df2]
ERROR: LoadError: UndefVarError: AbstractRNG not defined
Stacktrace:
 [1] top-level scope at none:0
 [2] include at .\boot.jl:317 [inlined]
 [3] include_relative(::Module, ::String) at .\loading.jl:1038
 [4] include(::Module, ::String) at .\sysimg.jl:29
 [5] top-level scope at none:2
 [6] eval at .\boot.jl:319 [inlined]
 [7] eval(::Expr) at .\client.jl:389
 [8] top-level scope at .\none:3
in expression starting at C:\Users\fuyaf\.julia\packages\GeneticAlgorithms\hKkPQ\src\GeneticAlgorithms.jl:37

出错源代码的链接在这里:

这个的意思是说,找不到AbstractRNG这个变量名,AbstractRNG目前是放在Random里了,没有放在Base里,所以应该只需要引入下就可以了

julia> AbstractRNG
ERROR: UndefVarError: AbstractRNG not defined

julia> using Random

julia> AbstractRNG
AbstractRNG

我看这个库有一年没更新了,改的时候多注意下兼容性方面的问题

谢谢,加了using Random之后,又出现以下问题:

谢谢,我尽量,我还是新手

你的截图看不到行号

我用的julia1.0,编辑器是最新版atom,自己装了julia client插件,windows 10操作系统,

看报错信息应该是要把 {...} 换成 [...]

不过没看出来哪里调用了,
可能需要看下你的 testga.jl 文件,

所有的test文件都是用这个里面的源文件,只不过我自己改的时候把testga.jl文件的开头using改成了include

我尝试改了下,改动挺多的,一会还有事,估计帮不到你了。。。

看代码逻辑不复杂,建议等你熟悉Julia一段时候后,可以自己动手写一个。

谢谢,急用啊,好改吗?

不知道这个包兼不兼容 0.7,你可以试试。
如果兼容,你可以用 0.7 运行它,然后把出来的 warning 改掉应该就能在 1.0 上运行了。
不兼容,可以搜错误然后先让他兼容 0.7,然后再进行如上操作。

有问题建议先去 google 一下看看有没有解决方案,再过来提问。或者你也可以看看有没有可以替代的包。急用也可以换到这个包兼容的版本。

module GeneticAlgorithms

-------

using Base
using Random
export Entity,
GAmodel,

    runga,
    freeze,
    defrost,
    generation_num,
    population

-------

abstract type Entity end

isless(lhs::Entity, rhs::Entity) = lhs.fitness < rhs.fitness

fitness!(ent::Entity, fitness_score) = ent.fitness = fitness_score

-------

struct EntityData
entity
generation::Int

EntityData(entity, generation::Int) = new(entity, generation)
EntityData(entity, model) = new(entity, model.gen_num)

end

-------

struct GAmodel
initial_pop_size::Int
gen_num::Int

population::Array
pop_data::Array{EntityData}
freezer::Array{EntityData}

rng::AbstractRNG

ga

GAmodel() = new(0, 1, Any[], EntityData[], EntityData[], MersenneTwister(time_ns()), nothing)

end

global _g_model

-------

function freeze(model::GAmodel, entity::EntityData)
push!(model.freezer, entity)
println("Freezing: ", entity)
end

function freeze(model::GAmodel, entity)
entitydata = EntityData(entity, model.gen_num)
freeze(model, entitydata)
end

freeze(entity) = freeze(_g_model, entity)

function defrost(model::GAmodel, generation::Int)
filter(model.freezer) do entitydata
entitydata.generation == generation
end
end

defrost(generation::Int) = defrost(_g_model, generation)

generation_num(model::GAmodel = _g_model) = model.gen_num

population(model::GAmodel = _g_model) = model.population

function runga(mdl::Module; initial_pop_size = 128)
model = GAmodel()
model.ga = mdl
model.initial_pop_size = initial_pop_size

runga(model)

end

function runga(model::GAmodel)
reset_model(model)
create_initial_population(model)

while true
    evaluate_population(model)

    grouper = @task model.ga.group_entities(model.population)
    groupings = Any[]
    while !istaskdone(grouper)
        group = consume(grouper)
        group != nothing && push!(groupings, group)
    end

    if length(groupings) < 1
        break
    end

    crossover_population(model, groupings)
    mutate_population(model)
end

model

end

-------

function reset_model(model::GAmodel)
global _g_model = model

model.gen_num = 1
empty!(model.population)
empty!(model.pop_data)
empty!(model.freezer)

end

function create_initial_population(model::GAmodel)
for i = 1:model.initial_pop_size
entity = model.ga.create_entity(i)

    push!(model.population, entity)
    push!(model.pop_data, EntityData(entity, model.gen_num))
end

end

function evaluate_population(model::GAmodel)
scores = pmap(model.ga.fitness, model.population)
for i in 1:length(scores)
fitness!(model.population[i], scores[i])
end

sort!(model.population; rev = true)

end

function crossover_population(model::GAmodel, groupings)
old_pop = model.population

model.population = Any[]
sizehint(model.population, length(old_pop))

model.pop_data = EntityData[]
sizehint(model.pop_data, length(old_pop))

model.gen_num += 1

for group in groupings
    parents = ( old_pop[i] for i in group )
    entity = model.ga.crossover(parents)

    push!(model.population, entity)
    push!(model.pop_data, EntityData(model.ga.crossover(parents), model.gen_num))
end

end

function mutate_population(model::GAmodel)
for entity in model.population
model.ga.mutate(entity)
end
end

end
这是能够通过的代码(1.0),但是没有测试,需要你自己测试下