Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for binary chromosomes? #4

Closed
datawookie opened this issue Oct 3, 2015 · 3 comments
Closed

Support for binary chromosomes? #4

datawookie opened this issue Oct 3, 2015 · 3 comments

Comments

@datawookie
Copy link

Hi,

I have been exploring Julia's GA capabilities, looking at GeneticAlgorithms and Evolutionary packages. I really enjoy the simple interface presented by Evolutionary and it also seems to be more advanced in terms of development. Great work and thank you for your efforts.

However, I am having trouble figuring out how I can implement a binary GA. I am using a knapsack problem as a simple illustrative example. I've written a fitness function which looks like this:

mass    = [1, 5, 3, 7, 2, 10, 5]
utility = [1, 3, 5, 2, 5,  8, 3]

function fitness(n::Vector{Int})
    total_mass = sum(mass .* n)
    return (total_mass <= 20) ? sum(utility .* n) : 0
end

but when I try to run the GA as follows

ga(fitness, 7)

I get

ERROR: wrong number of arguments
 in anonymous at /home/colliera/.julia/v0.3/Evolutionary/src/ga.jl:23
 in ga at /home/colliera/.julia/v0.3/Evolutionary/src/ga.jl:66

Looking at the debug output from ga() I see that the vectors being passed into my fitness function are floating point rather than binary (or at least a vector of integer 0 or 1). I'm not sure whether this is the source of the ERROR above, but it's certainly something that I need to resolve in order to get my problem up and running.

Is there support for binary chromosomes? Have I missed something?

Best regards,
Andrew.

@wildart
Copy link
Owner

wildart commented Oct 3, 2015

It is just default selection and crossover functions are incorrect. Try some other functions. BTW, binary chromosomes supported as Vector{Bool}. I'll write some examples. Thanks for bringing this issue.

@wildart
Copy link
Owner

wildart commented Oct 3, 2015

If you call your example as follows:

julia> ga(fitness, 7, selection=((x,n)->1:n))
([0.984655,0.684742,0.49106,0.204153,0.26621,0.660357,0.42944],13.804710257390415,700,0.0)

If you want binary chromosomes use Vector{Bool} to initialize the population (BitVector isn't supported}:

julia> ga(fitness, 7, selection=((x,n)->1:n), initPopulation = collect(randbool(7)))
(Bool[false,true,false,true,true,false,false],10,700,0)

@datawookie
Copy link
Author

Thank you, that sorted out the problem perfectly!

@wildart wildart closed this as completed Oct 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants