BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
GreedyController.cpp
Go to the documentation of this file.
1#include "GreedyController.h"
8#include <iostream>
9
10using namespace BPMNOS::Execution;
11
13 : evaluator(evaluator)
14 , config(config)
15{
16 // add event dispatcher
17 if ( config.bestFirstEntry ) {
18 eventDispatchers.push_back( std::make_unique<BestFirstEntry>(evaluator) );
19 }
20 else {
21 eventDispatchers.push_back( std::make_unique<GreedyEntry>(evaluator) );
22 }
23 if ( config.bestFirstExit ) {
24 eventDispatchers.push_back( std::make_unique<BestFirstExit>(evaluator) );
25 }
26 else {
27 eventDispatchers.push_back( std::make_unique<GreedyExit>(evaluator) );
28 }
29 eventDispatchers.push_back( std::make_unique<BisectionalChoice>(evaluator) );
30 eventDispatchers.push_back( std::make_unique<BestMatchingMessageDelivery>(evaluator) );
31}
32
34 for ( auto& eventDispatcher : eventDispatchers ) {
35 eventDispatcher->connect(this);
36 }
37 Controller::connect(mediator);
38}
39
40std::shared_ptr<Event> GreedyController::dispatchEvent(const SystemState* systemState) {
41 std::shared_ptr<Decision> best = nullptr;
42 for ( auto& eventDispatcher : eventDispatchers ) {
43 if ( auto event = eventDispatcher->dispatchEvent(systemState) ) {
44 if ( auto decision = dynamic_pointer_cast<Decision>(event) ) {
45 if ( decision->reward.has_value() ) {
46 if ( !best ) {
47 // first feasible decision is used as best
48 best = decision;
49 }
50 else if ( decision->reward.value() > best->reward.value() ) {
51 // decision has better reward than current best
52 best = decision;
53 }
54 }
55 }
56 else {
57 // events are immediately forwarded
58//std::cerr << "\nEvent " << event->jsonify() << " without evaluation" << std::endl;
59 return event;
60 }
61 }
62 }
63
64//if ( best ) std::cerr << "\nGreedyController: Best decision " << best->jsonify() << " evaluated with " << best->evaluation.value_or(-999) << std::endl;
65
66 return best;
67}
virtual void connect(Mediator *mediator)
Definition Controller.cpp:9
Represents an abstract base class for an evaluator determining feasibility and reward of a decision.
Definition Evaluator.h:15
std::vector< std::unique_ptr< EventDispatcher > > eventDispatchers
GreedyController(Evaluator *evaluator, Config config=default_config())
std::shared_ptr< Event > dispatchEvent(const SystemState *systemState)
A class representing the state that the execution or simulation of a given scenario is in.
Definition SystemState.h:21
Represents an abstract base class for a class that is an event listener and notifier.
Definition Mediator.h:13