AgentsSourcesManager.cpp 22.2 KB
Newer Older
1 2 3 4
/**
 * \file        AgentsSourcesManager.cpp
 * \date        Apr 14, 2015
 * \version     v0.7
Mohcine Chraibi's avatar
Mohcine Chraibi committed
5
 * \copyright   <2009-2015> Forschungszentrum Jülich GmbH. All rights reserved.
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * \section License
 * This file is part of JuPedSim.
 *
 * JuPedSim is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * JuPedSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
 *
 * \section Description
 * This class is responsible for materialising agent in a given location at a given frequency up to a maximum number.
 * The optimal position where to put the agents is given by various algorithms, for instance
 * the Voronoi algorithm or the Mitchell Best candidate algorithm.
 *
 **/
29 30 31

#include "AgentsSourcesManager.h"
#include "Pedestrian.h"
32
#include "../mpi/LCGrid.h"
33
#include <thread>
34
#include "AgentsQueue.h"
35

36 37
#include "../voronoi-boost/VoronoiPositionGenerator.h"

38 39
using namespace std;

40
bool AgentsSourcesManager::_isCompleted=true;
41

42 43 44 45 46 47 48 49
AgentsSourcesManager::AgentsSourcesManager()
{
}

AgentsSourcesManager::~AgentsSourcesManager()
{
}

50
void AgentsSourcesManager::operator()()
51
{
52
     Run();
53
}
54

55 56
void AgentsSourcesManager::Run()
{
57
      SetRunning(true);
58
     Log->Write("INFO:\tStarting agent manager thread");
Mohcine Chraibi's avatar
Mohcine Chraibi committed
59
     std::cout<< KRED << "\n Starting agent manager thread\n" << ">> time: " << Pedestrian::GetGlobalTime() << RESET << "\n";
60 61
     //Generate all agents required for the complete simulation
     //It might be more efficient to generate at each frequency step
62
     //TODO  this loop is exactly GenerateAgents( --> REFACTOR)
63 64
     for (const auto& src : _sources)
     {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
65
          std::cout << "Generate src: " << src->GetId() << "\n" ;
66 67 68 69 70 71 72 73 74 75
          src->GenerateAgentsAndAddToPool(src->GetMaxAgents(), _building);
     }

     //first call ignoring the return value
     ProcessAllSources();

     //the loop is updated each x second.
     //it might be better to use a timer
     _isCompleted = false;
     bool finished = false;
76
     SetBuildingUpdated(false);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
77
     long updateFrequency = 1; // @todo parse this from inifile
78
     /* std::cout << KMAG << "RUN Starting thread manager with _lastUpdateTime " << _lastUpdateTime<< std::endl; */
79
     do
80
     {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
81
          int current_time = (int)Pedestrian::GetGlobalTime();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
82
          // std::cout << KBLU << ">> RUN: current_time " << current_time << " last update  " << _lastUpdateTime << "\n" << RESET;
83

84
          if ((current_time != _lastUpdateTime)
85
              && ((current_time % updateFrequency) == 0))
Mohcine Chraibi's avatar
Mohcine Chraibi committed
86
          {
87
                /* std::cout << "   ---  Enter IF  --- \n" << KRED << "QUEUE isempty: " << AgentsQueueIn::IsEmpty() << "\n" << RESET; */
88 89 90
                if(AgentsQueueIn::IsEmpty())
                //if queue is empty. Otherwise, wait for main thread to empty it and update _building
                {
91
                      /* std::cout << "   ---  Enter QUEUE EMPTY --- \n"; */
92 93
                      finished=ProcessAllSources();
                      _lastUpdateTime = current_time;
94
                      //SetBuildingUpdated(false);
95
                }
96
          }
97
          // wait for main thread to update building
98 99
          if(current_time >= GetMaxSimTime())
                break; // break if max simulation time is reached.
100

101
     } while (!finished);
102 103
     Log->Write("INFO:\tTerminating agent manager thread");
     _isCompleted = true;
104 105
}

106 107
bool AgentsSourcesManager::ProcessAllSources() const
{
Mohcine Chraibi's avatar
Mohcine Chraibi committed
108
     // std::cout << "\nSTART   AgentsSourcesManager::ProcessAllSources()\n";
109

110
     bool empty=true;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
111
     double current_time = (int)Pedestrian::GetGlobalTime();
112
     vector<Pedestrian*> source_peds; // we have to collect peds from all sources, so that we can consider them  while computing new positions
113 114
     for (const auto& src : _sources)
     {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
115
          // std::cout << KRED << "\nprocessing src: " <<  src->GetId() << " -- current time: " << current_time << " schedule time: " << src->GetPlanTime() <<". number of peds in building " << _building->GetAllPedestrians().size() << "\n" << RESET;
116

Mohcine Chraibi's avatar
Mohcine Chraibi committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
          auto srcLifeSpan = src-> GetLifeSpan();
          bool inTime = (current_time >= srcLifeSpan[0]) && (current_time <= srcLifeSpan[1]);
          // inTime is always true if src got some PlanTime (default values
          // if src has no PlanTime, then this is set to 0. In this case inTime
          // is important in the following condition
          bool newCycle =  std::fmod(current_time, src->GetFrequency()) == 0;
          bool subCycle;
          subCycle = (current_time > src->GetFrequency())?std::fmod((current_time-src->GetFrequency()), src->GetRate()) == 0:false;

          if(newCycle)
               src->ResetRemainingAgents();

          bool timeToCreate = newCycle || subCycle;
          // if(subCycle)
          //      std::cout << KGRN << " freq: " << src->GetFrequency() << ", rate: " << src->GetRate() << ", " << ": remaining: " << src->GetRemainingAgents() <<"\n" << RESET;                                                                                                                                                             std::cout << " <<<<  time to create " <<  timeToCreate  << "  newCycle: " << newCycle << ", subcycle: " << subCycle << ", inTime: " << inTime<< "\n";


          if (timeToCreate && src->GetPoolSize() && (src->GetPlanTime() <= current_time) && inTime && src->GetRemainingAgents())// maybe diff<eps
135 136
          {
               vector<Pedestrian*> peds;
137

Mohcine Chraibi's avatar
Mohcine Chraibi committed
138 139 140
               src->RemoveAgentsFromPool(peds, src->GetChunkAgents() * src->GetPercent());
               src->UpdateRemainingAgents(src->GetChunkAgents() * src->GetPercent());

141 142
               source_peds.reserve(source_peds.size() + peds.size());

Mohcine Chraibi's avatar
Mohcine Chraibi committed
143
               Log->Write("\nINFO:\tSource %d generating %d agents at %3.3f s, %d (%d remaining in pool)\n",src->GetId(),peds.size(), current_time,src->GetRemainingAgents(),src->GetPoolSize());
144
               printf("\nINFO:\tSource %d generating %lu agents (%d remaining)\n",src->GetId(), peds.size(),src->GetPoolSize());
145

146 147
               //ComputeBestPositionRandom(src.get(), peds);
               //todo: here every pedestrian needs an exitline
148 149 150 151 152 153
                if(!std::isnan(src->GetStartX()) && !std::isnan(src->GetStartY()))
                {
                      printf("INFO:\tSet source agent on fixed position (%.2f, %.2f)", src->GetStartX(), src->GetStartY());
                      InitFixedPosition(src.get(), peds);
                }
                else
Mohcine Chraibi's avatar
Mohcine Chraibi committed
154 155
                     if( !ComputeBestPositionVoronoiBoost(src.get(), peds, _building, source_peds) )
                          Log->Write("WARNING:\tThere was no place for some pedestrians");
156

157 158 159 160 161 162 163 164
               source_peds.insert(source_peds.end(), peds.begin(), peds.end());
               /* std::cout << KRED << ">>  Add to queue " << peds.size() << "\n" << RESET; */
                /* for( auto pp: peds) */
                /*       std::cout << "id: "<< pp->GetID() << "  pos " << pp->GetPos()._x << ", " << pp->GetPos()._y << "\n"; */
                /* std::cout << "------\n"; */
                /* for( auto pp: source_peds) */
                /*       std::cout  <<  "id: "<< pp->GetID() << "  POS " << pp->GetPos()._x << ", " << pp->GetPos()._y << "\n"; */

165 166 167
               AgentsQueueIn::Add(peds);
               empty = false;
               //src->Dump();
168
          }
Mohcine Chraibi's avatar
Mohcine Chraibi committed
169 170
          bool timeConstraint = (src->GetPlanTime() > current_time) || (current_time < srcLifeSpan[1]);
          if (timeConstraint) // for the case we still expect
171 172
               // agents coming
               empty = false;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
173

174
     }
Mohcine Chraibi's avatar
Mohcine Chraibi committed
175 176 177 178
     // std::cout << "LEAVE   AgentsSourcesManager::ProcessAllSources()\n";
     // std::cout << current_time << "\n";

     // std::cout << " Source building: "<<  _building << " size "  << _building->GetAllPedestrians().size()<< " empty = " << empty << std::endl;
179

Mohcine Chraibi's avatar
Mohcine Chraibi committed
180 181
     // for(auto pp: _building->GetAllPedestrians())
     //      std::cout<< KBLU << "BUL: agentssourcesManager: " << pp->GetPos()._x << ", " << pp->GetPos()._y << RESET << std::endl;
182
     //
Mohcine Chraibi's avatar
Mohcine Chraibi committed
183
// std::cout << "========================\n";
184
          return empty;
185
}
186

