You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env python3
  2. from . import randomSAT
  3. import math
  4. import random
  5. import numpy as np
  6. import seaborn as sns
  7. import matplotlib.pyplot as plt
  8. class Random_instance_pool:
  9. def __init__(self, parameter_range):
  10. self.__parameter_range = parameter_range
  11. def __iter__(self):
  12. return self
  13. def __next__(self):
  14. return self.Random(self.__parameter_range.next())
  15. class Random:
  16. def __init__(self, parameters):
  17. self.__params = parameters
  18. def random(self):
  19. return randomSAT.generateRandomKSAT(self.__params.number_of_clauses,
  20. self.__params.number_of_variables,
  21. self.__params.variables_per_clause)
  22. class Instance_parameters:
  23. def __init__(self,
  24. number_of_clauses,
  25. number_of_variables,
  26. variables_per_clause = 3):
  27. self.number_of_clauses = number_of_clauses
  28. self.number_of_variables = number_of_variables
  29. self.variables_per_clause = variables_per_clause
  30. def __str__(self):
  31. return ("number of clauses: {}\n"
  32. "number of variables: {}\n"
  33. "variables per clause: {}").format(self.number_of_clauses,
  34. self.number_of_variables,
  35. self.variables_per_clause)
  36. class Instance_parameter_variable_range:
  37. def __init__(self, start_parameter, variable_range):
  38. self.start_parameter = start_parameter
  39. self.__variable_range = variable_range
  40. def __iter__(self):
  41. return self
  42. def __next__(self):
  43. self.start_parameter.number_of_variables = self.__variable_range.next()
  44. return self.start_parameter
  45. def next(self):
  46. return self.__next__()
  47. class Manual_range:
  48. def __init__(self, start, stop, step = 1):
  49. self.start = start
  50. self.stop = stop
  51. self.step = step
  52. self.__current = start
  53. def __iter__(self):
  54. return self
  55. def __next__(self):
  56. if self.__current >= self.stop:
  57. raise StopIteration
  58. self.__current += self.step
  59. return self.__current
  60. def next(self):
  61. return self.__next__()
  62. class Random_range:
  63. def __init__(self, random_generator, steps):
  64. self.__random_generator = random_generator
  65. self.__steps = steps
  66. self.__current_step = 0
  67. def __iter__(self):
  68. return self
  69. def __next__(self):
  70. if self.__current_step < self.__steps:
  71. self.__current_step += 1
  72. return self.__random_generator.random()
  73. else:
  74. raise StopIteration
  75. def next(self):
  76. return self.__next__()
  77. class Random_logistic_variable_distribution:
  78. def __init__(self,
  79. number_of_clauses,
  80. min_variables,
  81. max_variables,
  82. alpha_point_of_interest,
  83. steepness):
  84. self.__number_of_clauses = number_of_clauses
  85. self.__min_variables = min_variables
  86. self.__max_variables = max_variables
  87. self.__alpha_point_of_interest = alpha_point_of_interest
  88. self.__steepness = steepness
  89. def random(self):
  90. number_of_variables = 0
  91. while (number_of_variables < self.__min_variables or
  92. number_of_variables > self.__max_variables):
  93. alpha = inv_logistic(random.random(),
  94. 1,
  95. self.__steepness,
  96. self.__alpha_point_of_interest)
  97. number_of_variables = int(self.__number_of_clauses / alpha)
  98. return number_of_variables
  99. def inv_logistic(x, L, k, x0):
  100. return math.log(math.pow(x / (L-x), 1/k)) + x0
  101. def create_random_logistic_pool(logistic_variable_distr_params, number_of_instances,
  102. instance_params):
  103. logist_distr = Random_logistic_variable_distribution(**logistic_variable_distr_params)
  104. rnd_range = Random_range(logist_distr, number_of_instances)
  105. param_range = Instance_parameter_variable_range(instance_params, rnd_range)
  106. return Random_instance_pool(param_range)
  107. def test():
  108. sns.set()
  109. data = []
  110. logistic_distr = Random_logistic_variable_distribution(42, 5, 84, 4.5, 1)
  111. rnd_range = Random_range(logistic_distr, 500)
  112. for i in range(500):
  113. #data.append(50/math.exp(random.uniform(5, 50)))
  114. #v = 0
  115. #while v < 5 or v > 84:
  116. #v = int(42 / inv_logistic(random.random(), 1, 1, 4.5))
  117. data.append(rnd_range.next())
  118. data = np.array(data)
  119. sns.distplot(data,
  120. #bins=30,
  121. norm_hist=True)
  122. #sns.lineplot(list(range(2, 100)), data)
  123. plt.show()
  124. sns.distplot(42/data,
  125. #bins=30,
  126. norm_hist=True)
  127. #sns.lineplot(list(range(2, 100)), data)
  128. plt.show()
  129. return data