MaxHalford / eaopt

:four_leaf_clover: Evolutionary optimization library for Go (genetic algorithm, partical swarm optimization, differential evolution)
https://pkg.go.dev/github.com/MaxHalford/eaopt
MIT License
886 stars 96 forks source link

Pass aditional data in Evaluate function #42

Closed franzon closed 5 years ago

franzon commented 5 years ago

Hi

I need to access some data (which is not contained in Genome) to compute fitness. I looked in the documentation but didn't find a way to pass additional data to the Evaluate function. One option would be using global variables, but i think that is not the best way.

Do you have any ideas?

MaxHalford commented 5 years ago

Hey, what kind of data do you need to access? Can you describe a bit more specifically what you're trying to do?

franzon commented 5 years ago

I'm trying to find the best schedulling of matches of a tournament based on some constraints. I have a list of slots, a list of players (and which slots they can play) and a list of matches (consisting of two players). I'm using the total number of times that the genome breaks the contraints as fitness value, and this depends on the highlighted data.

I got very good results using eaopt! But now i want to move this to a http server, so i need some way to pass these data to the Evaluate function.

MaxHalford commented 5 years ago

I'm not sure I understand everything because I'm tired :). Typically I would proceed by adding properties to your genome. That is, the genome would have 3 properties which are your lists (in Go they're called slices).

franzon commented 5 years ago

Oh sorry, didn't know it would be too simple. Thanks for help!

franzon commented 5 years ago

Sorry for reopening, i just have one more question: i also need access to this data in GenomeFactory function, but it also doesn't accept additional parameters.

MaxHalford commented 5 years ago

No worries mate! Well a solution would to create a struct with the parameters of your experiment. You could then assign a method to this struct which would create random Genomes. Do you see what I mean?

franzon commented 5 years ago

It worked! The only bad point is that the data is repeated in every Genome. But thanks for the help!

MaxHalford commented 5 years ago

Normally that should not be an issue because each genome has a slice, which is a pointer to a data array. That is, the data is not duplicated. If you change one of the lists it will be modified across each genome.

franzon commented 5 years ago

Thanks for clarify! I'm learning Go as i do this project.

MaxHalford commented 5 years ago

No worries! Feel free to open other issues. I'll close this one for now, but you can keep commenting and I'll answer.