Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
AGAdult.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2010-2024 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 <iostream>
30#include "AGWorkPosition.h"
31#include "AGAdult.h"
32
33
34// ===========================================================================
35// method definitions
36// ===========================================================================
38AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition>* wps) {
39 std::vector<AGWorkPosition*> freePos;
40 for (std::vector<AGWorkPosition>::iterator i = wps->begin(); i != wps->end(); ++i) {
41 if (!i->isTaken()) {
42 freePos.push_back(&*i);
43 }
44 }
45 if (freePos.empty()) {
46 return nullptr;
47 }
48 return RandHelper::getRandomFrom(freePos);
49}
50
51
53 : AGPerson(age), work(nullptr) {}
54
55
56void
58 std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
59}
60
61
62void
63AGAdult::tryToWork(double rate, std::vector<AGWorkPosition>* wps) {
64 if (decide(rate)) {
65 // Select the new work position before giving up the current one.
66 // This avoids that the current one is the same as the new one.
68
69 if (work != nullptr) {
70 work->let();
71 }
72 work = newWork;
73 if (work != nullptr) {
74 work->take(this);
75 }
76 } else {
77 if (work != nullptr) {
78 // Also sets work = 0 with the call back lostWorkPosition
79 work->let();
80 }
81 }
82}
83
84
85bool
87 return (work != nullptr);
88}
89
90
91void
93 work = nullptr;
94}
95
96
97void
99 if (work != nullptr) {
100 work->let();
101 }
102}
103
104
105const AGWorkPosition&
107 if (work != nullptr) {
108 return *work;
109 }
110 throw ProcessError("AGAdult::getWorkPosition: Adult is unemployed.");
111}
112
113
114/****************************************************************************/
void print() const
Puts out a summary of the attributes.
Definition AGAdult.cpp:57
void tryToWork(double employmentRate, std::vector< AGWorkPosition > *wps)
Tries to get a new work position.
Definition AGAdult.cpp:63
bool isWorking() const
States whether this person occupies a work position at present.
Definition AGAdult.cpp:86
AGAdult(int age)
Initialises the base class and the own attributes.
Definition AGAdult.cpp:52
static AGWorkPosition * randomFreeWorkPosition(std::vector< AGWorkPosition > *wps)
Randomly selects a free work position from the list.
Definition AGAdult.cpp:38
const AGWorkPosition & getWorkPosition() const
Provides the work position of the adult.
Definition AGAdult.cpp:106
void lostWorkPosition()
Called when the adult has lost her job.
Definition AGAdult.cpp:92
AGWorkPosition * work
Definition AGAdult.h:110
void resignFromWorkPosition()
Called when the adult should resign her job.
Definition AGAdult.cpp:98
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:206