コード例 #1
0
    def test_read_instance(self):
        test_file_path = './sat_test_data/test.cnf'

        cnf = SatUtils.read_instance(test_file_path)
        self.assertEqual(len(cnf), 2)
        self.assertEqual(len(cnf[0]), 6)
        self.assertEqual(len(cnf[1]), 4)
コード例 #2
0
    def main(self,
             max_restarts=10,
             max_iterations=1000,
             instance_path="./sat_data/test.cnf"):
        instance = SatUtils.read_instance(instance_path)
        var_count = len(instance[0])

        start = time.time()
        end = None

        for restart in range(max_restarts):
            best_solution = SatUtils.initialize_variables(var_count)

            for iteration in range(max_iterations):
                solution_status, no_of_unsat_clauses = SatUtils.solution_status(
                    instance, best_solution)

                # if solution has been found terminate the search
                if solution_status is True:
                    end = time.time()

                    print("Iteration,{0},Restart,{1},Duration,{2}".format(
                        iteration, restart, end - start))
                    return
                best_solution = self.get_best_var_to_flip(
                    instance, best_solution, no_of_unsat_clauses)

            # resetting tabu list in between the restarts
            GsatSolver.tabu = []
コード例 #3
0
    def main(self, wp, p, max_iterations, instance_path):
        cnf_contents = SatUtils.read_instance(instance_path)
        # instantiate required search and util objects
        novelty_search = NoveltySearch(cnf_contents, p, 1)
        variables = cnf_contents[0]
        walkSat = WalkSat()

        # initialize first solution proposal
        proposed_solution = SatUtils.initialize_variables(len(variables))

        # start the timer
        start = time.time()
        end = None

        for i in range(max_iterations):
            # checking for timeout terminate condition
            if time.time() > start + 60:
                return
            solution_found, unsat_clause, unsat_clause_list = SatUtils.solution_status_with_unsat_clauses(
                cnf_contents, proposed_solution)
            # if a solution has been identified break out of the search loop and record it
            if solution_found is True:
                end = time.time()
                print("Iteration,{0},Duration,{1}".format(i, end - start))
                return

            # pick algorithm to run the solution search based on probability
            if wp < random.uniform(0, 1):
                proposed_solution = walkSat.execute_walk(
                    cnf_contents, proposed_solution)
            else:
                random_variable_to_flip = random.choice(variables)
                random_unsat_clause = random.choice(unsat_clause_list)

                best_flip = novelty_search.execute_search(
                    proposed_solution, random_variable_to_flip,
                    random_unsat_clause)

                proposed_solution[best_flip] = SatUtils.flip_var(
                    proposed_solution[best_flip])
コード例 #4
0
                                                flip_scores,
                                                key=flip_scores.get)

            best = two_smallest_keys[0]
            second_best = two_smallest_keys[1]

            # generate noise value
            noise_value = random.uniform(0, 1)

            # compare noise value generated to the probability of second best variable being selected
            if noise_value < self.probability:
                return second_best
            else:
                return best


if __name__ == '__main__':
    instance_path = None

    # check whether a path to input dataset has been provided or should the default path be used
    if len(sys.argv) > 1:
        instance_path = sys.argv[1]
    else:
        instance_path = "./sat_data/uf20-020.cnf"

    cnf_contents = SatUtils.read_instance(instance_path)
    solver = NoveltySearch(cnf_contents, 0.4, 100000)

    for i in range(1):
        solver.main()