Skip to content

Crossover method not modifying genome #45

Description

@ableeda

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions