示例#1
0
def run():
    algo = parameters['algo']
    files = [open(x) for x in parameters['files']]
    configs = []
    p = parameters['params']
    max_processes = 3
    semaphore = Semaphore(max_processes)

    # generate configurations as compination of possible
    # keys and product of values
    for keys in it.combinations(p.keys(), len(p.keys())):
        v = [p[k] for k in keys]
        for values in it.product(*v):
            config = {}
            for i, k in enumerate(keys):
                config[k] = values[i]
            configs.append(config)
    for f in files:
        for conf in configs:
            config = {'FILENAME': f.name}
            config.update(conf)

            f.seek(0)
            num_vars, clauses = parser.parse(f)

            p = MyProcess(target=run_algorithm,
                          args=(algo, num_vars, clauses, config, semaphore))

            semaphore.acquire()
            p.start()
示例#2
0
def run():
    algo = parameters["algo"]
    files = [open(x) for x in parameters["files"]]
    configs = []
    p = parameters["params"]
    max_processes = 3
    semaphore = Semaphore(max_processes)

    # generate configurations as compination of possible
    # keys and product of values
    for keys in it.combinations(p.keys(), len(p.keys())):
        v = [p[k] for k in keys]
        for values in it.product(*v):
            config = {}
            for i, k in enumerate(keys):
                config[k] = values[i]
            configs.append(config)
    for f in files:
        for conf in configs:
            config = {"FILENAME": f.name}
            config.update(conf)

            f.seek(0)
            num_vars, clauses = parser.parse(f)

            p = MyProcess(target=run_algorithm, args=(algo, num_vars, clauses, config, semaphore))

            semaphore.acquire()
            p.start()
示例#3
0
def run():
    algo = parameters['algo']
    files = [open(x) for x in parameters['files']]
    configs = []
    p = parameters['params']

    # generate configurations as compination of possible
    # keys and product of values
    for keys in it.combinations(p.keys(), len(p.keys())):
        v = [p[k] for k in keys]
        for values in it.product(*v):
            config = {}
            for i, k in enumerate(keys):
                config[k] = values[i]
            configs.append(config)
    for f in files:
        for conf in configs:
            print "=========="
            print f.name
            print conf

            f.seek(0)
            num_vars, clauses = parser.parse(f)
            a = algo(num_vars, clauses, conf)

            start = time.time()
            a.run()
            elapsed = time.time() - start

            print elapsed, 'seconds'
    print "=========="
    print "Done"
示例#4
0
def run():
    algo = ga.GeneticAlgorithm
    filenames = ["instances/random_ksat13.dimacs"]
    files = [open(x) for x in filenames]

    results = {}

    f = files[0]
    num_vars, clauses = parser.parse(f)

    for i, seed in enumerate(np.arange(SAMPLES) + 42):
        print i, "of", SAMPLES
        conf = {"SEED": seed}
        a = algo(num_vars, clauses, conf)

        start = time.time()
        try:
            # timeout in seconds
            with time_limit(20):
                a.run()
        except TimeoutException, msg:
            print msg
        elapsed = time.time() - start

        results[seed] = elapsed
示例#5
0
def run():
    algo = ga.GeneticAlgorithm
    filenames = ['instances/random_ksat13.dimacs']
    files = [open(x) for x in filenames]

    results = {}

    f = files[0]
    num_vars, clauses = parser.parse(f)

    for i, seed in enumerate(np.arange(SAMPLES) + 42):
        print i, 'of', SAMPLES
        conf = {'SEED': seed}
        a = algo(num_vars, clauses, conf)

        start = time.time()
        try:
            # timeout in seconds
            with time_limit(20):
                a.run()
        except TimeoutException, msg:
            print msg
        elapsed = time.time() - start

        results[seed] = elapsed
示例#6
0
def run():
    algo = ga.GeneticAlgorithm
    filenames = ['instances/random_ksat13.dimacs']
    files = [open(x) for x in filenames]

    results = {}

    f = files[0]
    num_vars, clauses = parser.parse(f)

    data = [float(x) / 100 for x in range(0, 50, 2)]
    for i, erate in enumerate(data):
        print i, 'of', len(data)
        times = []
        for seed in range(5):
            conf = {'ELITES_RATE': erate, 'SEED': 42 + seed}
            a = algo(num_vars, clauses, conf)

            start = time.time()
            try:
                # timeout in seconds
                with time_limit(5):
                    a.run()
            except TimeoutException, msg:
                print msg
            else:
                elapsed = time.time() - start
                times.append(elapsed)

        results[erate] = {
            'no_to': len(times),
            'avg': sum(times) / len(times),
            'min': min(times),
            'values': times
        }
示例#7
0
def run():
    algo = parameters['algo']
    files = [open(x) for x in parameters['files']]
    configs = []
    p = parameters['params']

    # generate configurations as compination of possible
    # keys and product of values
    for keys in it.combinations(p.keys(), len(p.keys())):
        v = [p[k] for k in keys]
        for values in it.product(*v):
            config = {}
            for i, k in enumerate(keys):
                config[k] = values[i]
            configs.append(config)
    for f in files:
        for conf in configs:
            print "=========="
            print f.name
            print conf

            f.seek(0)
            num_vars, clauses = parser.parse(f)
            a = algo(num_vars, clauses, conf)

            start = time.time()
            a.run()
            elapsed = time.time() - start

            print elapsed, 'seconds'
    print "=========="
    print "Done"
示例#8
0
def run():
    algo = ga.GeneticAlgorithm
    filenames = ['instances/random_ksat13.dimacs']
    files = [open(x) for x in filenames]

    results = {}

    f = files[0]
    num_vars, clauses = parser.parse(f)

    data = range(30, 100, 10) + [150, 200, 300, 400, 600, 800, 1000]
    for i, popsize in enumerate(data):
        print i, 'of', len(data)
        times = []
        TIMEOUT = 8
        for seed in range(3):
            conf = {
                'NUM_CHROMOSOMES': popsize,
                'SEED': 42 + seed
            }
            a = algo(num_vars, clauses, conf)

            start = time.time()
            try:
                # timeout in seconds
                with time_limit(TIMEOUT):
                    a.run()
            except TimeoutException, msg:
                print msg
            else:
                elapsed = time.time() - start
                times.append(elapsed)

        # timeout
        results[popsize] = {
            'no_to': 0,
            'avg': TIMEOUT,
            'min': TIMEOUT,
            'values': [TIMEOUT]
        }
        if len(times):
            results[popsize] = {
                'no_to': len(times),
                'avg': sum(times) / len(times),
                'min': min(times),
                'values': times
            }
示例#9
0
def run():
    algo = ga.GeneticAlgorithm
    f = open('instances/random_ksat3.dimacs')

    results = {}

    num_vars, clauses = parser.parse(f)

    in_data = {
        'MUTATION_RATE': [float(x) / 20 for x in range(1, 18)],
        'SELECTION_RATE': [float(x) / 40 for x in range(1, 20)]
    }

    for parameter in in_data.keys():
        print parameter
        data = in_data[parameter]
        results[parameter] = {}
        for i, value in enumerate(data):
            print i, 'of', len(data)
            times = []
            for seed in range(10):
                conf = {
                    parameter: value,
                    'SEED': 42 + seed,
                    'ELITES_RATE': 0
                }
                a = algo(num_vars, clauses, conf)

                start = time.time()
                try:
                    # timeout in seconds
                    with time_limit(30):
                        a.run()
                except TimeoutException, msg:
                    print msg
                else:
                    elapsed = time.time() - start
                    times.append(elapsed)

            if len(times) > 1:
                results[parameter][value] = {
                    'std': np.std(np.array(times)),
                    'avg': sum(times) / len(times)
                }
示例#10
0
def run():
    algo = ga.GeneticAlgorithm
    f = open('instances/random_ksat3.dimacs')

    results = {}

    num_vars, clauses = parser.parse(f)

    in_data = {
        'MUTATION_RATE': [float(x) / 20 for x in range(1, 18)],
        'SELECTION_RATE': [float(x) / 40 for x in range(1, 20)]
    }

    for parameter in in_data.keys():
        print parameter
        data = in_data[parameter]
        results[parameter] = {}
        for i, value in enumerate(data):
            print i, 'of', len(data)
            times = []
            for seed in range(10):
                conf = {parameter: value, 'SEED': 42 + seed, 'ELITES_RATE': 0}
                a = algo(num_vars, clauses, conf)

                start = time.time()
                try:
                    # timeout in seconds
                    with time_limit(30):
                        a.run()
                except TimeoutException, msg:
                    print msg
                else:
                    elapsed = time.time() - start
                    times.append(elapsed)

            if len(times) > 1:
                results[parameter][value] = {
                    'std': np.std(np.array(times)),
                    'avg': sum(times) / len(times)
                }
示例#11
0
def run():
    algo = ga.GeneticAlgorithm
    filenames = ['instances/random_ksat13.dimacs']
    files = [open(x) for x in filenames]

    results = {}

    f = files[0]
    num_vars, clauses = parser.parse(f)

    data = [float(x) / 100 for x in range(0, 50, 2)]
    for i, erate in enumerate(data):
        print i, 'of', len(data)
        times = []
        for seed in range(5):
            conf = {
                'ELITES_RATE': erate,
                'SEED': 42 + seed
            }
            a = algo(num_vars, clauses, conf)

            start = time.time()
            try:
                # timeout in seconds
                with time_limit(5):
                    a.run()
            except TimeoutException, msg:
                print msg
            else:
                elapsed = time.time() - start
                times.append(elapsed)

        results[erate] = {
            'no_to': len(times),
            'avg': sum(times) / len(times),
            'min': min(times),
            'values': times
        }
示例#12
0
 def test_parse_simple_file(self):
     parsed = parser.parse(['c foo bar', 'p cnf 20 2', '10 12 15 5 3 0', '6 13 14 -11 20 0'])
     assert_equal(parsed, (20, [[10, 12, 15, 5, 3], [6, 13, 14, -11, 20]]))
示例#13
0
 def test_knf(self):
     parser.parse(['p knf 20 0'])
示例#14
0
文件: main.py 项目: vk4arm/SoSAT
def main():
    global VERBOSE

    # entry point for the program
    np.set_printoptions(linewidth=2000000000)

    clp = argparse.ArgumentParser(description='SAT solver.')
    clp.add_argument('-a',
                     '--algorithm',
                     dest='algo',
                     default='genetic',
                     choices=['genetic', 'ant', 'walksat'],
                     help='select algorithm for solving')
    clp.add_argument('-v',
                     '--verbose',
                     dest='verbose',
                     default=False,
                     action='store_true',
                     help='print stats and status message')
    clp.add_argument('-s',
                     '--seed',
                     dest='seed',
                     type=int,
                     default=42,
                     help='seed for random number generator, 0 means random')
    clp.add_argument('-N',
                     '--number',
                     dest='N',
                     default=None,
                     type=int,
                     help='number of worker processes')
    clp.add_argument('-i',
                     '--max_iterations',
                     dest='max_iterations',
                     default=25000,
                     type=int,
                     help='number of worker processes')
    clp.add_argument('-f',
                     '--factor',
                     dest='f',
                     default=0,
                     type=int,
                     help='number of factored (most-constrained) variables')
    clp.add_argument('-c',
                     '--cse573',
                     dest='cse573',
                     default=False,
                     action='store_true',
                     help='use format for CSE 573')
    clp.add_argument('-np',
                     '--no-preprocessing',
                     dest='no_preprocessing',
                     default=False,
                     action='store_true',
                     help='disable factorization and unit propagation')
    clp.add_argument('infile',
                     nargs='?',
                     type=argparse.FileType('r'),
                     default=sys.stdin)

    args = clp.parse_args()
    VERBOSE = args.verbose

    # parse input file
    if args.cse573:
        num_vars, clauses, mapping = parser.parse_cse573(args.infile)
    else:
        num_vars, clauses = parser.parse(args.infile)

    if args.seed == 0:
        args.seed = random.randint(0, 1e9)

    def print_solution(instance_solution):
        """
        Print the solution. To retrieve the correct solution, preprocessing steps have to be undone.
        """
        if instance_solution is False:
            # unsatisfiability can only be determined during preprocessing
            sys.stdout.write("s UNSATISFIABLE\n")
        elif instance_solution[1] is not None:
            instance, p_solution = instance_solution
            solution = preprocessing.restore_original_solution(
                instance, p_solution, num_vars)
            sol = []
            if args.cse573:
                for i, lit in enumerate(solution):
                    orig_lit = i + 1 if lit else -(i + 1)
                    neg = orig_lit < 0
                    sol.append(('!' if neg else '') + mapping[abs(orig_lit)])
                sys.stdout.write("v " + ' '.join(sol) + '\n')
            else:
                for i, lit in enumerate(solution):
                    sol.append(str(i + 1 if lit else -(i + 1)))
                sys.stdout.write("v " + ' '.join(sol) + ' 0\n')
            sys.stdout.write("s SATISFIABLE\n")
        else:
            sys.stdout.write("s UNKNOWN\n")

    # make clauses sets
    clauses = preprocessing.clause_dupl_elim(clauses)

    if args.no_preprocessing:
        factored_instances = [(num_vars, clauses, [],
                               {v: v
                                for v in range(1, num_vars + 1)})]
    else:
        # pre-process instance and generate factored instances
        factored_instances = preprocessing.factored_instances(
            num_vars, clauses, min(args.f, num_vars))

    # filter unsatisfiable factored instances (detected during preprocessing)
    factored_instances = filter(lambda i: i is not False, factored_instances)

    # find instances solved completely during preprocessing
    solved_instances = filter(lambda i: len(i[1]) == 0, factored_instances)

    # initialize Python random with seed (not used during computation, only for choosing next configuration)
    random.seed(args.seed)

    if len(factored_instances) == 0:
        # if all factored instances are unsatisfiable, then the instance is unsatisfiable
        dprint("Proved unsatisfiable during preprocessing.")
        print_solution(False)
        exit()
    elif len(solved_instances) > 0:
        dprint("Solved during preprocessing.")
        print_solution((solved_instances[0], []))
        exit()

    if args.algo == 'genetic':
        dprint("Selected genetic algorithm.")
        algo = ga.GeneticAlgorithm
    elif args.algo == 'ant':
        dprint("Selected ant colony optimization.")
        algo = aa.AntColonyAlgorithm
    elif args.algo == 'walksat':
        dprint("Selected WalkSAT.")
        algo = ws.WalkSAT
    else:
        print("Argument error: No such algorithm.")

    dprint("Reduced instances:")

    for i in factored_instances:
        dprint(i, " / ", num_vars, "original variables")

    processes = []
    queue = Queue()
    MAX_ITERATIONS = args.max_iterations

    if not args.N:
        args.N = multiprocessing.cpu_count()

    # run profiles
    dprint("Run {} processes (N = {}, {} factored instances)".format(
        max(args.N, len(factored_instances)), args.N, len(factored_instances)))

    # no spawning if only one task
    if max(args.N, len(factored_instances)) == 1:
        instance = factored_instances[0]
        profile = algo.profiles[0]
        config = {
            'VERBOSE': VERBOSE,
            'SEED': args.seed,
            'MAX_ITERATIONS': MAX_ITERATIONS
        }
        config.update(profile)
        dprint("Start", config)
        dprint("Start on same process with config", config)

        a = algo(instance[0], instance[1], config)

        solution = a.run()
        dprint("Solution", str(solution))
        print_solution((instance, solution))
        return

    # run algorithm for every factored instance with every profile
    for instance in factored_instances:
        profile = algo.profiles[0]
        start_process(algo, instance, profile, args.seed, MAX_ITERATIONS,
                      queue, VERBOSE, processes)

    for i in range(args.N - len(factored_instances)):
        profile = random.choice(algo.profiles)
        instance = random.choice(factored_instances)
        start_process(algo, instance, profile, random.randint(1, 10000),
                      MAX_ITERATIONS, queue, VERBOSE, processes)

    # wait for the first process to finish with a solution
    while True:
        solution = queue.get()

        for instance in factored_instances:
            MAX_ITERATIONS = int(MAX_ITERATIONS * 1.75)

            if solution[1] is not None:
                print_solution(solution)

                for process in processes:
                    process.terminate()

                exit()

            else:
                # start another thread with more iterations and different configuration
                profile = random.choice(algo.profiles)
                start_process(algo, instance, profile,
                              random.randint(1, 10000), MAX_ITERATIONS, queue,
                              VERBOSE, processes)
示例#15
0
 def test_knf(self):
     parser.parse(['p knf 20 0'])
示例#16
0
 def test_parse_simple_file(self):
     parsed = parser.parse(
         ['c foo bar', 'p cnf 20 2', '10 12 15 5 3 0', '6 13 14 -11 20 0'])
     assert_equal(parsed, (20, [[10, 12, 15, 5, 3], [6, 13, 14, -11, 20]]))
示例#17
0
def run():
    algo = ga.GeneticAlgorithm
    fi = open('instances/random_ksat13.dimacs')
    num_vars, clauses = parser.parse(fi)

    conf = {
        'COLLECT_STATS': True,
        'MAX_ITERATIONS': 1500,
        'MUTATION_RATE': 0.5,
        'CATASTROPHE_THRESHOLD': 2.3,
        'CATASTROPHES': True
    }

    a = algo(num_vars, clauses, conf)
    a.run()

    # subplots where the first one is twice as large
    ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
    ax2 = plt.subplot2grid((3, 1), (2, 0))

    xs = []
    ys = []
    for k, d in enumerate(a.progress):
        for v in d['values']:
            xs.append(k)
            ys.append(v)

    import matplotlib.cm as cm
    from matplotlib.colors import LogNorm
    x_range = max(xs) - min(xs)
    y_range = max(ys) - min(ys)

    #ax1.scatter(xs, ys, s=10, alpha=0.15)

    #'''
    h = ax1.hist2d(xs, ys, bins=(x_range, y_range), norm=LogNorm())
    #f.subplots_adjust(right=0.8)
    #f.colorbar(h[3], ax=ax1)
    #'''
    '''
    ax1.hexbin(xs, ys, gridsize=(x_range, y_range), cmap=cm.jet, bins=None)
    ax1.axis([min(xs), max(xs), min(ys), max(ys)])

    #cb = plt.colorbar()
    #cb.set_label('Number of clauses')
    #'''

    data = np.array([x['best'] for x in a.progress])
    xs = range(len(data))
    ys = data
    #ys = np.abs(data - len(clauses))
    #plt.ylim([0, np.max(data)])
    plt.xlim([0, len(data)])
    #plt.semilogy(xs, ys, 'bo-', label='Performance')
    ax1.plot(xs, ys, 'ro-', label='best performance')

    # average
    data = np.array([x['values'] for x in a.progress])
    xs = range(len(data))
    ys = np.average(data, axis=1)
    plt.xlim([0, len(data)])
    ax1.plot(xs, ys, 'go-', label='average performance')

    # std
    data = np.array([x['values'] for x in a.progress])
    xs = range(len(data))
    ys = np.std(data, axis=1)
    ax2.plot(xs, ys, 'b-', label='standard deviation')

    # mr
    data = np.array([x['mr'] for x in a.progress])
    xs = range(len(data))
    ys = data
    #ax3.plot(xs, ys, 'g-', label='Mutation rate')

    # Styling

    # no space between plots
    plt.subplots_adjust(hspace=0)
    ax1.set_xticklabels([])

    plt.xlabel('iteration')
    ax1.set_ylabel('# of satisfied clauses')
    ax2.set_ylabel('standard deviation')
    ax1.grid(True, which="both", linestyle="dotted")
    ax1.legend(loc='lower right')
    #ax2.legend(loc='lower right')

    #plt.set_size_inches(12, 8)

    from matplotlib.backends.backend_pdf import PdfPages
    pp = PdfPages('plots/ga_performance_ksat13.pdf')
    plt.savefig(pp, format='pdf')
    pp.close()

    print "Plotted"
    plt.show()
