SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AGAdult.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Person in working age: can be linked to a work position.
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 // activitygen module
14 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include "AGAdult.h"
36 #include "AGWorkPosition.h"
38 #include <iostream>
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
45 AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition>* wps) {
46  size_t wpsIndex = 0;
47 
48  // TODO: Could end up in an endless loop
49  do {
50  wpsIndex = RandHelper::rand(wps->size());
51  } while (wps->at(wpsIndex).isTaken());
52 
53  return &wps->at(wpsIndex);
54 }
55 
56 /****************************************************************************/
57 
59  : AGPerson(age), work(0) {}
60 
61 /****************************************************************************/
62 
63 void
64 AGAdult::print() const {
65  std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
66 }
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 
92 bool
94  return (work != 0);
95 }
96 
97 /****************************************************************************/
98 
99 void
101  work = 0;
102 }
103 
104 /****************************************************************************/
105 
106 void
108  if (work != 0) {
109  work->let();
110  }
111 }
112 
113 /****************************************************************************/
114 
115 const AGWorkPosition&
117  if (work != 0) {
118  return *work;
119  }
120 
121  else {
122  throw(std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed."));
123  }
124 }
125 
126 /****************************************************************************/
void tryToWork(SUMOReal employmentRate, std::vector< AGWorkPosition > *wps)
Tries to get a new work position.
Definition: AGAdult.cpp:71
void take(AGAdult *ad)
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
AGWorkPosition * work
Definition: AGAdult.h:119
virtual bool decide(SUMOReal probability) const
Lets the person make a decision.
Definition: AGPerson.cpp:62
void print() const
Puts out a summary of the attributes.
Definition: AGAdult.cpp:64
void resignFromWorkPosition()
Called when the adult should resign her job.
Definition: AGAdult.cpp:107
static AGWorkPosition * randomFreeWorkPosition(std::vector< AGWorkPosition > *wps)
Randomly selects a free work position from the list.
Definition: AGAdult.cpp:45
void lostWorkPosition()
Called when the adult has lost her job.
Definition: AGAdult.cpp:100
AGAdult(int age)
Initialises the base class and the own attributes.
Definition: AGAdult.cpp:58
int age
Definition: AGPerson.h:72
#define SUMOReal
Definition: config.h:215
bool isWorking() const
States whether this person occupies a work position at present.
Definition: AGAdult.cpp:93
const AGWorkPosition & getWorkPosition() const
Provides the work position of the adult.
Definition: AGAdult.cpp:116
Base class of every person in the city (adults and children)
Definition: AGPerson.h:49