Eclipse 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-2022 German Aerospace Center (DLR) and others.
4 // activitygen module
5 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
6 // This program and the accompanying materials are made available under the
7 // terms of the Eclipse Public License 2.0 which is available at
8 // https://www.eclipse.org/legal/epl-2.0/
9 // This Source Code may also be made available under the following Secondary
10 // Licenses when the conditions for such availability set forth in the Eclipse
11 // Public License 2.0 are satisfied: GNU General Public License, version 2
12 // or later which is available at
13 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
15 /****************************************************************************/
23 // Person in working age: can be linked to a work position.
24 /****************************************************************************/
25 #include <config.h>
26 
27 #include "AGAdult.h"
28 #include "AGWorkPosition.h"
30 #include <iostream>
31 
32 
33 // ===========================================================================
34 // method definitions
35 // ===========================================================================
37 AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition>* wps) {
38  std::vector<AGWorkPosition*> freePos;
39  for (std::vector<AGWorkPosition>::iterator i = wps->begin(); i != wps->end(); ++i) {
40  if (!i->isTaken()) {
41  freePos.push_back(&*i);
42  }
43  }
44  if (freePos.empty()) {
45  return nullptr;
46  }
47  return RandHelper::getRandomFrom(freePos);
48 }
49 
50 
52  : AGPerson(age), work(nullptr) {}
53 
54 
55 void
56 AGAdult::print() const {
57  std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
58 }
59 
60 
61 void
62 AGAdult::tryToWork(double rate, std::vector<AGWorkPosition>* wps) {
63  if (decide(rate)) {
64  // Select the new work position before giving up the current one.
65  // This avoids that the current one is the same as the new one.
66  AGWorkPosition* newWork = randomFreeWorkPosition(wps);
67 
68  if (work != nullptr) {
69  work->let();
70  }
71  work = newWork;
72  work->take(this);
73  } else {
74  if (work != nullptr) {
75  // Also sets work = 0 with the call back lostWorkPosition
76  work->let();
77  }
78  }
79 }
80 
81 
82 bool
84  return (work != nullptr);
85 }
86 
87 
88 void
90  work = nullptr;
91 }
92 
93 
94 void
96  if (work != nullptr) {
97  work->let();
98  }
99 }
100 
101 
102 const AGWorkPosition&
104  if (work != nullptr) {
105  return *work;
106  }
107  throw std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed.");
108 }
109 
110 
111 /****************************************************************************/
void print() const
Puts out a summary of the attributes.
Definition: AGAdult.cpp:56
void tryToWork(double employmentRate, std::vector< AGWorkPosition > *wps)
Tries to get a new work position.
Definition: AGAdult.cpp:62
bool isWorking() const
States whether this person occupies a work position at present.
Definition: AGAdult.cpp:83
AGAdult(int age)
Initialises the base class and the own attributes.
Definition: AGAdult.cpp:51
static AGWorkPosition * randomFreeWorkPosition(std::vector< AGWorkPosition > *wps)
Randomly selects a free work position from the list.
Definition: AGAdult.cpp:37
const AGWorkPosition & getWorkPosition() const
Provides the work position of the adult.
Definition: AGAdult.cpp:103
void lostWorkPosition()
Called when the adult has lost her job.
Definition: AGAdult.cpp:89
AGWorkPosition * work
Definition: AGAdult.h:110
void resignFromWorkPosition()
Called when the adult should resign her job.
Definition: AGAdult.cpp:95
Base class of every person in the city (adults and children)
Definition: AGPerson.h:39
virtual bool decide(double probability) const
Lets the person make a decision.
Definition: AGPerson.cpp:54
int age
Definition: AGPerson.h:62
void take(AGAdult *ad)
static const T & getRandomFrom(const std::vector< T > &v, SumoRNG *rng=nullptr)
Returns a random element from the given vector.
Definition: RandHelper.h:208