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 /****************************************************************************/
19 // Person in working age: can be linked to a work position.
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include "AGAdult.h"
29 #include "AGWorkPosition.h"
31 #include <iostream>
32 
33 
34 // ===========================================================================
35 // method definitions
36 // ===========================================================================
38 AGAdult::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 
56 void
57 AGAdult::print() const {
58  std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;
59 }
60 
61 
62 void
63 AGAdult::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.
67  AGWorkPosition* newWork = randomFreeWorkPosition(wps);
68 
69  if (work != nullptr) {
70  work->let();
71  }
72  work = newWork;
73  work->take(this);
74  } else {
75  if (work != nullptr) {
76  // Also sets work = 0 with the call back lostWorkPosition
77  work->let();
78  }
79  }
80 }
81 
82 
83 bool
85  return (work != nullptr);
86 }
87 
88 
89 void
91  work = nullptr;
92 }
93 
94 
95 void
97  if (work != nullptr) {
98  work->let();
99  }
100 }
101 
102 
103 const AGWorkPosition&
105  if (work != nullptr) {
106  return *work;
107  }
108  throw std::runtime_error("AGAdult::getWorkPosition: Adult is unemployed.");
109 }
110 
111 /****************************************************************************/
AGAdult::AGAdult
AGAdult(int age)
Initialises the base class and the own attributes.
Definition: AGAdult.cpp:52
AGAdult::lostWorkPosition
void lostWorkPosition()
Called when the adult has lost her job.
Definition: AGAdult.cpp:90
AGPerson::decide
virtual bool decide(double probability) const
Lets the person make a decision.
Definition: AGPerson.cpp:55
AGAdult.h
AGAdult::resignFromWorkPosition
void resignFromWorkPosition()
Called when the adult should resign her job.
Definition: AGAdult.cpp:96
AGAdult::randomFreeWorkPosition
static AGWorkPosition * randomFreeWorkPosition(std::vector< AGWorkPosition > *wps)
Randomly selects a free work position from the list.
Definition: AGAdult.cpp:38
AGWorkPosition.h
AGAdult::tryToWork
void tryToWork(double employmentRate, std::vector< AGWorkPosition > *wps)
Tries to get a new work position.
Definition: AGAdult.cpp:63
AGAdult::isWorking
bool isWorking() const
States whether this person occupies a work position at present.
Definition: AGAdult.cpp:84
AGAdult::getWorkPosition
const AGWorkPosition & getWorkPosition() const
Provides the work position of the adult.
Definition: AGAdult.cpp:104
AGPerson
Base class of every person in the city (adults and children)
Definition: AGPerson.h:41
AGAdult::print
void print() const
Puts out a summary of the attributes.
Definition: AGAdult.cpp:57
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:149
AGAdult::work
AGWorkPosition * work
Definition: AGAdult.h:112
AGWorkPosition::take
void take(AGAdult *ad)
Definition: AGWorkPosition.cpp:122
AGWorkPosition
Definition: AGWorkPosition.h:48
AGPerson::age
int age
Definition: AGPerson.h:64
AGWorkPosition::let
void let()
Definition: AGWorkPosition.cpp:112
config.h
RandHelper.h