You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
The text was updated successfully, but these errors were encountered:
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.
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).
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) {
.. 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
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:
new data structure:
type Route []*Delivery
The text was updated successfully, but these errors were encountered: