Map.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "definitions.hpp"
  2. #include "Map.hpp"
  3. #include "Particle.hpp"
  4. #include <sys/types.h>
  5. #include <omp.h>
  6. Map::Map(size_t width, size_t height): m_width(width), m_height(height)
  7. {
  8. m_surface = (size_t) width * (size_t) height;
  9. m_tiles.resize(m_surface);
  10. for(auto &tile : m_tiles){
  11. tile.pheromone = 0;
  12. tile.variation = 0;
  13. }
  14. m_tiles_swap.resize(m_surface);
  15. for(auto &tile : m_tiles_swap){
  16. tile.pheromone = 0;
  17. tile.variation = 0;
  18. }
  19. }
  20. void Map::populate(std::vector<Particle> && particles){
  21. this->m_particles = std::move(particles);
  22. }
  23. inline size_t index(ssize_t i, ssize_t j, size_t height, size_t width){
  24. if(i < 0)
  25. i += height;
  26. if(j < 0)
  27. j += width;
  28. if(i >= height)
  29. i -= height;
  30. if(j >= width)
  31. j -= width;
  32. return i * width + j;
  33. }
  34. void Map::update(numeric_t delta_t)
  35. {
  36. #pragma omp parallel for
  37. for(size_t i = 0 ; i < m_particles.size(); ++i){
  38. m_particles[i].update(*this, delta_t, speed, steering, sampling_angle, sampling_distance);
  39. }
  40. for(size_t i = 0 ; i < m_particles.size(); ++i){
  41. m_tiles[m_particles[i].tile_index(*this)].variation += increase * delta_t;
  42. m_tiles_swap[m_particles[i].tile_index(*this)].population ++;
  43. }
  44. #pragma omp parallel for
  45. for(size_t i = 0 ; i < m_tiles.size(); ++i){
  46. m_tiles[i].variation -= decay * delta_t;
  47. m_tiles[i].update();
  48. }
  49. #pragma omp parallel for
  50. for(ssize_t i = 0 ; i < m_height; ++i){
  51. for(ssize_t j = 0 ; j < m_width; ++j){
  52. m_tiles_swap[index(i, j, m_height, m_width)].pheromone =
  53. m_tiles[index(i, j, m_height, m_width)].pheromone * (1 - 4 * diffusion * delta_t)
  54. + m_tiles[index(i - 1, j, m_height, m_width)].pheromone * diffusion * delta_t
  55. + m_tiles[index(i + 1, j, m_height, m_width)].pheromone * diffusion * delta_t
  56. + m_tiles[index(i, j - 1, m_height, m_width)].pheromone * diffusion * delta_t
  57. + m_tiles[index(i, j + 1, m_height, m_width)].pheromone * diffusion * delta_t;
  58. }
  59. }
  60. std::swap(m_tiles, m_tiles_swap);
  61. }