mono.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.getData()):
  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.getData()):
  81. for index_j, val_j in enumerate(solution.getData()):
  82. fitness += self._data['F'][index_i,
  83. index_j] * self._data['D'][val_i,
  84. val_j]
  85. return fitness
  86. class UBQPEvaluator(Evaluator):
  87. """Unconstrained Binary Quadratic Programming (UBQP) evaluator class which enables to compute UBQP solution using specific `_data`
  88. - stores into its `_data` dictionary attritute required measures when computing a UBQP solution
  89. - `_data['Q']` matrix of size n x n with real values data (stored as numpy array)
  90. - `compute` method enables to compute and associate a score to a given UBQP solution
  91. Example:
  92. >>> import random
  93. >>> import numpy as np
  94. >>> # binary solution import
  95. >>> from macop.solutions.discrete import BinarySolution
  96. >>> # evaluator import
  97. >>> from macop.evaluators.discrete.mono import UBQPEvaluator
  98. >>> # define problem data using UBQP example instance
  99. >>> ubqp_instance_file = 'examples/instances/ubqp/ubqp_instance.txt'
  100. >>> n = 100 # problem size
  101. >>> # loading data
  102. >>> f = open(ubqp_instance_file, 'r')
  103. >>> file_data = f.readlines()
  104. >>> # get all string floating point values of matrix
  105. >>> Q_data = ''.join([ line.replace('\\n', '') for line in file_data[8:] ])
  106. >>> # load the concatenate obtained string
  107. >>> Q_matrix = np.fromstring(Q_data, dtype=float, sep=' ').reshape(n, n)
  108. >>> f.close()
  109. >>> # create evaluator instance using loading data
  110. >>> evaluator = UBQPEvaluator(data={'Q': Q_matrix})
  111. >>> # create new random combinatory solution using n, the instance QAP size
  112. >>> solution = BinarySolution.random(n)
  113. >>> # compute solution score
  114. >>> evaluator.compute(solution)
  115. 477.0
  116. """
  117. def compute(self, solution):
  118. """Apply the computation of fitness from solution
  119. Args:
  120. solution: {Solution} -- UBQP solution instance
  121. Returns:
  122. {float} -- fitness score of solution
  123. """
  124. fitness = 0
  125. for index_i, val_i in enumerate(solution.getData()):
  126. for index_j, val_j in enumerate(solution.getData()):
  127. fitness += self._data['Q'][index_i, index_j] * val_i * val_j
  128. return fitness