Commit 5cd39533 authored by Kučera Petr RNDr. Ph.D.'s avatar Kučera Petr RNDr. Ph.D.
Browse files

Activity update based on topo order

parent e213b799
......@@ -149,6 +149,6 @@ main([[maybe_unused]] int argc, char *argv[])
routes.UpdateActivities(input_data.mTrainList);
{
std::ofstream f(cfg.output.model_data);
routes.PrintActivities(f, "", true);
routes.PrintActivities(f, "", false);
}
}
......@@ -159,7 +159,7 @@ Loco::TrainRouteGraph::PrintActivities(std::ostream &os,
os << prefix << "{" << std::endl;
}
os << prefix << kTabString;
PrintActivity(os, act) << "," << std::endl;
PrintActivity(os, act, only_used) << "," << std::endl;
}
if (has_used)
{
......@@ -169,41 +169,41 @@ Loco::TrainRouteGraph::PrintActivities(std::ostream &os,
}
std::ostream &
Loco::TrainRouteGraph::PrintActivity(std::ostream &os,
const Activity &act) const
Loco::TrainRouteGraph::PrintActivity(std::ostream &os, const Activity &act,
bool only_used) const
{
os << "<" << act.mName << ", " << (act.mForced ? 1 : 0) << ", "
<< act.mMinStart << ", " << act.mMaxEnd << ", " << act.mMinDuration
<< ", " << act.mMaxDuration << ", " << place_ident(act.mPlace) << ", ";
PrintActNamesList(os, act.mPred);
PrintActNamesList(os, act.mPred, only_used);
os << ", ";
PrintActNamesList(os, act.mSucc);
PrintActNamesList(os, act.mSucc, only_used);
return os << ", " << act.mTransitionTime << ">";
}
std::ostream &
Loco::TrainRouteGraph::PrintActNamesList(
std::ostream &os, const std::vector<unsigned> &act_ids) const
Loco::TrainRouteGraph::PrintActNamesList(std::ostream &os,
const std::vector<unsigned> &act_ids,
bool only_used) const
{
os << "{";
if (!act_ids.empty())
bool print_comma = false;
for (const auto &id : act_ids)
{
bool print_comma = false;
for (const auto &id : act_ids)
const auto &act = mActList[id];
if (only_used && !act.mUse)
{
continue;
}
if (print_comma)
{
os << ", ";
}
else
{
if (mActList[id].mUse)
{
if (print_comma)
{
os << ", ";
}
else
{
print_comma = true;
}
os << mActList[act_ids.front()].mName;
}
print_comma = true;
}
os << mActList[act_ids.front()].mName;
}
return os << "}";
}
......@@ -233,6 +233,7 @@ Loco::TrainRouteGraph::ConnectEnterActivity()
if (mActList[i].mPred.empty())
{
mActList[i].mPred.push_back(0);
mActList.front().mSucc.push_back(i);
}
}
}
......@@ -245,6 +246,7 @@ Loco::TrainRouteGraph::ConnectLeaveActivity()
{
auto i = mElements.back().mSectActivities.at(leaf);
pred.push_back(i);
mActList[i].mSucc.push_back(mActList.size() - 1);
}
}
......@@ -271,39 +273,59 @@ Loco::TrainRouteGraph::ConnectElementActivities(unsigned element_id)
void
Loco::TrainRouteGraph::UpdateActivities(const SectionCPtr &initial_section)
{
if (!initial_section)
ClearActivities(false);
/*if (!initial_section)
{
ClearActivities(true);
mActList.front().mForced = true;
mActList.back().mForced = true;
return;
mActList.front().mUse = true;
}
UpdateWithInitialSection(mSectionGraph->SectionId(initial_section));
else
{
unsigned sect_id = mSectionGraph->SectionId(initial_section);
unsigned elem_id = ElementWithSection(sect_id);
if (elem_id == mElements.size())
{
std::cerr
<< "[TrainRouteGraph::UpdateWithInitialSection] Train is not "
"on the route"
<< std::endl;
return;
}
UpdateInitialActivity(elem_id, sect_id);
}*/
for (unsigned elem_id = UpdateInitialActivity(initial_section);
elem_id < mElements.size(); ++elem_id)
{
UpdateElement(mElements[elem_id]);
}
UpdateLastActivity();
}
void
Loco::TrainRouteGraph::UpdateWithInitialSection(unsigned sect_id)
unsigned
Loco::TrainRouteGraph::UpdateInitialActivity(const SectionCPtr &initial_section)
{
ClearActivities(false);
if (!initial_section)
{
mActList.front().mForced = true;
mActList.front().mUse = true;
return 0;
}
unsigned sect_id = mSectionGraph->SectionId(initial_section);
unsigned elem_id = ElementWithSection(sect_id);
if (elem_id == mElements.size())
{
std::cerr << "[TrainRouteGraph::UpdateWithInitialSection] Train is not "
"on the route"
"on the route, assume it is before the route start"
<< std::endl;
return;
}
UpdateInitialActivity(elem_id, sect_id);
std::vector<unsigned> sources;
sources.push_back(sect_id);
for (unsigned el = elem_id; el < mElements.size(); ++el)
{
const auto &element = mElements[el];
UpdateSubgraph(element, sources);
sources.clear();
AddUsedLeaves(element, sources);
mActList.front().mForced = true;
mActList.front().mUse = true;
return 0;
}
UpdateLastActivity(sources);
const auto act_id = mElements[elem_id].mSectActivities[sect_id];
auto &act = mActList[act_id];
act.mUse = true;
act.mForced = true;
return elem_id;
}
void
......@@ -312,6 +334,7 @@ Loco::TrainRouteGraph::ClearActivities(bool use_flag)
for (auto &act : mActList)
{
act.mUse = use_flag;
act.mForced = false;
}
}
......@@ -337,8 +360,7 @@ Loco::TrainRouteGraph::ElementWithSection(unsigned sect_id) const
}
void
Loco::TrainRoutes::UpdateActivities(const TrainList &train_list
[[maybe_unused]])
Loco::TrainRoutes::UpdateActivities(const TrainList &train_list)
{
for (const auto &[train, sect] : train_list.TrainSections())
{
......@@ -347,55 +369,41 @@ Loco::TrainRoutes::UpdateActivities(const TrainList &train_list
}
void
Loco::TrainRouteGraph::UpdateSubgraph(const Element &el,
const std::vector<unsigned> &sources)
Loco::TrainRouteGraph::UpdateElement(const Element &el)
{
for (const auto &src : sources)
for (const auto &node : el.mSubgraph->mTopoOrder)
{
UpdateSubgraphNode(el, src);
UpdateSubgraphNode(el, node);
}
}
void
Loco::TrainRouteGraph::UpdateSubgraphNode(const Element &el, unsigned node)
{
mActList[el.mSectActivities.at(node)].mUse = true;
for (const auto &succ : el.mSubgraph->mSections.at(node).mSucc)
// Look at predecessors
auto &node_act = mActList[el.mSectActivities.at(node)];
for (unsigned pred : node_act.mPred)
{
if (!mActList[el.mSectActivities.at(succ)].mUse)
if (mActList[pred].mUse)
{
UpdateSubgraphNode(el, succ);
node_act.mUse = true;
break;
}
}
}
void
Loco::TrainRouteGraph::AddUsedLeaves(const Element &el,
std::vector<unsigned> &leaves)
Loco::TrainRouteGraph::UpdateLastActivity()
{
for (unsigned leaf : el.mSubgraph->mTargets)
// look at predecessors
for (unsigned pred : mActList.back().mPred)
{
if (mActList[el.mSectActivities.at(leaf)].mUse)
if (mActList[pred].mUse)
{
leaves.push_back(leaf);
mActList.back().mUse = true;
mActList.back().mForced = true;
break;
}
}
}
void
Loco::TrainRouteGraph::UpdateInitialActivity(unsigned elem_id, unsigned sect_id)
{
const auto act_id = mElements[elem_id].mSectActivities[sect_id];
auto &act = mActList[act_id];
act.mUse = true;
}
void
Loco::TrainRouteGraph::UpdateLastActivity(const std::vector<unsigned> &sources)
{
if (!sources.empty())
{
mActList.back().mUse = true;
}
}
......@@ -84,20 +84,18 @@ namespace Loco
void ConnectElementActivities(unsigned element_id);
void ConnectLeaveActivity();
std::ostream &
PrintActNamesList(std::ostream &os,
const std::vector<unsigned> &act_ids) const;
std::ostream &PrintActivity(std::ostream &os, const Activity &act) const;
std::ostream &PrintActNamesList(std::ostream &os,
const std::vector<unsigned> &act_ids,
bool only_used) const;
std::ostream &PrintActivity(std::ostream &os, const Activity &act,
bool only_used) const;
void ClearActivities(bool use_flag);
void UpdateWithInitialSection(unsigned sect_id);
unsigned ElementWithSection(unsigned sect_id) const;
void UpdateSubgraph(const Element &el,
const std::vector<unsigned> &sources);
void AddUsedLeaves(const Element &el, std::vector<unsigned> &leaves);
void UpdateElement(const Element &el);
void UpdateSubgraphNode(const Element &el, unsigned node);
void UpdateInitialActivity(unsigned elem_id, unsigned sect_id);
void UpdateLastActivity(const std::vector<unsigned> &sources);
unsigned UpdateInitialActivity(const SectionCPtr &initial_section);
void UpdateLastActivity();
public:
TrainRouteGraph(TrainCPtr train, unsigned train_id,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment