Exemplo n.º 1
0
def load_dir(path, no_sat=False):
    data = []
    for filename in os.listdir(path):
        name, ext = os.path.splitext(filename)
        if ext != '.cnf':
            continue
        f = CNF.from_file(os.path.join(path, filename))
        sat = int(not name.startswith('uu')) if not no_sat else -1
        data.append(DataSample(filename, f, adj(f), sat))
    return data
Exemplo n.º 2
0
def load_dir(path):
    data = []
    for filename in os.listdir(path):
        name, ext = os.path.splitext(filename)
        if ext != '.cnf':
            continue
        f = CNF.from_file(os.path.join(path, filename))
        if name.startswith('uu'):
            continue
        data.append(DataSample(filename, f, adj(f), None))
    return data
Exemplo n.º 3
0
Arquivo: dot.py Projeto: shi27feng/sat
import sys

from graphviz import Graph

from cnf import CNF
from util import adj

f = CNF.from_file(sys.argv[1])
a_pos, a_neg = adj(f)
a_pos = a_pos.todense()
a_neg = a_neg.todense()

n, m = a_pos.shape

g = Graph('G', filename=sys.argv[2], format='pdf', engine='neato')

for x in range(1, n + 1):
    g.node(f'x{x}', label='x', color='blue', fillcolor='blue', shape='point')

for c in range(1, m + 1):
    g.node(f'c{c}', label='y', color='red', fillcolor='red', shape='point')

pw = sys.argv[3]

for x in range(n):
    for c in range(m):
        var = f'x{x + 1}'
        cl = f'c{c + 1}'
        if a_pos[x, c] == 1:
            # g.edge(var, cl, color='#ff0000', penwidth='0.001')
            g.edge(var, cl, color='#ff0000', penwidth=pw)
Exemplo n.º 4
0
def main(args):
    if args.seed > -1:
        random.seed(args.seed)

    if args.dir_path:
        med_flips = []
        avg_flips = []
        max_flips = []
        solved = []
        med_backflips = []
        avg_backflips = []
        max_backflips = []
        unsats = []
        mean_delta = []
        downwards = []
        upwards = []
        sideways = []
        for i, filename in enumerate(os.listdir(args.dir_path)):
            if i >= args.samples:
                break
            formula = CNF.from_file(os.path.join(args.dir_path, filename))
            walksat = WalkSAT(args.max_tries, args.max_flips, args.p)
            walksat.run(formula)
            flips = walksat.flips_to_solution
            backflips = walksat.backflips
            unsats = walksat.unsat_clauses
            diffs = [np.diff(u) for u in unsats]
            diff_a = np.array(diffs)
            upwards.append(np.mean([np.sum(a > 0) for a in diff_a]))
            downwards.append(np.mean([np.sum(a < 0) for a in diff_a]))
            sideways.append(np.mean([np.sum(a == 0) for a in diff_a]))
            mean_delta.append(np.mean([np.mean(d) for d in diffs]))
            med_backflips.append(np.median(backflips))
            avg_backflips.append(np.mean(backflips))
            max_backflips.append(np.max(backflips))
            med = np.median(flips)
            med_flips.append(med)
            avg_flips.append(np.mean(flips))
            max_flips.append(np.max(flips))
            solved.append(int(med < args.max_flips))
        # print(med_flips)
        # print(avg_flips)
        # print(max_flips)
        logging.info(
            'Acc: {:.4f}, Med: {:.4f}, Mean: {:.4f}, Max: {:.4f}'.format(
                100 * np.mean(solved), np.median(med_flips),
                np.mean(avg_flips), np.mean(max_flips)))
        logging.info(
            '(Backflips) Med: {:.4f}, Mean: {:.4f}, Max: {:.4f}'.format(
                np.median(med_backflips), np.mean(avg_backflips),
                np.mean(max_backflips)))
        logging.info('(Delta) Mean: {:.4f}'.format(np.mean(mean_delta)))
        logging.info(
            '(Movement) Up: {:.4f}, Side: {:.4f}, Down: {:.4f}'.format(
                np.mean(upwards), np.mean(sideways), np.mean(downwards)))
    elif args.file_path:
        formula = CNF.from_file(args.file_path)
        walksat = WalkSAT(args.max_tries, args.max_flips, args.p)
        walksat.run(formula)
        logging.info('Number of solutions found: {}\n'
                     'Avg, std of flips to solution: {:.4f}, {:.4f}'.format(
                         len(walksat.flips_to_solution),
                         np.mean(walksat.flips_to_solution),
                         np.std(walksat.flips_to_solution),
                     ))