AgentsSource.cpp 4.75 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

#include "AgentsSource.h"
#include "Pedestrian.h"
32
#include "StartDistribution.h"
33 34 35 36
#include "../IO/OutputHandler.h"

#include <iostream>

37 38
AgentsSource::AgentsSource(int id, const std::string& caption,int max_agents,int group_id,int frequency, bool greedy, double time, int agent_id):
     _id(id), _frequency(frequency), _maxAgents(max_agents), _groupID(group_id), _caption(caption), _greedy(greedy), _agent_id(agent_id), _time(time)
39
{
40
    _agentsGenerated=0;
41 42 43 44
    _boundaries[0] = 0;
    _boundaries[1] = 0;
    _boundaries[2] = 0;
    _boundaries[3] = 0;
45 46 47 48 49 50 51
    _agents.clear();
}

AgentsSource::~AgentsSource()
{
}

52
void AgentsSource::GenerateAgentsAndAddToPool(int count, Building* building)
53
{
54 55 56 57 58 59 60 61 62
     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)
63
     {
64 65
          ped.insert(ped.begin(),_agents.begin(),_agents.begin()+count);
          _agents.erase(_agents.begin(),_agents.begin()+count);
66 67 68 69 70 71 72 73
     }
     else
     {
          ped.insert(ped.begin(),_agents.begin(),_agents.end());
          _agents.clear();
     }
}

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

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

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

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

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

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

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

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
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;
}

136 137 138 139 140 141 142 143 144 145 146
int AgentsSource::GetAgentId() const
{
     return _agent_id;
}

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


147 148 149 150
int AgentsSource::GetMaxAgents() const
{
     return _maxAgents;
}
151 152 153 154 155 156

void AgentsSource::SetStartDistribution(std::shared_ptr<StartDistribution> startDistribution)
{
     _startDistribution=startDistribution;
}

157 158 159 160 161
const std::shared_ptr<StartDistribution> AgentsSource::GetStartDistribution() const
{
     return _startDistribution;
}

162 163 164
void AgentsSource::GenerateAgents(std::vector<Pedestrian*>& peds, int count, Building* building)
{
     std::vector<Point> emptyPositions;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
165
     int pid=Pedestrian::GetAgentsCreated(); // will be discarded
166 167 168 169 170 171 172
     for(int i=0;i<count;i++)
     {
          peds.push_back(_startDistribution->GenerateAgent(building, &pid,emptyPositions));
     }
}


173 174 175 176
void AgentsSource::Dump() const
{
     Log->Write("\n--------------------------");
     Log->Write("Dumping Source");
177
     Log->Write("Caption: %s", this->GetCaption().c_str());
178 179 180 181
     Log->Write("ID: %d", _id);
     Log->Write("Group ID: %d", _groupID);
     Log->Write("Frequency: %d", _frequency);
     Log->Write("Agents Max: %d", _maxAgents);
182
     Log->Write("Agents Pool: %d", _agents.size());
183 184 185
     Log->Write("Agent id: %d", this->GetAgentId());
     Log->Write("Time: %f", this->GetPlanTime());
     Log->Write("\n--------------------------\n");
186 187
     //getc(stdin);

188
}