示例#18
0
文件: main.py 项目: domoritz/SoSAT
def main():
    global VERBOSE

    # entry point for the program
    np.set_printoptions(linewidth=2000000000)

    clp = argparse.ArgumentParser(description='SAT solver.')
    clp.add_argument('-a', '--algorithm', dest='algo',
                     default='genetic', choices=['genetic', 'ant', 'walksat'],
                     help='select algorithm for solving')
    clp.add_argument('-v', '--verbose', dest='verbose',
                     default=False, action='store_true',
                     help='print stats and status message')
    clp.add_argument('-s', '--seed', dest='seed', type=int,
                     default=42,
                     help='seed for random number generator, 0 means random')
    clp.add_argument('-N', '--number', dest='N',
                     default=None, type=int,
                     help='number of worker processes')
    clp.add_argument('-i', '--max_iterations', dest='max_iterations',
                     default=25000, type=int,
                     help='number of worker processes')
    clp.add_argument('-f', '--factor', dest='f',
                     default=0, type=int,
                     help='number of factored (most-constrained) variables')
    clp.add_argument('-c', '--cse573', dest='cse573',
                     default=False, action='store_true',
                     help='use format for CSE 573')
    clp.add_argument('-np', '--no-preprocessing', dest='no_preprocessing',
                     default=False, action='store_true',
                     help='disable factorization and unit propagation')
    clp.add_argument('infile', nargs='?', type=argparse.FileType('r'),
                     default=sys.stdin)

    args = clp.parse_args()
    VERBOSE = args.verbose

    # parse input file
    if args.cse573:
        num_vars, clauses, mapping = parser.parse_cse573(args.infile)
    else:
        num_vars, clauses = parser.parse(args.infile)

    if args.seed == 0:
        args.seed = random.randint(0, 1e9)

    def print_solution(instance_solution):
        """
        Print the solution. To retrieve the correct solution, preprocessing steps have to be undone.
        """
        if instance_solution is False:
            # unsatisfiability can only be determined during preprocessing
            sys.stdout.write("s UNSATISFIABLE\n")
        elif instance_solution[1] is not None:
            instance, p_solution = instance_solution
            solution = preprocessing.restore_original_solution(instance, p_solution, num_vars)
            sol = []
            if args.cse573:
                for i, lit in enumerate(solution):
                    orig_lit = i + 1 if lit else -(i + 1)
                    neg = orig_lit < 0
                    sol.append(('!' if neg else '') + mapping[abs(orig_lit)])
                sys.stdout.write("v " + ' '.join(sol) + '\n')
            else:
                for i, lit in enumerate(solution):
                    sol.append(str(i + 1 if lit else -(i + 1)))
                sys.stdout.write("v " + ' '.join(sol) + ' 0\n')
            sys.stdout.write("s SATISFIABLE\n")
        else:
            sys.stdout.write("s UNKNOWN\n")

    # make clauses sets
    clauses = preprocessing.clause_dupl_elim(clauses)

    if args.no_preprocessing:
        factored_instances = [(num_vars, clauses, [], {v: v for v in range(1, num_vars + 1)})]
    else:
        # pre-process instance and generate factored instances
        factored_instances = preprocessing.factored_instances(num_vars, clauses, min(args.f, num_vars))

    # filter unsatisfiable factored instances (detected during preprocessing)
    factored_instances = filter(lambda i: i is not False, factored_instances)

    # find instances solved completely during preprocessing
    solved_instances = filter(lambda i: len(i[1]) == 0, factored_instances)

    # initialize Python random with seed (not used during computation, only for choosing next configuration)
    random.seed(args.seed)

    if len(factored_instances) == 0:
        # if all factored instances are unsatisfiable, then the instance is unsatisfiable
        dprint("Proved unsatisfiable during preprocessing.")
        print_solution(False)
        exit()
    elif len(solved_instances) > 0:
        dprint("Solved during preprocessing.")
        print_solution((solved_instances[0], []))
        exit()

    if args.algo == 'genetic':
        dprint("Selected genetic algorithm.")
        algo = ga.GeneticAlgorithm
    elif args.algo == 'ant':
        dprint("Selected ant colony optimization.")
        algo = aa.AntColonyAlgorithm
    elif args.algo == 'walksat':
        dprint("Selected WalkSAT.")
        algo = ws.WalkSAT
    else:
        print("Argument error: No such algorithm.")

    dprint("Reduced instances:")

    for i in factored_instances:
        dprint(i, " / ", num_vars, "original variables")

    processes = []
    queue = Queue()
    MAX_ITERATIONS = args.max_iterations

    if not args.N:
        args.N = multiprocessing.cpu_count()

    # run profiles
    dprint("Run {} processes (N = {}, {} factored instances)".format(max(args.N, len(factored_instances)), args.N, len(factored_instances)))

    # no spawning if only one task
    if max(args.N, len(factored_instances)) == 1:
        instance = factored_instances[0]
        profile = algo.profiles[0]
        config = {
            'VERBOSE': VERBOSE,
            'SEED': args.seed,
            'MAX_ITERATIONS': MAX_ITERATIONS
        }
        config.update(profile)
        dprint("Start", config)
        dprint("Start on same process with config", config)

        a = algo(instance[0], instance[1], config)

        solution = a.run()
        dprint("Solution", str(solution))
        print_solution((instance, solution))
        return

    # run algorithm for every factored instance with every profile
    for instance in factored_instances:
        profile = algo.profiles[0]
        start_process(algo, instance, profile, args.seed, MAX_ITERATIONS, queue, VERBOSE, processes)

    for i in range(args.N - len(factored_instances)):
        profile = random.choice(algo.profiles)
        instance = random.choice(factored_instances)
        start_process(algo, instance, profile, random.randint(1, 10000), MAX_ITERATIONS, queue, VERBOSE, processes)

    # wait for the first process to finish with a solution
    while True:
        solution = queue.get()

        for instance in factored_instances:
            MAX_ITERATIONS = int(MAX_ITERATIONS * 1.75)

            if solution[1] is not None:
                print_solution(solution)

                for process in processes:
                    process.terminate()

                exit()

            else:
                # start another thread with more iterations and different configuration
                profile = random.choice(algo.profiles)
                start_process(algo, instance, profile, random.randint(1, 10000), MAX_ITERATIONS, queue, VERBOSE, processes)
