Skip to content

Commit

Permalink
Debugged topological sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed May 2, 2024
1 parent 2afb5d6 commit ed0e1ef
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 295 deletions.
1 change: 0 additions & 1 deletion sbg/multidim_inter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
******************************************************************************/

#include <iostream>
#include "sbg/multidim_inter.hpp"

namespace SBG {
Expand Down
1 change: 0 additions & 1 deletion sbg/pw_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
******************************************************************************/

#include <iostream>
#include "sbg/pw_map.hpp"

namespace SBG {
Expand Down
173 changes: 90 additions & 83 deletions sbg/sbg_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,16 +816,15 @@ PWMap<Set> SBGSCC<Set>::calculate()

template<typename Set>
SBGTopSort<Set>::SBGTopSort()
: dsbg_(), smap_(), E_(), mapB_(), mapD_(), unordered_(), not_dependant_()
, visitedE_(), end_(), new_end_(), dom_(), exp_(), debug_(false) {}
: dsbg_(), smap_(), E_(), mapB_(), mapD_(), unordered_(), not_dependent_()
, visitedV_(), curr_(), debug_(false) {}
template<typename Set>
SBGTopSort<Set>::SBGTopSort(DSBGraph<Set> dsbg, bool debug)
: dsbg_(dsbg), smap_(), E_(dsbg.E()), mapB_(dsbg.mapB()), mapD_(dsbg.mapD())
, unordered_(dsbg.V()), not_dependant_(), visitedE_(), end_(), new_end_()
, dom_(), exp_(), debug_(debug) {
not_dependant_ = dsbg.V().difference(mapB().image());
end_ = not_dependant_.minElem();
new_end_ = end_;
, unordered_(dsbg.V()), not_dependent_(), visitedV_(), curr_()
, debug_(debug) {
not_dependent_ = dsbg.V().difference(mapB().image());
curr_ = not_dependent_.minElem();
}

member_imp_temp(template<typename Set>, SBGTopSort<Set>, DSBGraph<Set>, dsbg);
Expand All @@ -834,97 +833,92 @@ member_imp_temp(template<typename Set>, SBGTopSort<Set>, Set, E);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, PWMap<Set>, mapB);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, PWMap<Set>, mapD);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Set, unordered);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Set, not_dependant);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Set, visitedE);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Util::MD_NAT, end);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Util::MD_NAT, new_end);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Set, dom);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Exp, exp);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Set, not_dependent);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Set, visitedV);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, Util::MD_NAT, curr);
member_imp_temp(template<typename Set>, SBGTopSort<Set>, bool, debug);

template<typename Set>
void SBGTopSort<Set>::calculateExp()
Exp SBGTopSort<Set>::calculateExp(Util::MD_NAT n1, Util::MD_NAT n2)
{
Exp res;

Util::MD_NAT aux_end = end(), aux_ne = new_end();
Util::RATIONAL one(1, 1);
for (unsigned int j = 0; j < end().size(); ++j) {
Util::RATIONAL x1(aux_end[j]), x2(aux_ne[j]);
for (unsigned int j = 0; j < n1.size(); ++j) {
Util::RATIONAL x1(n1[j]), x2(n2[j]);
res.emplaceBack(LExp(1, x1 - x2));
}

set_exp(res);

return;
return res;
}

template<typename Set>
void SBGTopSort<Set>::topSortStep()
{
Set end_set(end());
set_dom(end_set);
Set nd = not_dependent();
Util::MD_NAT next = nd.minElem();
Set next_set(next), dom = next_set;
Exp exp = calculateExp(curr(), next);
set_curr(next);

Set nd = not_dependant();
PW Vmap = dsbg().Vmap();
Set vend = Vmap.preImage(Vmap.image(end_set));
vend = nd.intersection(vend).difference(end_set);
if (vend.isEmpty()) {
Set ne_set(nd.minElem());
set_dom(ne_set);
set_new_end(nd.minElem());
Set vnew_end = Vmap.preImage(Vmap.image(ne_set));
vnew_end = unordered().intersection(vnew_end);
if (!vnew_end.difference(nd).isEmpty())
set_dom(vnew_end);
set_E(E().difference(mapD().preImage(ne_set)));
calculateExp();
set_end(new_end());
}
else {
set_dom(vend);
set_new_end(vend.minElem());
calculateExp();
set_end(vend.maxElem());
if (debug()) {
Util::SBG_LOG << "curr: " << curr() << "\n";
Util::SBG_LOG << "dom: " << dom << "\n";
Util::SBG_LOG << "exp: " << exp << "\n\n";
}

smap_ref().emplaceBack(SBGMap<Set>(dom(), exp()));

Set ingoing = mapD().preImage(dom);
updateStatus(dom, exp, ingoing);

return;
}

template<typename Set>
void SBGTopSort<Set>::updateStatus()
void SBGTopSort<Set>::updateStatus(Set dom, Exp exp, Set ingoing)
{
Set ordv = smap().dom();
Set ordB = mapB().preImage(ordv), ordD = mapD().preImage(ordv);
Set orde = ordB.intersection(ordD);

ordv = mapB().image(orde).cup(mapD().image(orde));
Set ingoing = mapD().preImage(ordv);
Set newE = E().difference(orde);
newE = newE.difference(ingoing);
set_E(newE);
PW Vmap = dsbg().Vmap();

if (!orde.intersection(visitedE()).isEmpty())
set_end(smap().dom().difference(smap().image()).minElem());
Set newE = E();
Set ith_dom = dom, new_unord = unordered();
if (!dom.intersection(visitedV()).isEmpty()) {
Set dom_sv = Vmap.preImage(Vmap.image(dom));
ith_dom = dom_sv.difference(smap().dom());

PW subE = dsbg().subE_map();
Set SE = subE.preImage(subE.image(newE));
set_visitedE(visitedE().cup(SE));

set_mapB(mapB().restrict(newE));
set_mapD(mapD().restrict(newE));
smap_ref().emplaceBack(SBGMap<Set>(ith_dom, exp));

Set ordv = smap().dom(), ith_dom_succs = smap().image(ith_dom);
if (ith_dom_succs.difference(ordv).isEmpty()) {
new_unord = new_unord.difference(ith_dom);
newE = newE.difference(mapD().preImage(ith_dom));
}
}
else {
smap_ref().emplaceBack(SBGMap<Set>(dom, exp));
}
new_unord = new_unord.difference(dom);
newE = newE.difference(mapD().preImage(dom));

Set SV = Vmap.preImage(Vmap.image(ith_dom));
set_visitedV(visitedV().cup(SV));

set_E(newE);
set_mapB(mapB().restrict(E()));
set_mapD(mapD().restrict(E()));

set_unordered(unordered().difference(dom()));
set_not_dependant(unordered().difference(mapB().image()));
set_unordered(new_unord);
set_not_dependent(unordered().difference(mapB().image()));

Set start = smap().dom().difference(smap().image());
if (start.cardinal() == 1)
set_curr(start.minElem());

if (debug()) {
Util::SBG_LOG << "curr: " << curr() << "\n";
Util::SBG_LOG << "smap: " << smap() << "\n";
Util::SBG_LOG << "mapB: " << mapB() << "\n";
Util::SBG_LOG << "mapD: " << mapD() << "\n";
Util::SBG_LOG << "end: " << end() << "\n";
Util::SBG_LOG << "new_end: " << new_end() << "\n\n";
Util::SBG_LOG << "E: " << E() << "\n";
Util::SBG_LOG << "unord: " << unordered() << "\n";
Util::SBG_LOG << "nd: " << not_dependent() << "\n";
Util::SBG_LOG << "visitedV: " << visitedV() << "\n\n";
}

return;
Expand All @@ -936,15 +930,18 @@ PWMap<Set> SBGTopSort<Set>::calculate()
if (debug())
Util::SBG_LOG << "Topological sort dsbg:\n" << dsbg() << "\n\n";

while (!unordered().isEmpty()) {
Util::MD_NAT aux_curr = curr();
Set curr_set(curr());
Exp exp = calculateExp(curr(), curr());
updateStatus(curr_set, exp, mapD().preImage(curr_set));
set_curr(aux_curr);
while (!unordered().isEmpty())
topSortStep();
updateStatus();
}

if (debug())
Util::SBG_LOG << "Topological sort result:\n" << smap() << "\n\n";
Util::SBG_LOG << "Topological sort result:\n" << smap().compact() << "\n\n";

return smap();
return smap().compact();
}

// Template instantiations -----------------------------------------------------
Expand Down Expand Up @@ -1035,12 +1032,31 @@ DSBGraph<Set> buildSortFromSCC(
const SBGSCC<Set> &scc, const PWMap<Set> &rmap
)
{
DSBGraph dsbg = scc.dsbg();
Set Ediff = dsbg.E().difference(scc.E());
PWMap<Set> subE_map = dsbg.subE_map().restrict(Ediff);
PWMap<Set> mapB = rmap.composition(dsbg.mapB().restrict(Ediff));
mapB = mapB.compact();
PWMap<Set> mapD = rmap.composition(dsbg.mapD().restrict(Ediff));
mapD = mapD.compact();

PWMap<Set> aux_rmap = rmap.compact();
PWMap<Set> reps_rmap = aux_rmap.filterMap([](const SBGMap<Set> &sbgmap) {
return eqId(sbgmap);
});
Set V = reps_rmap.dom();

for (const SBGMap<Set> &map : subE_map) {
Set ith_dom = mapB.image(map.dom()).intersection(V);
V = V.difference(ith_dom);
V = V.concatenation(ith_dom);
}
for (const SBGMap<Set> &map : subE_map) {
Set ith_dom = mapD.image(map.dom()).intersection(V);
V = V.difference(ith_dom);
V = V.concatenation(ith_dom);
}

PWMap<Set> Vmap;
unsigned int j = 1, dims = rmap.nmbrDims();
for (SetPiece mdi : V) {
Expand All @@ -1049,13 +1065,6 @@ DSBGraph<Set> buildSortFromSCC(
++j;
}

DSBGraph dsbg = scc.dsbg();
Set Ediff = dsbg.E().difference(scc.E());
PWMap<Set> mapB = rmap.composition(dsbg.mapB().restrict(Ediff));
mapB = mapB.compact();
PWMap<Set> mapD = rmap.composition(dsbg.mapD().restrict(Ediff));
mapD = mapD.compact();

j = 1;
PWMap<Set> Emap;
for (const SBGMap<Set> &map1 : Vmap) {
Expand All @@ -1069,8 +1078,6 @@ DSBGraph<Set> buildSortFromSCC(
++j;
}
}
// TODO: partition subset-edge
PWMap<Set> subE_map = dsbg.subE_map().restrict(Ediff);

DSBGraph<Set> res(V, Vmap, mapB, mapD, Emap);
res.set_subE_map(subE_map);
Expand Down
14 changes: 5 additions & 9 deletions sbg/sbg_algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,10 @@ struct SBGTopSort {
member_class(PW, mapD);

member_class(Set, unordered);
member_class(Set, not_dependant);
member_class(Set, visitedE);
member_class(Set, not_dependent);
member_class(Set, visitedV);

member_class(Util::MD_NAT, end);
member_class(Util::MD_NAT, new_end);

member_class(Set, dom);
member_class(Exp, exp);
member_class(Util::MD_NAT, curr);

member_class(bool, debug);

Expand All @@ -258,9 +254,9 @@ struct SBGTopSort {
PW calculate();

private:
void calculateExp();
Exp calculateExp(Util::MD_NAT x1, Util::MD_NAT x2);
void topSortStep();
void updateStatus();
void updateStatus(Set dom, Exp exp, Set ingoing);
};

typedef SBGTopSort<UnordSet> BaseTopSort;
Expand Down
30 changes: 15 additions & 15 deletions test/rl1_matching.test
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
model testRL1
//iR[i+1] must be computed out of iR[i]
//uL[i] must be computed out of uL[i+1]
//iR[i] must be computed out of iR[i+1]
//uL[i+1] must be computed out of uL[i]

constant Integer N=10;
Real iL[N],iR[N],uL[N];
parameter Real L=1,R=1,L1=1,I=1,R0=1;
Expand All @@ -14,8 +14,8 @@ equation
iR[i]-iR[i+1]-iL[i]=0;
uL[i]-uL[i+1]-R*iR[i+1]=0;
end for;
iR[1]=I;
uL[N]-(iR[N]-iL[N])*R0=0;
iR[N]-iL[N]+I=0;
uL[1]+(R+R0)*iR[1]=0;
end testRL1;
*/

Expand All @@ -37,9 +37,9 @@ E4 = N-1+E3 // F2 - iR[i+1]
E5 = N-1+E4 // F3 - iR[i+1]
E6 = N-1+E5 // F3 - uL[i]
E7 = N-1+E6 // F3 - uL[i+1]
E8 = 1+E7 // F4 - iR[1]
E9 = 1+E8 // F5 - iR[N]
E10 = 1+E9 // F5 - uL[N]
E8 = 1+E7 // F4 - iR[N]
E9 = 1+E8 // F5 - iR[1]
E10 = 1+E9 // F5 - uL[1]

off1d = r(F1, 1)-r(E1, 1)
off2d = r(F1, 1)-r(E2, 1)
Expand All @@ -59,9 +59,9 @@ off4b = r(U2, 1)-r(E4, 1)
off5b = r(U2, 1)-r(E5, 1)
off6b = r(U3, 1)-r(E6, 1)-1
off7b = r(U3, 1)-r(E7, 1)
off8b = r(U2, 1)-r(E8, 1)-N+1
off9b = r(U2, 1)-r(E9, 1)
off10b = r(U3, 1)-E10
off8b = r(U2, 1)-r(E8, 1)
off9b = r(U2, 1)-r(E9, 1)-N+1
off10b = r(U3, 1)-E10-N+1

V %= {[1:1:F1], [F1+1:1:F2], [F2+1:1:F3], [F3+1:1:F4], [F4+1:1:F5]
, [F5+1:1:U1], [U1+1:1:U2], [U2+1:1:U3]};
Expand All @@ -77,8 +77,8 @@ map2 %= <<{[1:1:E1]} -> 1*x+off1b, {[E1+1:1:E2]} -> 1*x+off2b, {[E2+1:1:E3]} ->
, {[E6+1:1:E7]} -> 1*x+off7b, {[E7+1:1:E8]} -> 1*x+off8b, {[E8+1:1:E9]} -> 1*x+off9b
, {[E9+1:1:E10]} -> 1*x+off10b>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2]} -> 0*x+2, {[E2+1:1:E3], [E3+1:1:E4]} -> 0*x+3
, {[E4+1:1:E5], [E5+1:1:E6]} -> 0*x+4
, {[E6+1:1:E7]} -> 0*x+6, {[E7+1:1:E8]} -> 0*x+7, {[E8+1:1:E9]} -> 0*x+8
, {[E4+1:1:E5]} -> 0*x+4
, {[E5+1:1:E6], [E6+1:1:E7]} -> 0*x+6, {[E7+1:1:E8]} -> 0*x+7, {[E8+1:1:E9]} -> 0*x+8
, {[E9+1:1:E10]} -> 0*x+9>>;

matching(
Expand All @@ -96,8 +96,8 @@ map2 %= <<{[1:1:E1]} -> 1*x+off1b, {[E1+1:1:E2]} -> 1*x+off2b, {[E2+1:1:E3]} ->
, {[E6+1:1:E7]} -> 1*x+off7b, {[E7+1:1:E8]} -> 1*x+off8b, {[E8+1:1:E9]} -> 1*x+off9b
, {[E9+1:1:E10]} -> 1*x+off10b>>;
Emap %= <<{[1:1:E1]} -> 0*x+1, {[E1+1:1:E2]} -> 0*x+2, {[E2+1:1:E3], [E3+1:1:E4]} -> 0*x+3
, {[E4+1:1:E5], [E5+1:1:E6]} -> 0*x+4
, {[E6+1:1:E7]} -> 0*x+6, {[E7+1:1:E8]} -> 0*x+7, {[E8+1:1:E9]} -> 0*x+8
, {[E4+1:1:E5]} -> 0*x+4
, {[E5+1:1:E6], [E6+1:1:E7]} -> 0*x+6, {[E7+1:1:E8]} -> 0*x+7, {[E8+1:1:E9]} -> 0*x+8
, {[E9+1:1:E10]} -> 0*x+9>>;
, 1, 6
)
Loading

0 comments on commit ed0e1ef

Please sign in to comment.