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

HallOfFame does not store the best when np.nan involved #440

Open
zhengle2008 opened this issue Dec 3, 2019 · 5 comments · May be fixed by #445 or #738
Open

HallOfFame does not store the best when np.nan involved #440

zhengle2008 opened this issue Dec 3, 2019 · 5 comments · May be fixed by #445 or #738

Comments

@zhengle2008
Copy link

my evaluate function may return nan occasionally. I found it's strange that the halloffame didn't store the best individal. So I tracked down, and found the problem is in Fitness'

    def __gt__(self, other):
        return not self.__le__(other)

    def __ge__(self, other):
        return not self.__lt__(other)

It returns True where np.nan involved. So is it properly to change the code to:

    def __gt__(self, other):
         return self.wvalues > other.wvalues

    def __ge__(self, other):
        return self.wvalues >= other.wvalues

Or there is other considerations?

@zhengle2008 zhengle2008 changed the title HallOfFame is not working well on np.nan HallOfFame does not store the best when np.nan involved Dec 3, 2019
@yxliang01
Copy link
Contributor

I feel having np.nan as the fitness value makes the fitness invalid in the first place... Therefore, it doesn't make sense to sort them either?

@zhengle2008
Copy link
Author

I feel having np.nan as the fitness value makes the fitness invalid in the first place... Therefore, it doesn't make sense to sort them either?

In my case, some individual just didn't work out, and return nan. I am not sure if there is a more proper way to prune out these individuals. Now I just choose to neglect these nan value as long as the best one is fine.

@yxliang01
Copy link
Contributor

yxliang01 commented Dec 3, 2019

Since you didn't describe your concrete application, so it's hard to say what else better can be done. But, if you really want to do individual ranking, you are supposed to convert np.nan to numbers e.g. np.inf or -np.inf.

@zhengle2008
Copy link
Author

zhengle2008 commented Dec 3, 2019

For temporary using, I change the code in HallOfFame::update() to prevent the nan from ruining my best results:

if ind.fitness > self[-1].fitness or len(self) < self.maxsize:
...

To

if (ind.fitness.values[0]==ind.fitness.values[0] and ind.fitness > self[-1].fitness) or len(self) < self.maxsize:
...

-np.inf is also a way to go, but the statistics may seem strange.
Thank you very much for suggestions! I close this now.

@fmder
Copy link
Member

fmder commented Dec 11, 2019

I would leave that open, as it would be interesting to handle properly nans as invalid.

@fmder fmder reopened this Dec 11, 2019
wukan1986 added a commit to wukan1986/deap that referenced this issue Jan 4, 2024
@wukan1986 wukan1986 linked a pull request Jan 4, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants