#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 "< std::cout<<"\nNumber iterations = "< std::cout<<"Number particles in swarm = "< std::vector dimension bestGlobalPosition; // best solution found by any particle in the swarm. implicit initialization to all 0.0 double bestGlobalFitness = DBL_MAX; // smaller values better double minV = -1.0 * maxX; double maxV = maxX; size_t i,j; std::cout<<"\nInitializing swarm with random positions/solutions"< 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
C: there are no methods in C. C++: no.
c is procedure oriented and c++ is object oriented & much newer.
If a + b + c + d + 80 + 90 = 100, then a + b + c + d = -70.
C++ is related to C, the language from which it is derived.
println is not a C++ keyword.
a. an alpha particle
5 deg c
b+b+b+c+c+c+c =3b+4c
c + c + 2c + c + c = 6c
b + b + b + c + c + c + c = 3b + 4c
4c
c + c + c + c + c = 5 * c.
There are no "primary and secondary keys" in c and c plus plus.
3c
Yes
There is no such thing as 'unix C++'.
True! C: