satisfiability based on wmis sample majority vote
This commit is contained in:
@@ -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(
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user