예제 #1
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 = []
예제 #2
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])
예제 #3
0
    def main(self):
        proposed_solution = SatUtils.initialize_variables(self.variables)

        start = time.time()
        end = None
        for i in range(self.max_iterations):
            solution_found, unsat_clause, unsat_clause_list = SatUtils.solution_status_with_unsat_clauses(
                self.instance, proposed_solution)

            if solution_found is True:
                end = time.time()
                print("Iteration,{0},Duration,{1}".format(i, end - start))
                return

            random_flip = random.choice(self.variables)
            random_clause = random.choice(unsat_clause_list)

            flip_this_index = self.execute_search(proposed_solution,
                                                  random_flip, random_clause)

            proposed_solution[flip_this_index] = SatUtils.flip_var(
                proposed_solution[flip_this_index])
    def test_initializing_variables(self):
        result = SatUtils.initialize_variables(10)

        print(result)

        self.assertEqual(len(result), 10)