BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
GreedyExit.cpp
Go to the documentation of this file.
1#include "GreedyExit.h"
3#include <cassert>
4
5using namespace BPMNOS::Execution;
6
8 : GreedyDispatcher(evaluator)
9{
10}
11
13 mediator->addSubscriber(this,
15 );
17}
18
19void GreedyExit::notice(const Observable* observable) {
20 if ( observable->getObservableType() == Observable::Type::ExitRequest ) {
21 assert( dynamic_cast<const DecisionRequest*>(observable) );
22 auto request = static_cast<const DecisionRequest*>(observable);
23 auto decision = std::make_shared<ExitDecision>(request->token, evaluator);
24 decisionsWithoutEvaluation.emplace_back( request->token->weak_from_this(), request->weak_from_this(), decision );
25 }
26 else {
27 GreedyDispatcher::notice(observable);
28 }
29}
30
31
32std::shared_ptr<Event> GreedyExit::dispatchEvent( const SystemState* systemState ) {
33//std::cout << "dispatchEvent" << std::endl;
34 if ( systemState->currentTime > timestamp ) {
35 timestamp = systemState->currentTime;
36 clockTick();
37 }
38
39 for (auto it = decisionsWithoutEvaluation.begin(); it != decisionsWithoutEvaluation.end(); ) {
40 auto [ token_ptr, request_ptr, decision ] = std::move(*it); // Move out the tuple to avoid dangling reference
41 it = decisionsWithoutEvaluation.erase(it);
42 assert(decision);
43
44 // Call decision->evaluate() and add the evaluation
45 auto reward = decision->evaluate();
46 addEvaluation(token_ptr, request_ptr, decision, reward);
47 if ( reward.has_value() ) {
48 return std::make_shared<ExitEvent>(decision->token);
49 }
50 }
51
52 // all evaluated decisions are infeasible unless a previously dispatched decision was not deployed
53 for ( auto decisionTuple : evaluatedDecisions ) {
54 constexpr std::size_t last = std::tuple_size<decltype(decisionTuple)>::value - 1;
55 std::weak_ptr<Event>& event_ptr = std::get<last>(decisionTuple);
56 if ( auto event = event_ptr.lock();
57 event && std::get<0>(decisionTuple) < std::numeric_limits<double>::max()
58 ) {
59//std::cerr << "\nBest decision " << event->jsonify() << " evaluated with " << std::get<0>(decisionTuple) << std::endl;
60 return event;
61 }
62 else {
63 // best decision is infeasible, no need to inspect others
64 break;
65 }
66 }
67
68 return nullptr;
69}
70
Represents an abstract base class for an evaluator determining feasibility and reward of a decision.
Definition Evaluator.h:15
Class for dispatching the event with the best evaluation.
void addEvaluation(WeakPtrs..., std::shared_ptr< Decision > decision, std::optional< double > reward)
void notice(const Observable *observable) override
void connect(Mediator *mediator) override
void notice(const Observable *observable) override
void connect(Mediator *mediator) override
GreedyExit(Evaluator *evaluator)
Definition GreedyExit.cpp:7
std::shared_ptr< Event > dispatchEvent(const SystemState *systemState) override
void addSubscriber(Observer *subscriber, ObservableTypes... observableTypes)
Definition Notifier.h:17
A class representing the state that the execution or simulation of a given scenario is in.
Definition SystemState.h:21
BPMNOS::number currentTime
Timestamp holding the point in time that the engine is in (this is usually representing now).
Definition SystemState.h:37
Represents a pending decision.
Represents an abstract base class for a class that is an event listener and notifier.
Definition Mediator.h:13
virtual constexpr Type getObservableType() const =0