# particle swarm optimizer

**URL:** <https://discourse.itk.org/t/particle-swarm-optimizer/3504>\
**Category:** Algorithms\
**Created:** [October 2, 2020, 11:06am UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504 "2020-10-02T11:06:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)\
**Post date:** [October 2, 2020, 11:06am UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/1 "2020-10-02T11:06:02Z")

</div>

Hello everyone,  
I was wondering if itk developers provided a registration example between two 3d images using particle swarm optimizer ? Thank you

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [October 2, 2020, 2:26pm UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/2 "2020-10-02T14:26:11Z")

</div>

I don’t think there is such an example, but you can be inspired by particle swarm’s [unit test](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.1.1/Modules/Numerics/Optimizers/test/itkParticleSwarmOptimizerTest.cxx) and a registration example of your choice (perhaps [this one](https://github.com/InsightSoftwareConsortium/ITKExamples/blob/master/src/Registration/Common/GlobalRegistrationOfTwoImages/Code.cxx)).

---

<div class="post-metadata">

**Author:** ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)\
**Post date:** [October 5, 2020, 7:00am UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/3 "2020-10-05T07:00:37Z")

</div>

ok, thank you @dzenanz

---

<div class="post-metadata">

**Author:** ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)\
**Post date:** [October 5, 2020, 11:55am UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/4 "2020-10-05T11:55:07Z")

</div>

I tried your solution @dzenanz and when I execute my program it stops just before finishing the registration process with this error message: `Description: itk::ERROR: ParticleSwarmOptimizer(0x5578e8497ac0): cost function and parameter bounds dimensions mismatch `  
My question is: how can I declare a cost function?

---

<div class="post-metadata">

**Author:** ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)\
**Post date:** [October 5, 2020, 2:56pm UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/5 "2020-10-05T14:56:39Z")

</div>

okay, I managed to understand where the error came from. Just another question: what is `OptimizerType::ParameterBoundsType bounds;`?  
If I use 3d euler coordinates system, do I have to initialize `bounds` like this: ![itk_pso_bounds](https://discourse.itk.org/uploads/default/original/2X/b/b88eb133516edd07a3a3b31c9ec3ab7248ec370a.png) ?  
Where the first three pushbacks bounds euler angles and the three last pushbacks bounds the translation among x, y, and z?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [October 5, 2020, 4:32pm UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/6 "2020-10-05T16:32:33Z")

</div>

> [@pierrectl](#):
>
> what is `OptimizerType::ParameterBoundsType bounds;` ?

Use your IDE to explore the exact definition of this type.

You initialization with 6 pairs of values is good in principle, but I don’t know whether it compiles.

---

<div class="post-metadata">

**Author:** ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)\
**Post date:** [October 6, 2020, 6:29am UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/7 "2020-10-06T06:29:31Z")

</div>

again and again, thank you @dzenanz, my program works fine now

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [October 6, 2020, 12:06pm UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/8 "2020-10-06T12:06:16Z")

</div>

Maybe explain what was the exact problem and solution, in case somebody comes across this thread in the future?

---

<div class="post-metadata">

**Author:** ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)\
**Post date:** [November 26, 2020, 12:42pm UTC](https://discourse.itk.org/t/particle-swarm-optimizer/3504/9 "2020-11-26T12:42:36Z")

</div>

excuse me for the late reply. My problem was in fact very basic: I had a segfault when launching my registration program which uses a particle swarm optimizer. I just had to declare `ParameterBoundsType` and `push_back()` some `double` values in it.  
The first three `push_back()` concerns Euler rotation bounds, the three last translation bounds.  
Here is a sample of code which compiles and runs:

```
const uint16_t MAXIMUM_NUMBER_OF_ITERATIONS = 100;
const uint16_t RANDOM_SEED = 50;
const bool SEED = true;
const double GLOBAL_COEFFICIENT = 1.1;
const double INERTIA_COEFFICIENT = 0.6;
const double PERSONAL_COEFFICIENT = 1.1;
const double NUMBER_OF_GENERATION_WITH_MINIMAL_IMPROVMENT = 10;
const double NUMBER_OF_PARTICLES = 100;
const double PERCENTAGE_PARTICLES_CONVERGED = 15;
const double X_TOLERANCE = 0.01;
const double F_TOLERENCE = 0.01;
const bool INITIALIZE_WITH_NORMAL_DISTRIBUTION = true;
const double EULER_ANGLES_BOUNDS = 0.1; // in degrees
const double XYZ_TRANSLATION_BOUNDS = 0.1; // in mm
const double ROTATION_TRANSLATION_OPTIMIZER_VALUE = 1; // divids rotation and translation parametric space by this value

optimizer->SetGlobalCoefficient( GLOBAL_COEFFICIENT );
optimizer->SetInertiaCoefficient( INERTIA_COEFFICIENT );
optimizer->SetPersonalCoefficient( PERSONAL_COEFFICIENT );
optimizer->SetMaximalNumberOfIterations( MAXIMUM_NUMBER_OF_ITERATIONS );
optimizer->SetNumberOfGenerationsWithMinimalImprovement( NUMBER_OF_GENERATION_WITH_MINIMAL_IMPROVMENT );
optimizer->SetNumberOfParticles( NUMBER_OF_PARTICLES );
optimizer->SetPercentageParticlesConverged( PERCENTAGE_PARTICLES_CONVERGED );
optimizer->SetSeed( RANDOM_SEED );
optimizer->SetUseSeed( SEED );
optimizer->SetParametersConvergenceTolerance( X_TOLERANCE, NUMBER_OF_PARAMETERS );
optimizer->SetFunctionConvergenceTolerance( F_TOLERENCE );
optimizer->SetInitializeNormalDistribution( INITIALIZE_WITH_NORMAL_DISTRIBUTION );

ParametersType alignment_parameters = transform->GetParameters();
alignment_parameters.Fill( 0 );
optimizer->SetInitialPosition( alignment_parameters );

OptimizerType::ParameterBoundsType bounds;
bounds.push_back( std::make_pair( - EULER_ANGLES_BOUNDS, EULER_ANGLES_BOUNDS ) );
bounds.push_back( std::make_pair( - EULER_ANGLES_BOUNDS, EULER_ANGLES_BOUNDS ) );
bounds.push_back( std::make_pair( - EULER_ANGLES_BOUNDS, EULER_ANGLES_BOUNDS ) );
bounds.push_back( std::make_pair( - XYZ_TRANSLATION_BOUNDS, XYZ_TRANSLATION_BOUNDS ) );
bounds.push_back( std::make_pair( - XYZ_TRANSLATION_BOUNDS, XYZ_TRANSLATION_BOUNDS ) );
bounds.push_back( std::make_pair( - XYZ_TRANSLATION_BOUNDS, XYZ_TRANSLATION_BOUNDS ) );
optimizer->SetParameterBounds( bounds );
```
