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

Crossover method not modifying genome #45

Open
ableeda opened this issue Jul 23, 2020 · 2 comments
Open

Crossover method not modifying genome #45

ableeda opened this issue Jul 23, 2020 · 2 comments

Comments

@ableeda
Copy link

ableeda commented Jul 23, 2020

Hello @MaxHalford ,

Thanks for making a cool open source project! It's been interesting to learn about genetic algorithms by working with it. I'm also new to golang, so I may be missing something, but I'm having issues getting my Crossover function to work. When I set a pointer receiver (indicated here as the way to modify variables https://tour.golang.org/methods/4 ), I get a build error like below:

func (r *Route) Crossover(s eaopt.Genome, rng *rand.Rand) {

cannot use nearestNeighborRoute (type Route) as type eaopt.Genome in return argument:
	Route does not implement eaopt.Genome (Crossover method has pointer receiver)

.. When I change this to not being a pointer, it builds and executes
func (r Route) Crossover(s eaopt.Genome, rng *rand.Rand) {

at the end of the Crossover method, I'm assigning the new offspring to the r variable, but when I step thru the code from individual.go, the genome is unchanged

// bottom of my Crossover method:
r = offspring1

// in individual.go, indi.Genome is unchanged, it has the original r value, not the new crossover one.
func (indi *Individual) Crossover(mate Individual, rng *rand.Rand) {
	indi.Genome.Crossover(mate.Genome, rng)
	indi.Evaluated = false
	mate.Evaluated = false
}

I saw the note about how slices work better, so I changed my code to use Route instead of RouteState, which is a slice of Deliveries, but still seeing the issue

old data structure:

type RouteState struct {
	Deliveries []*Delivery
}

new data structure:
type Route []*Delivery

@aljungberg
Copy link

I think the documentation which says to use a pointer receiver is correct in spirit -- you do need to modify the actual genome and not a copy of it. But as you point out using a pointer receiver doesn't actually comply with the genome interface.

That said, you write that you want to do r = offspring. That wouldn't have worked even with a pointer receiver. The framework expects you to modify the genome instance in place and there's no way you can tell it to use another instance instead.

Seems to me the easiest thing would be to define your genome as an array/slice of length 1 containing a single Route, Then you can do routeSlice[0] = offspring1 at the bottom of your crossover method.

@KerkDovan
Copy link

The answer is maybe a bit late, but it may help some future generations of this package's users.

The solution here is to define methods with pointer receiver and then use the pointer as the Genome. Yes, the struct will not implement eaopt.Genome, but the pointer will. This is not very obvious to those new to Golang (I discovered it a few months after I started using Golang).
See an example on the playground (the above solution is Foo).

The alternate way is to define the struct's fields as the pointers (in the above example it's Bar). But in this case, you must ensure that those fields are non-nil or handled properly (read-only because you can't allocate a new pointer in the method without the pointer receiver).

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

3 participants