/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2010-2025 German Aerospace Center (DLR) and others.3// activitygen module4// Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)5// This program and the accompanying materials are made available under the6// terms of the Eclipse Public License 2.0 which is available at7// https://www.eclipse.org/legal/epl-2.0/8// This Source Code may also be made available under the following Secondary9// Licenses when the conditions for such availability set forth in the Eclipse10// Public License 2.0 are satisfied: GNU General Public License, version 211// or later which is available at12// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html13// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later14/****************************************************************************/15/// @file AGAdult.cpp16/// @author Piotr Woznica17/// @author Walter Bamberger18/// @author Daniel Krajzewicz19/// @author Michael Behrisch20/// @date July 201021///22// Person in working age: can be linked to a work position.23/****************************************************************************/24#include <config.h>2526#include <iostream>27#include <utils/common/RandHelper.h>28#include <utils/common/UtilExceptions.h>29#include "AGWorkPosition.h"30#include "AGAdult.h"313233// ===========================================================================34// method definitions35// ===========================================================================36AGWorkPosition*37AGAdult::randomFreeWorkPosition(std::vector<AGWorkPosition>* wps) {38std::vector<AGWorkPosition*> freePos;39for (std::vector<AGWorkPosition>::iterator i = wps->begin(); i != wps->end(); ++i) {40if (!i->isTaken()) {41freePos.push_back(&*i);42}43}44if (freePos.empty()) {45return nullptr;46}47return RandHelper::getRandomFrom(freePos);48}495051AGAdult::AGAdult(int age)52: AGPerson(age), work(nullptr) {}535455void56AGAdult::print() const {57std::cout << "- AGAdult: Age=" << age << " Work=" << work << std::endl;58}596061void62AGAdult::tryToWork(double rate, std::vector<AGWorkPosition>* wps) {63if (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.66AGWorkPosition* newWork = randomFreeWorkPosition(wps);6768if (work != nullptr) {69work->let();70}71work = newWork;72if (work != nullptr) {73work->take(this);74}75} else {76if (work != nullptr) {77// Also sets work = 0 with the call back lostWorkPosition78work->let();79}80}81}828384bool85AGAdult::isWorking() const {86return (work != nullptr);87}888990void91AGAdult::lostWorkPosition() {92work = nullptr;93}949596void97AGAdult::resignFromWorkPosition() {98if (work != nullptr) {99work->let();100}101}102103104const AGWorkPosition&105AGAdult::getWorkPosition() const {106if (work != nullptr) {107return *work;108}109throw ProcessError("AGAdult::getWorkPosition: Adult is unemployed.");110}111112113/****************************************************************************/114115116