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.sourceforge.net/
12 // Copyright (C) 2001-2012 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 /****************************************************************************/