answersLogoWhite

0


Best Answer

#include

#include

#include

struct dimension

{

static const size_t size;

double dimension_0;

double dimension_1;

double& operator[](size_t dim){ return( dim?dimension_1:dimension_0 ); }

dimension():

dimension_0(0.0),

dimension_1(0.0)

{}

};

const size_t dimension::size=2;

struct particle

{

dimension m_position;

double m_fitness;

dimension m_velocity;

dimension m_best_position;

double m_best_fitness;

particle(

dimension position,

double fitness,

dimension velocity,

dimension best_position,

double best_fitness):

m_position(position),

m_fitness(fitness),

m_velocity(velocity),

m_best_position(best_position),

m_best_fitness(best_fitness)

{}

};

static double objective_function(dimension x)

{

return( 3.0 + (x[0] * x[0]) + (x[1] * x[1]) );

}

int main()

{

srand((unsigned)time(NULL));

std::cout<<"\nBegin Particle Swarm Optimization demonstration\n"<

std::cout<<"\nObjective function to minimize has dimension = 2"<

std::cout<<"Objective function is f(x) = 3 + (x0^2 + x1^2)"<

const size_t numberParticles = 10;

size_t numberIterations = 1000;

size_t iteration = 0;

double minX = -100.0;

double maxX = 100.0;

std::cout<<"Range for all x values is "<

for(i=0; i

{

dimension randomPosition;

dimension randomVelocity;

for(j=0; j

{

double lo = minX;

double hi = maxX;

randomPosition[j]=(hi - lo) * rand() + lo;

lo = -1.0 * std::abs(maxX - minX);

hi = std::abs(maxX - minX);

randomVelocity[j]=(hi - lo) * rand() + lo;

}

//double fitness = SphereFunction(randomPosition); // smaller values are better

//double fitness = GP(randomPosition); // smaller values are better

double fitness = objective_function(randomPosition);

swarm.push_back( new particle(randomPosition, fitness, randomVelocity, randomPosition, fitness));

// does current Particle have global best position/solution?

if( swarm[i]->m_fitness < bestGlobalFitness)

{

bestGlobalFitness = swarm[i]->m_fitness;

bestGlobalPosition = swarm[i]->m_position;

}

}

std::cout<<"\nInitialization complete"<

std::cout.precision(4);

std::cout<<"Initial best fitness = "<

std::cout<<"Best initial position/solution:"<

for(i=0; i

std::cout<<"x"<

double w = 0.729; // inertia weight. see http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=00870279

double c1 = 1.49445; // cognitive/local weight

double c2 = 1.49445; // social/global weight

double r1, r2; // cognitive and social randomizations

std::cout<<"\nEntering main PSO processing loop"<

while(iteration++

{

dimension newVelocity;

dimension newPosition;

double newFitness;

for(i=0; i

{

particle& currP(*swarm[i]);

for(j=0; j

{

r1 = rand();

r2 = rand();

newVelocity[j] = (w * currP.m_velocity[j]) +

(c1 * r1 * (currP.m_best_position[j] - currP.m_position[j])) +

(c2 * r2 * (bestGlobalPosition[j] - currP.m_position[j]));

if (newVelocity[j] < minV)

newVelocity[j] = minV;

else if (newVelocity[j] > maxV)

newVelocity[j] = maxV;

}

currP.m_velocity=newVelocity;

for(j=0; j

{

newPosition[j] = currP.m_position[j] + newVelocity[j];

if (newPosition[j] < minX)

newPosition[j] = minX;

else if (newPosition[j] > maxX)

newPosition[j] = maxX;

}

currP.m_position = newPosition;

newFitness = objective_function(newPosition);

currP.m_fitness = newFitness;

if(newFitness < currP.m_best_fitness)

{

currP.m_best_position = newPosition;

currP.m_best_fitness = newFitness;

}

if (newFitness < bestGlobalFitness)

{

bestGlobalPosition = newPosition;

bestGlobalFitness = newFitness;

}

}

}

std::cout<<"\nProcessing complete"<

std::cout.precision(4);

std::cout<<"Final best fitness = "<

std::cout<<"Best position/solution:"<

for(i=0; i

{

std::cout<<"x"<

std::cout<

}

std::cout<

std::cout<<"\nEnd PSO demonstration\n"<

}

Output

Begin Particle Swarm Optimization demonstration

Objective function to minimize has dimension = 2

Objective function is f(x) = 3 + (x0^2 + x1^2)

Range for all x values is -100 <= x <= 100

Number iterations = 1000

Number particles in swarm = 10

Initializing swarm with random positions/solutions

Initialization complete

Initial best fitness = 3917271700003.0000

Best initial position/solution:

x0 = 1423700.0000

x1 = 1374900.0000

Entering main PSO processing loop

Processing complete

Final best fitness = 3.0000

Best position/solution:

x0 = 0.0000

x1 = 0.0000

End PSO demonstration

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you do Particle Swarm Optimization in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you declare a method within a method in c or c plus plus?

C: there are no methods in C. C++: no.


What is the different of c and c plus plus?

c is procedure oriented and c++ is object oriented &amp; much newer.


How A plus B plus C plus D plus 80 plus 90 equal to 100 what is the value of A B C and D?

If a + b + c + d + 80 + 90 = 100, then a + b + c + d = -70.


In computer language C plus plus is related to?

C++ is related to C, the language from which it is derived.


What is meant by println in c plus plus?

println is not a C++ keyword.