thieu1995 / mealpy

A Collection Of The State-of-the-art Metaheuristic Algorithms In Python (Metaheuristic/Optimizer/Nature-inspired/Biology)
https://mealpy.readthedocs.io
GNU General Public License v3.0
871 stars 177 forks source link

[FEAT]: #124

Closed theshyyi closed 9 months ago

theshyyi commented 10 months ago

Description

Hello,Very useful library,While working on my algorithms using CotiaA, PSO etc. noticed an issue.

Every run of mine using the same algorithm gets the same results for the same CEC test function.

But what I've learned is that different runs will come up with different results because of the random function, so this has been confusing me. I hope you can answer my doubts. Thank you. image

Additional Information

No response

thieu1995 commented 9 months ago

Hi @theshyyi,

I think you are setting the seed value in model.solve function. So that is why the results are always the same. You should use different seed value for each trial, may be use trial index as seed value. Then each trial will generate a different result.

for trial in range(0, 50):
     model = BaseGA(...)
     model.solve(seed=trial)

In old version of numpy and mealpy. We don't have seed value, but we can still make the code is reproducible by setting a global seed value at the top of the script file like this:

import numpy as np
np.random.seed(10)

for trial in range(0, 50):
    model =....

Then it will generate different results for each trial. When others run your script on their machines, it will still produce the same values, that is the concept of 'reproducibility. You can still try to set the global seed like that, but I am not sure it will work or not.

import numpy as np
np.random.seed(10)

for trial in range(0, 50):
    model = BaseGA(...)
    model.solve(seed=None)
theshyyi commented 9 months ago

Thank you for answering this query for me, I think I have understood why.

Thanks again, your response has helped me a great deal!

thieu1995 commented 9 months ago

That is good to hear. I will close this issue now. Feel free to re-open if need be.