Skip to content

Commit

Permalink
Updated the code to the latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Santini committed Oct 27, 2018
1 parent 217b433 commit a6f334e
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 80 deletions.
14 changes: 10 additions & 4 deletions source-code/Graph.cpp
Expand Up @@ -18,7 +18,7 @@ namespace op {
namespace fs = std::experimental::filesystem;

Graph::Graph(fs::path graph_file) : graph_file{graph_file}, opi{graph_file.string()} {
n_vertices = opi->number_of_vertices();
n_vertices = n_reachable_vertices = opi->number_of_vertices();
max_travel_time = opi->get_max_travel_time();

for(auto i = 0u; i < n_vertices; ++i) {
Expand All @@ -36,7 +36,10 @@ namespace op {

std::size_t edge_id = 0u;
for(auto i = 0u; i < n_vertices; ++i) {
if(!g[i].reachable) { continue; }
if(!g[i].reachable) {
--n_reachable_vertices;
continue;
}

for(auto j = i + 1; j < n_vertices; ++j) {
if(!g[j].reachable) { continue; }
Expand Down Expand Up @@ -87,11 +90,14 @@ namespace op {
for(const auto& vertex : vertices) {
boost::add_vertex(vertex, g);
}
n_vertices = boost::num_vertices(g);
n_vertices = n_reachable_vertices = boost::num_vertices(g);

std::size_t edge_id = 0u;
for(auto i = 0u; i < n_vertices; ++i) {
if(!vertices[i].reachable) { continue; }
if(!vertices[i].reachable) {
--n_reachable_vertices;
continue;
}

for(auto j = i + 1; j < n_vertices; ++j) {
if(!vertices[j].reachable) { continue; }
Expand Down
5 changes: 5 additions & 0 deletions source-code/Graph.h
Expand Up @@ -51,6 +51,11 @@ namespace op {
*/
std::size_t n_vertices;

/**
* Number of reachable vertices in the graph.
*/
std::size_t n_reachable_vertices;

/**
* Smallest x coordinate for the vertices.
*/
Expand Down
55 changes: 44 additions & 11 deletions source-code/GreedyHeuristic.cpp
Expand Up @@ -6,6 +6,7 @@
#include <as/console.h>
#include <as/random.h>
#include "palns/PALNSSolution.h"
#include "palns/repair/GreedyRepair.h"
#include "GreedyHeuristic.h"
#include "BCSolver.h"

Expand All @@ -32,8 +33,16 @@ namespace op {
tour = solve_without_clustering();
}

// Don't lose time with the initial solution: don't do 2-opt.
// tour.do_2opt();
if(params.initial_solution.local_search) {
tour.do_2opt();

PALNSSolution sol(tour);
GreedyRepair rep(&params);
auto mt = as::rnd::get_seeded_mt();

rep.repair_solution(sol, mt);
tour = sol.tour;
}

if(params.repair.restore_feasibility_optimal > 0.0f) {
tour.make_travel_time_feasible_optimal();
Expand Down Expand Up @@ -67,10 +76,36 @@ namespace op {
Tour GreedyHeuristic::solve_without_clustering() const {
assert(graph.n_vertices >= 2u);

std::vector<BoostVertex> vertices = { 0u, 1u };
std::vector<BoostVertex> other_vertices(graph.n_vertices - 2u);
std::iota(other_vertices.begin(), other_vertices.end(), 2u);
std::shuffle(other_vertices.begin(), other_vertices.end(), as::rnd::get_seeded_mt());
std::vector<BoostVertex> vertices = { 0u };
std::vector<BoostVertex> other_vertices(graph.n_vertices - 1u);
std::iota(other_vertices.begin(), other_vertices.end(), 1u);

other_vertices.erase(
std::remove_if(other_vertices.begin(), other_vertices.end(),
[&] (const auto& v) -> bool {
return !graph.g[v].reachable;
}
),
other_vertices.end()
);

if(params.initial_solution.vertex_order == "random") {
std::shuffle(other_vertices.begin(), other_vertices.end(), as::rnd::get_seeded_mt());
} else if(params.initial_solution.vertex_order == "prize") {
std::sort(other_vertices.begin(), other_vertices.end(),
[&] (const auto& v1, const auto& v2) -> bool {
return graph.g[v1].prize < graph.g[v2].prize;
}
);
} else if(params.initial_solution.vertex_order == "distance") {
std::sort(other_vertices.begin(), other_vertices.end(),
[&] (const auto& v1, const auto& v2) -> bool {
return graph.travel_time(0u, v1) < graph.travel_time(0u, v2);
}
);
} else {
std::cerr << as::console::warning << "Unrecognised vertex order " << params.initial_solution.vertex_order << "\n";
}

Tour tour(&graph, vertices);

Expand All @@ -80,9 +115,7 @@ namespace op {
PALNSSolution sol(tour);

for(const auto& v : other_vertices) {
if(graph.g[v].reachable) {
sol.add_vertex_in_best_pos_any(v);
}
sol.add_vertex_in_best_pos_any(v);
}

return sol.tour;
Expand All @@ -91,12 +124,12 @@ namespace op {
Tour GreedyHeuristic::solve_with_clustering_constructive(ReducedGraph& red) const {
assert(red.reduced_graph.n_vertices >= 2u);

std::vector<BoostVertex> vertices = { 0u, 1u };
std::vector<BoostVertex> vertices = { 0u };
Tour tour(&red.reduced_graph, vertices);

PALNSSolution sol(tour);

for(auto v = 2u; v < red.reduced_graph.n_vertices; ++v) {
for(auto v = 1u; v < red.reduced_graph.n_vertices; ++v) {
if(red.reduced_graph.g[v].reachable) {
sol.add_vertex_in_best_pos_any(v);
}
Expand Down
8 changes: 7 additions & 1 deletion source-code/GreedyHeuristic.h
Expand Up @@ -41,10 +41,16 @@ namespace op {
*/
Tour solve() const;

/**
* Produce a greedy heuristic solution, without using the graph clustering.
*
* @return The greedy solution.
*/
Tour solve_without_clustering() const;

private:
Tour solve_with_clustering_and_mip(ReducedGraph& red) const;
Tour solve_with_clustering_constructive(ReducedGraph& red) const;
Tour solve_without_clustering() const;
};
}

Expand Down
2 changes: 1 addition & 1 deletion source-code/Plotter.h
Expand Up @@ -8,7 +8,7 @@
#include "Graph.h"
#include "Tour.h"
#include <experimental/filesystem>
#include <cimg/CImg.h>
#include <CImg.h>

namespace op {
/**
Expand Down
4 changes: 2 additions & 2 deletions source-code/RTreeUtils.cpp
Expand Up @@ -184,12 +184,12 @@ namespace op {
// Get the distance of each vertex to its nearest neighbour.
const auto distances = nearest_neighbour_distances(g);

// Get the smallest distance.
// Get the largest distance.
const auto radius = distances.back();

std::cout << as::console::notice << "DBSCAN auto-tuned radius: " << radius << std::endl;

// Now we know that 95% of the vertices have their nearest
// Now we know that all the vertices have their nearest
// neighbour within this radius. We now check how many points
// lie within each vertex's neighbourhood, using this radius.
// In other words, we want to know how many points will be
Expand Down

0 comments on commit a6f334e

Please sign in to comment.