SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AGAdult.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Person in working age: can be linked to a work position.
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2010-2016 DLR (http://www.dlr.de/) and contributors
14 // activitygen module
15 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include "AGAdult.h"
37 #include "AGWorkPosition.h"
39 #include <iostream>
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46 AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition>* wps) {
47  std::vector<AGWorkPosition*> freePos;
48  for (std::vector<AGWorkPosition>::iterator i = wps->begin(); i != wps->end(); ++i) {
49  if (!i->isTaken()) {
50  freePos.push_back(&*i);
51  }
52  }
53  if (freePos.empty()) {
54  return 0;
55  }
56  return RandHelper::getRandomFrom(freePos);
57 }
58 
59 
61  : AGPerson(age), work(0) {}
62 
63 
64 void
65 AGAdult::print() const {
66  std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
67 }
68 
69 
70 void
71 AGAdult::tryToWork(SUMOReal rate, std::vector<AGWorkPosition>* wps) {
72  if (decide(rate)) {
73  // Select the new work position before giving up the current one.
74  // This avoids that the current one is the same as the new one.
75  AGWorkPosition* newWork = randomFreeWorkPosition(wps);
76 
77  if (work != 0) {
78  work->let();
79  }
80  work = newWork;
81  work->take(this);
82  } else {
83  if (work != 0) {
84  // Also sets work = 0 with the call back lostWorkPosition
85  work->let();
86  }
87  }
88 }
89 
90 
91 bool
93  return (work != 0);
94 }
95 
96 
97 void
99  work = 0;
100 }
101 
102 
103 void
105  if (work != 0) {
106  work->let();
107  }
108 }
109 
110 
111 const AGWorkPosition&
113  if (work != 0) {
114  return *work;
115  }
116  throw std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed.");
117 }
118 
119 /****************************************************************************/
static const T & getRandomFrom(const std::vector< T > &v)
Returns a random element from the given vector.
Definition: RandHelper.h:114
void tryToWork(SUMOReal employmentRate, std::vector< AGWorkPosition > *wps)
Tries to get a new work position.
Definition: AGAdult.cpp:71
void take(AGAdult *ad)
AGWorkPosition * work
Definition: AGAdult.h:120
virtual bool decide(SUMOReal probability) const
Lets the person make a decision.
Definition: AGPerson.cpp:63
void print() const
Puts out a summary of the attributes.
Definition: AGAdult.cpp:65
void resignFromWorkPosition()
Called when the adult should resign her job.
Definition: AGAdult.cpp:104
static AGWorkPosition * randomFreeWorkPosition(std::vector< AGWorkPosition > *wps)
Randomly selects a free work position from the list.
Definition: AGAdult.cpp:46
void lostWorkPosition()
Called when the adult has lost her job.
Definition: AGAdult.cpp:98
AGAdult(int age)
Initialises the base class and the own attributes.
Definition: AGAdult.cpp:60
int age
Definition: AGPerson.h:72
#define SUMOReal
Definition: config.h:214
bool isWorking() const
States whether this person occupies a work position at present.
Definition: AGAdult.cpp:92
const AGWorkPosition & getWorkPosition() const
Provides the work position of the adult.
Definition: AGAdult.cpp:112
Base class of every person in the city (adults and children)
Definition: AGPerson.h:49