Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Prevent manually declared layout obstructions to be cleared by the application of Yen's Algorithm #143

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions include/fiction/algorithms/path_finding/k_shortest_paths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cassert>
#include <cstdint>
#include <iterator>
#include <utility>
#include <vector>

namespace fiction
Expand Down Expand Up @@ -51,7 +52,7 @@ class yen_k_shortest_paths_impl
unit_cost_functor<obstruction_layout<Lyt>, uint8_t>(), ps.astar_params));
}

path_collection<Path> run()
path_collection<Path> run() noexcept
{
assert(!objective.source.is_dead() && !objective.target.is_dead() &&
"Neither source nor target coordinate can be dead");
Expand Down Expand Up @@ -87,6 +88,8 @@ class yen_k_shortest_paths_impl
{
// block the connection that was already used in the previous shortest path
layout.obstruct_connection(p[i], p[i + 1]);
// store connection for later clearing
temporarily_obstructed_connections.push_back({p[i], p[i + 1]});
}
}

Expand All @@ -98,6 +101,8 @@ class yen_k_shortest_paths_impl
{
// block them from further exploration
layout.obstruct_coordinate(root);
// store coordinate for later clearing
temporarily_obstructed_coordinates.push_back(root);
}
}

Expand All @@ -124,8 +129,7 @@ class yen_k_shortest_paths_impl
}

// clear obstructions again (prepare for the next potential path)
layout.clear_obstructed_coordinates();
layout.clear_obstructed_connections();
reset_temporary_obstructions();
}

// if there were no spur paths or if all spur paths have been added to k_shortest_paths already
Expand Down Expand Up @@ -173,6 +177,14 @@ class yen_k_shortest_paths_impl
* A set of potential shortest paths.
*/
path_set<Path> shortest_path_candidates{};
/**
* A temporary storage for coordinates that are obstructed during the algorithm.
*/
std::vector<coordinate<Lyt>> temporarily_obstructed_coordinates{};
/**
* A temporary storage for coordinates that are obstructed during the algorithm.
*/
std::vector<std::pair<coordinate<Lyt>, coordinate<Lyt>>> temporarily_obstructed_connections{};
/**
* Computes the cost of a path. This function can be adjusted to fetch paths of differing costs.
*
Expand All @@ -185,6 +197,23 @@ class yen_k_shortest_paths_impl
{
return p.size();
}
/**
* Resets all temporary obstructions.
*/
void reset_temporary_obstructions() noexcept
{
for (const auto& c : temporarily_obstructed_coordinates)
{
layout.clear_obstructed_coordinate(c);
}
for (const auto& c : temporarily_obstructed_connections)
{
layout.clear_obstructed_connection(c.first, c.second);
}

temporarily_obstructed_coordinates.clear();
temporarily_obstructed_connections.clear();
}
};

} // namespace detail
Expand Down
21 changes: 21 additions & 0 deletions include/fiction/layouts/obstruction_layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ class obstruction_layout<Lyt, false> : public Lyt
{
strg->obstructed_connections.insert({src, tgt});
}
/**
* Clears the obstruction status of the given coordinate `c` if the obstruction was manually marked via
* `obstruct_coordinate`.
*
* @param c Coordinate to clear.
*/
void clear_obstructed_coordinate(const typename Lyt::coordinate& c) noexcept
{
strg->obstructed_coordinates.erase(c);
}
/**
* Clears the obstruction status of the connection from coordinate `src` to coordinate `tgt` if the obstruction was
* manually marked via `obstruct_connection`.
*
* @param src Source coordinate.
* @param tgt Target coordinate.
*/
void clear_obstructed_connection(const typename Lyt::coordinate& src, const typename Lyt::coordinate& tgt) noexcept
{
strg->obstructed_connections.erase({src, tgt});
}
/**
* Clears all obstructed coordinates that were manually marked via `obstruct_coordinate`.
*/
Expand Down
63 changes: 61 additions & 2 deletions test/algorithms/path_finding/k_shortest_paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ TEST_CASE("Yen's algorithm on 4x4 gate-level layouts with coordinate obstruction
{
const gate_lyt layout{{3, 3}, twoddwave_clocking<gate_lyt>()};

SECTION("(0,0) to (3,3) with coordinate obstruction") // path of length 7
SECTION("(0,0) to (3,3) with coordinate obstruction via PIs") // path of length 7
{
obstruction_layout obstr_lyt{layout};

Expand All @@ -303,12 +303,41 @@ TEST_CASE("Yen's algorithm on 4x4 gate-level layouts with coordinate obstruction
CHECK(path[4] == coordinate<gate_lyt>{1, 3});
CHECK(path[5] == coordinate<gate_lyt>{2, 3});
}
SECTION("(0,0) to (3,3) with coordinate obstruction via declaration") // path of length 7
{
obstruction_layout obstr_lyt{layout};

// create some PIs as obstruction
obstr_lyt.obstruct_coordinate({3, 0});
obstr_lyt.obstruct_coordinate({3, 1});
obstr_lyt.obstruct_coordinate({1, 2});
obstr_lyt.obstruct_coordinate({2, 2});
// effectively blocking (3,2) as well

const auto collection =
yen_k_shortest_paths<coord_path>(obstr_lyt, {{0, 0}, {3, 3}}, 1); // only one path possible

REQUIRE(collection.size() == 1);
const auto& path = collection[0];
REQUIRE(path.size() == 7);
CHECK(path[1] == coordinate<gate_lyt>{0, 1});
CHECK(path[2] == coordinate<gate_lyt>{0, 2});
CHECK(path[3] == coordinate<gate_lyt>{0, 3});
CHECK(path[4] == coordinate<gate_lyt>{1, 3});
CHECK(path[5] == coordinate<gate_lyt>{2, 3});

// coordinates should still be obstructed
CHECK(obstr_lyt.is_obstructed_coordinate({3, 0}));
CHECK(obstr_lyt.is_obstructed_coordinate({3, 1}));
CHECK(obstr_lyt.is_obstructed_coordinate({1, 2}));
CHECK(obstr_lyt.is_obstructed_coordinate({2, 2}));
}
}
SECTION("USE")
{
const gate_lyt layout{{3, 3}, use_clocking<gate_lyt>()};

SECTION("(0,0) to (3,3) with coordinate obstruction") // path of length 7
SECTION("(0,0) to (3,3) with coordinate obstruction via PIs") // path of length 7
{
obstruction_layout obstr_lyt{layout};

Expand All @@ -327,6 +356,28 @@ TEST_CASE("Yen's algorithm on 4x4 gate-level layouts with coordinate obstruction
CHECK(path[4] == coordinate<gate_lyt>{2, 2});
CHECK(path[5] == coordinate<gate_lyt>{3, 2});
}
SECTION("(0,0) to (3,3) with coordinate obstruction via declaration") // path of length 7
{
obstruction_layout obstr_lyt{layout};

// create a PI as obstruction
obstr_lyt.obstruct_coordinate({3, 0}); // blocks 3 paths

const auto collection =
yen_k_shortest_paths<coord_path>(obstr_lyt, {{0, 0}, {3, 3}}, 1); // only one path possible

REQUIRE(collection.size() == 1);
const auto& path = collection[0];
REQUIRE(path.size() == 7);
CHECK(path[1] == coordinate<gate_lyt>{1, 0});
CHECK(path[2] == coordinate<gate_lyt>{1, 1});
CHECK(path[3] == coordinate<gate_lyt>{1, 2});
CHECK(path[4] == coordinate<gate_lyt>{2, 2});
CHECK(path[5] == coordinate<gate_lyt>{3, 2});

// coordinates should still be obstructed
CHECK(obstr_lyt.is_obstructed_coordinate({3, 0}));
}
}
}

Expand Down Expand Up @@ -481,6 +532,11 @@ TEST_CASE("Yen's algorithm on 4x4 gate-level layouts with connection obstruction
CHECK(path[3] == coordinate<gate_lyt>{0, 3});
CHECK(path[4] == coordinate<gate_lyt>{1, 3});
CHECK(path[5] == coordinate<gate_lyt>{2, 3});

// connections should still be obstructed
CHECK(obstr_lyt.is_obstructed_connection({0, 0}, {1, 0}));
CHECK(obstr_lyt.is_obstructed_connection({0, 1}, {1, 1}));
CHECK(obstr_lyt.is_obstructed_connection({0, 2}, {1, 2}));
}
}
SECTION("USE")
Expand All @@ -504,6 +560,9 @@ TEST_CASE("Yen's algorithm on 4x4 gate-level layouts with connection obstruction
CHECK(path[3] == coordinate<gate_lyt>{1, 2});
CHECK(path[4] == coordinate<gate_lyt>{2, 2});
CHECK(path[5] == coordinate<gate_lyt>{3, 2});

// connections should still be obstructed
CHECK(obstr_lyt.is_obstructed_connection({2, 0}, {3, 0}));
}
}
}
92 changes: 92 additions & 0 deletions test/layouts/obstruction_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ TEST_CASE("Coordinate obstruction", "[obstruction-layout]")
CHECK(obstr_lyt.is_obstructed_coordinate({3, 0}));
CHECK(obstr_lyt.is_obstructed_coordinate({4, 0}));

// remove some artificial obstructions
obstr_lyt.clear_obstructed_coordinate({0, 0});
obstr_lyt.clear_obstructed_coordinate({1, 0});
obstr_lyt.clear_obstructed_coordinate({2, 0});

CHECK(!obstr_lyt.is_obstructed_coordinate({0, 0}));
CHECK(!obstr_lyt.is_obstructed_coordinate({1, 0}));
CHECK(!obstr_lyt.is_obstructed_coordinate({2, 0}));
CHECK(obstr_lyt.is_obstructed_coordinate({3, 0}));
CHECK(obstr_lyt.is_obstructed_coordinate({4, 0}));

// remove all obstructions
obstr_lyt.clear_obstructed_coordinates();

CHECK(!obstr_lyt.is_obstructed_coordinate({0, 0}));
Expand Down Expand Up @@ -161,6 +173,24 @@ TEST_CASE("Coordinate obstruction", "[obstruction-layout]")
CHECK(!obstr_lyt.is_obstructed_coordinate({3, 0}));
CHECK(obstr_lyt.is_obstructed_coordinate({3, 2}));

// remove some manually added obstructions
obstr_lyt.clear_obstructed_coordinate({0, 1});
obstr_lyt.clear_obstructed_coordinate({1, 2});

CHECK(!obstr_lyt.is_obstructed_coordinate({0, 1}));
CHECK(!obstr_lyt.is_obstructed_coordinate({1, 2}));
CHECK(obstr_lyt.is_obstructed_coordinate({3, 2}));

// removing an obstruction that was not added manually should not change anything
obstr_lyt.clear_obstructed_coordinate({1, 1});
obstr_lyt.clear_obstructed_coordinate({2, 0});
obstr_lyt.clear_obstructed_coordinate({3, 1});

CHECK(obstr_lyt.is_obstructed_coordinate({1, 1}));
CHECK(obstr_lyt.is_obstructed_coordinate({2, 0}));
CHECK(obstr_lyt.is_obstructed_coordinate({3, 1}));

// remove all obstructions
obstr_lyt.clear_obstructed_coordinates();

CHECK(!obstr_lyt.is_obstructed_coordinate({0, 1}));
Expand Down Expand Up @@ -231,6 +261,35 @@ TEST_CASE("Coordinate obstruction", "[obstruction-layout]")
CHECK(!obstr_lyt.is_obstructed_coordinate({4, 3}));
CHECK(!obstr_lyt.is_obstructed_coordinate({4, 4}));

// remove some artificial obstructions
obstr_lyt.clear_obstructed_coordinate({0, 0});
obstr_lyt.clear_obstructed_coordinate({0, 1});
obstr_lyt.clear_obstructed_coordinate({0, 3});
obstr_lyt.clear_obstructed_coordinate({0, 4});

CHECK(!obstr_lyt.is_obstructed_coordinate({0, 0}));
CHECK(!obstr_lyt.is_obstructed_coordinate({0, 1}));
CHECK(!obstr_lyt.is_obstructed_coordinate({0, 3}));
CHECK(!obstr_lyt.is_obstructed_coordinate({0, 4}));
CHECK(obstr_lyt.is_obstructed_coordinate({1, 0}));
CHECK(obstr_lyt.is_obstructed_coordinate({1, 1}));
CHECK(obstr_lyt.is_obstructed_coordinate({1, 3}));
CHECK(obstr_lyt.is_obstructed_coordinate({1, 4}));

// removing an obstruction that was not added manually should not change anything
obstr_lyt.clear_obstructed_coordinate({0, 2});
obstr_lyt.clear_obstructed_coordinate({2, 4});
obstr_lyt.clear_obstructed_coordinate({2, 0});
obstr_lyt.clear_obstructed_coordinate({2, 1});
obstr_lyt.clear_obstructed_coordinate({2, 2});

CHECK(obstr_lyt.is_obstructed_coordinate({0, 2}));
CHECK(obstr_lyt.is_obstructed_coordinate({2, 4}));
CHECK(obstr_lyt.is_obstructed_coordinate({2, 0}));
CHECK(obstr_lyt.is_obstructed_coordinate({2, 1}));
CHECK(obstr_lyt.is_obstructed_coordinate({2, 2}));

// remove all artificial obstructions
obstr_lyt.clear_obstructed_coordinates();

CHECK(!obstr_lyt.is_obstructed_coordinate({0, 0}));
Expand Down Expand Up @@ -286,6 +345,14 @@ TEST_CASE("Connection obstruction", "[obstruction-layout]")
CHECK(!obstr_lyt.is_obstructed_connection({3, 3}, {2, 2}));
CHECK(!obstr_lyt.is_obstructed_connection({3, 3}, {2, 3}));

// remove some artificial obstructions
obstr_lyt.clear_obstructed_connection({0, 0}, {0, 1});

CHECK(!obstr_lyt.is_obstructed_connection({0, 0}, {0, 1}));
CHECK(obstr_lyt.is_obstructed_connection({2, 2}, {2, 3}));
CHECK(obstr_lyt.is_obstructed_connection({2, 4}, {4, 0}));

// remove all artificial obstructions
obstr_lyt.clear_obstructed_connections();

CHECK(!obstr_lyt.is_obstructed_connection({0, 0}, {0, 1}));
Expand Down Expand Up @@ -334,6 +401,23 @@ TEST_CASE("Connection obstruction", "[obstruction-layout]")
CHECK(!obstr_lyt.is_obstructed_connection({3, 3}, {2, 2}));
CHECK(!obstr_lyt.is_obstructed_connection({3, 3}, {2, 3}));

// remove some artificial obstructions
obstr_lyt.clear_obstructed_connection({0, 0}, {0, 1});

CHECK(!obstr_lyt.is_obstructed_connection({0, 0}, {0, 1}));
CHECK(obstr_lyt.is_obstructed_connection({2, 2}, {2, 3}));
CHECK(obstr_lyt.is_obstructed_connection({2, 4}, {4, 0}));

// removing an obstruction that was not added manually should not change anything
obstr_lyt.clear_obstructed_connection({1, 1}, {2, 1});
obstr_lyt.clear_obstructed_connection({2, 0}, {2, 1});
obstr_lyt.clear_obstructed_connection({3, 1}, {2, 1});

CHECK(obstr_lyt.is_obstructed_connection({1, 1}, {2, 1}));
CHECK(obstr_lyt.is_obstructed_connection({2, 0}, {2, 1}));
CHECK(obstr_lyt.is_obstructed_connection({3, 1}, {2, 1}));

// remove all artificial obstructions
obstr_lyt.clear_obstructed_connections();

CHECK(!obstr_lyt.is_obstructed_connection({0, 0}, {0, 1}));
Expand Down Expand Up @@ -379,6 +463,14 @@ TEST_CASE("Connection obstruction", "[obstruction-layout]")
CHECK(!obstr_lyt.is_obstructed_connection({3, 3}, {2, 2}));
CHECK(!obstr_lyt.is_obstructed_connection({3, 3}, {2, 3}));

// remove some artificial obstructions
obstr_lyt.clear_obstructed_connection({0, 0}, {0, 1});

CHECK(!obstr_lyt.is_obstructed_connection({0, 0}, {0, 1}));
CHECK(obstr_lyt.is_obstructed_connection({2, 2}, {2, 3}));
CHECK(obstr_lyt.is_obstructed_connection({2, 4}, {4, 0}));

// remove all artificial obstructions
obstr_lyt.clear_obstructed_connections();

CHECK(!obstr_lyt.is_obstructed_connection({0, 0}, {0, 1}));
Expand Down