answersLogoWhite

0

#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

12y ago

What else can I help you with?

Trending Questions
What device is used to remove poisonous gases from industrial emissions before they are released into the atmosphere? How do you overload function call and Array sub-scripting operators? How does the doping concentration affect in semiconductor? Polyurethane Foam Technology? 6 equal resistors of 1 ohm each are connected to form the sides of a hexagon ABCDEF calculate the resistance offered by the combination if the current enters at A and leaves at D? What do you call the links between the pages or other websites in HTML? How can you script your ASP code in JavaScript? A new communication protocol used with gsm and tdma is called which sends voice text or video data in packets similar to voip? WHAT is the rating of earth leakage circuit breaker used for welding machines? What is grain boundary scattering? What is the purpose of the diode in the GM AC clutch circuit? Is junior engineer equivalent to jco in military engineer services? How thick is 13 gauge? Which of these elements is NOT common to all programming languages. A. set of operators B. structured classes C. supported data types or D. rules of syntax? What is resistance What is an electron Can electrons flow through all materials What materials are good for an electron to go through Is resistance inside of an electron? What are the verb noun adjective adverb forms of inventor? What industries use fluids? What things have been invented by George De Mestral? How did the manilamen showed reason and logic? How can energy be used more efficiently?