187

188 189 190 191 192 193 194 195 196
void AgentsSourcesManager::InitFixedPosition(AgentsSource* src,
                                                    vector<Pedestrian*>& peds)const
{
     for(auto&& ped : peds)
     {
          Point v;
          if (ped->GetExitLine()) {
               v = (ped->GetExitLine()->ShortestPoint(ped->GetPos())- ped->GetPos()).Normalized();
          } else {
197 198 199 200 201
               auto transitions = ped->GetBuilding()->GetAllTransitions();
               auto transition = transitions[0];
               int trans_ID = transition->GetID();
               ped->SetExitLine(transition); // set dummy line
               ped->SetExitIndex(trans_ID);
202
               //ped->SetFinalDestination(trans_ID);
203
               v = Point(0., 0.);
204
          }
205 206 207 208
          double speed=ped->GetEllipse().GetV0();
          v=v*speed;
          ped->SetV(v);
          ped->SetPos( Point(src->GetStartX(), src->GetStartY()) );
209
     }
210
}
211 212

void AgentsSourcesManager::ComputeBestPositionCompleteRandom(AgentsSource* src,
213
                                                             vector<Pedestrian*>& peds)const
214 215 216 217
{
     auto dist = src->GetStartDistribution();
     auto subroom = _building->GetRoom(dist->GetRoomId())->GetSubRoom(dist->GetSubroomID());
     vector<Point> positions = PedDistributor::PossiblePositions(*subroom);
218
     double seed = time(0);
219
     //TODO: get the seed from the simulation
Mohcine Chraibi's avatar
Mohcine Chraibi committed
220 221 222
     std:: cout << "seed: "<< seed << std::endl;

     srand (seed);
223 224 225 226 227 228 229 230 231

     for (auto& ped : peds)
     {
          if( positions.size() )
          {
               int index = rand()%positions.size();
               Point new_pos = positions[index];
               positions.erase(positions.begin() + index);
               ped->SetPos(new_pos, true);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
232 233
               std:: cout << "pos: " << new_pos._x << new_pos._y << std::endl;

234 235 236 237 238 239 240 241 242 243 244 245
               AdjustVelocityByNeighbour(ped);
          }
          else
          {
               Log->Write("\t No place for a pedestrian");
               break;
          }
     }

}

/*
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  void AgentsSourcesManager::ComputeBestPositionVoronoi(AgentsSource* src,
  Pedestrian* agent) const
  {
  auto dist = src->GetStartDistribution();
  double bounds[4];
  dist->Getbounds(bounds);
  int roomID = dist->GetRoomId();
  int subroomID = dist->GetSubroomID();

  //Get all pedestrians in that location
  vector<Pedestrian*> peds;
  _building->GetPedestrians(roomID, subroomID, peds);

  //filter the points that are not within the boundaries
  for (auto&& iter = peds.begin(); iter != peds.end();)
  {
  const Point& pos = (*iter)->GetPos();
  if ((bounds[0] <= pos._x && pos._x <= bounds[1])
  && (bounds[1] <= pos._y && pos._y <= bounds[2]))
  {
  iter = peds.erase(iter);
  cout << "removing (testing only)..." << endl;
  exit(0);
  } else
  {
  ++iter;
  }
  }

  //special case with 1, 2 or only three pedestrians in the area
  if (peds.size() < 3)
  {
  //TODO/random position in the area
  return;

  }
  // compute the cells and cut with the bounds
  const int count = peds.size();
  float* xValues = new float[count];
  float* yValues = new float[count];
  //float xValues[count];
  //float yValues[count];

  for (int i = 0; i < count; i++)
  {
  xValues[i] = peds[i]->GetPos()._x;
  yValues[i] = peds[i]->GetPos()._y;
  }

  VoronoiDiagramGenerator vdg;
  vdg.generateVoronoi(xValues, yValues, count, bounds[0], bounds[1],
  bounds[2], bounds[3], 3);
  vdg.resetIterator();
  vdg.resetVerticesIterator();

  printf("\n------vertices---------\n");
  //collect the positions
  vector<Point> positions;
  float x1, y1;
  while (vdg.getNextVertex(x1, y1))
  {
  printf("GOT Point (%f,%f)\n", x1, y1);
  positions.push_back(Point(x1, y1));
  }

  //look for the biggest spot
  map<double, Point> map_dist_to_position;

  for (auto&& pos : positions)
  {
  double min_dist = FLT_MAX;

  for (auto&& ped : peds)
  {
  double dist = (pos - ped->GetPos()).NormSquare();
  if (dist < min_dist)
  {
  min_dist = dist;
  }
  }
  map_dist_to_position[min_dist] = pos;
  }

  //list the result
  for (auto&& mp : map_dist_to_position)
  {
  cout << "dist: " << mp.first << " pos: " << mp.second.toString()
  << endl;
  //agent->SetPos(mp.second, true);
  }

  //the elements are ordered.
  // so the last one has the largest distance
  if (!map_dist_to_position.empty())
  {
  agent->SetPos(map_dist_to_position.rbegin()->second, true);
  cout << "position:" << agent->GetPos().toString() << endl;
  //exit(0);

  } else
  {
  cout << "position not set:" << endl;
  cout << "size: " << map_dist_to_position.size() << endl;
  cout << " for " << peds.size() << " pedestrians" << endl;
  exit(0);
  }
  //exit(0);
  // float x1,y1,x2,y2;
  //while(vdg.getNext(x1,y1,x2,y2))
  //{
  //     printf("GOT Line (%f,%f)->(%f,%f)\n",x1,y1,x2, y2);
  //
  //}
  //compute the best position
  //exit(0);
  }
*/
363 364


365

366
void AgentsSourcesManager::ComputeBestPositionRandom(AgentsSource* src,
367
                                                     std::vector<Pedestrian*>& peds) const
368 369 370
{

     //generate the agents with default positions
371 372
     auto dist = src->GetStartDistribution();
     auto subroom = _building->GetRoom(dist->GetRoomId())->GetSubRoom(
373
          dist->GetSubroomID());
374 375
     vector<Point> positions = PedDistributor::PossiblePositions(*subroom);
     double bounds[4] = { 0, 0, 0, 0 };
376 377
     dist->Getbounds(bounds);

378 379
     std::vector<Pedestrian*> peds_without_place;

380 381
     vector<Point> extra_positions;

382 383
     std::vector<Pedestrian*>::iterator iter_ped;
     for (iter_ped = peds.begin(); iter_ped != peds.end(); )
384
     {
385 386 387
          //need to be called at each iteration
          SortPositionByDensity(positions, extra_positions);

388
          int index = -1;
389
          double radius = ( (*iter_ped)->GetEllipse() ).GetBmax()   ;
390 391

          //in the case a range was specified
392
          //just take the first element
393
          for (unsigned int a = 0; a < positions.size(); a++)
394
          {
395
               Point pos = positions[a];
396 397
               //cout<<"checking: "<<pos.toString()<<endl;
               // for positions inside bounds, check it there is enough space
398
               if ((bounds[0] <= pos._x) && (pos._x <= bounds[1])
399
                   && (bounds[2] <= pos._y) && (pos._y < bounds[3]))
400
               {
401

402
                    bool enough_space = true;
403

404 405 406
                    //checking enough space!!
                    vector<Pedestrian*> neighbours;
                    _building->GetGrid()->GetNeighbourhood(pos,neighbours);
407

408 409 410 411 412 413
                    for (const auto& ngh: neighbours)
                         if(  (ngh->GetPos() - pos).NormSquare() < 4*radius*radius )
                         {
                              enough_space = false;
                              break;
                         }
414 415


416 417 418 419 420
                    if( enough_space )
                    {
                         index = a;
                         break;
                    }
421

422 423
               }
          }
424
          if (index == -1)
425
          {
426
               if (positions.size())
427
               {
428
                    Log->Write(
429 430
                         "ERROR:\t AgentSourceManager Cannot distribute pedestrians in the mentioned area [%0.2f,%0.2f,%0.2f,%0.2f]",
                         bounds[0], bounds[1], bounds[2], bounds[3]);
431 432 433
                    Log->Write("     \t Specifying a subroom_id might help");
                    Log->Write("     \t %d positions were available",positions.size());
                    //exit(EXIT_FAILURE);
434
               }
435 436 437
               //dump the pedestrian, move iterator
               peds_without_place.push_back(*iter_ped);
               iter_ped=peds.erase(iter_ped);
438
          }
439
          else //we found a position with enough space
440 441 442
          {
               const Point& pos = positions[index];

443 444 445
               extra_positions.push_back(pos);
               (*iter_ped)->SetPos(pos, true); //true for the initial position
               positions.erase(positions.begin() + index);
446

447 448 449 450 451 452
               //at this point we have a position
               //so we can adjust the velocity
               //AdjustVelocityUsingWeidmann(ped);
               AdjustVelocityByNeighbour( (*iter_ped) );
               //move iterator
               iter_ped++;
453

454
          }
455 456

          //return the pedestrians without place
457
     }
458
     if(peds_without_place.size()>0)
459
          src->AddAgentsToPool(peds_without_place);
460
}
461

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
void AgentsSourcesManager::AdjustVelocityByNeighbour(Pedestrian* ped) const
{
     //get the density
     vector<Pedestrian*> neighbours;
     _building->GetGrid()->GetNeighbourhood(ped,neighbours);

     double speed=0.0;
     double radius_square=0.56*0.56;//corresponding to an area of 1m3
     int count=0;

     for(const auto& p: neighbours)
     {
          //only pedes in a sepcific rance
          if( (ped->GetPos()-p->GetPos()).NormSquare()<=radius_square)
          {
               //only peds with the same destination
               if(ped->GetExitIndex()==p->GetExitIndex())
               {
                    double dist1=ped->GetDistanceToNextTarget();
                    double dist2=p->GetDistanceToNextTarget();
                    //only peds in front of me
                    if(dist2<dist1)
                    {
                         speed+=p->GetV().Norm();
                         count++;
                    }
               }
          }

     }
     //mean speed
     if(count==0)
     {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
495
          speed=ped->GetEllipse().GetV0(); // FIXME:  bad fix for: peds without navline (ar.graf)
496
          //speed=ped->GetV0Norm();
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
     }
     else
     {
          speed=speed/count;
     }

     if(ped->FindRoute()!=-1)
     {
          //get the next destination point
          Point v =(ped->GetExitLine()->ShortestPoint(ped->GetPos())- ped->GetPos()).Normalized();
          v=v*speed;
          ped->SetV(v);
     }
     else
     {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
512
          Log->Write(">> ERROR:\t no route could be found for agent [%d] going to [%d]",ped->GetID(),ped->GetFinalDestination());
513 514 515 516 517 518
          //that will be most probably be fixed in the next computation step.
          // so do not abort
     }

}

