コード例 #1
0
ファイル: Main_debug.C プロジェクト: Sealos/AI
void verifyModel(cchar* name, cchar* model)
{
    // Parse CNF:
    VSolver S;
    int     len  = strlen(name);
    char*   tmp  = NULL;
    int     stat = 0;
    FILE*   in;
    if (len > 5 && strcmp(name+len-5, ".bcnf") == 0){
        reportf("(cannot verify BCNF files)\n"); return; }
    if (len > 3 && strcmp(name+len-3, ".gz") == 0){
        tmp = xstrdup("tmp_XXXXXX");
        int fd = mkstemp(tmp);
        if (fd == -1)
            fprintf(stderr, "ERROR! Could not create temporary file for unpacking problem.\n"),
            exit(1);
        else
            close(fd);
        stat = system(sFree(nsprintf("zcat %s  > %s", name, tmp)));
        in = fopen(tmp, "rb");
    }else
        in = fopen(name, "rb");
    if (stat != 0 || in == NULL)
        fprintf(stderr, "ERROR! Could not open file: %s\n", name),
        exit(1);
    parse_DIMACS(in, S);
    fclose(in);
    if (tmp != NULL)
        remove(tmp);

    // Parse model:
    vec<bool>   true_lits(S.nVars()*2, false);
    int         lit;
    in = fopen(model, "rb"); assert(in != NULL);
    for(;;){
        int n = fscanf(in, "%d", &lit);
        if (n != 1 || lit == 0) break;
        if (lit < 0)
            true_lits[index(~Lit(-lit-1))] = true;
        else
            true_lits[index( Lit( lit-1))] = true;
    }
    fclose(in);

    //for (int i = 0; i < true_lits.size(); i++)
    //    if (true_lits[i]) printf(L_LIT" ", L_lit(toLit(i)));
    //printf("\n");

    // Check satisfaction:
    for (int i = 0; i < S.clauses.size(); i++){
        VClause& c = *S.clauses[i];
        for (int j = 0; j < c.size(); j++){
            if (true_lits[index(c[j])])
                goto Satisfied;
        }

        printf("FALSE MODEL!!!\n");
        printf("{");
        for (int j = 0; j < c.size(); j++)
            printf(" x%d:%d", var(c[j]), true_lits[index(c[j])]);
        printf(" }\n");
        exit(0);

      Satisfied:;
    }
}
コード例 #2
0
ファイル: Main.C プロジェクト: pmatos/maxsatzilla
int main(int argc, char** argv)
{
    Solver      S;
    S.verbosity = 1;


    int         i, j;
    const char* value;
    for (i = j = 0; i < argc; i++){
        if ((value = hasPrefix(argv[i], "-polarity-mode="))){
            if (strcmp(value, "true") == 0)
                S.polarity_mode = Solver::polarity_true;
            else if (strcmp(value, "false") == 0)
                S.polarity_mode = Solver::polarity_false;
            else if (strcmp(value, "rnd") == 0)
                S.polarity_mode = Solver::polarity_rnd;
            else{
                reportf("ERROR! unknown polarity-mode %s\n", value);
                exit(0); }

        }else if ((value = hasPrefix(argv[i], "-rnd-freq="))){
            double rnd;
            if (sscanf(value, "%lf", &rnd) <= 0 || rnd < 0 || rnd > 1){
                reportf("ERROR! illegal rnd-freq constant %s\n", value);
                exit(0); }
            S.random_var_freq = rnd;

        }else if ((value = hasPrefix(argv[i], "-decay="))){
            double decay;
            if (sscanf(value, "%lf", &decay) <= 0 || decay <= 0 || decay > 1){
                reportf("ERROR! illegal decay constant %s\n", value);
                exit(0); }
            S.var_decay = 1 / decay;

        }else if ((value = hasPrefix(argv[i], "-verbosity="))){
            int verbosity = (int)strtol(value, NULL, 10);
            if (verbosity == 0 && errno == EINVAL){
                reportf("ERROR! illegal verbosity level %s\n", value);
                exit(0); }
            S.verbosity = verbosity;

        }else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0){
            printUsage(argv);
            exit(0);

        }else if (strncmp(argv[i], "-", 1) == 0){
            reportf("ERROR! unknown flag %s\n", argv[i]);
            exit(0);

        }else
            argv[j++] = argv[i];
    }
    argc = j;


    reportf("This is MiniSat 2.0 beta\n");
