void ControlDevice::add(std::string const& name, Mutator value_ref) { if(required_flag == all_required_flag) value_ref.make_required(); else if(required_flag == all_optional_flag) value_ref.make_optional(); impl->add(name,value_ref); }
inline void Value::accept(Mutator& v) { switch (k) { case error_value: return v.visit(r.err_); case integer_value: return v.visit(r.int_); case function_value: return v.visit(r.fn_); case reference_value: return v.visit(r.ref_); case array_value: return v.visit(r.arr_); case tuple_value: return v.visit(r.tup_); } }
HRESULT MutationEngine::addMutator(PCHAR scriptFileName, PCHAR filter) { if(_mutators.size() >= this->_maxMutatorsNum) { dprintf("[windbgshark] MutationEngine::addMutator: sorry, mutators number reached _maxMutatorsNum = %d\n", this->_maxMutatorsNum); return E_FAIL; } Mutator *pMutator = new Mutator(); if(pMutator == NULL) { dprintf("[windbgshark] MutationEngine::addMutator: error! could not allocate memory for a new mutator\n"); return E_FAIL; } ULONG mutatorId; for(mutatorId = 0; mutatorId < this->_maxMutatorsNum; mutatorId++) { if(_mutators.find(mutatorId) == _mutators.end()) { break; } } if(mutatorId >= this->_maxMutatorsNum) { dprintf("[windbgshark] MutationEngine::addMutator: error! could not find suitable Id for mutator\n"); return E_FAIL; } pMutator->setMutator(mutatorId, scriptFileName, filter); _mutators[mutatorId] = pMutator; return S_OK; }
void accept(Mutator& v) { v.visit(*this); }
int main() { sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Main"); // Setup test creature Creature creature = Creature(); creature.position[0] = 100; creature.position[1] = 100; // creature.velocity[0] = 20; // creature.velocity[1] = 20; creature.setRotation(50); env.addObject(&creature); // Setup creatures for (int i = 0; i < NUM_CREATURES; ++i) { creatures[i] = Creature(); creatures[i].position[0] = 50 * (i + 1); creatures[i].position[1] = 50 * (i + 1); creatures[i].setRotation(0); env.addObject(&creatures[i]); } // Setup food for (int i = 0; i < NUM_FOOD; ++i) { Food f = Food(); f.reset(WIDTH, HEIGHT); food.push_back(f); } sf::Clock frame_clock; sf::Clock gen_clock; int frame_count = 0; while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { switch (event.type) { case sf::Event::Closed: window.close(); break; case sf::Event::KeyPressed: if (event.key.code == sf::Keyboard::W) { creature.moveForward(); } else if (event.key.code == sf::Keyboard::S) { printf("%s\n", "S PRESSED"); } else if (event.key.code == sf::Keyboard::A) { creature.setRotation(creature.getRotation() + 5); } else if (event.key.code == sf::Keyboard::D) { creature.setRotation(creature.getRotation() - 5); } else if (event.key.code == sf::Keyboard::P) { launchConsole(); frame_clock.restart(); } break; case sf::Event::MouseButtonPressed: if (event.mouseButton.button == sf::Mouse::Right) { sf::Vector2f position = static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)); } break; default: break; } } window.clear(); for (int i = 0; i < NUM_FOOD; ++i) { if (!food[i].isConsumed()) window.draw(food[i]); } // Debug creature creature.draw(&window); for (int i = 0; i < NUM_FOOD; ++i) { if (creature.isPointInFOV(food[i].getPosition())) { // printf("%f\n", creature.distanceToPoint(food[i].getPosition())); } } for (int i = 0; i < NUM_CREATURES; ++i) { creatures[i].process(&food); creatures[i].draw(&window); } // Handle generations if (gen_clock.getElapsedTime().asSeconds() > GENERATION_LENGTH_SECONDS) { std::cout << "Generation: " << ++gen_count << std::endl; writeOutData(); // Reset food for (int i = 0; i < NUM_FOOD; ++i) { food[i].reset(WIDTH, HEIGHT); } // Sort by energy level std::sort(creatures, creatures + NUM_CREATURES, compareCreatures); // Select and mutate the top 50% of creatures Mutator mutator = Mutator(); for (int i = 0; i < NUM_CREATURES/2; ++i) { printf("Creature %d %f\n", i, creatures[i].getEnergy()); mutator.setTarget(&creatures[i]); creatures[i].resetEnergy(); creatures[i].position[0] = WIDTH / 2; creatures[i].position[1] = HEIGHT / 2; mutator.mutate(&creatures[NUM_CREATURES - 1 - i]); creatures[NUM_CREATURES - 1 - i].resetEnergy(); creatures[NUM_CREATURES - 1 - i].position[0] = WIDTH / 2; creatures[NUM_CREATURES - 1 - i].position[1] = HEIGHT / 2; } gen_clock.restart(); } window.display(); sf::Time elapsed = frame_clock.restart(); env.step(elapsed.asSeconds()); } return 0; }
void ControlDevice::add(std::string const& name, Mutator value_ref, Checker c) { value_ref.set_checker(c); add(name, value_ref); }
void ControlDevice::add(char const* name, Mutator value_ref, Checker c) { value_ref.set_checker(c); add(name, value_ref); }
void process(const WorkUnit *workUnit, WorkResult *workResult, const bool &stop) { ImageBlock *result = static_cast<ImageBlock *>(workResult); const SeedWorkUnit *wu = static_cast<const SeedWorkUnit *>(workUnit); Path *current = new Path(), *proposed = new Path(); Spectrum relWeight(0.0f); result->clear(); /// Reconstruct the seed path m_pathSampler->reconstructPath(wu->getSeed(), m_config.importanceMap, *current); relWeight = current->getRelativeWeight(); BDAssert(!relWeight.isZero()); DiscreteDistribution suitabilities(m_mutators.size()); MutationRecord muRec, currentMuRec(Mutator::EMutationTypeCount,0,0,0,Spectrum(0.f)); ref<Timer> timer = new Timer(); size_t consecRejections = 0; Float accumulatedWeight = 0; #if defined(MTS_DEBUG_FP) enableFPExceptions(); #endif #if defined(MTS_BD_DEBUG_HEAVY) std::ostringstream oss; Path backup; #endif for (size_t mutationCtr=0; mutationCtr < m_config.nMutations && !stop; ++mutationCtr) { if (wu->getTimeout() > 0 && (mutationCtr % 8192) == 0 && (int) timer->getMilliseconds() > wu->getTimeout()) break; /* Query all mutators for their suitability */ suitabilities.clear(); for (size_t j=0; j<m_mutators.size(); ++j) suitabilities.append(m_mutators[j]->suitability(*current)); #if defined(MTS_BD_DEBUG_HEAVY) current->clone(backup, *m_pool); #endif size_t mutatorIdx = 0; bool success = false; Mutator *mutator = NULL; if (suitabilities.normalize() == 0) { /* No mutator can handle this path -- give up */ size_t skip = m_config.nMutations - mutationCtr; accumulatedWeight += skip; consecRejections += skip; break; } mutatorIdx = suitabilities.sample(m_sampler->next1D()); mutator = m_mutators[mutatorIdx].get(); /* Sample a mutated path */ success = mutator->sampleMutation(*current, *proposed, muRec, currentMuRec); #if defined(MTS_BD_DEBUG_HEAVY) if (backup != *current) Log(EError, "Detected an unexpected path modification after a " "mutation of type %s (k=%i)!", muRec.toString().c_str(), current->length()); if (success) { bool fail = false; for (int i=0; i<muRec.l; ++i) if (*backup.vertex(i) != *proposed->vertex(i)) fail = true; for (int i=1; i <= backup.length() - muRec.m; ++i) if (*backup.vertex(muRec.m+i) != *proposed->vertex(muRec.l+muRec.ka+i)) fail = true; if (fail) Log(EError, "Detected an unexpected path modification outside of the " "specified range after a mutation of type %s (k=%i)!", muRec.toString().c_str(), current->length()); } backup.release(*m_pool); #endif statsAccepted.incrementBase(1); if (success) { Float Qxy = mutator->Q(*current, *proposed, muRec) * suitabilities[mutatorIdx]; suitabilities.clear(); for (size_t j=0; j<m_mutators.size(); ++j) suitabilities.append(m_mutators[j]->suitability(*proposed)); suitabilities.normalize(); Float Qyx = mutator->Q(*proposed, *current, muRec.reverse()) * suitabilities[mutatorIdx]; Float a; if (!m_config.importanceMap) { if(Qxy > RCPOVERFLOW) a = std::min((Float) 1, Qyx / Qxy); else a = 0.f; } else { const Float *luminanceValues = m_config.importanceMap->getFloatData(); const Point2 &curPos = current->getSamplePosition(); const Point2 &propPos = proposed->getSamplePosition(); Vector2i size = m_config.importanceMap->getSize(); Point2i curPosI( std::min(std::max(0, (int) curPos.x), size.x-1), std::min(std::max(0, (int) curPos.y), size.y-1)); Point2i propPosI( std::min(std::max(0, (int) propPos.x), size.x-1), std::min(std::max(0, (int) propPos.y), size.y-1)); Float curValue = luminanceValues[curPosI.x + curPosI.y * size.x]; Float propValue = luminanceValues[propPosI.x + propPosI.y * size.x]; a = std::min((Float) 1, (Qyx * curValue) / (Qxy * propValue)); } #if defined(MTS_BD_DEBUG_HEAVY) if (!proposed->verify(m_scene, EImportance, oss)) { Log(EWarn, "%s proposed as %s, Qxy=%f, Qyx=%f", oss.str().c_str(), muRec.toString().c_str(), Qxy, Qyx); proposed->release(muRec.l, muRec.l + muRec.ka + 1, *m_pool); oss.str(""); continue; } #endif if (Qxy == 0) { // be tolerant of this (can occasionally happen due to floating point inaccuracies) a = 0; } else if (Qxy < 0 || Qyx < 0 || std::isnan(Qxy) || std::isnan(Qyx)) { #if defined(MTS_BD_DEBUG) Log(EDebug, "Source path: %s", current->toString().c_str()); Log(EDebug, "Proposal path: %s", proposed->toString().c_str()); Log(EWarn, "Internal error while computing acceptance probabilities: " "Qxy=%f, Qyx=%f, muRec=%s", Qxy, Qyx, muRec.toString().c_str()); #endif a = 0; } accumulatedWeight += 1-a; /* Accept with probability 'a' */ if (a == 1 || m_sampler->next1D() < a) { current->release(muRec.l, muRec.m+1, *m_pool); Spectrum value = relWeight * accumulatedWeight; if (!value.isZero()) result->put(current->getSamplePosition(), &value[0]); /* The mutation was accepted */ std::swap(current, proposed); relWeight = current->getRelativeWeight(); mutator->accept(muRec); currentMuRec = muRec; accumulatedWeight = a; consecRejections = 0; ++statsAccepted; } else { /* The mutation was rejected */ proposed->release(muRec.l, muRec.l + muRec.ka + 1, *m_pool); consecRejections++; if (a > 0) { Spectrum value = proposed->getRelativeWeight() * a; result->put(proposed->getSamplePosition(), &value[0]); } } } else { accumulatedWeight += 1; consecRejections++; } } #if defined(MTS_BD_DEBUG) if (consecRejections == m_config.nMutations) Log(EWarn, "Encountered a path that could *never* be mutated!: %s", current->toString().c_str()); #endif if (accumulatedWeight > 0) { Spectrum value = relWeight * accumulatedWeight; result->put(current->getSamplePosition(), &value[0]); } #if defined(MTS_DEBUG_FP) disableFPExceptions(); #endif current->release(*m_pool); delete current; delete proposed; if (!m_pool->unused()) Log(EError, "Internal error: detected a memory pool leak!"); }
void accept(Mutator& v) { return v.visit(this); }