ljvmiranda921 / pyswarms

A research toolkit for particle swarm optimization in Python
https://pyswarms.readthedocs.io/en/latest/
MIT License
1.28k stars 332 forks source link

pbcost default values set to inf #522

Open alipmv opened 1 year ago

alipmv commented 1 year ago

In the custom loop example, the objective function is called twice, which is a problem for computationally expensive objective functions, only because the array of personal best did not have default values for the first iteration. This has been fixed by setting the default values of pbcost to inf.

Description

Motivation and Context

Without a default value for my_swar.pbcost the compute_pbcost function would raise an error in the first iteration, as there is nothing to compare the current cost to. To avoid this error, the objective function was previously called twice in the custom loop example, and not just at the first iteration but at every iteration. This almost doubles runtime, which is a major problem for computationally expensive objective functions. To resolve this issue, the default value of swarm.pbcost has been set to np.inf, to ensure it will be overwritten in the first iteration.

How Has This Been Tested?

One can successfully run all of the example files after the change.

Screenshots (if appropriate):

Types of changes

Checklist:

review-notebook-app[bot] commented 1 year ago

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB