implementation.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. Macop QAP implementation
  2. ========================
  3. Let's see how it is possible with the use of the **Macop** package to implement and deal with this QAP instance problem.
  4. Solution structure definition
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. Firstly, we are going to use a type of solution that will allow us to define the structure of our solutions.
  7. The available ``macop.solutions.discrete.CombinatoryIntegerSolution`` type of solution within the Macop package represents exactly what one would wish for.
  8. I.e. a solution that stores a sequence of integers relative to the size of the problem, the order of which is not sorted.
  9. Let's see an example of its use:
  10. .. code:: python
  11. from macop.solutions.discrete import CombinatoryIntegerSolution
  12. solution = CombinatoryIntegerSolution.random(10)
  13. print(solution)
  14. The resulting solution obtained:
  15. .. code:: bash
  16. Combinatory integer solution [2 9 8 1 7 6 0 4 3 5]
  17. QAP Evaluator
  18. ~~~~~~~~~~~~~
  19. Now that we have the structure of our solutions, and the means to generate them, we will seek to evaluate them.
  20. To do this, we need to create a new evaluator specific to our problem and the relative evaluation function:
  21. - :math:`min_{ϕ∈S_n}\sum_{i=1}^{n}{\sum_{j=1}^{n}{f_{ij}⋅d_{\phi(i)\phi(j)}}}`
  22. So we are going to create a class that will inherit from the abstract class ``macop.evalutarors.base.Evaluator``:
  23. .. code:: python
  24. from macop.evaluators.base import Evaluator
  25. class QAPEvaluator(Evaluator):
  26. """QAP evaluator class which enables to compute QAP solution using specific `_data`
  27. - stores into its `_data` dictionary attritute required measures when computing a QAP solution
  28. - `_data['F']` matrix of size n x n with flows data between facilities (stored as numpy array)
  29. - `_data['D']` matrix of size n x n with distances data between locations (stored as numpy array)
  30. - `compute` method enables to compute and associate a score to a given QAP solution
  31. """
  32. def compute(self, solution):
  33. """Apply the computation of fitness from solution
  34. Args:
  35. solution: {Solution} -- QAP solution instance
  36. Returns:
  37. {float} -- fitness score of solution
  38. """
  39. fitness = 0
  40. for index_i, val_i in enumerate(solution.getdata = )):
  41. for index_j, val_j in enumerate(solution.getdata = )):
  42. fitness += self._data['F'][index_i, index_j] * self._data['D'][val_i, val_j]
  43. return fitness
  44. The cost function for the quadratic problem is now well defined.
  45. .. tip::
  46. The class proposed here, is available in the Macop package ``macop.evaluators.discrete.mono.QAPEvaluator``.
  47. Running algorithm
  48. ~~~~~~~~~~~~~~~~~
  49. Now that the necessary tools are available, we will be able to deal with our problem and look for solutions in the search space of our QAP instance.
  50. Here we will use local search algorithms already implemented in **Macop**.
  51. If you are uncomfortable with some of the elements in the code that will follow, you can refer to the more complete **Macop** documentation_ that focuses more on the concepts and tools of the package.
  52. .. code:: python
  53. # main imports
  54. import numpy as np
  55. # module imports
  56. from macop.solutions.discrete import CombinatoryIntegerSolution
  57. from macop.evaluators.discrete.mono import QAPEvaluator
  58. from macop.operators.discrete.mutators import SimpleMutation
  59. from macop.policies.classicals import RandomPolicy
  60. from macop.algorithms.mono import IteratedLocalSearch as ILS
  61. from macop.algorithms.mono import HillClimberFirstImprovment
  62. # usefull instance data
  63. n = 100
  64. qap_instance_file = 'qap_instance.txt'
  65. # default validator (check the consistency of our data, i.e. only unique element)
  66. def validator(solution):
  67. if len(list(solution.getdata = ))) > len(set(list(solution.getdata = )))):
  68. print("not valid")
  69. return False
  70. return True
  71. # define init random solution
  72. def init():
  73. return CombinatoryIntegerSolution.random(n, validator)
  74. # load qap instance
  75. with open(qap_instance_file, 'r') as f:
  76. file_data = f.readlines()
  77. print(f'Instance information {file_data[0]}')
  78. D_lines = file_data[1:n + 1]
  79. D_data = ''.join(D_lines).replace('\n', '')
  80. F_lines = file_data[n:2 * n + 1]
  81. F_data = ''.join(F_lines).replace('\n', '')
  82. D_matrix = np.fromstring(D_data, dtype=float, sep=' ').reshape(n, n)
  83. print(f'D matrix shape: {D_matrix.shape}')
  84. F_matrix = np.fromstring(F_data, dtype=float, sep=' ').reshape(n, n)
  85. print(f'F matrix shape: {F_matrix.shape}')
  86. # only one operator here
  87. operators = [SimpleMutation()]
  88. # random policy even if list of solution has only one element
  89. policy = RandomPolicy(operators)
  90. # use of loaded data from QAP instance
  91. evaluator = QAPEvaluator(data={'F': F_matrix, 'D': D_matrix})
  92. # passing global evaluation param from ILS
  93. hcfi = HillClimberFirstImprovment(init, evaluator, operators, policy, validator, maximise=False, verbose=True)
  94. algo = ILS(init, evaluator, operators, policy, validator, localSearch=hcfi, maximise=False, verbose=True)
  95. # run the algorithm
  96. bestSol = algo.run(10000, ls_evaluations=100)
  97. print('Solution for QAP instance score is {}'.format(evaluator.compute(bestSol)))
  98. QAP problem solving is now possible with **Macop**. As a reminder, the complete code is available in the qapExample.py_ file.
  99. .. _qapExample.py: https://github.com/jbuisine/macop/blob/master/examples/qapExample.py
  100. .. _documentation: https://jbuisine.github.io/macop/_build/html/documentations