discrete.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Discrete solution classes implementations
  2. """
  3. import numpy as np
  4. # modules imports
  5. from .base import Solution
  6. class BinarySolution(Solution):
  7. """
  8. Binary integer solution class
  9. Attributes:
  10. data: {ndarray} -- array of binary values
  11. size: {int} -- size of binary array values
  12. score: {float} -- fitness score value
  13. """
  14. def __init__(self, data, size):
  15. """
  16. Initialize binary solution using specific data
  17. Args:
  18. data: {ndarray} -- array of binary values
  19. size: {int} -- size of binary array values
  20. """
  21. super().__init__(np.array(data), size)
  22. def random(self, validator):
  23. """
  24. Intialize binary array with use of validator to generate valid random solution
  25. Args:
  26. validator: {function} -- specific function which validates or not a solution
  27. Returns:
  28. {BinarySolution} -- new generated binary solution
  29. """
  30. self._data = np.random.randint(2, size=self._size)
  31. while not self.isValid(validator):
  32. self._data = np.random.randint(2, size=self._size)
  33. return self
  34. def __str__(self):
  35. return "Binary solution %s" % (self._data)
  36. class CombinatoryIntegerSolution(Solution):
  37. """
  38. Combinatory integer solution class
  39. Attributes:
  40. data: {ndarray} -- array of binary values
  41. size: {int} -- size of binary array values
  42. score: {float} -- fitness score value
  43. """
  44. def __init__(self, data, size):
  45. """
  46. Initialize binary solution using specific data
  47. Args:
  48. data: {ndarray} -- array of binary values
  49. size: {int} -- size of binary array values
  50. """
  51. super().__init__(data, size)
  52. def random(self, validator):
  53. """
  54. Intialize combinatory integer array with use of validator to generate valid random solution
  55. Args:
  56. validator: {function} -- specific function which validates or not a solution
  57. Returns:
  58. {CombinatoryIntegerSolution} -- new generated combinatory integer solution
  59. """
  60. self._data = np.random.shuffle(np.arange(self._size))
  61. while not self.isValid(validator):
  62. self._data = np.random.shuffle(np.arange(self._size))
  63. return self
  64. def __str__(self):
  65. return "Combinatory integer solution %s" % (self._data)
  66. class IntegerSolution(Solution):
  67. """
  68. Integer solution class
  69. Attributes:
  70. data: {ndarray} -- array of binary values
  71. size: {int} -- size of binary array values
  72. score: {float} -- fitness score value
  73. """
  74. def __init__(self, data, size):
  75. """
  76. Initialize integer solution using specific data
  77. Args:
  78. data: {ndarray} -- array of binary values
  79. size: {int} -- size of binary array values
  80. """
  81. super().__init__(data, size)
  82. def random(self, validator):
  83. """
  84. Intialize integer array with use of validator to generate valid random solution
  85. Args:
  86. validator: {function} -- specific function which validates or not a solution
  87. Returns:
  88. {IntegerSolution} -- new generated integer solution
  89. """
  90. self._data = np.random.randint(self._size, size=self._size)
  91. while not self.isValid(validator):
  92. self._data = np.random.randint(self._size, size=self._size)
  93. return self
  94. def __str__(self):
  95. return "Integer solution %s" % (self._data)