Skip to content

Commit 02edcca

Browse files
committed
refactoring
1 parent 8ba696a commit 02edcca

File tree

11 files changed

+182
-135
lines changed

11 files changed

+182
-135
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"IOFSM",
4+
"MPAR"
5+
]
6+
}

include/maxplus/base/fsm/fsm.h

Lines changed: 100 additions & 66 deletions
Large diffs are not rendered by default.

include/maxplus/base/fsm/iofsm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using IOAEdgeLabel = std::pair<InputAction, OutputAction>;
1313
using IOAState = ::FSM::Labeled::State<CId, IOAEdgeLabel>;
1414
using IOAStateRef = const IOAState*;
1515
using IOAEdge = ::FSM::Labeled::Edge<CId, IOAEdgeLabel>;
16+
using IOAEdgeRef = ::FSM::Labeled::EdgeRef<CId, IOAEdgeLabel>;
1617
using IOASetOfStates = ::FSM::Labeled::SetOfStates<CId, CString>;
1718
using IOASetOfStateRefs = ::FSM::Labeled::SetOfStateRefs<CId, CString>;
1819
using IOASetOfEdges = ::FSM::Labeled::SetOfEdges<CId, CString>;

include/maxplus/game/policyiteration.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ template <typename SL, typename EL> class PolicyIteration {
167167
// Outgoing edges.
168168
auto es = dynamic_cast<const FSM::Abstract::SetOfEdgeRefs &>(v->getOutgoingEdges());
169169
for (const auto &ei : es) {
170-
auto e = dynamic_cast<Edge<SL, EL> *>(ei);
170+
auto e = dynamic_cast<EdgeRef<SL, EL>>(ei);
171171

172172
const auto &u = dynamic_cast<const State<SL, EL> *>(&(e->getDestination()));
173173

@@ -260,7 +260,7 @@ template <typename SL, typename EL> class PolicyIteration {
260260
const auto &es =
261261
dynamic_cast<const FSM::Abstract::SetOfEdgeRefs &>(v->getOutgoingEdges());
262262
for (const auto &ei : es) {
263-
auto e = dynamic_cast<Edge<SL, EL> *>(ei);
263+
auto e = dynamic_cast<EdgeRef<SL, EL>>(ei);
264264

265265
auto u = dynamic_cast<const State<SL, EL> *>(&(e->getDestination()));
266266

@@ -393,7 +393,7 @@ template <typename SL, typename EL> class PolicyIteration {
393393
const State<SL, EL> *x = currentStrategy->getSuccessor(u);
394394

395395
// Initialize both numerator and denominator.
396-
Edge<SL, EL> *e = game.getEdge(*u, *(currentStrategy->getSuccessor(u)));
396+
EdgeRef<SL, EL> e = game.getEdge(*u, *(currentStrategy->getSuccessor(u)));
397397
auto w1sum = static_cast<CDouble>(game.getWeight1(e));
398398
auto w2sum = static_cast<CDouble>(game.getWeight2(e));
399399

@@ -404,7 +404,7 @@ template <typename SL, typename EL> class PolicyIteration {
404404
if (stateIds[x] < stateIds[v_s]) {
405405
v_s = x;
406406
}
407-
Edge<SL, EL> *x_succ =
407+
EdgeRef<SL, EL> x_succ =
408408
game.getEdge(*x, *(currentStrategy->getSuccessor(x)));
409409
auto w1 = static_cast<CDouble>(game.getWeight1(x_succ));
410410
auto w2 = static_cast<CDouble>(game.getWeight2(x_succ));
@@ -491,7 +491,7 @@ template <typename SL, typename EL> class PolicyIteration {
491491
while (!stack.empty()) {
492492
const State<SL, EL> *x = stack.top();
493493
stack.pop();
494-
Edge<SL, EL> *e = game.getEdge(*x, *u);
494+
EdgeRef<SL, EL> e = game.getEdge(*x, *u);
495495
auto w1 = static_cast<CDouble>(game.getWeight1(e));
496496
auto w2 = static_cast<CDouble>(game.getWeight2(e));
497497

include/maxplus/game/strategyvector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ template <typename SL, typename EL> class StrategyVector {
8484
dynamic_cast<const FSM::Abstract::SetOfEdgeRefs &>(src.getOutgoingEdges());
8585

8686
// Find the first outgoing edge, and get the target.
87-
auto *e = dynamic_cast<Edge<SL, EL> *>(*es.begin());
88-
auto &dest = dynamic_cast<const State<SL, EL> &>(e->getDestination());
87+
auto *e = dynamic_cast<EdgeRef<SL, EL>>(*es.begin());
88+
auto dest = dynamic_cast<StateRef<SL, EL>>(e->getDestination());
8989

90-
strategyVector[&src] = &dest;
90+
strategyVector[&src] = dest;
9191
}
9292
}
9393

include/maxplus/graph/mpautomaton.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ inline MPAEdgeLabel makeMPAEdgeLabel(MPDelay delay, CString &mode) {
144144

145145
// Types for edges and states and sets.
146146
using MPAState = ::FSM::Labeled::State<MPAStateLabel, MPAEdgeLabel>;
147+
using MPAStateRef = ::FSM::Labeled::StateRef<MPAStateLabel, MPAEdgeLabel>;
147148
using MPAEdge = ::FSM::Labeled::Edge<MPAStateLabel, MPAEdgeLabel>;
149+
using MPAEdgeRef = ::FSM::Labeled::EdgeRef<MPAStateLabel, MPAEdgeLabel>;
148150
using MPASetOfStates = ::FSM::Labeled::SetOfStates<MPAStateLabel, MPAEdgeLabel>;
149151
using MPASetOfEdges = ::FSM::Abstract::SetOfEdges;
150152

@@ -210,7 +212,9 @@ inline CString toString(const MPAREdgeLabel &l) {
210212

211213
// Types of states, edges, sets and cycle of an MPA with rewards.
212214
using MPARState = ::FSM::Labeled::State<MPAStateLabel, MPAREdgeLabel>;
215+
using MPARStateRef = ::FSM::Labeled::StateRef<MPAStateLabel, MPAREdgeLabel>;
213216
using MPAREdge = ::FSM::Labeled::Edge<MPAStateLabel, MPAREdgeLabel>;
217+
using MPAREdgeRef = ::FSM::Labeled::EdgeRef<MPAStateLabel, MPAREdgeLabel>;
214218
using MPARSetOfStates = ::FSM::Labeled::SetOfStates<MPAStateLabel, MPAREdgeLabel>;
215219
using MPARSetOfEdges = ::FSM::Abstract::SetOfEdges;
216220
using MPARCycle = std::list<const ::FSM::Abstract::Edge *>;

include/maxplus/graph/mpstatespace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ namespace MaxPlus {
5050

5151
using ELSState = ::FSM::Labeled::State<CId, CString>;
5252
using ELSEdge = ::FSM::Labeled::Edge<CId, CString>;
53+
using ELSEdgeRef = ::FSM::Labeled::EdgeRef<CId, CString>;
5354
using ELSSetOfStates = ::FSM::Labeled::SetOfStates<CId, CString>;
5455
using ELSSetOfEdges = ::FSM::Labeled::SetOfEdges<CId, CString>;
5556

src/base/fsm/fsm.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ namespace StateStringLabeled {
5151
void FiniteStateMachine::addStateLabeled(const CString &sl) { this->addState(sl); }
5252

5353
void FiniteStateMachine::addEdgeLabeled(const CString &src, const CString &dst) {
54-
Labeled::State<CString, char> &s_src = this->getStateLabeled(src);
55-
Labeled::State<CString, char> &s_dst = this->getStateLabeled(dst);
56-
Labeled::FiniteStateMachine<CString, char>::addEdge(s_src, 'X', s_dst);
54+
const Labeled::StateRef<CString, char> s_src = this->getStateLabeled(src);
55+
const Labeled::StateRef<CString, char> s_dst = this->getStateLabeled(dst);
56+
Labeled::FiniteStateMachine<CString, char>::addEdge(*s_src, 'X', *s_dst);
5757
}
5858

5959
std::shared_ptr<Abstract::SetOfStateRefs> FiniteStateMachine::reachableStates() {

src/graph/mpautomaton.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ CDouble MaxPlusAutomatonWithRewards::calculateMCR() {
2323
CId eId = 0;
2424
for (const auto &s : this->getStates()) {
2525
for (auto *e : (s.second)->getOutgoingEdges()) {
26-
auto *mpae = dynamic_cast<MPAREdge *>(e);
26+
auto *mpae = dynamic_cast<MPAREdgeRef>(e);
2727
g.addEdge(eId++,
28-
*nodeMap[&(mpae->getSource())],
29-
*nodeMap[&(mpae->getDestination())],
28+
*nodeMap[(mpae->getSource())],
29+
*nodeMap[(mpae->getDestination())],
3030
static_cast<CDouble>(mpae->getLabel().delay),
3131
mpae->getLabel().reward);
3232
}
@@ -56,10 +56,10 @@ CDouble MaxPlusAutomatonWithRewards::calculateMCRAndCycle(
5656

5757
for (const auto &s : this->getStates()) {
5858
for (auto *e : (s.second)->getOutgoingEdges()) {
59-
auto *mpae = dynamic_cast<MPAREdge *>(e);
59+
auto *mpae = dynamic_cast<MPAREdgeRef>(e);
6060
g.addEdge(eId++,
61-
*nodeMap[&(mpae->getSource())],
62-
*nodeMap[&(mpae->getDestination())],
61+
*nodeMap[mpae->getSource()],
62+
*nodeMap[mpae->getDestination()],
6363
static_cast<CDouble>(mpae->getLabel().delay),
6464
mpae->getLabel().reward);
6565
}

src/graph/smpls.cc

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
#include "maxplus/base/fsm/fsm.h"
12
#include "graph/smpls.h"
23
#include "algebra/mptype.h"
4+
#include "maxplus/base/fsm/iofsm.h"
5+
#include "maxplus/graph/mpautomaton.h"
36
#include <memory>
47

58
using namespace FSM;
69

710
using ELSEdge = ::FSM::Labeled::Edge<CId, CString>;
11+
using ELSEdgeRef = ::FSM::Labeled::EdgeRef<CId, CString>;
812
using ELSState = ::FSM::Labeled::State<CId, CString>;
13+
using ELSStateRef = ::FSM::Labeled::StateRef<CId, CString>;
914
using ELSSetOfStates = ::FSM::Labeled::SetOfStates<CId, CString>;
1015
using ELSSetOfEdges = ::FSM::Abstract::SetOfEdges;
1116
using ELSSetOfEdgeRefs = ::FSM::Abstract::SetOfEdgeRefs;
@@ -36,13 +41,13 @@ namespace MaxPlus::SMPLS {
3641
for (const auto& it: elsEdges)
3742
{
3843

39-
auto& e = dynamic_cast<ELSEdge&>(*it.second);
40-
const auto& s = dynamic_cast<const ELSState&>(e.getDestination());
41-
const auto& oEdges = dynamic_cast<const ELSSetOfEdges&>(s.getOutgoingEdges());
44+
auto e = dynamic_cast<ELSEdgeRef>(&(*it.second));
45+
const auto& s = dynamic_cast<ELSStateRef>(e->getDestination());
46+
const auto& oEdges = dynamic_cast<const ELSSetOfEdges&>(s->getOutgoingEdges());
4247
if (oEdges.empty())
4348
{
44-
edgesToBeRemoved.insert(&e);
45-
statesToBeRemoved.insert(&s);
49+
edgesToBeRemoved.insert(e);
50+
statesToBeRemoved.insert(s);
4651
}
4752
}
4853

@@ -57,16 +62,13 @@ namespace MaxPlus::SMPLS {
5762

5863
//remove edges ending in dangling states
5964
//remove edges ending in dangling states from the outgoing edges of their source states
60-
for (auto *erIt: edgesToBeRemoved) {
65+
for (const auto *erIt: edgesToBeRemoved) {
6166
const auto& e = dynamic_cast<const ELSEdge&>(*erIt);
6267
this->removeEdge(e);
63-
const auto& s = dynamic_cast<const ELSState&>(e.getSource());
68+
const auto *const s = dynamic_cast<ELSStateRef>(e.getSource());
6469

65-
66-
// TODO (Marc Geilen) redesign for opportunities for subclasses to modify their FSM structure while external users get const access only. Perhaps through protected access.
67-
// Maybe request modifiable access to a given const state/edge?
68-
auto& ms = const_cast<ELSState&>(s);
69-
ms.removeOutgoingEdge(e);
70+
const auto *ms = dynamic_cast<ELSStateRef>(s);
71+
this->removeEdge(e);
7072
}
7173

7274
//empty the temporary sets
@@ -81,13 +83,13 @@ namespace MaxPlus::SMPLS {
8183
for (const auto& it: elsEdges)
8284
{
8385

84-
auto& e = dynamic_cast<ELSEdge&>(*(it.second));
85-
const auto& s = dynamic_cast<const ELSState&>(e.getDestination());
86-
const auto& oEdges = dynamic_cast<const ELSSetOfEdges&>(s.getOutgoingEdges());
86+
auto e = dynamic_cast<ELSEdgeRef>(&(*(it.second)));
87+
const auto s = dynamic_cast<ELSStateRef>(e->getDestination());
88+
const auto& oEdges = dynamic_cast<const ELSSetOfEdges&>(s->getOutgoingEdges());
8789
if (oEdges.empty())
8890
{
89-
edgesToBeRemoved.insert(&e);
90-
statesToBeRemoved.insert(&s);
91+
edgesToBeRemoved.insert(e);
92+
statesToBeRemoved.insert(s);
9193
}
9294
}
9395
}
@@ -165,8 +167,8 @@ namespace MaxPlus::SMPLS {
165167
// create the FSM states for every pair of a states of the FSM
166168
// and an initial token
167169

168-
const auto& I = dynamic_cast<const ELSSetOfStates&>(this->elsFSM.getInitialStates());
169-
const auto& F = dynamic_cast<const ELSSetOfStates&>(this->elsFSM.getFinalStates());
170+
const auto& I = dynamic_cast<const ELSSetOfStateRefs&>(this->elsFSM.getInitialStates());
171+
const auto& F = dynamic_cast<const ELSSetOfStateRefs&>(this->elsFSM.getFinalStates());
170172
const auto& Q = dynamic_cast<const ELSSetOfStates&>(this->elsFSM.getStates());
171173
for (const auto& q: Q)
172174
{
@@ -175,7 +177,7 @@ namespace MaxPlus::SMPLS {
175177
unsigned int nrTokens = 0;
176178
if (! e.empty())
177179
{
178-
CString label = (dynamic_cast<ELSEdge*>(*e.begin()))->getLabel();
180+
CString label = (dynamic_cast<ELSEdgeRef>(*e.begin()))->getLabel();
179181

180182
for (const auto& smIt: this->sm)
181183
{
@@ -191,13 +193,13 @@ namespace MaxPlus::SMPLS {
191193
nrTokens = (*this->sm.begin()).second->getCols();
192194
}
193195
// create a state for (q, k)
194-
CId qId = (dynamic_cast<ELSState&>(qq)).getLabel();
196+
const CId qId = (dynamic_cast<ELSState&>(qq)).getLabel();
195197
bool isInitial = false;
196198
bool isFinal = false;
197199
// if els state is initial
198200
for (const auto& i: I)
199201
{
200-
CId iId = (dynamic_cast<ELSState&>(*(i.second))).getLabel();
202+
CId iId = (dynamic_cast<const ELSState&>(*i)).getLabel();
201203
if (iId == qId)
202204
{
203205
isInitial = true;
@@ -207,7 +209,7 @@ namespace MaxPlus::SMPLS {
207209
// if els state is final
208210
for (const auto& f: F)
209211
{
210-
CId fId = (dynamic_cast<ELSState&>(*(f.second))).getLabel();
212+
CId fId = (dynamic_cast<const ELSState&>(*f)).getLabel();
211213
if (fId == qId)
212214
{
213215
isFinal = true;
@@ -241,11 +243,11 @@ namespace MaxPlus::SMPLS {
241243

242244
// for every outgoing edge of the state
243245
const auto& t = dynamic_cast<const ELSSetOfEdgeRefs&>(q1.getOutgoingEdges());
244-
for (auto *e: t)
246+
for (const auto *e: t)
245247
{
246-
auto& tr = dynamic_cast<ELSEdge&>(*e);
247-
CId q2Id = dynamic_cast<const ELSState&>(tr.getDestination()).getLabel();
248-
CString sc = tr.getLabel();
248+
const auto *tr = dynamic_cast<ELSEdgeRef>(e);
249+
CId q2Id = dynamic_cast<ELSStateRef>(tr->getDestination())->getLabel();
250+
CString sc = tr->getLabel();
249251
std::shared_ptr<Matrix> Ms = this->sm.at(sc);
250252
size_t r = Ms->getRows();
251253
size_t c = Ms->getCols();
@@ -255,14 +257,14 @@ namespace MaxPlus::SMPLS {
255257
{
256258
for (size_t col = 0; col < c; col++)
257259
{
258-
MPDelay d = Ms->get(static_cast<unsigned int>(row), static_cast<unsigned int>(col));
260+
const MPDelay d = Ms->get(static_cast<unsigned int>(row), static_cast<unsigned int>(col));
259261

260262
if (!MP_IS_MINUS_INFINITY(d))
261263
{
262-
MPAState& src = mpa->getStateLabeled(makeMPAStateLabel(q1Id, static_cast<unsigned int>(col)));
264+
MPAStateRef src = mpa->getStateLabeled(makeMPAStateLabel(q1Id, static_cast<unsigned int>(col)));
263265
MPAState& dst = mpa->getStateLabeled(makeMPAStateLabel(q2Id, static_cast<unsigned int>(row)));
264266
MPAEdgeLabel el = makeMPAEdgeLabel(d, sc);
265-
el.mode = CString(tr.getLabel());
267+
el.mode = CString(tr->getLabel());
266268
mpa->addEdge(src, el, dst);
267269
}
268270
}
@@ -434,27 +436,27 @@ namespace MaxPlus::SMPLS {
434436
void SMPLSwithEvents::prepareMatrices(const IOAState& s, std::multiset<Event>& eventList, IOASetOfEdgeRefs& visitedEdges)
435437
{
436438
const auto& adj = dynamic_cast<const IOASetOfEdgeRefs&>(s.getOutgoingEdges());
437-
for (auto *i: adj)
439+
for (const auto *i: adj)
438440
{
439-
auto& e = dynamic_cast<IOAEdge&>(*i);
440-
if (visitedEdges.count(&e) > 0) {
441+
const auto *e = dynamic_cast<IOAEdgeRef>(i);
442+
if (visitedEdges.count(e) > 0) {
441443
continue;
442444
}
443445

444446
//create a unique label for the new edge. this name will also be the mode name for sm
445447
CString modeName = CString(s.getId());
446448
modeName += ",";
447-
modeName += e.getLabel().first + "," + e.getLabel().second;
449+
modeName += e->getLabel().first + "," + e->getLabel().second;
448450

449451
// add the edge with unique name between the corresponding states
450-
this->elsFSM.addEdge(this->elsFSM.getStateLabeled(s.getLabel()), modeName, this->elsFSM.getStateLabeled((dynamic_cast<const IOAState&>(e.getDestination())).getLabel()));
452+
this->elsFSM.addEdge(this->elsFSM.getStateLabeled(s.getLabel()), modeName, this->elsFSM.getStateLabeled((dynamic_cast<const IOAState&>(e->getDestination())).getLabel()));
451453

452454
// make a copy so that child node can not modify the parent nodes list of events
453455
// only adds and removes and passes it to it's children
454456
std::multiset<Event> eList(eventList);
455457

456-
OutputAction output = e.getLabel().second;
457-
InputAction input = e.getLabel().first;
458+
OutputAction output = e->getLabel().second;
459+
InputAction input = e->getLabel().first;
458460

459461
auto disSm = std::make_shared<DissectedModeMatrix>();
460462
std::shared_ptr<Matrix> sMatrix; //matrix to be generated and assigned to this edge
@@ -579,12 +581,12 @@ namespace MaxPlus::SMPLS {
579581
}
580582

581583
this->sm[modeName] = sMatrix;
582-
visitedEdges.insert(&e);
584+
visitedEdges.insert(e);
583585

584586
// we need this later to make all matrices square
585587
biggestMatrixSize = std::max(biggestMatrixSize, std::max(sMatrix->getCols(), sMatrix->getRows()));
586588

587-
prepareMatrices(dynamic_cast<const IOAState&>(e.getDestination()), eList, visitedEdges);
589+
prepareMatrices(dynamic_cast<const IOAState&>(e->getDestination()), eList, visitedEdges);
588590
}
589591
}
590592

@@ -629,21 +631,20 @@ namespace MaxPlus::SMPLS {
629631
else // If current state is not final
630632
{
631633
const auto& adj = dynamic_cast<const IOASetOfEdgeRefs&>(s.getOutgoingEdges());
632-
for (auto *i: adj)
634+
for (const auto *i: adj)
633635
{
634636
// make a copy so that child node can not modify the parent nodes list
635637
// only adds and removes and passes it to its children
636638
EventList eList(eventList);
637639

638-
auto& e = dynamic_cast<IOAEdge&>(*i);
639-
OutputAction output = e.getLabel().second;
640-
InputAction input = e.getLabel().first;
640+
const auto *e = dynamic_cast<IOAEdgeRef>(i);
641+
const OutputAction output = e->getLabel().second;
642+
const InputAction input = e->getLabel().first;
641643
if (!input.empty()) {
642644
bool eventFound = false;
643645
EventList::iterator eventIter;
644646
for (eventIter = eList.begin(); eventIter != eList.end(); eventIter++)
645647
{
646-
std::list<EventOutcomePair>::iterator gammaIterator;
647648
for (const auto& gammaIterator: this->gamma)
648649
{
649650
auto p = (EventOutcomePair(gammaIterator));
@@ -682,7 +683,7 @@ namespace MaxPlus::SMPLS {
682683
}
683684
}
684685
}
685-
const auto& s2 = dynamic_cast<const IOAState&>(e.getDestination());
686+
const auto& s2 = dynamic_cast<const IOAState&>(e->getDestination());
686687

687688
isConsistentUtil(s2, eList, finalStates, errMsg, visited);
688689
}
@@ -722,7 +723,7 @@ namespace MaxPlus::SMPLS {
722723
//we go through all the rest of the edges and remove them from the automaton
723724
for (; i != outEdge.end(); ++i)
724725
{
725-
e = dynamic_cast<IOAEdge*>(*i);
726+
e = dynamic_cast<IOAEdgeRef>(*i);
726727
ioa->removeEdge(*e);
727728
// since we only start with one initial state and remove the others, then the destination state of this edge will be unreachable
728729
//ioa->removeState(dynamic_cast<IOAState*>(e->getDestination()));

0 commit comments

Comments
 (0)