From 348c733656217719dc8948fc999bb7d7078e0c36 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 11 Apr 2019 12:20:45 +0200 Subject: [PATCH] instance creation following a logistic distribution/ inverted qubo weights in SAT2QUBO --- test_data_extraction.py | 28 ++++++++++- util/SAT2QUBO.py | 4 +- util/random_instance_pool.py | 93 +++++++++++++++++++++++++++++++++++- util/script.py | 24 ++++++++-- 4 files changed, 141 insertions(+), 8 deletions(-) diff --git a/test_data_extraction.py b/test_data_extraction.py index 9260edd..d91b9e1 100755 --- a/test_data_extraction.py +++ b/test_data_extraction.py @@ -7,10 +7,36 @@ import dimod from tqdm import tqdm def main(): + instance_parameters() #wmis_results() - wmis_siman_results_alpha_num_of_assignments() + #wmis_siman_results_alpha_num_of_assignments() #wmis_siman_results() #minisat_runs() + +def instance_parameters(): + edb = script.connect_to_experimetns_db() + edb_cursor = edb.cursor() + + idb = script.connect_to_instance_pool() + + instances = queries.Instance_scope_query(idb) + instances.query("c42_vLogistic_1") + + insert_row = ("INSERT INTO c42_vLogistic_1_instances " + "(instance_id, " + " number_of_clauses, " + " number_of_variables) " + "VALUES (%s, %s, %s)") + + for instance, instance_id in instances: + edb_cursor.execute(insert_row, (str(instance_id), + int(instance.getNumberOfClauses()), + int(instance.getNumberOfVariables()))) + + edb.commit() + edb_cursor.close() + edb.close() + def wmis_siman_results(): db = script.connect_to_instance_pool() diff --git a/util/SAT2QUBO.py b/util/SAT2QUBO.py index ff8c240..053a043 100644 --- a/util/SAT2QUBO.py +++ b/util/SAT2QUBO.py @@ -5,8 +5,8 @@ from . import kSAT from tqdm import tqdm import math -__VERTEX_WEIGHT__ = 1 -__EDGE_WEIGHT__ = -2 +__VERTEX_WEIGHT__ = -1 +__EDGE_WEIGHT__ = 2 def WMISdictQUBO(kSATInstance): quboInstance = {} diff --git a/util/random_instance_pool.py b/util/random_instance_pool.py index 7f3bed1..8e1a8a7 100644 --- a/util/random_instance_pool.py +++ b/util/random_instance_pool.py @@ -2,6 +2,12 @@ from . import randomSAT +import math +import random +import numpy as np +import seaborn as sns +import matplotlib.pyplot as plt + class Random_instance_pool: def __init__(self, parameter_range): @@ -83,5 +89,90 @@ class Manual_range: def next(self): return self.__next__() - +class Random_range: + + def __init__(self, random_generator, steps): + self.__random_generator = random_generator + self.__steps = steps + self.__current_step = 0 + + def __iter__(self): + return self + + def __next__(self): + if self.__current_step < self.__steps: + self.__current_step += 1 + return self.__random_generator.random() + else: + raise StopIteration + + def next(self): + return self.__next__() + +class Random_logistic_variable_distribution: + + def __init__(self, + number_of_clauses, + min_variables, + max_variables, + alpha_point_of_interest, + steepnesss): + self.__number_of_clauses = number_of_clauses + self.__min_variables = min_variables + self.__max_variables = max_variables + self.__alpha_point_of_interest = alpha_point_of_interest + self.__steepnesss = steepnesss + + def random(self): + number_of_variables = 0 + + while (number_of_variables < self.__min_variables or + number_of_variables > self.__max_variables): + + alpha = inv_logistic(random.random(), + 1, + self.__steepnesss, + self.__alpha_point_of_interest) + + number_of_variables = int(self.__number_of_clauses / alpha) + + return number_of_variables + +def inv_logistic(x, L, k, x0): + return math.log(math.pow(x / (L-x), 1/k)) + x0 + +def test(): + sns.set() + + data = [] + + logistic_distr = Random_logistic_variable_distribution(42, 5, 84, 4.5, 1) + rnd_range = Random_range(logistic_distr, 500) + + for i in range(500): + #data.append(50/math.exp(random.uniform(5, 50))) + #v = 0 + + #while v < 5 or v > 84: + #v = int(42 / inv_logistic(random.random(), 1, 1, 4.5)) + + data.append(rnd_range.next()) + + data = np.array(data) + + sns.distplot(data, + #bins=30, + norm_hist=True) + #sns.lineplot(list(range(2, 100)), data) + plt.show() + + sns.distplot(42/data, + #bins=30, + norm_hist=True) + #sns.lineplot(list(range(2, 100)), data) + plt.show() + + return data + + diff --git a/util/script.py b/util/script.py index 7c70293..bb97334 100644 --- a/util/script.py +++ b/util/script.py @@ -10,6 +10,8 @@ from . import graph import minorminer from tqdm import tqdm import numpy as np +import random +import sys def readConfig(configFilePath): config = configparser.ConfigParser() @@ -183,13 +185,22 @@ def __qubo_to_JSON(qubo): return quboJSON -def write_wmis_embedding_to_pool_db(collection, qubo_id, solver_graph_id, embedding): +def write_wmis_embedding_to_pool_db(collection, qubo_id, solver_graph_id, seed, embedding): if not __embedding_entry_exists(collection, qubo_id, solver_graph_id): __prepare_new_wmis_embedding_entry(collection, qubo_id, solver_graph_id) collection.update_one( {"qubo": qubo_id, "solver_graph": solver_graph_id}, - {"$push": {"embeddings": __embedding_to_array(embedding)}} + { + "$push": + { + "embeddings": + { + "embedding": __embedding_to_array(embedding), + "seed": seed + } + } + } ) def __embedding_entry_exists(collection, qubo_id, solver_graph_id): @@ -250,20 +261,25 @@ def find_wmis_embeddings_for_scope(db, scope, solver_graph): max_no_improvement = 10 for i in range(5): if __embedding_entry_exists(db["embeddings"], qubo_id, solver_graph_id): - already_found += 1 + if i == 0: + already_found += 1 break; else: nx_qubo = graph.qubo_to_nx_graph(qubo) + seed = random.randint(0, sys.maxsize) + emb = minorminer.find_embedding(nx_qubo.edges(), solver_graph.edges(), return_overlap=True, - max_no_improvement=max_no_improvement) + max_no_improvement=max_no_improvement, + random_seed=seed) if emb[1] == 1: write_wmis_embedding_to_pool_db(db["embeddings"], qubo_id, solver_graph_id, + seed, emb[0]) new_embeddings_found += 1