From a5cc2cad133acc30cf5e042ad6848cf20935c78a Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 2 Apr 2019 16:55:34 +0200 Subject: [PATCH] data extraction --- test_data_extraction.py | 60 +++++++++++++++++++++++++++++++++++ test_sat_to_qubo_workflow.py | 2 +- util/queries.py | 61 ++++++++++++++++++++++++++++++++++++ util/script.py | 12 +++++-- 4 files changed, 131 insertions(+), 4 deletions(-) create mode 100755 test_data_extraction.py diff --git a/test_data_extraction.py b/test_data_extraction.py new file mode 100755 index 0000000..5a9b30a --- /dev/null +++ b/test_data_extraction.py @@ -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() diff --git a/test_sat_to_qubo_workflow.py b/test_sat_to_qubo_workflow.py index 972e4b9..d837338 100755 --- a/test_sat_to_qubo_workflow.py +++ b/test_sat_to_qubo_workflow.py @@ -78,7 +78,7 @@ def __wmis3(): script.save_simulated_annealing_result(db["wmis_siman_results"], res, solver_input, - 0) + 0) def __wmis(): diff --git a/util/queries.py b/util/queries.py index 4e4fa75..d4577c2 100644 --- a/util/queries.py +++ b/util/queries.py @@ -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 + diff --git a/util/script.py b/util/script.py index 78dad0e..796647e 100644 --- a/util/script.py +++ b/util/script.py @@ -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