519 520 521 522 523 524
void AgentsSourcesManager::AdjustVelocityUsingWeidmann(Pedestrian* ped) const
{
     //get the density
     vector<Pedestrian*> neighbours;
     _building->GetGrid()->GetNeighbourhood(ped,neighbours);

525
     //density in pers per m2
526 527
     double density = 1.0;
     //radius corresponding to a surface of 1m2
528
     //double radius_square=0.564*0.564;
529
     double radius_square=1.0;
530

531 532
     for(const auto& p: neighbours)
     {
533
          if( (ped->GetPos()-p->GetPos()).NormSquare()<=radius_square)
534 535
               density+=1.0;
     }
536 537
     density=density/(radius_square*M_PI);

538 539
     //get the velocity
     double density_max=5.4;
540

541 542 543 544 545 546
     //speed from taken from weidmann FD
     double speed=1.34*(1-exp(-1.913*(1.0/density-1.0/density_max)));
     if(speed>=ped->GetV0Norm())
     {
          speed=ped->GetV0Norm();
     }
547

548 549
     //set the velocity vector
     if(ped->FindRoute()!=-1)
550 551 552 553 554
     {
          //get the next destination point
          Point v =(ped->GetExitLine()->ShortestPoint(ped->GetPos())- ped->GetPos()).Normalized();
          v=v*speed;
          ped->SetV(v);
555
          //cout<<"density: "<<density<<endl;
556 557 558
     }
     else
     {
559
          Log->Write(">>> SOURCE ERROR:\t no route could be found for agent [%d] going to [%d]",ped->GetID(),ped->GetFinalDestination());
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
          //that will be most probably be fixed in the next computation step.
          // so do not abort
     }

}

void AgentsSourcesManager::SortPositionByDensity(std::vector<Point>& positions, std::vector<Point>& extra_positions) const
{
     std::multimap<double,Point> density2pt;
     //std::map<double,Point> density2pt;

     for(auto&& pt:positions)
     {
          vector<Pedestrian*> neighbours;
          _building->GetGrid()->GetNeighbourhood(pt,neighbours);
          //density in pers per m2
          double density = 0.0;
          double radius_square=0.40*0.40;

          for(const auto& p: neighbours)
          {
581 582
               //FIXME: p  can be null, if deleted in the main simulation thread.
               if( p && (pt-p->GetPos()).NormSquare()<=radius_square)
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
                    density+=1.0;
          }

          //consider the extra positions
          for(const auto& ptx: extra_positions)
          {
               if( (ptx-pt).NormSquare()<=radius_square)
                    density+=1.0;
          }
          density=density/(radius_square*M_PI);

          density2pt.insert(std::pair<double,Point>(density,pt));

     }

     //cout<<"------------------"<<positions.size()<<"-------"<<endl;
     positions.clear();
     for(auto&& d: density2pt)
     {
          positions.push_back(d.second);
          //     printf("density [%lf, %s]\n",d.first, d.second.toString().c_str());
604
     }
605 606

}
607 608 609 610 611 612 613 614 615 616 617


void AgentsSourcesManager::GenerateAgents()
{

     for (const auto& src : _sources)
     {
          src->GenerateAgentsAndAddToPool(src->GetMaxAgents(), _building);
     }
}

618 619 620
void AgentsSourcesManager::AddSource(std::shared_ptr<AgentsSource> src)
{
     _sources.push_back(src);
621
     _isCompleted=false;//at least one source was provided
622 623 624 625 626 627
}

const std::vector<std::shared_ptr<AgentsSource> >& AgentsSourcesManager::GetSources() const
{
     return _sources;
}
628 629 630

void AgentsSourcesManager::SetBuilding(Building* building)
{
631
     _building = building;
632
}
633 634 635

bool AgentsSourcesManager::IsCompleted() const
{
636
     return _isCompleted;
637
}
638

639 640 641 642 643
bool AgentsSourcesManager::IsRunning() const
{
     return _isRunning;
}

644

645 646 647 648 649 650 651 652 653 654
bool AgentsSourcesManager::IsBuildingUpdated() const
{
     return _buildingUpdated;
}

void AgentsSourcesManager::SetBuildingUpdated(bool update)
{
     _buildingUpdated = update;
}

655 656 657 658 659 660

void AgentsSourcesManager::SetRunning(bool running)
{
     _isRunning = running;
}

661 662 663 664
Building* AgentsSourcesManager::GetBuilding() const
{
     return _building;
}
665 666 667 668 669 670 671 672 673 674

long AgentsSourcesManager::GetMaxAgentNumber() const
{
     long pop=0;
     for (const auto& src : _sources)
     {
          pop+=src->GetMaxAgents();
     }
     return pop;
}
675 676 677 678 679 680 681 682


 int AgentsSourcesManager::GetMaxSimTime() const{
      return maxSimTime;
}
void AgentsSourcesManager::SetMaxSimTime(int t){
      maxSimTime = t;
}