本文整理汇总了C++中Velocity::push_back方法的典型用法代码示例。如果您正苦于以下问题:C++ Velocity::push_back方法的具体用法?C++ Velocity::push_back怎么用?C++ Velocity::push_back使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Velocity
的用法示例。
在下文中一共展示了Velocity::push_back方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getRandomVelocity
Velocity Solver::getRandomVelocity()
{
Velocity velocity;
for (int i = 0; i < currentProblem->n; ++i) {
double randomVariable = getRandomDoubleValue(0.0, 1.0);
velocity.push_back(randomVariable);
}
return velocity;
}
示例2: findSolution
void Solver::findSolution()
{
for (auto &i : swarm.getParticles()) {
Velocity newVelocity;
Solution newPosition;
/**
* Iterate through all dimensions of a Solution/position
* to calculate the new velocity and update the position.
*
* j = dimension
*/
for (int j = 0; j < currentProblem->n; ++j) {
int currentPositionD = i.getPosition().at(j);
double currentVelocityD = i.getVelocity().at(j);
int pBestD = i.getBestPosition().at(j);
int gBestD = swarm.getBestPosition().at(j);
int randomParticleNumber = getRandomIntegerValue(0, 1);
int randomGlobalNumber = getRandomIntegerValue(0, 1);
double newVelocityD = parameters.getInertiaWeight() * currentVelocityD +
parameters.getConstant1() * randomParticleNumber * (pBestD - currentPositionD) +
parameters.getConstant2() * randomGlobalNumber * (gBestD - currentPositionD);
if (newVelocityD > parameters.getVMax())
newVelocityD = parameters.getVMax();
else if (newVelocityD < -parameters.getVMax())
newVelocityD = -parameters.getVMax();
int newPositionD = updateStrategy->updatePosition(currentPositionD, newVelocityD);
newVelocity.push_back(newVelocityD);
newPosition.push_back(newPositionD);
}
i.setVelocity(newVelocity);
i.setPosition(newPosition);
int pBestTmp = calculateProfit(i.getPosition());
pBestTmp -= calculatePenalty(i.getPosition(), pBestTmp);
// Update pBest and gBest position/solution
if (pBestTmp > i.getBestValue()) {
i.setBestPositionAndValue(i.getPosition(), pBestTmp);
if (pBestTmp > swarm.getBestValue())
swarm.setBestPositionAndValue(i.getPosition(), pBestTmp);
}
}
}