ExprConstSP Parser::buildExpression(const std::vector<Command>& commands) { stack<ExprConstSP> expStack; ExprConstSP node, nullShared; Numbers::Number* number; std::vector<ExprConstSP> children; for (std::vector<Command>::const_iterator it = commands.begin(); it != commands.end(); ++it) { switch ((*it).getNodeType()) { case Command::literal: number = nFormatter->format((*it).getNodeName()); if (!number) return nullShared; node = eBuilder->literal(number); expStack.push(node); break; case Command::symbol: for (unsigned int i = 0; i < (*it).getNumberOfChildren(); i++) { if (expStack.size() == 0) return ExprConstSP(); children.insert(children.begin(), getPop(expStack)); } node = eBuilder->operator()((*it).getNodeName(), children); if (!node) return nullShared; expStack.push(node); children.resize(0); break; default: throw std::invalid_argument("invalid node type in Expressions::Parser::buildExpressions(std::vector<Command>)"); } } if (expStack.size() != 1) return nullShared; return expStack.top(); }
cinder::Surface ColorAlgoGen::operator()(const cinder::Surface& realImage) { std::vector<SurfaceWrapper> nextGenPopulation; nextGenPopulation.reserve(m_popSize); std::for_each(m_population.begin(), m_population.end(), [this, &realImage](SurfaceWrapper& s) { s.fitness = getFitness(realImage, s.image); }); std::sort(m_population.begin(), m_population.end(), [](const SurfaceWrapper& s1, const SurfaceWrapper& s2) { return s1.fitness < s2.fitness; }); unsigned int copyRatio = tools::clamp<int>( static_cast<int>(getPercent(COPY) * m_popSize), 0, m_popSize ); if (copyRatio > 0) { std::transform(m_population.begin(), m_population.begin() + copyRatio, std::back_inserter(nextGenPopulation), [&realImage, this](const SurfaceWrapper& sw) { return sw; }); } unsigned int mutateRatio = tools::clamp<int>( static_cast<int>(getPercent(MUTATE) * m_popSize), 0, m_popSize - copyRatio ); if (mutateRatio > 0) { std::transform(m_population.begin(), m_population.begin() + mutateRatio, std::back_inserter(nextGenPopulation), [&realImage, this](const SurfaceWrapper& sw) { cinder::Surface s = mutate(sw.image); return SurfaceWrapper(s, getFitness(realImage, s)); }); } unsigned int crossOverRatio = tools::clamp<int>( static_cast<int>(getPercent(CROSSOVER) * m_popSize), 0, m_popSize - copyRatio - mutateRatio ); if (crossOverRatio > 0) { std::transform(m_population.begin(), m_population.begin() + crossOverRatio, std::back_inserter(nextGenPopulation), [&realImage, this](const SurfaceWrapper& sw) { auto& pop = getPop(); cinder::Surface s = crossOver(sw.image, pop[RANDOMIZER.nextUint(pop.size())].image); return SurfaceWrapper(s, getFitness(realImage, s)); }); } unsigned int randomRatio = tools::clamp<int>( static_cast<int>(getPercent(RANDOM) * m_popSize), 0, m_popSize - copyRatio - mutateRatio - crossOverRatio ); if (nextGenPopulation.size() + randomRatio < m_popSize) { randomRatio = m_popSize - nextGenPopulation.size(); } if (randomRatio > 0) { std::transform(m_population.begin(), m_population.begin() + randomRatio, std::back_inserter(nextGenPopulation), [&realImage, this](const SurfaceWrapper& sw) { cinder::Surface s = getRandomSurface(sw.image.getWidth(), sw.image.getHeight()); return SurfaceWrapper(s, getFitness(realImage, s)); }); } std::sort(nextGenPopulation.begin(), nextGenPopulation.end(), [](const SurfaceWrapper& s1, const SurfaceWrapper& s2) { return s1.fitness < s2.fitness; }); m_population = nextGenPopulation; return m_population.front().image; }
inline void popBaseOffset() noexcept { baseOffset = getPop().get<int>(); }