Browse Source

satisfiability based on wmis sample majority vote

master
Tom 5 years ago
parent
commit
25e201177f
5 changed files with 163 additions and 8 deletions
  1. +13
    -0
      create_wmis_result_table.sql
  2. +69
    -5
      test_data_extraction.py
  3. +5
    -3
      test_sat_to_qubo_workflow.py
  4. +40
    -0
      util/queries.py
  5. +36
    -0
      util/script.py

+ 13
- 0
create_wmis_result_table.sql View File

@ -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)
);

+ 69
- 5
test_data_extraction.py View File

@ -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()

+ 5
- 3
test_sat_to_qubo_workflow.py View File

@ -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():


+ 40
- 0
util/queries.py View File

@ -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(


+ 36
- 0
util/script.py View File

@ -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)

Loading…
Cancel
Save