[routing] Some minor fixes for routing_tests

This commit is contained in:
Mikhail Gorbushin 2019-04-30 15:47:15 +03:00 committed by Vladimir Byko-Ianko
parent ce283cf2c3
commit a540d5791a
3 changed files with 115 additions and 49 deletions

View file

@ -4,6 +4,8 @@
#include "routing/base/astar_graph.hpp"
#include "routing/base/routing_result.hpp"
#include "routing/routing_tests/routing_algorithm.hpp"
#include <map>
#include <utility>
#include <vector>
@ -13,52 +15,7 @@ namespace routing_test
using namespace routing;
using namespace std;
struct Edge
{
Edge(unsigned v, double w) : v(v), w(w) {}
unsigned GetTarget() const { return v; }
double GetWeight() const { return w; }
unsigned v;
double w;
};
class UndirectedGraph : public AStarGraph<unsigned, routing_test::Edge, double>
{
public:
void AddEdge(unsigned u, unsigned v, unsigned w)
{
m_adjs[u].push_back(Edge(v, w));
m_adjs[v].push_back(Edge(u, w));
}
void GetAdjacencyList(unsigned v, vector<Edge> & adj) const
{
adj.clear();
auto const it = m_adjs.find(v);
if (it != m_adjs.end())
adj = it->second;
}
void GetIngoingEdgesList(Vertex const & v, vector<Edge> & adj) override
{
GetAdjacencyList(v, adj);
}
void GetOutgoingEdgesList(Vertex const & v, vector<Edge> & adj) override
{
GetAdjacencyList(v, adj);
}
double HeuristicCostEstimate(Vertex const & v, Vertex const & w) override { return 0; }
private:
map<unsigned, vector<Edge>> m_adjs;
};
using Algorithm = AStarAlgorithm<unsigned, routing_test::Edge, double>;
using Algorithm = AStarAlgorithm<uint32_t, SimpleEdge, double>;
void TestAStar(UndirectedGraph & graph, vector<unsigned> const & expectedRoute, double const & expectedDistance)
{
@ -132,7 +89,7 @@ UNIT_TEST(AdjustRoute)
graph.AddEdge(6, 2, 1);
// Each edge contains {vertexId, weight}.
vector<Edge> const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}};
vector<SimpleEdge> const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}};
Algorithm algo;
Algorithm::ParamsForTests params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute,
@ -154,7 +111,7 @@ UNIT_TEST(AdjustRouteNoPath)
graph.AddEdge(i /* from */, i + 1 /* to */, 1 /* weight */);
// Each edge contains {vertexId, weight}.
vector<Edge> const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}};
vector<SimpleEdge> const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}};
Algorithm algo;
Algorithm::ParamsForTests params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute,
@ -176,7 +133,7 @@ UNIT_TEST(AdjustRouteOutOfLimit)
graph.AddEdge(6, 2, 2);
// Each edge contains {vertexId, weight}.
vector<Edge> const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}};
vector<SimpleEdge> const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}};
Algorithm algo;
Algorithm::ParamsForTests params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute,

View file

@ -14,6 +14,59 @@
#include <cstdint>
#include <vector>
namespace routing_test
{
void UndirectedGraph::AddEdge(Vertex u, Vertex v, Weight w)
{
m_adjs[u].emplace_back(v, w);
m_adjs[v].emplace_back(u, w);
}
size_t UndirectedGraph::GetNodesNumber() const
{
return m_adjs.size();
}
void UndirectedGraph::GetIngoingEdgesList(Vertex const & v, std::vector<SimpleEdge> & adj)
{
GetAdjacencyList(v, adj);
}
void UndirectedGraph::GetOutgoingEdgesList(Vertex const & v, std::vector<SimpleEdge> & adj)
{
GetAdjacencyList(v, adj);
}
double UndirectedGraph::HeuristicCostEstimate(Vertex const & v, Vertex const & w)
{
return 0;
}
void UndirectedGraph::GetAdjacencyList(Vertex v, std::vector<Edge> & adj) const
{
adj.clear();
auto const it = m_adjs.find(v);
if (it != m_adjs.cend())
adj = it->second;
}
void DirectedGraph::AddEdge(Vertex from, Vertex to, Weight w)
{
m_outgoing[from].emplace_back(to, w);
m_ingoing[to].emplace_back(from, w);
}
void DirectedGraph::GetIngoingEdgesList(Vertex const & v, vector<Edge> & adj)
{
adj = m_ingoing[v];
}
void DirectedGraph::GetOutgoingEdgesList(Vertex const & v, vector<Edge> & adj)
{
adj = m_outgoing[v];
}
} // namespace routing_tests
namespace routing
{
using namespace std;

View file

@ -1,11 +1,67 @@
#pragma once
#include "routing/base/astar_algorithm.hpp"
#include "routing/base/routing_result.hpp"
#include "routing/road_graph.hpp"
#include "routing/router.hpp"
#include <cstdint>
#include <map>
#include <string>
namespace routing_test
{
using namespace routing;
struct SimpleEdge
{
SimpleEdge(uint32_t to, double weight) : m_to(to), m_weight(weight) {}
uint32_t GetTarget() const { return m_to; }
double GetWeight() const { return m_weight; }
uint32_t m_to;
double m_weight;
};
class UndirectedGraph : public AStarGraph<uint32_t, SimpleEdge, double>
{
public:
void AddEdge(Vertex u, Vertex v, Weight w);
size_t GetNodesNumber() const;
// AStarGraph overrides
// @{
void GetIngoingEdgesList(Vertex const & v, vector<Edge> & adj) override;
void GetOutgoingEdgesList(Vertex const & v, vector<Edge> & adj) override;
double HeuristicCostEstimate(Vertex const & v, Vertex const & w) override;
// @}
private:
void GetAdjacencyList(Vertex v, vector<Edge> & adj) const;
std::map<uint32_t, std::vector<Edge>> m_adjs;
};
class DirectedGraph
{
public:
using Vertex = uint32_t;
using Edge = SimpleEdge;
using Weight = double;
void AddEdge(Vertex from, Vertex to, Weight w);
void GetIngoingEdgesList(Vertex const & v, vector<Edge> & adj);
void GetOutgoingEdgesList(Vertex const & v, vector<Edge> & adj);
private:
std::map<uint32_t, std::vector<Edge>> m_outgoing;
std::map<uint32_t, std::vector<Edge>> m_ingoing;
};
} // namespace routing_tests
namespace routing
{
class TestAStarBidirectionalAlgo