在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:WestleyArgentum/GeneticAlgorithms.jl开源软件地址:https://github.com/WestleyArgentum/GeneticAlgorithms.jl开源编程语言:Julia 100.0%开源软件介绍:GeneticAlgorithms.jlThis is a lightweight framework that simplifies the process of creating genetic algorithms and running them in parallel.Basic UsageWhat's your problem???Let's say you've got a simple equality Create a ModuleStart by creating a file and a module for your ga. Your module will be loaded into the framework and things inside it will be used to run your algroithm. module equalityga
# implement the GA interface inside here
end Define an EntityYour entity should inherit from the abstract type EqualityMonster <: Entity
abcde::Array
fitness
EqualityMonster() = new(Array(Int, 5), nothing)
EqualityMonster(abcde) = new(abcde, nothing)
end
function create_entity(num)
# for simplicity sake, let's limit the values for abcde to be integers in [-42, 42]
EqualityMonster(rand(Int, 5) % 43)
end Note that Create a Fitness FunctionThe framework will expect a function fitness(ent)
# we want the expression `a + 2b + 3c + 4d + 5e - 42`
# to be as close to 0 as possible
score = ent.abcde[1] +
2 * ent.abcde[2] +
3 * ent.abcde[3] +
4 * ent.abcde[4] +
5 * ent.abcde[5]
abs(score - 42)
end Note that function isless(lhs::EqualityMonster, rhs::EqualityMonster)
abs(lhs.fitness) > abs(rhs.fitness)
end Group Entities
function group_entities(pop)
if pop[1].fitness == 0
return
end
# simple naive groupings that pair the best entitiy with every other
for i in 1:length(pop)
produce([1, i])
end
end Define Crossover
function crossover(group)
child = EqualityMonster()
# grab each element from a random parent
num_parents = length(group)
for i in 1:length(group[1].abcde)
parent = (rand(Uint) % num_parents) + 1
child.abcde[i] = group[parent].abcde[i]
end
child
end Define Mutation
function mutate(ent)
# let's go crazy and mutate 20% of the time
rand(Float64) < 0.8 && return
rand_element = rand(Uint) % 5 + 1
ent.abcde[rand_element] = rand(Int) % 43
end Run your GA!using GeneticAlgorithms
require("GeneticAlgorithms/test/equalityga.jl")
model = runga(equalityga; initial_pop_size = 16)
population(model) # the the latest population when the GA exited |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论