本文整理汇总了C++中Domain::facts_begin方法的典型用法代码示例。如果您正苦于以下问题:C++ Domain::facts_begin方法的具体用法?C++ Domain::facts_begin怎么用?C++ Domain::facts_begin使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Domain
的用法示例。
在下文中一共展示了Domain::facts_begin方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: walksatSolver
boost::unordered_set<Model> MCSat::sampleSat(const Model& initialModel, const Domain& d, boost::mt19937& rng) {
boost::unordered_set<Model> models;
models.insert(initialModel); // always include the initial model
// transform domain into a SAT problem
Domain dSat;
for (Domain::fact_const_iterator it = d.facts_begin(); it != d.facts_end(); it++) {
dSat.addFact(*it);
}
for (Domain::formula_const_iterator it = d.formulas_begin(); it != d.formulas_end(); it++) {
ELSentence s = *it;
s.setHasInfWeight(true);
dSat.addFormula(s);
}
dSat.addAtoms(d.atoms_begin(), d.atoms_end());
// perform UP (if possible)
Domain reduced;
if (useUnitPropagation_) {
try {
reduced = MCSat::applyUP(dSat);
} catch (contradiction& c) {
return models; // can't continue, just return our models which has only one item.
}
} else {
reduced = dSat;
}
// rewrite infinite weighted formulas so they have singular weight
reduced = reduced.replaceInfForms();
// do some random restarts and hope for different models
MWSSolver walksatSolver(walksatIterations_, walksatRandomMoveProb_, &reduced);
for (unsigned int i = 1; i <= walksatNumRandomRestarts_; i++) {
Model iterInitModel = reduced.randomModel(rng); // TODO: better way to make random models
if (reduced.formulas_size() == 0) {
// just add the random model and continue
models.insert(reduced.randomModel(rng));
continue;
}
//std::cout << "--\nFormulas for maxwalksat: ";
//std::copy(reduced.formulas_begin(), reduced.formulas_end(), std::ostream_iterator<ELSentence>(std::cout, ", "));
//std::cout << std::endl;
Model iterModel = walksatSolver.run(rng, iterInitModel);
if (reduced.isFullySatisfied(iterModel)) models.insert(iterModel);
}
return models;
}
示例2: run
void MCSat::run(boost::mt19937& rng) { // TODO: setup using random initial models
if (d_ == 0) {
throw std::logic_error("MCSat::run() - Domain not set");
}
if (sampleStrategy_ == 0) {
throw std::logic_error("MCSat::run() - SampleStrategy not set");
}
samples_.clear();
samples_.reserve(numSamples_);
//std::cout << "initial domain: ";
//d_->printDebugDescription(std::cout);
// first, run unit propagation on our domain to get a new reduced one.
Domain reduced;
if (useUnitPropagation_) {
reduced = MCSat::applyUP(*d_);
} else {
reduced = *d_;
}
//std::cout << "reduced domain: ";
//reduced.printDebugDescription(std::cout);
Model prevModel = (useRandomInitialModels_ ? reduced.randomModel(rng) : reduced.defaultModel());
// do a starting run on the whole problem as our initial sample
//boost::unordered_set<Model> initModels = sampleSat(prevModel, reduced);
//prevModel = *initModels.begin();
Domain prevDomain = reduced;
if (burnInIterations_ == 0) samples_.push_back(prevModel);
unsigned int totalIterations = numSamples_+burnInIterations_;
for (unsigned int iteration = 1; iteration < totalIterations; iteration++) {
std::vector<ELSentence> newSentences;
if ( totalIterations < 20 ||
iteration % (totalIterations / 20) == 0) {
std::cout << (((double)iteration) / ((double) totalIterations))*100 << "% done." << std::endl;
}
sampleStrategy_->sampleSentences(prevModel, reduced, rng, newSentences);
// if (iteration == 1) {
// std::cout << "initial sampled sentences: ";
// std::copy(newSentences.begin(), newSentences.end(), std::ostream_iterator<ELSentence>(std::cout, "\n"));
// std::cout << "initial model:";
// std::cout << prevModel;
// for (Domain::formula_const_iterator it = prevDomain.formulas_begin();
// it != prevDomain.formulas_end();
// it++) {
// std::cout << "formula " << *it << " is satisfied at: " << it->dSatisfied(prevModel, prevDomain) << std::endl;
// }
// std::cin.get();
// }
// make a new domain using new Sentences
Domain curDomain;
curDomain.setMaxInterval(prevDomain.maxInterval());
for (Domain::fact_const_iterator it = prevDomain.facts_begin(); it != prevDomain.facts_end(); it++) {
curDomain.addFact(*it);
}
for (std::vector<ELSentence>::const_iterator it = newSentences.begin(); it != newSentences.end(); it++) {
curDomain.addFormula(*it);
}
curDomain.addAtoms(prevDomain.atoms_begin(), prevDomain.atoms_end());
//
// if (iteration == burnInIterations_ + numSamples_/2) {
// std::cout << "ITERATION: " << iteration << std::endl;
// std::cout << "curDomain";
// curDomain.printDebugDescription(std::cout);
// std::cout << "sampled sentences: ";
// std::copy(newSentences.begin(), newSentences.end(), std::ostream_iterator<ELSentence>(std::cout, "\n"));
// }
boost::unordered_set<Model> curModels = sampleSat(prevModel, curDomain, rng);
assert(!curModels.empty());
// choose a random model
boost::uniform_int<std::size_t> pickModel(0, curModels.size()-1);
boost::unordered_set<Model>::size_type index = pickModel(rng);
boost::unordered_set<Model>::const_iterator it = curModels.begin();
while (index > 0) {
it++;
index--;
}
// add the model
if (iteration >= burnInIterations_) samples_.push_back(*it);
prevModel = *it;
prevDomain = curDomain;
}
}
示例3: main
//.........这里部分代码省略.........
"10: [ BallContact(them) -> !BallContact(frontmiddle) ]\n"
"\n"
// "# if the backleft player served, nobody posed before that action\n"
// "10: Serve(backleft) -> !(<>{<} [Spike(backleft) v Set(backleft) v Serve(backleft) v Dig(backleft) v Block(backleft) v Squat(backleft)])\n"
// "10: Serve(backleft) -> !(<>{<} [Spike(backright) v Set(backright) v Serve(backright) v Dig(backright) v Block(backright) v Squat(backright)])\n"
// "10: Serve(backleft) -> !(<>{<} [Spike(backmiddle) v Set(backmiddle) v Serve(backmiddle) v Dig(backmiddle) v Block(backmiddle) v Squat(backmiddle)])\n"
// "10: Serve(backleft) -> !(<>{<} [Spike(frontleft) v Set(frontleft) v Serve(frontleft) v Dig(frontleft) v Block(frontleft) v Squat(frontleft)])\n"
// "10: Serve(backleft) -> !(<>{<} [Spike(frontright) v Set(frontright) v Serve(frontright) v Dig(frontright) v Block(frontright) v Squat(frontright)])\n"
// "10: Serve(backleft) -> !(<>{<} [Spike(frontmiddle) v Set(frontmiddle) v Serve(frontmiddle) v Dig(frontmiddle) v Block(frontmiddle) v Squat(frontmiddle)])\n"
// "\n"
// "# if a huddle occurs, it\'s the final pose\n"
// "10: Huddle() -> !(<>{>} [Spike(backleft) v Set(backleft) v Serve(backleft) v Dig(backleft) v Block(backleft) v Squat(backleft)])\n"
// "10: Huddle() -> !(<>{>} [Spike(backright) v Set(backright) v Serve(backright) v Dig(backright) v Block(backright) v Squat(backright)])\n"
// "10: Huddle() -> !(<>{>} [Spike(backmiddle) v Set(backmiddle) v Serve(backmiddle) v Dig(backmiddle) v Block(backmiddle) v Squat(backmiddle)])\n"
// "10: Huddle() -> !(<>{>} [Spike(frontleft) v Set(frontleft) v Serve(frontleft) v Dig(frontleft) v Block(frontleft) v Squat(frontleft)])\n"
// "10: Huddle() -> !(<>{>} [Spike(frontright) v Set(frontright) v Serve(frontright) v Dig(frontright) v Block(frontright) v Squat(frontright)])\n"
// "10: Huddle() -> !(<>{>} [Spike(frontmiddle) v Set(frontmiddle) v Serve(frontmiddle) v Dig(frontmiddle) v Block(frontmiddle) v Squat(frontmiddle)])\n"
// "\n"
"# if a player contacts the ball, they are in some sort of pose\n"
"10: [ BallContact(backleft) -> Spike(backleft) v Set(backleft) v Serve(backleft) v Dig(backleft) v Block(backleft) v Squat(backleft) ]\n"
"10: [ BallContact(backright) -> Spike(backright) v Set(backright) v Serve(backright) v Dig(backright) v Block(backright) v Squat(backright) ]\n"
"10: [ BallContact(backmiddle) -> Spike(backmiddle) v Set(backmiddle) v Serve(backmiddle) v Dig(backmiddle) v Block(backmiddle) v Squat(backmiddle) ]\n"
"10: [ BallContact(frontleft) -> Spike(frontleft) v Set(frontleft) v Serve(frontleft) v Dig(frontleft) v Block(frontleft) v Squat(frontleft) ]\n"
"10: [ BallContact(frontright) -> Spike(frontright) v Set(frontright) v Serve(frontright) v Dig(frontright) v Block(frontright) v Squat(frontright) ]\n"
"10: [ BallContact(frontmiddle) -> Spike(frontmiddle) v Set(frontmiddle) v Serve(frontmiddle) v Dig(frontmiddle) v Block(frontmiddle) v Squat(frontmiddle) ]\n"
"\n"
// "# if a player served, nobody on our team will touch the ball before the opponents\n"
// "10: Serve(backleft) ^{<} BallContact(them) -> (<>{d} BallContact(them)) v (!<>{d} [BallContact(backright) v BallContact(backmiddle) v BallContact(frontleft) v BallContact(frontright) v BallContact(frontmiddle)])\n"
// "\n"
"# only the back left player may serve\n"
"10: [ !Serve(backright) ^ !Serve(backmiddle) ^ !Serve(frontleft) ^ !Serve(frontright) ^ !Serve(frontmiddle) ]\n"
"\n"
"# only players in the front row may spike\n"
"10: [ !Spike(backleft) ^ !Spike(backright) ^ !Spike(backmiddle) ]\n"
"\n"
);
Domain d = loadDomainWithStreams(facts, formulas);
std::set<unsigned int> timePoints;
for (Domain::fact_const_iterator it = d.facts_begin(); it != d.facts_end(); it++) {
SISet set = it->second;
for (SISet::const_iterator siIt = set.begin(); siIt != set.end(); siIt++) {
SpanInterval si = *siIt;
timePoints.insert(si.start().start());
timePoints.insert(si.start().finish());
timePoints.insert(si.finish().start());
timePoints.insert(si.finish().finish());
}
}
std::cout << "found " << timePoints.size() << " timepoints." << std::endl;
MCSat mcSatSolver(&d);
mcSatSolver.setBurnInIterations(1);
mcSatSolver.setNumSamples(1);
if (vm.count("disable-up")) {
mcSatSolver.setUseUnitPropagation(false);
} else {
mcSatSolver.setUseUnitPropagation(true);
}
std::cout << "running mcSatSolver with " << mcSatSolver.burnInIterations() << " burn in iterations and a sample size of " << mcSatSolver.numSamples() << std::endl;
std::string prefix = (vm.count("name") ? vm["name"].as<std::string>() : "mcsat-volleyball");
std::cout << "saving random seed..." << std::endl;
{
std::string rngFilename = prefix + "-seed.txt.gz";
io::file_descriptor_sink rngOutFile(rngFilename.c_str(), std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
if (!rngOutFile.is_open()){
std::cerr << "unable to open file " + rngFilename + "for RNG writing." << std::endl;
} else {
// gzip it using boost iostreams
io::filtering_ostream rngOut;
rngOut.push(io::gzip_compressor());
rngOut.push(rngOutFile);
rngOut << rng;
}
}
//std::cout << "rng = " << rng << std::endl;
std::cout << "starting run..." << std::endl;
mcSatSolver.setSampleStrategy(new MCSatSampleLiquidlyStrategy());
std::clock_t start = std::clock();
mcSatSolver.run(rng);
std::clock_t end = std::clock();
std::cout << "ran in " << (end - start)/CLOCKS_PER_SEC << " seconds." << std::endl;
std::cout << "saving model file..." << std::endl;
{
std::string outFilename = prefix + "-model.dat.gz";
io::file_descriptor_sink outFile(outFilename.c_str(), std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
if (!outFile.is_open()) {
std::cerr << "unable to open file " + outFilename + " for model writing." << std::endl;
} else {
io::filtering_ostream dataOut;
dataOut.push(io::gzip_compressor());
dataOut.push(outFile);
boost::archive::text_oarchive tout(dataOut);
registerAllPELTypes(tout);
tout << mcSatSolver;
}
}
}