SUMO - Simulation of Urban MObility
AGAdult.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2010-2017 German Aerospace Center (DLR) and others.
4 // activitygen module
5 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
6 /****************************************************************************/
7 //
8 // This program and the accompanying materials
9 // are made available under the terms of the Eclipse Public License v2.0
10 // which accompanies this distribution, and is available at
11 // http://www.eclipse.org/legal/epl-v20.html
12 //
13 /****************************************************************************/
22 // Person in working age: can be linked to a work position.
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  std::vector<AGWorkPosition*> freePos;
47  for (std::vector<AGWorkPosition>::iterator i = wps->begin(); i != wps->end(); ++i) {
48  if (!i->isTaken()) {
49  freePos.push_back(&*i);
50  }
51  }
52  if (freePos.empty()) {
53  return 0;
54  }
55  return RandHelper::getRandomFrom(freePos);
56 }
57 
58 
60  : AGPerson(age), work(0) {}
61 
62 
63 void
64 AGAdult::print() const {
65  std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
66 }
67 
68 
69 void
70 AGAdult::tryToWork(double rate, std::vector<AGWorkPosition>* wps) {
71  if (decide(rate)) {
72  // Select the new work position before giving up the current one.
73  // This avoids that the current one is the same as the new one.
74  AGWorkPosition* newWork = randomFreeWorkPosition(wps);
75 
76  if (work != 0) {
77  work->let();
78  }
79  work = newWork;
80  work->take(this);
81  } else {
82  if (work != 0) {
83  // Also sets work = 0 with the call back lostWorkPosition
84  work->let();
85  }
86  }
87 }
88 
89 
90 bool
92  return (work != 0);
93 }
94 
95 
96 void
98  work = 0;
99 }
100 
101 
102 void
104  if (work != 0) {
105  work->let();
106  }
107 }
108 
109 
110 const AGWorkPosition&
112  if (work != 0) {
113  return *work;
114  }
115  throw std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed.");
116 }
117 
118 /****************************************************************************/
void take(AGAdult *ad)
const AGWorkPosition & getWorkPosition() const
Provides the work position of the adult.
Definition: AGAdult.cpp:111
virtual bool decide(double probability) const
Lets the person make a decision.
Definition: AGPerson.cpp:62
bool isWorking() const
States whether this person occupies a work position at present.
Definition: AGAdult.cpp:91
AGWorkPosition * work
Definition: AGAdult.h:119
void resignFromWorkPosition()
Called when the adult should resign her job.
Definition: AGAdult.cpp:103
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:97
void tryToWork(double employmentRate, std::vector< AGWorkPosition > *wps)
Tries to get a new work position.
Definition: AGAdult.cpp:70
AGAdult(int age)
Initialises the base class and the own attributes.
Definition: AGAdult.cpp:59
static const T & getRandomFrom(const std::vector< T > &v, std::mt19937 *rng=0)
Returns a random element from the given vector.
Definition: RandHelper.h:150
void print() const
Puts out a summary of the attributes.
Definition: AGAdult.cpp:64
int age
Definition: AGPerson.h:71
Base class of every person in the city (adults and children)
Definition: AGPerson.h:48