Browse Source

data extraction

master
Tom 5 years ago
parent
commit
a5cc2cad13
4 changed files with 131 additions and 4 deletions
  1. +60
    -0
      test_data_extraction.py
  2. +1
    -1
      test_sat_to_qubo_workflow.py
  3. +61
    -0
      util/queries.py
  4. +9
    -3
      util/script.py

+ 60
- 0
test_data_extraction.py View File

@ -0,0 +1,60 @@
#!/usr/bin/env python3
import util.script as script
import util.queries as queries
import dimod
def main():
wmis_siman_results_alpha_num_of_assignments()
def wmis_siman_results():
db = script.connect_to_instance_pool()
q = queries.WMIS_result_scope_query_raw(db)
q.query("c50_v[5, 50]_1", "wmis_siman_results")
for i in range(30):
result = q.__next__()
sample_set = queries.read_raw_wmis_sample_set(result["data"])
data = script.analyze_wmis_sample(sample_set.first)
print(data)
def wmis_siman_results_alpha_num_of_assignments():
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("c50_v[5, 50]_1", "wmis_siman_results")
insert_row = ("INSERT INTO c50_v5to50_1_wmis_alpha_number_of_assignments "
"(result_id, "
" number_of_clauses, "
" number_of_variables, "
" number_of_found_assignments) "
"VALUES (%s, %s, %s, %s) ")
for result in q:
sample_set = queries.read_raw_wmis_sample_set(result["data"])
data = script.analyze_wmis_sample(sample_set.first)
sat = queries.get_instance_by_id(idb["instances"], result["instance"])
edb_cursor.execute(insert_row, (str(result["_id"]),
int(sat.getNumberOfClauses()),
int(sat.getNumberOfVariables()),
int(data["number_of_assignments"])))
print(data)
edb.commit()
edb_cursor.close()
edb.close()
if __name__ == "__main__":
main()

+ 1
- 1
test_sat_to_qubo_workflow.py View File

@ -78,7 +78,7 @@ def __wmis3():
script.save_simulated_annealing_result(db["wmis_siman_results"],
res,
solver_input,
0)
0)
def __wmis():


+ 61
- 0
util/queries.py View File

@ -1,5 +1,6 @@
from .kSAT import kSAT
import bson
import dimod
class Instance_scope_query:
@ -195,6 +196,45 @@ class WMIS_solver_input_scope_query (WMIS_solver_input_scope_query_raw):
data["embeddings"].append(read_raw_embedding(raw_emb))
return data
class WMIS_result_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": "result"
}
},
{
"$unwind": "$result"
},
{
"$replaceRoot": {"newRoot": "$result"}
}
])
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(
@ -262,3 +302,24 @@ def read_raw_embedding(raw_embedding):
emb[tuple(entry[0])] = entry[1]
return emb
def read_raw_wmis_sample_set(raw_sample_set):
sample_set_data = raw_sample_set.copy()
sample_set_data["variable_labels"] = []
for label in raw_sample_set["variable_labels"]:
sample_set_data["variable_labels"].append(tuple(label))
return dimod.SampleSet.from_serializable(sample_set_data)
def get_instance_by_id(collection, id):
doc = collection.find_one({"_id": bson.ObjectId(id)})
sat = kSAT()
for clause in doc["clauses"]:
sat.addClause(clause);
return sat

+ 9
- 3
util/script.py View File

@ -9,7 +9,7 @@ from . import queries
from . import graph
import minorminer
from tqdm import tqdm
import numpy as np
def readConfig(configFilePath):
config = configparser.ConfigParser()
@ -104,7 +104,7 @@ def getDBContext(dbConfigPath):
return dbContext
def connect_to_instance_pool(dbConfigPath):
def connect_to_instance_pool(dbConfigPath = "database.config"):
dbConf = readConfig(dbConfigPath)
client = pymongo.MongoClient(
@ -119,7 +119,7 @@ def connect_to_instance_pool(dbConfigPath):
return client[dbConf["INSTANCE_POOL"]["database"]]
def connect_to_experimetns_db(dbConfigPath):
def connect_to_experimetns_db(dbConfigPath = "database.config"):
dbConfig = readConfig(dbConfigPath)
return mysql.connector.connect(
@ -267,3 +267,9 @@ def save_simulated_annealing_result(collection, result, solver_input, emb_list_i
collection.insert_one(doc)
def analyze_wmis_sample(sample):
data = {}
data["number_of_assignments"] = np.count_nonzero(list(sample.sample.values()))
return data

Loading…
Cancel
Save