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-2019 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
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 // SPDX-License-Identifier: EPL-2.0
11 /****************************************************************************/
20 // Person in working age: can be linked to a work position.
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include "AGAdult.h"
30 #include "AGWorkPosition.h"
32 #include <iostream>
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
39 AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition>* wps) {
40  std::vector<AGWorkPosition*> freePos;
41  for (std::vector<AGWorkPosition>::iterator i = wps->begin(); i != wps->end(); ++i) {
42  if (!i->isTaken()) {
43  freePos.push_back(&*i);
44  }
45  }
46  if (freePos.empty()) {
47  return nullptr;
48  }
49  return RandHelper::getRandomFrom(freePos);
50 }
51 
52 
54  : AGPerson(age), work(nullptr) {}
55 
56 
57 void
58 AGAdult::print() const {
59  std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
60 }
61 
62 
63 void
64 AGAdult::tryToWork(double rate, std::vector<AGWorkPosition>* wps) {
65  if (decide(rate)) {
66  // Select the new work position before giving up the current one.
67  // This avoids that the current one is the same as the new one.
68  AGWorkPosition* newWork = randomFreeWorkPosition(wps);
69 
70  if (work != nullptr) {
71  work->let();
72  }
73  work = newWork;
74  work->take(this);
75  } else {
76  if (work != nullptr) {
77  // Also sets work = 0 with the call back lostWorkPosition
78  work->let();
79  }
80  }
81 }
82 
83 
84 bool
86  return (work != nullptr);
87 }
88 
89 
90 void
92  work = nullptr;
93 }
94 
95 
96 void
98  if (work != nullptr) {
99  work->let();
100  }
101 }
102 
103 
104 const AGWorkPosition&
106  if (work != nullptr) {
107  return *work;
108  }
109  throw std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed.");
110 }
111 
112 /****************************************************************************/
AGAdult::AGAdult
AGAdult(int age)
Initialises the base class and the own attributes.
Definition: AGAdult.cpp:53
AGAdult::lostWorkPosition
void lostWorkPosition()
Called when the adult has lost her job.
Definition: AGAdult.cpp:91
AGPerson::decide
virtual bool decide(double probability) const
Lets the person make a decision.
Definition: AGPerson.cpp:56
AGAdult.h
AGAdult::resignFromWorkPosition
void resignFromWorkPosition()
Called when the adult should resign her job.
Definition: AGAdult.cpp:97
AGAdult::randomFreeWorkPosition
static AGWorkPosition * randomFreeWorkPosition(std::vector< AGWorkPosition > *wps)
Randomly selects a free work position from the list.
Definition: AGAdult.cpp:39
AGWorkPosition.h
AGAdult::tryToWork
void tryToWork(double employmentRate, std::vector< AGWorkPosition > *wps)
Tries to get a new work position.
Definition: AGAdult.cpp:64
AGAdult::isWorking
bool isWorking() const
States whether this person occupies a work position at present.
Definition: AGAdult.cpp:85
AGAdult::getWorkPosition
const AGWorkPosition & getWorkPosition() const
Provides the work position of the adult.
Definition: AGAdult.cpp:105
AGPerson
Base class of every person in the city (adults and children)
Definition: AGPerson.h:42
AGAdult::print
void print() const
Puts out a summary of the attributes.
Definition: AGAdult.cpp:58
RandHelper::getRandomFrom
static const T & getRandomFrom(const std::vector< T > &v, std::mt19937 *rng=0)
Returns a random element from the given vector.
Definition: RandHelper.h:154
AGAdult::work
AGWorkPosition * work
Definition: AGAdult.h:113
AGWorkPosition::take
void take(AGAdult *ad)
Definition: AGWorkPosition.cpp:123
AGWorkPosition
Definition: AGWorkPosition.h:49
AGPerson::age
int age
Definition: AGPerson.h:65
AGWorkPosition::let
void let()
Definition: AGWorkPosition.cpp:113
config.h
RandHelper.h