diff --git a/create_wmis_result_table.sql b/create_wmis_result_table.sql new file mode 100644 index 0000000..cf332c4 --- /dev/null +++ b/create_wmis_result_table.sql @@ -0,0 +1,13 @@ +create table c42_v5to84_1_wmis_qbsolv_results ( + result_id char(24), + instance_id char(24), + number_of_clauses int, + number_of_variables int, + number_of_found_assignments int, + chain_break_fraction float, + num_occurrences int, + energy float, + satisfiable boolean, + + primary key (result_id) +); \ No newline at end of file diff --git a/test_data_extraction.py b/test_data_extraction.py index 778571d..9260edd 100755 --- a/test_data_extraction.py +++ b/test_data_extraction.py @@ -7,8 +7,10 @@ import dimod from tqdm import tqdm def main(): + #wmis_results() wmis_siman_results_alpha_num_of_assignments() #wmis_siman_results() + #minisat_runs() def wmis_siman_results(): db = script.connect_to_instance_pool() @@ -32,17 +34,19 @@ def wmis_siman_results_alpha_num_of_assignments(): idb = script.connect_to_instance_pool() q = queries.WMIS_result_scope_query_raw(idb) - q.query("c42_v[5-84]_1", "wmis_siman_results") + q.query("c42_v[5-84]_1", "wmis_qbsolv_results") - insert_row = ("INSERT INTO c42_v5to84_1_wmis_results " + insert_row = ("INSERT INTO c42_v5to84_1_wmis_qbsolv_results " "(result_id, " + " instance_id, " " number_of_clauses, " " number_of_variables, " " number_of_found_assignments, " " chain_break_fraction, " " num_occurrences, " - " energy) " - "VALUES (%s, %s, %s, %s, %s, %s, %s) ") + " energy, " + " satisfiable) " + "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ") for result in tqdm(q): sample_set = queries.read_raw_wmis_sample_set(result["data"]) @@ -51,17 +55,77 @@ def wmis_siman_results_alpha_num_of_assignments(): sat = queries.get_instance_by_id(idb["instances"], result["instance"]) + model = script.majority_vote_sample(sample_set.first.sample) + + isSatisfiable = sat.checkAssignment(model) + edb_cursor.execute(insert_row, (str(result["_id"]), + str(result["instance"]), int(sat.getNumberOfClauses()), int(sat.getNumberOfVariables()), int(data["number_of_assignments"]), float(data["chain_break_fraction"]), int(data["num_occurrences"]), - int(data["energy"]))) + int(data["energy"]), + isSatisfiable)) edb.commit() edb_cursor.close() edb.close() +def minisat_runs(): + edb = script.connect_to_experimetns_db() + edb_cursor = edb.cursor(); + + idb = script.connect_to_instance_pool() + + runs = queries.Minisat_run_scope_query_raw(idb) + runs.query("c42_v[5-84]_1", "minisat_runs") + + insert_row = ("INSERT INTO c42_v5to84_1_minisat_runs " + "(run_id, " + " instance_id, " + " satisfiable) " + "VALUES (%s, %s, %s) ") + + for run in tqdm(runs): + data = script.analyde_minisat_run(run) + + edb_cursor.execute(insert_row, (str(run["_id"]), + str(run["instance"]), + int(data["satisfiable"]))) + + edb.commit() + edb_cursor.close() + edb.close() + +def wmis_results(): + edb = script.connect_to_experimetns_db() + edb_cursor = edb.cursor(); + + idb = script.connect_to_instance_pool() + + q = queries.WMIS_result_scope_query_raw(idb) + q.query("c42_v[5-84]_1", "wmis_qbsolv_results") + + for i in range(501): + q.__next__() + + res = q.__next__() + + sample_set = queries.read_raw_wmis_sample_set(res["data"]) + + + model = script.majority_vote_sample(sample_set.first.sample) + + sat = queries.get_instance_by_id(idb["instances"], res["instance"]) + + print(model) + + print(sat.getNumberOfVariables()) + print(sat.checkAssignment(model)) + + + if __name__ == "__main__": main() diff --git a/test_sat_to_qubo_workflow.py b/test_sat_to_qubo_workflow.py index dfd3ba9..991789f 100755 --- a/test_sat_to_qubo_workflow.py +++ b/test_sat_to_qubo_workflow.py @@ -65,7 +65,8 @@ def __wmis3(): solver_input_query = queries.WMIS_solver_input_scope_query(db) solver_input_query.query("c42_v[5-84]_1", target_graph_id) - base_sampler = SimulatedAnnealingSampler() + #base_sampler = SimulatedAnnealingSampler() + base_sampler = QBSolv() chimera_sampler = dimod.StructureComposite(base_sampler, target_graph.nodes(), target_graph.edges()) @@ -73,12 +74,13 @@ def __wmis3(): for solver_input in tqdm(solver_input_query): sampler = FixedEmbeddingComposite(chimera_sampler, solver_input["embeddings"][0]) - res = sampler.sample_qubo(__negate_qubo(solver_input["qubo"])) + res = sampler.sample_qubo(__negate_qubo(solver_input["qubo"]), solver_limit=2048) - script.save_simulated_annealing_result(db["wmis_siman_results"], + script.save_simulated_annealing_result(db["wmis_qbsolv_results"], res, solver_input, 0) + def __wmis(): diff --git a/util/queries.py b/util/queries.py index ce97675..8401a74 100644 --- a/util/queries.py +++ b/util/queries.py @@ -295,6 +295,46 @@ class WMIS_result_scope_query_raw: def __next__(self): return self.__query.next() + +class Minisat_run_scope_query_raw: + def __init__(self, database): + self.__database = database + self.__query = None + + def query(self, scope, collection): + self.__query = self.__database["experiment_scopes"].aggregate([ + { + "$match": {"_id": scope} + }, + { + "$unwind": "$instances" + }, + { + "$project": {"instance_id": "$instances"} + }, + { + "$lookup": + { + "from": collection, + "localField": "instance_id", + "foreignField": "instance", + "as": "run" + } + }, + { + "$unwind": "$run" + }, + { + "$replaceRoot": {"newRoot": "$run"} + } + ]) + + def __iter__(self): + return self + + def __next__(self): + return self.__query.next() + def load_embedding(collection, qubo_id, solver_graph_id): doc = collection.find_one( diff --git a/util/script.py b/util/script.py index c69bba5..7c70293 100644 --- a/util/script.py +++ b/util/script.py @@ -300,3 +300,39 @@ def analyze_wmis_sample(sample): data["energy"] = sample.energy return data + +def analyde_minisat_run(run_document): + data = {} + + data["satisfiable"] = run_document["satisfiable"] + + return data + +def majority_vote_sample(sample): + assignments = {} + + for coupler, energy in sample.items(): + + var = abs(coupler[1]) + + if var not in assignments: + assignments[var] = {"all": []} + + if energy == 1: + assignments[var]["all"].append(1 if coupler[1] > 0 else 0) + + for var, a in assignments.items(): + assignments[var]["majority"] = 1 if __true_percentage(a["all"]) >= 0.5 else 0 + + assignment = [0 for i in range(len(assignments))] + + for var, a in assignments.items(): + assignment[var - 1] = a["majority"] + + return assignment + +def __true_percentage(a): + if len(a) == 0: + return 0 + + return np.count_nonzero(a) / len(a)