AgentsSource.cpp 5.49 KB
Newer Older
1 2 3
/**
 * \file        AgentSource.cpp
 * \date        Apr 04, 2015
4 5
 * \version     v0.7
 * \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 32

#include "AgentsSource.h"
#include "Pedestrian.h"

33 34 35
AgentsSource::AgentsSource(int id, const std::string& caption,int max_agents,int group_id,int frequency, bool greedy, double time, int agent_id, float startx, float starty):
     _id(id), _frequency(frequency), _maxAgents(max_agents), _groupID(group_id), _caption(caption), _greedy(greedy), _agent_id(agent_id), _time(time), _startx(startx), _starty(starty)

36
{
37
    _agentsGenerated=0;
38 39 40 41
    _boundaries[0] = 0;
    _boundaries[1] = 0;
    _boundaries[2] = 0;
    _boundaries[3] = 0;
42 43 44 45 46 47 48
    _agents.clear();
}

AgentsSource::~AgentsSource()
{
}

49
void AgentsSource::GenerateAgentsAndAddToPool(int count, Building* building)
50
{
51 52 53 54 55 56 57 58 59
     std::vector<Pedestrian*> peds;
     GenerateAgents(peds, count, building);
     _agents.insert(_agents.begin(),peds.begin(),peds.end());
     _agentsGenerated+=count;
}

void AgentsSource::RemoveAgentsFromPool(std::vector<Pedestrian*>& ped, int count)
{
     if((int)_agents.size()>=count)
60
     {
61 62
          ped.insert(ped.begin(),_agents.begin(),_agents.begin()+count);
          _agents.erase(_agents.begin(),_agents.begin()+count);
63 64 65 66 67 68 69 70
     }
     else
     {
          ped.insert(ped.begin(),_agents.begin(),_agents.end());
          _agents.clear();
     }
}

71 72 73 74 75
void AgentsSource::AddAgentsToPool(std::vector<Pedestrian*>& peds)
{
     _agents.insert(_agents.begin(),peds.begin(),peds.end());
}

Mohcine Chraibi's avatar
Mohcine Chraibi committed
76 77 78 79
bool AgentsSource::Greedy() const
{
      return _greedy;
}
80 81
int AgentsSource::GetPoolSize() const
{
Mohcine Chraibi's avatar
Mohcine Chraibi committed
82
     return (int)_agents.size();
83 84
}

85
void AgentsSource::AddToPool(Pedestrian* ped)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
     _agents.push_back(ped);
}

int AgentsSource::GetAgentsGenerated() const
{
     return _agentsGenerated;
}

void AgentsSource::SetAgentsGenerated(int agentsGenerated)
{
     _agentsGenerated = agentsGenerated;
}

const double* AgentsSource::GetBoundaries() const
{
     return _boundaries;
}

105 106 107 108 109 110 111 112
void AgentsSource::Setboundaries(double * bounds)
{
     _boundaries[0]=bounds[0];
     _boundaries[1]=bounds[1];
     _boundaries[2]=bounds[2];
     _boundaries[3]=bounds[3];
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
const std::string& AgentsSource::GetCaption() const
{
     return _caption;
}

int AgentsSource::GetFrequency() const
{
     return _frequency;
}

int AgentsSource::GetGroupId() const
{
     return _groupID;
}

int AgentsSource::GetId() const
{
     return _id;
}

133 134 135 136 137 138 139 140 141 142 143
int AgentsSource::GetAgentId() const
{
     return _agent_id;
}

double AgentsSource::GetPlanTime() const
{
     return _time;
}


144 145 146 147
int AgentsSource::GetMaxAgents() const
{
     return _maxAgents;
}
148

149 150 151 152 153 154 155 156 157 158 159 160
float AgentsSource::GetStartX() const
{
     return _startx;
}

float AgentsSource::GetStartY() const
{
     return _starty;
}



161 162 163 164 165
void AgentsSource::SetStartDistribution(std::shared_ptr<StartDistribution> startDistribution)
{
     _startDistribution=startDistribution;
}

166 167 168 169 170
const std::shared_ptr<StartDistribution> AgentsSource::GetStartDistribution() const
{
     return _startDistribution;
}

171 172 173
void AgentsSource::GenerateAgents(std::vector<Pedestrian*>& peds, int count, Building* building)
{
     std::vector<Point> emptyPositions;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
174 175 176 177 178 179 180 181 182
     int pid;

          // if(this->GetAgentId() < 0)
     // {
     //      // TODO: get the reserved ids by other sources
     //      std::vector<int> reserved_ids;
     //      for (const auto &source: _start_dis_sources)
     //           if(source->GetAgentId() >= 0)
     //                reserved_ids.push_back(source->GetAgentId());
183

Mohcine Chraibi's avatar
Mohcine Chraibi committed
184
     //      while( std::find(reserved_ids.begin(), reserved_ids.end(), pid) != reserved_ids.end() ){
185 186
     //           std::cout << "\n\nSOURCE  SORRY " << pid << " is reserved!\n";
     //           pid += 1;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
187 188 189 190
     //      }
     // }

     pid = (this->GetAgentId() >=0 )?this->GetAgentId() : Pedestrian::GetAgentsCreated() + building->GetAllPedestrians().size();
191 192 193 194 195 196 197
     for(int i=0;i<count;i++)
     {
          peds.push_back(_startDistribution->GenerateAgent(building, &pid,emptyPositions));
     }
}


198 199 200 201
void AgentsSource::Dump() const
{
     Log->Write("\n--------------------------");
     Log->Write("Dumping Source");
202
     Log->Write("Caption: %s", this->GetCaption().c_str());
203 204 205 206
     Log->Write("ID: %d", _id);
     Log->Write("Group ID: %d", _groupID);
     Log->Write("Frequency: %d", _frequency);
     Log->Write("Agents Max: %d", _maxAgents);
207
     Log->Write("Agents Pool: %d", _agents.size());
208 209 210
     Log->Write("Agent id: %d", this->GetAgentId());
     Log->Write("Time: %f", this->GetPlanTime());
     Log->Write("\n--------------------------\n");
211 212
     //getc(stdin);

213
}