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

Particle swarm sampler #17

Merged
merged 11 commits into from
Oct 20, 2022
Merged

Particle swarm sampler #17

merged 11 commits into from
Oct 20, 2022

Conversation

marcofavoritobi
Copy link
Contributor

Proposed changes

This PR adds an implementation of the classical particle swarm sampler.

References:

  • Shi, Yuhui, and Russell Eberhart. "A modified particle swarm optimizer."
    1998 IEEE international conference on evolutionary computation proceedings.
  • Eberhart, Russell, and James Kennedy. "A new optimizer using particle swarm theory."
    MHS'95. Proceedings of the sixth international symposium on micro machine and human science.

Fixes

If it fixes a bug or resolves a feature request, be sure to link to that issue.

Types of changes

What types of changes does your code introduce?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING doc
  • I am making a pull request against the develop branch (left side). Also you should start your branch off our develop.
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

@marcofavoritobi marcofavoritobi self-assigned this Sep 28, 2022
@marcofavoritobi marcofavoritobi added the enhancement New feature or request label Sep 28, 2022
@marcofavoritobi marcofavoritobi force-pushed the particle-swarm-sampler branch 2 times, most recently from 69494d3 to 8e7992a Compare September 28, 2022 08:40
Implementation of the classical particle swarm sampler.

References:

  - Shi, Yuhui, and Russell Eberhart. "A modified particle swarm optimizer."
    1998 IEEE international conference on evolutionary computation proceedings.
  - Eberhart, Russell, and James Kennedy. "A new optimizer using particle swarm theory."
    MHS'95. Proceedings of the sixth international symposium on micro machine and human science.
@AldoGl AldoGl force-pushed the particle-swarm-sampler branch from 8e7992a to d5cec59 Compare October 19, 2022 09:55
@AldoGl AldoGl self-requested a review October 19, 2022 09:55
@codecov-commenter
Copy link

codecov-commenter commented Oct 19, 2022

Codecov Report

Merging #17 (bb7e80e) into main (3433555) will decrease coverage by 0.81%.
The diff coverage is 89.41%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #17      +/-   ##
==========================================
- Coverage   99.44%   98.63%   -0.82%     
==========================================
  Files          22       23       +1     
  Lines        1086     1171      +85     
==========================================
+ Hits         1080     1155      +75     
- Misses          6       16      +10     
Impacted Files Coverage Δ
black_it/samplers/particle_swarm.py 89.41% <89.41%> (ø)
black_it/samplers/base.py 98.03% <0.00%> (-1.97%) ⬇️

Copy link
Contributor

@AldoGl AldoGl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work @marcofavoritobi, thanks!

I have added this sampler to the "overview" notebook in the example folder and I have also added an essential test.

I would have only some these small observations:

  • Is it the case that the PS sampler simply cannot run in combination with other samplers? If so, we could add some kind of check in the code to guarantee that this never happens.

  • I would remove the possibility of printing position and velocities of the particles as this seem too low level information. What do you think? I am open to change my mind here.

  • I would move your notebook "particle_swarm_sampler" to the examples folder and create a symbolic link to the docs folder, as done for the other notebooks.

- r1 and r2 are two random numbers uniformly distributed in the range of (0, 1).

This sampling method has to be used alone in Black-it Calibrator, because existing_losses and existing_points
arguments of sample_batch are assumed to be computed by this sampler.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if one uses the PS in conjunction with other methods?

Copy link
Contributor Author

@marcofavoritobi marcofavoritobi Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact, the docstring is not quite correct (my bad). _previous_batch_index_start will necessarily point to the index of existing_points/existing_losses at the moment of the previous call to sample_batch. Any other existing batch is ignored. Therefore, atm, the PS sampler works as if it were alone.

We could switch the behaviour by considering the global loss as the true global loss of the calibration task, and not just the global loss within the PS sampler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of like this idea actually... It would allow different samplers to benefit from each other, and this is one of the nice features of Black-it I think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think we should leave this as an option

)
self._previous_batch_index_start = len(existing_points)

if self._verbose:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove these print statements for the moment as they seem useful only for debugging. We could maintain them if we had several level of verbosity (0, 1, 2, 3, ...) but I having only 0 and 1 printing positions and velocities of the particles seems to much

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, let's remove the prints and the verbose option

@AldoGl AldoGl force-pushed the particle-swarm-sampler branch from f2ca921 to bb7e80e Compare October 19, 2022 14:39
@marcofavoritobi
Copy link
Contributor Author

I would move your notebook "particle_swarm_sampler" to the examples folder and create a symbolic link to the docs folder, as done for the other notebooks.

Sounds good :)

@AldoGl AldoGl force-pushed the particle-swarm-sampler branch from bb7e80e to 9808aab Compare October 19, 2022 14:52
AldoGl and others added 5 commits October 19, 2022 17:05
…pler

This commit adds a new boolean option to the constructor of ParticleSwarmSampler,
namely global_minimum_across_samplers. This option allows to switch between
two types of behaviours of the particle swarm sampler:

If `global_minimum_across_samplers` is False, then the velocity update rule, for the global
minimum part, uses the current position of the particle that in its history
has sampled, so far, the best set of parameters.

Else, if `global_minimum_across_samplers` is True, then the velocity update rule
returns the point in parameter space that achieved the minimum loss.
Note that this point could have been sampled by a different sampler than "self".
@AldoGl
Copy link
Contributor

AldoGl commented Oct 20, 2022

Thanks @marcofavoritobi for the new "global minimum" feature 👍🏼. I updated the docstrings and the notebooks, I think we can merge now provided that tests pass

@marcofavoritobi marcofavoritobi merged commit 9dc0087 into main Oct 20, 2022
@marcofavoritobi marcofavoritobi deleted the particle-swarm-sampler branch October 20, 2022 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants