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

Basic activity initialization

parent a1a14599
......@@ -145,5 +145,9 @@ main([[maybe_unused]] int argc, char *argv[])
std::ofstream f("data/graph/subg_kopanky_petrov.dot");
sg->PrintSubgraph(f, subg_kopanky_petrov, "subg_kopanky_petrov");
}
// Loco::TrainRoutes routes(input_data, sg);
Loco::TrainRoutes routes(input_data, sg);
{
std::ofstream f(cfg.output.model_data);
routes.PrintActivities(f, "");
}
}
......@@ -14,6 +14,7 @@ default-directory = data
# directory = data
indications = indications.ini
diagnostic = deadlock.log
model-data = model.dat
[message]
# Soubory s dynamickými daty
......
......@@ -92,6 +92,7 @@ Loco::CfgOutput::Parse(const INISection &sect,
directory = sect.ValueWithDefault("directory", default_directory);
indications = sect.PathValue("indications", directory, indications);
diagnostic = sect.PathValue("diagnostic", directory, diagnostic);
model_data = sect.PathValue("model-data", directory, model_data);
return true;
}
......
......@@ -36,6 +36,7 @@ namespace Loco
std::filesystem::path directory{};
std::filesystem::path indications{"indications.ini"};
std::filesystem::path diagnostic{"deadlock.log"};
std::filesystem::path model_data{"model.dat"};
bool Parse(const INISection &sect, const std::string &default_directory);
};
......
......@@ -18,8 +18,8 @@
namespace Loco
{
using length_type = unsigned;
using activity_time_type = long unsigned;
struct GlobalIdent
{
......
......@@ -44,3 +44,11 @@ Loco::TrainPlace::Desc() const
{
return (mEnter ? kPrefixEnter : kPrefixLeave) + ":" + mTrain->Ident();
}
std::string
Loco::place_ident(const Place &place)
{
return std::holds_alternative<SGPlaceCPtr>(place)
? std::get<SGPlaceCPtr>(place)->Ident()
: std::get<TPlaceCPtr>(place)->Ident();
}
......@@ -65,6 +65,8 @@ namespace Loco
using SGPlaceCPtr = std::shared_ptr<const SectionGroupPlace>;
using TPlaceCPtr = std::shared_ptr<const TrainPlace>;
using Place = std::variant<SGPlaceCPtr, TPlaceCPtr>;
std::string place_ident(const Place &place);
} // namespace Loco
inline auto
......
......@@ -297,7 +297,8 @@ Loco::SectionGraph::BuildPlaces()
}
auto
Loco::SectionGraph::PlaceOfSection(unsigned section_id) const -> SGPlaceCPtr
Loco::SectionGraph::PlaceOfSection(unsigned section_id) const
-> const SGPlaceCPtr &
{
return mPlaces[mSections[section_id].mConflictGroupId];
}
......@@ -483,9 +484,11 @@ Loco::SectionGraph::NewSubgraphWithIds(std::vector<unsigned> sources,
{
continue;
}
SGSubgraph::NODE_TYPE ntype{SGSubgraph::NODE_TYPE::INNER};
if (state[i] == SEARCH_STATE::TERMINAL)
{
// Targets have no successors
ntype = SGSubgraph::NODE_TYPE::LEAF;
succ[i].clear();
}
else
......@@ -498,6 +501,7 @@ Loco::SectionGraph::NewSubgraphWithIds(std::vector<unsigned> sources,
if (back_state[i] == SEARCH_STATE::TERMINAL)
{
// Sources have no predecessors
ntype = SGSubgraph::NODE_TYPE::SOURCE;
pred[i].clear();
}
else
......@@ -510,7 +514,7 @@ Loco::SectionGraph::NewSubgraphWithIds(std::vector<unsigned> sources,
|| back_state[i] == SEARCH_STATE::TERMINAL
|| (!pred[i].empty() && !succ[i].empty()))
{
node_info.insert({i, {std::move(succ[i]), std::move(pred[i])}});
node_info.insert({i, {ntype, std::move(succ[i]), std::move(pred[i])}});
}
}
return std::make_shared<const SGSubgraph>(sources, targets, node_info);
......
......@@ -26,8 +26,16 @@ namespace Loco
struct SGSubgraph
{
enum class NODE_TYPE
{
SOURCE,
LEAF,
INNER
};
struct NodeInfo
{
NODE_TYPE mNodeType;
std::vector<unsigned> mSucc;
std::vector<unsigned> mPred;
};
......@@ -96,7 +104,6 @@ namespace Loco
void ChangeIdOfConflictGroup(unsigned old_id, unsigned new_id);
void CompressConflictGroups();
void BuildPlaces();
SGPlaceCPtr PlaceOfSection(unsigned section_id) const;
std::vector<unsigned>
SectionIds(const std::vector<SectionCPtr> &sect) const;
std::ostream &PrintVertex(std::ostream &os, unsigned i) const;
......@@ -120,6 +127,7 @@ namespace Loco
SGSubgraphCPtr Subgraph(const TrafficPointCPtr &source,
const TrafficPointCPtr &target) const;
const SGPlaceCPtr &PlaceOfSection(unsigned section_id) const;
std::ostream &PrintSubgraph(std::ostream &os, const SGSubgraphCPtr &subg,
const std::string &label) const;
......
......@@ -29,11 +29,13 @@ Loco::TrainRoutes::BuildRoutes(const InputData &input_data,
Loco::TrainRouteGraph::TrainRouteGraph(TrainCPtr train, unsigned train_id,
SectionGraphCPtr section_graph)
: mTrain(train), mEnterPlace(true, train_id, train),
mLeavePlace(false, train_id, std::move(train)),
: mTrain(std::move(train)), mTrainId(train_id),
mSectionGraph(std::move(section_graph))
{
mEnterPlace = std::make_shared<const TrainPlace>(true, mTrainId, mTrain);
mLeavePlace = std::make_shared<const TrainPlace>(false, mTrainId, mTrain);
BuildElements();
InitActList();
}
void
......@@ -62,3 +64,109 @@ Loco::TrainRouteGraph::AddElement(unsigned start)
mSectionGraph->PrintSubgraph(std::cout, mElements.back().mSubgraph,
"subg_" + std::to_string(start));
}
void
Loco::TrainRouteGraph::InitActList()
{
AddEnterActivity();
for (unsigned i = 0; i < mElements.size(); ++i)
{
AddElementActivities(i, i + 1 == mElements.size());
}
AddLeaveActivity();
}
void
Loco::TrainRouteGraph::AddEnterActivity()
{
mActList.emplace_back(mEnterPlace->Ident(), mEnterPlace, false);
}
void
Loco::TrainRouteGraph::AddLeaveActivity()
{
mActList.emplace_back(mLeavePlace->Ident(), mLeavePlace, true);
}
void
Loco::TrainRouteGraph::AddElementActivities(unsigned element_id,
bool add_leaves)
{
auto &element = mElements[element_id];
for (const auto &[sect, node_info] : element.mSubgraph->mSections)
{
if (!add_leaves && node_info.mNodeType == SGSubgraph::NODE_TYPE::LEAF)
{
continue;
}
const SGPlaceCPtr &place = mSectionGraph->PlaceOfSection(sect);
if (element.mPlaceActivities.find(place)
!= element.mPlaceActivities.end())
{
continue;
}
std::string name =
(node_info.mNodeType == SGSubgraph::NODE_TYPE::INNER ? "Traverse"
: "StopAt");
name += place_ident(place) + "_" + std::to_string(mTrainId) + "_";
name += std::to_string(node_info.mNodeType == SGSubgraph::NODE_TYPE::LEAF
? element_id + 1
: element_id);
element.mPlaceActivities[place] = mActList.size();
mActList.emplace_back(name, std::move(place), false);
}
}
std::ostream &
Loco::TrainRoutes::PrintActivities(std::ostream &os,
const std::string &prefix) const
{
os << "activities = #[" << std::endl;
for (const auto &[train, g] : mRouteGraphs)
{
g.PrintActivities(os, prefix + kTabString);
}
return os << "]#" << std::endl;
}
std::ostream &
Loco::TrainRouteGraph::PrintActivities(std::ostream &os,
const std::string &prefix) const
{
os << prefix << "Train" << mTrainId << ":" << std::endl;
os << prefix << "{" << std::endl;
for (const auto &act : mActList)
{
os << prefix << kTabString;
PrintActivity(os, act) << "," << std::endl;
}
return os << prefix << "}" << std::endl;
}
std::ostream &
Loco::TrainRouteGraph::PrintActivity(std::ostream &os,
const Activity &act) const
{
os << "<" << act.mName << ", " << (act.mForced ? 1 : 0) << ", "
<< act.mMinStart << ", " << act.mMaxEnd << ", " << act.mMinDuration
<< ", " << act.mMaxDuration << ", " << place_ident(act.mPlace) << ", ";
PrintActNamesList(os, act.mPred);
os << ", ";
PrintActNamesList(os, act.mSucc);
return os << ", " << act.mTransitionTime << ">";
}
std::ostream &
Loco::TrainRouteGraph::PrintActNamesList(
std::ostream &os, const std::vector<unsigned> &act_ids) const
{
os << "{";
if (!act_ids.empty())
{
os << mActList[act_ids.front()].mName;
std::for_each(
act_ids.begin() + 1, act_ids.end(),
[this, &os](const auto &i) { os << ", " << mActList[i].mName; });
}
return os << "}";
}
......@@ -16,6 +16,28 @@
namespace Loco
{
struct Activity
{
static const activity_time_type kDefaultTransitionTime{10};
std::string mName;
Place mPlace;
bool mForced{false};
activity_time_type mMinStart{};
activity_time_type mMaxEnd{};
activity_time_type mMinDuration{};
activity_time_type mMaxDuration{};
std::vector<unsigned> mPred{};
std::vector<unsigned> mSucc{};
activity_time_type mTransitionTime{kDefaultTransitionTime};
bool mUse{false};
Activity(std::string name, Place place, bool forced)
: mName(std::move(name)), mPlace(std::move(place)), mForced(forced)
{
}
};
// Composed of relevant subgraphs
class TrainRouteGraph
{
......@@ -26,6 +48,12 @@ namespace Loco
// We assume that the next stop is at index mStartStop+1.
unsigned mStartStop;
SGSubgraphCPtr mSubgraph;
// For each place within the subgraph, mPlaceActivities contains the
// index of its activity
// - Activities associated with the sources are shared with the
// activities activities associated with the targets of the subgraph
// in the previous stop.
std::unordered_map<SGPlaceCPtr, unsigned> mPlaceActivities;
Element(unsigned start_stop, SGSubgraphCPtr subgraph)
: mStartStop(start_stop), mSubgraph(std::move(subgraph))
......@@ -34,18 +62,34 @@ namespace Loco
};
private:
TrainCPtr mTrain;
TrainPlace mEnterPlace;
TrainPlace mLeavePlace;
SectionGraphCPtr mSectionGraph;
TrainCPtr mTrain{};
unsigned mTrainId{};
TPlaceCPtr mEnterPlace{};
TPlaceCPtr mLeavePlace{};
SectionGraphCPtr mSectionGraph{};
std::vector<Element> mElements;
// Enter activity is always the first in the list
// Leave activity is always the last in the list
std::vector<Activity> mActList;
void AddElement(unsigned start);
void BuildElements();
void InitActList();
void AddEnterActivity();
void AddLeaveActivity();
void AddElementActivities(unsigned element_id, bool add_leaves);
std::ostream &
PrintActNamesList(std::ostream &os,
const std::vector<unsigned> &act_ids) const;
std::ostream &PrintActivity(std::ostream &os, const Activity &act) const;
public:
TrainRouteGraph(TrainCPtr train, unsigned train_id,
SectionGraphCPtr section_graph);
std::ostream &PrintActivities(std::ostream &os,
const std::string &prefix) const;
};
// Manages train route graphs for all trains
......@@ -61,6 +105,9 @@ namespace Loco
public:
TrainRoutes(const InputData &input_data,
const SectionGraphCPtr &section_graph);
std::ostream &PrintActivities(std::ostream &os,
const std::string &prefix) const;
};
} // namespace Loco
......
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