mono.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. """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. >>> # combinatory solution import
  47. >>> from macop.solutions.discrete import CombinatoryIntegerSolution
  48. >>> # evaluator import
  49. >>> from macop.evaluators.discrete.QAPEvaluator import QAPEvaluator
  50. >>> # define problem data using QAP example instance
  51. >>> qap_instance_file = '../../../examples/instances/qap/qap_instance.txt'
  52. >>> n = 100 # problem size
  53. >>> size = len(solution_data)
  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. 40
  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