示例#19
0
文件: plot_ga.py 项目: domoritz/SoSAT
def run():
    algo = ga.GeneticAlgorithm
    fi = open('instances/random_ksat13.dimacs')
    num_vars, clauses = parser.parse(fi)

    conf = {
        'COLLECT_STATS': True,
        'MAX_ITERATIONS': 1500,
        'MUTATION_RATE': 0.5,
        'CATASTROPHE_THRESHOLD': 2.3,
        'CATASTROPHES': True
    }

    a = algo(num_vars, clauses, conf)
    a.run()

    # subplots where the first one is twice as large
    ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
    ax2 = plt.subplot2grid((3, 1), (2, 0))

    xs = []
    ys = []
    for k, d in enumerate(a.progress):
        for v in d['values']:
            xs.append(k)
            ys.append(v)

    import matplotlib.cm as cm
    from matplotlib.colors import LogNorm
    x_range = max(xs) - min(xs)
    y_range = max(ys) - min(ys)

    #ax1.scatter(xs, ys, s=10, alpha=0.15)

    #'''
    h = ax1.hist2d(xs, ys, bins=(x_range, y_range), norm=LogNorm())
    #f.subplots_adjust(right=0.8)
    #f.colorbar(h[3], ax=ax1)
    #'''
    '''
    ax1.hexbin(xs, ys, gridsize=(x_range, y_range), cmap=cm.jet, bins=None)
    ax1.axis([min(xs), max(xs), min(ys), max(ys)])

    #cb = plt.colorbar()
    #cb.set_label('Number of clauses')
    #'''

    data = np.array([x['best'] for x in a.progress])
    xs = range(len(data))
    ys = data
    #ys = np.abs(data - len(clauses))
    #plt.ylim([0, np.max(data)])
    plt.xlim([0, len(data)])
    #plt.semilogy(xs, ys, 'bo-', label='Performance')
    ax1.plot(xs, ys, 'ro-', label='best performance')

    # average
    data = np.array([x['values'] for x in a.progress])
    xs = range(len(data))
    ys = np.average(data, axis=1)
    plt.xlim([0, len(data)])
    ax1.plot(xs, ys, 'go-', label='average performance')

    # std
    data = np.array([x['values'] for x in a.progress])
    xs = range(len(data))
    ys = np.std(data, axis=1)
    ax2.plot(xs, ys, 'b-', label='standard deviation')

    # mr
    data = np.array([x['mr'] for x in a.progress])
    xs = range(len(data))
    ys = data
    #ax3.plot(xs, ys, 'g-', label='Mutation rate')

    # Styling

    # no space between plots
    plt.subplots_adjust(hspace=0)
    ax1.set_xticklabels([])

    plt.xlabel('iteration')
    ax1.set_ylabel('# of satisfied clauses')
    ax2.set_ylabel('standard deviation')
    ax1.grid(True, which="both", linestyle="dotted")
    ax1.legend(loc='lower right')
    #ax2.legend(loc='lower right')

    #plt.set_size_inches(12, 8)

    from matplotlib.backends.backend_pdf import PdfPages
    pp = PdfPages('plots/ga_performance_ksat13.pdf')
    plt.savefig(pp, format='pdf')
    pp.close()

    print "Plotted"
    plt.show()