satisfiability based on wmis sample majority vote
This commit is contained in:
13
create_wmis_result_table.sql
Normal file
13
create_wmis_result_table.sql
Normal 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)
|
||||||
|
);
|
@@ -7,8 +7,10 @@ import dimod
|
|||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
#wmis_results()
|
||||||
wmis_siman_results_alpha_num_of_assignments()
|
wmis_siman_results_alpha_num_of_assignments()
|
||||||
#wmis_siman_results()
|
#wmis_siman_results()
|
||||||
|
#minisat_runs()
|
||||||
|
|
||||||
def wmis_siman_results():
|
def wmis_siman_results():
|
||||||
db = script.connect_to_instance_pool()
|
db = script.connect_to_instance_pool()
|
||||||
@@ -32,17 +34,19 @@ def wmis_siman_results_alpha_num_of_assignments():
|
|||||||
idb = script.connect_to_instance_pool()
|
idb = script.connect_to_instance_pool()
|
||||||
|
|
||||||
q = queries.WMIS_result_scope_query_raw(idb)
|
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, "
|
"(result_id, "
|
||||||
|
" instance_id, "
|
||||||
" number_of_clauses, "
|
" number_of_clauses, "
|
||||||
" number_of_variables, "
|
" number_of_variables, "
|
||||||
" number_of_found_assignments, "
|
" number_of_found_assignments, "
|
||||||
" chain_break_fraction, "
|
" chain_break_fraction, "
|
||||||
" num_occurrences, "
|
" num_occurrences, "
|
||||||
" energy) "
|
" energy, "
|
||||||
"VALUES (%s, %s, %s, %s, %s, %s, %s) ")
|
" satisfiable) "
|
||||||
|
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ")
|
||||||
|
|
||||||
for result in tqdm(q):
|
for result in tqdm(q):
|
||||||
sample_set = queries.read_raw_wmis_sample_set(result["data"])
|
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"])
|
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"]),
|
edb_cursor.execute(insert_row, (str(result["_id"]),
|
||||||
|
str(result["instance"]),
|
||||||
int(sat.getNumberOfClauses()),
|
int(sat.getNumberOfClauses()),
|
||||||
int(sat.getNumberOfVariables()),
|
int(sat.getNumberOfVariables()),
|
||||||
int(data["number_of_assignments"]),
|
int(data["number_of_assignments"]),
|
||||||
float(data["chain_break_fraction"]),
|
float(data["chain_break_fraction"]),
|
||||||
int(data["num_occurrences"]),
|
int(data["num_occurrences"]),
|
||||||
int(data["energy"])))
|
int(data["energy"]),
|
||||||
|
isSatisfiable))
|
||||||
|
|
||||||
edb.commit()
|
edb.commit()
|
||||||
edb_cursor.close()
|
edb_cursor.close()
|
||||||
edb.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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
@@ -65,7 +65,8 @@ def __wmis3():
|
|||||||
solver_input_query = queries.WMIS_solver_input_scope_query(db)
|
solver_input_query = queries.WMIS_solver_input_scope_query(db)
|
||||||
solver_input_query.query("c42_v[5-84]_1", target_graph_id)
|
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,
|
chimera_sampler = dimod.StructureComposite(base_sampler,
|
||||||
target_graph.nodes(),
|
target_graph.nodes(),
|
||||||
target_graph.edges())
|
target_graph.edges())
|
||||||
@@ -73,14 +74,15 @@ def __wmis3():
|
|||||||
for solver_input in tqdm(solver_input_query):
|
for solver_input in tqdm(solver_input_query):
|
||||||
sampler = FixedEmbeddingComposite(chimera_sampler, solver_input["embeddings"][0])
|
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,
|
res,
|
||||||
solver_input,
|
solver_input,
|
||||||
0)
|
0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __wmis():
|
def __wmis():
|
||||||
sat = rand_sat.generateRandomKSAT(25, 6, 3)
|
sat = rand_sat.generateRandomKSAT(25, 6, 3)
|
||||||
qubo = __negate_qubo(SAT2QUBO.WMISdictQUBO(sat))
|
qubo = __negate_qubo(SAT2QUBO.WMISdictQUBO(sat))
|
||||||
|
@@ -296,6 +296,46 @@ class WMIS_result_scope_query_raw:
|
|||||||
def __next__(self):
|
def __next__(self):
|
||||||
return self.__query.next()
|
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):
|
def load_embedding(collection, qubo_id, solver_graph_id):
|
||||||
doc = collection.find_one(
|
doc = collection.find_one(
|
||||||
{
|
{
|
||||||
|
@@ -300,3 +300,39 @@ def analyze_wmis_sample(sample):
|
|||||||
data["energy"] = sample.energy
|
data["energy"] = sample.energy
|
||||||
|
|
||||||
return data
|
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)
|
||||||
|
Reference in New Issue
Block a user