mono.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """Mono-objective evaluators classes
  2. """
  3. # main imports
  4. from macop.evaluators.base import Evaluator
  5. class KnapsackEvaluator(Evaluator):
  6. """Knapsack evaluator class which enables to compute knapsack solution using specific `_data`
  7. - stores into its `_data` dictionary attritute required measures when computing a knapsack solution
  8. - `_data['worths']` stores knapsack objects worths information
  9. - `compute` method enables to compute and associate a score to a given knapsack solution
  10. Example:
  11. >>> import random
  12. >>> # binary solution import
  13. >>> from macop.solutions.discrete import BinarySolution
  14. >>> # evaluator import
  15. >>> from macop.evaluators.discrete.mono import KnapsackEvaluator
  16. >>> solution_data = [1, 0, 0, 1, 1, 0, 1, 0]
  17. >>> size = len(solution_data)
  18. >>> solution = BinarySolution(solution_data, size)
  19. >>> # evaluator initialization (worths objects passed into data)
  20. >>> worths = [ random.randint(5, 20) for i in range(size) ]
  21. >>> evaluator = KnapsackEvaluator(data={'worths': worths})
  22. >>> # compute solution score
  23. >>> evaluator.compute(solution)
  24. 40
  25. """
  26. def compute(self, solution):
  27. """Apply the computation of fitness from solution
  28. Args:
  29. solution: {Solution} -- Solution instance
  30. Returns:
  31. {float} -- fitness score of solution
  32. """
  33. fitness = 0
  34. for index, elem in enumerate(solution._data):
  35. fitness += self._data['worths'][index] * elem
  36. return fitness
  37. class QAPEvaluator(Evaluator):
  38. """Quadratic Assignment Problem (QAP) evaluator class which enables to compute qap solution using specific `_data`
  39. Solutions use for this evaluator are with type of `macop.solutions.discrete.CombinatoryIntegerSolution`
  40. - stores into its `_data` dictionary attritute required measures when computing a QAP solution
  41. - `_data['F']` matrix of size n x n with flows data between facilities (stored as numpy array)
  42. - `_data['D']` matrix of size n x n with distances data between locations (stored as numpy array)
  43. - `compute` method enables to compute and associate a score to a given QAP solution
  44. Example:
  45. >>> import random
  46. >>> import numpy as np
  47. >>> # combinatory solution import
  48. >>> from macop.solutions.discrete import CombinatoryIntegerSolution
  49. >>> # evaluator import
  50. >>> from macop.evaluators.discrete.mono import QAPEvaluator
  51. >>> # define problem data using QAP example instance
  52. >>> qap_instance_file = 'examples/instances/qap/qap_instance.txt'
  53. >>> n = 100 # problem size
  54. >>> # loading data
  55. >>> f = open(qap_instance_file, 'r')
  56. >>> file_data = f.readlines()
  57. >>> D_lines = file_data[1:n + 1]
  58. >>> D_data = ''.join(D_lines).replace('\\n', '')
  59. >>> F_lines = file_data[n:2 * n + 1]
  60. >>> F_data = ''.join(F_lines).replace('\\n', '')
  61. >>> D_matrix = np.fromstring(D_data, dtype=float, sep=' ').reshape(n, n)
  62. >>> F_matrix = np.fromstring(F_data, dtype=float, sep=' ').reshape(n, n)
  63. >>> f.close()
  64. >>> # create evaluator instance using loading data
  65. >>> evaluator = QAPEvaluator(data={'F': F_matrix, 'D': D_matrix})
  66. >>> # create new random combinatory solution using n, the instance QAP size
  67. >>> solution = CombinatoryIntegerSolution.random(n)
  68. >>> # compute solution score
  69. >>> evaluator.compute(solution)
  70. 6397983.0
  71. """
  72. def compute(self, solution):
  73. """Apply the computation of fitness from solution
  74. Args:
  75. solution: {Solution} -- QAP solution instance
  76. Returns:
  77. {float} -- fitness score of solution
  78. """
  79. fitness = 0
  80. for index_i, val_i in enumerate(solution._data):
  81. for index_j, val_j in enumerate(solution._data):
  82. fitness += self._data['F'][index_i, index_j] * self._data['D'][val_i, val_j]
  83. return fitness
  84. class UBQPEvaluator(Evaluator):
  85. """Unconstrained Binary Quadratic Programming (UBQP) evaluator class which enables to compute UBQP solution using specific `_data`
  86. - stores into its `_data` dictionary attritute required measures when computing a UBQP solution
  87. - `_data['Q']` matrix of size n x n with real values data (stored as numpy array)
  88. - `compute` method enables to compute and associate a score to a given UBQP solution
  89. Example:
  90. >>> import random
  91. >>> import numpy as np
  92. >>> # binary solution import
  93. >>> from macop.solutions.discrete import BinarySolution
  94. >>> # evaluator import
  95. >>> from macop.evaluators.discrete.mono import UBQPEvaluator
  96. >>> # define problem data using UBQP example instance
  97. >>> ubqp_instance_file = 'examples/instances/ubqp/ubqp_instance.txt'
  98. >>> n = 100 # problem size
  99. >>> # loading data
  100. >>> f = open(ubqp_instance_file, 'r')
  101. >>> file_data = f.readlines()
  102. >>> # get all string floating point values of matrix
  103. >>> Q_data = ''.join([ line.replace('\\n', '') for line in file_data[8:] ])
  104. >>> # load the concatenate obtained string
  105. >>> Q_matrix = np.fromstring(Q_data, dtype=float, sep=' ').reshape(n, n)
  106. >>> f.close()
  107. >>> # create evaluator instance using loading data
  108. >>> evaluator = UBQPEvaluator(data={'Q': Q_matrix})
  109. >>> # create new random combinatory solution using n, the instance QAP size
  110. >>> solution = BinarySolution.random(n)
  111. >>> # compute solution score
  112. >>> evaluator.compute(solution)
  113. 477.0
  114. """
  115. def compute(self, solution):
  116. """Apply the computation of fitness from solution
  117. Args:
  118. solution: {Solution} -- UBQP solution instance
  119. Returns:
  120. {float} -- fitness score of solution
  121. """
  122. fitness = 0
  123. for index_i, val_i in enumerate(solution._data):
  124. for index_j, val_j in enumerate(solution._data):
  125. fitness += self._data['Q'][index_i, index_j] * val_i * val_j
  126. return fitness