#if defined(__linux__)
    fpu_control_t oldcw, newcw;
    _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
    reportf("WARNING: for repeatability, setting FPU to use double precision\n");
#endif
    double cpu_time = cpuTime();

    solver = &S;
    signal(SIGINT,SIGINT_handler);
    signal(SIGHUP,SIGINT_handler);

    if (argc == 1)
        reportf("Reading from standard input... Use '-h' or '--help' for help.\n");

    gzFile in = (argc == 1) ? gzdopen(0, "rb") : gzopen(argv[1], "rb");
    if (in == NULL)
        reportf("ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]), exit(1);

    reportf("============================[ Problem Statistics ]=============================\n");
    reportf("|                                                                             |\n");

    parse_DIMACS(in, S);
    gzclose(in);
    FILE* res = (argc >= 3) ? fopen(argv[2], "wb") : NULL;

    double parse_time = cpuTime() - cpu_time;
    reportf("|  Parsing time:         %-12.2f s                                       |\n", parse_time);

    if (!S.simplify()){
        reportf("Solved by unit propagation\n");
        if (res != NULL) fprintf(res, "UNSAT\n"), fclose(res);
        printf("UNSATISFIABLE\n");
        exit(20);
    }

    bool ret = S.solve();
    printStats(S);
    reportf("\n");
    printf(ret ? "SATISFIABLE\n" : "UNSATISFIABLE\n");
    if (res != NULL){
        if (ret){
            fprintf(res, "SAT\n");
	    //S.liftModel();
            for (int i = 0; i < S.nVars(); i++)
                if (S.model[i] != l_Undef)
                    fprintf(res, "%s%s%d", (i==0)?"":" ", (S.model[i]==l_True)?"":"-", i+1);
	    //else fprintf(res, "\nuninstanciated variable %d\n", i+1);
            fprintf(res, " 0\n");
        }else
            fprintf(res, "UNSAT\n");
        fclose(res);
    }

#ifdef NDEBUG
    exit(ret ? 10 : 20);     // (faster than "return", which will invoke the destructor for 'Solver')
#endif
}
コード例 #3
0
ファイル: Main.C プロジェクト: tonyling/research-sat-solvers
int main(int argc, char** argv)
{
    Solver      S;
    bool        st;

    if (argc >= 2 && strlen(argv[1]) >= 5 && strcmp(&argv[1][strlen(argv[1])-5], ".bcnf") == 0)
        st = parse_BCNF(argv[1], S);
    else{
        parse_DIMACS(argv[1], S);
        st = S.okay();
        /*
        gzFile in = (argc == 1) ? gzdopen(0, "rb") : gzopen(argv[1], "rb");
        if (in == NULL)
            fprintf(stderr, "ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]),
            exit(1);
        st = parse_DIMACS(in, S);
        gzclose(in);
        */
    }
    FILE* res = (argc >= 3) ? fopen(argv[2], "wb") : NULL;

    if (!st){
        if (res != NULL) fprintf(res, "UNSAT\n"), fclose(res);
        reportf("Trivial problem\n");
        reportf("UNSATISFIABLE\n");
      #ifdef SAT_LIVE
        printf("s UNSATISFIABLE\n");
      #endif
        exit(20);
    }

    S.verbosity = 1;
    solver = &S;
    signal(SIGINT,SIGINT_handler);
    signal(SIGHUP,SIGINT_handler);
    st = S.solve();
    printStats(S.stats, cpuTime());
    reportf("\n");
    reportf(st ? "SATISFIABLE\n" : "UNSATISFIABLE\n");

    if (res != NULL){
        if (st){
            fprintf(res, "SAT\n");
            for (int i = 0; i < S.nVars(); i++)
                if (S.model[i] != l_Undef)
                    fprintf(res, "%s%s%d", (i==0)?"":" ", (S.model[i]==l_True)?"":"-", i+1);
            fprintf(res, " 0\n");
        }else
            fprintf(res, "UNSAT\n");
        fclose(res);
    }

  #ifdef SAT_LIVE
    printf(st ? "s SATISFIABLE\n" : "s UNSATISFIABLE\n");
    if (st){
        printf("v");
        for (int i = 0; i < S.nVars(); i++)
            if (S.model[i] != l_Undef)
                printf(" %s%d", (S.model[i]==l_True)?"":"-", i+1);
        printf(" 0\n");
    }
  #endif

    exit(st ? 10 : 20);     // (faster than "return", which will invoke the destructor for 'Solver')
}