예제 #1
0
int main(int argc, char** argv) {

  // set up vtree and manager
  SddLiteral var_count = 4;
  const char* type = "right";
  Vtree* vtree = sdd_vtree_new(var_count,type);
  SddManager* manager = sdd_manager_new(vtree);

  // construct the term X_1 ^ X_2 ^ X_3 ^ X_4
  SddNode* alpha = sdd_manager_literal(1,manager);
  alpha = sdd_conjoin(alpha,sdd_manager_literal(2,manager),manager);
  alpha = sdd_conjoin(alpha,sdd_manager_literal(3,manager),manager);
  alpha = sdd_conjoin(alpha,sdd_manager_literal(4,manager),manager);

  // construct the term ~X_1 ^ X_2 ^ X_3 ^ X_4
  SddNode* beta = sdd_manager_literal(-1,manager);
  beta = sdd_conjoin(beta,sdd_manager_literal(2,manager),manager);
  beta = sdd_conjoin(beta,sdd_manager_literal(3,manager),manager);
  beta = sdd_conjoin(beta,sdd_manager_literal(4,manager),manager);

  // construct the term ~X_1 ^ ~X_2 ^ X_3 ^ X_4
  SddNode* gamma = sdd_manager_literal(-1,manager);
  gamma = sdd_conjoin(gamma,sdd_manager_literal(-2,manager),manager);
  gamma = sdd_conjoin(gamma,sdd_manager_literal(3,manager),manager);
  gamma = sdd_conjoin(gamma,sdd_manager_literal(4,manager),manager);

  printf("== before referencing:\n");
  printf("  live sdd size = %zu\n", sdd_manager_live_size(manager));
  printf("  dead sdd size = %zu\n", sdd_manager_dead_size(manager));

  // ref SDDs so that they are not garbage collected
  sdd_ref(alpha,manager);
  sdd_ref(beta,manager);
  sdd_ref(gamma,manager);
  printf("== after referencing:\n");
  printf("  live sdd size = %zu\n", sdd_manager_live_size(manager));
  printf("  dead sdd size = %zu\n", sdd_manager_dead_size(manager));

  // garbage collect
  sdd_manager_garbage_collect(manager);
  printf("== after garbage collection:\n");
  printf("  live sdd size = %zu\n", sdd_manager_live_size(manager));
  printf("  dead sdd size = %zu\n", sdd_manager_dead_size(manager));

  sdd_deref(alpha,manager);
  sdd_deref(beta,manager);
  sdd_deref(gamma,manager);

  printf("saving vtree & shared sdd ...\n");
  sdd_vtree_save_as_dot("output/shared-vtree.dot",vtree);
  sdd_shared_save_as_dot("output/shared.dot",manager);

  sdd_vtree_free(vtree);
  sdd_manager_free(manager);

  return 0;
}
예제 #2
0
파일: sddtab.cpp 프로젝트: tpagram/sddtab
int main(int argc, char** argv) {
    
    //Read input and convert to KFormulas.
    Options* opts = new Options(argc,argv);
    KFormula* notPsiNNF;
    std::string input;
    getline (std::cin, input);
    if (opts->verbose) std::cout << "Building Kformulas from input...\n";
    if (input.length() == 0) {
        std::cout << "Empty formula is valid.";
        return 0;
    } else if (opts->satisfying) {
        notPsiNNF = KFormula::toBoxNNF(KFormula::parseKFormula(input.c_str()));
    } else {
        notPsiNNF = KFormula::toBoxNNF(new KFormula(KFormula::NOT,KFormula::parseKFormula(input.c_str()),NULL));
    }
    if (opts->verbose) std::cout << "	Kformulas built!\n";
   
    //Gather atoms from KFormulas and assign literals.
    if (opts->verbose) std::cout << "Assigning literals...\n";
    std::vector<KFormula*> atoms = KFormula::getAtoms(notPsiNNF);
    SddLiteral var_count = compiler::setLiterals(atoms, literalsToAtoms, atomsToLiterals);
    if (opts->verbose) {
        std::cout << "	Literals assigned!\n";
    }

    //Initialise SDD library.
    Vtree* vtree = sdd_vtree_new(var_count,"balanced");
    SddManager* m = sdd_manager_new(vtree);
    sdd_vtree_free(vtree);
    sdd_manager_auto_gc_and_minimize_on(m);
    //sdd_manager_set_minimize_function(sdd_vtree_minimize,m);

    //Build SDDs
    if (opts->verbose) std::cout << "Converting Kformulas to SDDs...\n";
    std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
    SddNode* notPsiSdd = compiler::KtoSDD(notPsiNNF,m);
    sdd_ref(notPsiSdd,m);
    std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
    auto SddDuration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
    int SddCount = sdd_count(notPsiSdd);
    int SddSize = sdd_size(notPsiSdd);
    if (opts->reportSize) {
        std::cout << SddCount << "\n";
        return 0;
    }
    if (opts->verbose) std::cout << "	SDD conversion complete!\n";
    
    //Check satisfiability or validity
    Prover* prover = proverFactory::getProver(opts->logic,literalsToAtoms, atomsToLiterals);
    t1 = std::chrono::high_resolution_clock::now();
    bool isSat = prover->isSatisfiable(notPsiSdd,m);
    t2 = std::chrono::high_resolution_clock::now();
    auto SolveDuration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
    
    
    //Output results.
    if (opts->verbose) {
	std::cout << "Stats:\n";
        std::cout << "	Initial Lit count = " << var_count << "\n";
        std::cout << "	Initial SDD size = " << SddSize << "\n";
        std::cout << "	Initial SDD count = " << SddCount << "\n"; 
	std::cout << "	Final manager size = " << sdd_manager_size(m) << "\n";
        std::cout << "	Time to build = " << SddDuration << "\n";
        std::cout << "	Time to solve = " << SolveDuration << "\n";
        std::cout << "	Total time = " << SddDuration + SolveDuration << "\n\n";
    }
    if (opts->satisfying) {
        if (!isSat) std::cout << "Psi is not satisfiable!\n";
        else std::cout << "Psi is satisfiable!\n";
    }
    else {
        if (!isSat) std::cout << "Psi is valid!\n";
        else std::cout << "Psi is not valid!\n";
    }
    
    //Finish up.
    sdd_manager_free(m);
    return 0;
}