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

How to use f32 / f64 as fitness scores? #18

Open
wbrickner opened this issue May 16, 2021 · 2 comments
Open

How to use f32 / f64 as fitness scores? #18

wbrickner opened this issue May 16, 2021 · 2 comments

Comments

@wbrickner
Copy link

Hello,

I am currently stuck using a janky float discretization technique because I can't use floats directly.

Is there a better workaround? Is there some truly compelling reason we cannot implement the Fitness trait for floats internally like is done for the signed and unsigned integers?

Thank you.

@haraldmaida
Copy link
Member

The evaluation functions must order the fitness score. The Ord trait is not implemented for floats. That's the reason why the Fitness trait can not be implemented for floats. Maybe someone has an idea how to implement the comparison of Fitness values using the PartialOrd trait only. Specifically what to do when partial_cmp returns None.

@ravicious
Copy link

ravicious commented Nov 10, 2021

I enabled the total_cmp nightly feature and then wrote my own wrapper over f64 that implements Ord using total_cmp.

Here's how it looks like: https://github.com/ravicious/collage/blob/808fb238da24415b72371bded32d49994f2b611a/image-processor/src/algorithm.rs#L22-L64

Unfortunately it's not something one can copy and paste, as I also had to flip the order. By default genevo treats higher fitness as better, but the cost function of the algorithm I was implementing treats lower fitness as better with zero being the best possible fitness. But you should be able to just remove the match on self.0.total_cmp(&other.0) (line 47).

If you're sure that your cost function never returns NAN, I think you should be able to YOLO it and create a wrapper implementing Ord which panics if partial_cmp returns None. This happens only when you attempt to compare NAN, but I might be wrong on this. total_cmp on the other hand orders NAN before/after infinity values.

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