Sprite::Sprite(const std::string& name, const Frame* const frame) : Drawable(name, Vector2f(Gamedata::getInstance()->getXmlInt(name+"/startLoc/x"), Gamedata::getInstance()->getXmlInt(name+"/startLoc/y")), Vector2f(Gamedata::getInstance()->getXmlInt(name+"/speedX")+getRandomFactor(), Gamedata::getInstance()->getXmlInt(name+"/speedY")+getRandomFactor()) ), frame( frame ), frameWidth(frame->getWidth()), frameHeight(frame->getHeight()), worldWidth(Gamedata::getInstance()->getXmlInt("world/width")), worldHeight(Gamedata::getInstance()->getXmlInt("world/height")) { }
void FGNelderMead::constructSimplex(const std::vector<double> & guess, const std::vector<double> & stepSize) { for (int vertex=0;vertex<m_nVert;vertex++) { m_simplex[vertex] = guess; } for (int dim=0;dim<m_nDim;dim++) { int vertex = dim + 1; m_simplex[vertex][dim] += stepSize[dim]*getRandomFactor(); boundVertex(m_simplex[vertex],m_lowerBound,m_upperBound); } if (showSimplex) { std::cout << "simplex: " << std::endl;; for (int j=0;j<m_nVert;j++) std::cout << "\t\t" << j; std::cout << std::endl; for (int i=0;i<m_nDim;i++) { for (int j=0;j<m_nVert;j++) std::cout << "\t" << std::setw(10) << m_simplex[j][i]; std::cout << std::endl; } } }
void FGNelderMead::contract() { for (int dim=0;dim<m_nDim;dim++) { for (int vertex=0;vertex<m_nVert;vertex++) { m_simplex[vertex][dim] = getRandomFactor()*0.5*(m_simplex[vertex][dim] + m_simplex[m_iMin][dim]); } } }
double FGNelderMead::tryStretch(double factor) { // randomize factor so we can avoid locking situations factor = factor*getRandomFactor(); // create trial vertex double a= (1.0-factor)/m_nDim; double b = a - factor; std::vector<double> tryVertex(m_nDim); for (int dim=0;dim<m_nDim;dim++) { tryVertex[dim] = m_elemSum[dim]*a - m_simplex[m_iMax][dim]*b; boundVertex(tryVertex,m_lowerBound,m_upperBound); } // find trial cost double costTry0 = 0, costTry = 0; try { costTry0 = m_f.eval(tryVertex); costTry = m_f.eval(tryVertex); } catch(const std::exception & e) { throw; return 0; } if (std::abs(costTry0-costTry) > std::numeric_limits<float>::epsilon()) { //std::cout << "\twarning: dynamics not stable!" << std::endl; //return 1000000*m_cost[m_iMax]; } // if trial cost lower than max if (costTry < m_cost[m_iMax]) { // update the element sum of the simplex for (int dim=0;dim<m_nDim;dim++) m_elemSum[dim] += tryVertex[dim] - m_simplex[m_iMax][dim]; // replace the max vertex with the trial vertex for (int dim=0;dim<m_nDim;dim++) m_simplex[m_iMax][dim] = tryVertex[dim]; // update the cost m_cost[m_iMax] = costTry; if (showSimplex) std::cout << "stretched\t" << m_iMax << "\tby : " << factor << std::endl; } return costTry; }