#!/usr/bin/env python3 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): self.__parameter_range = parameter_range def __iter__(self): return self def __next__(self): return self.Random(self.__parameter_range.next()) class Random: def __init__(self, parameters): self.__params = parameters def random(self): return randomSAT.generateRandomKSAT(self.__params.number_of_clauses, self.__params.number_of_variables, self.__params.variables_per_clause) class Instance_parameters: def __init__(self, number_of_clauses, number_of_variables, variables_per_clause = 3): self.number_of_clauses = number_of_clauses self.number_of_variables = number_of_variables self.variables_per_clause = variables_per_clause def __str__(self): return ("number of clauses: {}\n" "number of variables: {}\n" "variables per clause: {}").format(self.number_of_clauses, self.number_of_variables, self.variables_per_clause) class Instance_parameter_variable_range: def __init__(self, start_parameter, variable_range): self.start_parameter = start_parameter self.__variable_range = variable_range def __iter__(self): return self def __next__(self): self.start_parameter.number_of_variables = self.__variable_range.next() return self.start_parameter def next(self): return self.__next__() class Manual_range: def __init__(self, start, stop, step = 1): self.start = start self.stop = stop self.step = step self.__current = start def __iter__(self): return self def __next__(self): if self.__current >= self.stop: raise StopIteration self.__current += self.step return self.__current 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, steepness): 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.__steepness = steepness 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.__steepness, 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 create_random_logistic_pool(logistic_variable_distr_params, number_of_instances, instance_params): logist_distr = Random_logistic_variable_distribution(**logistic_variable_distr_params) rnd_range = Random_range(logist_distr, number_of_instances) param_range = Instance_parameter_variable_range(instance_params, rnd_range) return Random_instance_pool(param_range) 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