# Particle swarm optimization excet

**URL:** <https://discourse.itk.org/t/particle-swarm-optimization-excet/1850>\
**Category:** Algorithms\
**Tags:** optimization, registration\
**Created:** [May 8, 2019, 5:27pm UTC](https://discourse.itk.org/t/particle-swarm-optimization-excet/1850 "2019-05-08T17:27:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![c.spanakis83](https://discourse.itk.org/user_avatar/discourse.itk.org/c.spanakis83/32/737_2.png) [@c.spanakis83](https://discourse.itk.org/u/c.spanakis83)\
**Post date:** [May 8, 2019, 5:27pm UTC](https://discourse.itk.org/t/particle-swarm-optimization-excet/1850/1 "2019-05-08T17:27:30Z")

</div>

Hello. I want to use Particle Swarm Optimization (PSO) for Affine image registration. As you see below, I initialized PSO code

```
optimizer->SetInertiaCoefficient(0.8);
optimizer->SetMaximalNumberOfIterations(1000);
optimizer->SetNumberOfParticles(20);
optimizer->SetGlobalCoefficient(1.5);
optimizer->SetInitializeNormalDistribution(true);
optimizer->SetNumberOfGenerationsWithMinimalImprovement(50);
optimizer->SetPercentageParticlesConverged(0.85);

OptimizerType::ParameterBoundsType bounds;
// bounds.size();

for (int i = 0; i < 9; i++) {
    std::pair < double, double > value;
    value.first = -1;
    value.second = 1;
    bounds.push_back(value);
}

for (int i = 0; i < 3; i++) {
    std::pair < double, double > value;
    value.first = -200;
    value.second = 200;
    bounds.push_back(value);
}
OptimizerType::ParametersType init;
init.set_size(12);
itk::Statistics::MersenneTwisterRandomVariateGenerator::Pointer generator =itk::Statistics::MersenneTwisterRandomVariateGenerator::New();

for (int i = 0; i < 9; i++) {
    init(i) = generator->GetUniformVariate(-1, 1);
}

for (int i = 0; i < 3; i++) {
    init(i + 9) = generator->GetUniformVariate(-200, 200);
}
optimizer->SetInitialPosition(init);
optimizer->SetParameterBounds(bounds);
std::cout << optimizer->GetInitialPosition() << std::endl;

std::cout << "Parameter bounds:" << std::endl;
OptimizerType::ParameterBoundsType par = optimizer->GetParameterBounds();
for (int i = 0; i < 12; i++) {
    std::pair<double, double> values = par[i];
    std::cout << values.first << ", " << values.second << std::endl;
}

```

Althoug the initial position is within the bounds, I always get the exception

itk::ExceptionObject (000000F47353EB48)  
Location: “void \_\_cdecl itk::ParticleSwarmOptimizerBase::ValidateSettings(void)”  
File: C:\InsightToolkit-4.13.1\Modules\Numerics\Optimizers\src\itkParticleSwarmOptimizerBase.cxx  
Line: 369  
Description: itk::ERROR: ParticleSwarmOptimizer(000000F475BD3670): initial position is outside specified parameter bounds

How can it be possible?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [May 9, 2019, 2:17pm UTC](https://discourse.itk.org/t/particle-swarm-optimization-excet/1850/2 "2019-05-09T14:17:45Z")

</div>

From the code snippet there is nothing that looks clearly wrong. It would be great if you could provide a minimal working example illustrating the problem so that we can reproduce it. Possibly something like the PSOTest2 function found in [**itkParticleSwarmOptimizerTest.cxx**](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Numerics/Optimizers/test/itkParticleSwarmOptimizerTest.cxx).
