instance.rst 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. QAP Problem instance generation
  2. ===============================
  3. To define our quadratic assignment problem instance, we will use the available mQAP_ multi-objective quadratic problem generator.
  4. Genration of the instance
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~
  6. We will limit ourselves here to a single objective for the purposes of this example. The file **makeQAPuni.cc**, will be used to generate the instance.
  7. .. code:: bash
  8. g++ makeQAPuni.cc -o mQAPGenerator
  9. ./mQAPGenerator -n 100 -k 1 -f 30 -d 80 -s 42 > qap_instance.txt
  10. with the following parameters:
  11. - **-n** positive integer: number of facilities/locations;
  12. - **-k** positive integer: number of objectives;
  13. - **-f** positive integer: maximum flow between facilities;
  14. - **-d** positive integer: maximum distance between locations;
  15. - **-s** positive long: random seed.
  16. The generated qap_instance.txt_ file contains the two matrices :math:`F` and :math:`D` and define our instance problem.
  17. .. _mQAP: https://www.cs.bham.ac.uk/~jdk/mQAP/
  18. .. _qap_instance.txt: https://github.com/jbuisine/macop/blob/master/examples/instances/qap/qap_instance.txt
  19. Load data instance
  20. ~~~~~~~~~~~~~~~~~~
  21. We are now going to load this instance via a Python code which will be useful to us later on:
  22. .. code:: Python
  23. qap_instance_file = 'qap_instance.txt'
  24. n = 100 # the instance size
  25. with open(qap_instance_file, 'r') as f:
  26. file_data = f.readlines()
  27. print(f'Instance information {file_data[0]}')
  28. D_lines = file_data[1:n + 1]
  29. D_data = ''.join(D_lines).replace('\n', '')
  30. F_lines = file_data[n:2 * n + 1]
  31. F_data = ''.join(F_lines).replace('\n', '')
  32. D_matrix = np.fromstring(D_data, dtype=float, sep=' ').reshape(n, n)
  33. print(f'D matrix shape: {D_matrix.shape}')
  34. F_matrix = np.fromstring(F_data, dtype=float, sep=' ').reshape(n, n)
  35. print(f'F matrix shape: {F_matrix.shape}')
  36. .. note::
  37. As we know the size of our instance and the structure of the document, it is quite quick to look for the lines related to the :math:`F` and :math:`D` matrices.