Skip to content

Commit

Permalink
Use faster set implementation in ruin&recreate
Browse files Browse the repository at this point in the history
  • Loading branch information
acco93 committed Mar 13, 2024
1 parent 42276b7 commit 2127972
Show file tree
Hide file tree
Showing 27 changed files with 109 additions and 106 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ add_executable(filo2
base/PrettyPrinter.hpp
base/SmallFlatMap.hpp
base/SmallFlatSet.hpp
base/SparseIntSet.hpp
base/Timer.hpp
base/VectorView.hpp
base/VertexSet.hpp
base/Welford.hpp

instance/Instance.cpp
Expand Down
64 changes: 64 additions & 0 deletions base/SparseIntSet.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef _FILO2_SPARSEINTSET_HPP_
#define _FILO2_SPARSEINTSET_HPP_

#include <bitset>
#include <cmath>
#include <vector>

namespace cobra {

class SparseIntSet {

public:
explicit SparseIntSet(unsigned int entries_num) {
flags.resize(entries_num);
}

SparseIntSet(const SparseIntSet &other) {
flags = other.flags;
}

SparseIntSet &operator=(const SparseIntSet &other) {
flags = other.flags;
return *this;
}

inline void insert(int value) {
const auto already_here = contains(value);
if (!already_here) {
insert_without_checking_existance(value);
}
}

inline void insert_without_checking_existance(int value) {
flags[value] = true;
elements.push_back(value);
}

inline bool contains(int value) {
return flags[value];
}

void clear() {
for (auto value : elements) {
flags[value] = false;
}
elements.clear();
}

const std::vector<int> &get_elements() const {
return elements;
}

unsigned int size() const {
return elements.size();
}

private:
std::vector<bool> flags;
std::vector<int> elements;
};

} // namespace cobra

#endif
65 changes: 0 additions & 65 deletions base/VertexSet.hpp

This file was deleted.

10 changes: 5 additions & 5 deletions localsearch/AbstractOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace cobra {

// Applies `move` to `solution` and populates `affected_vertices` with the vertices `i` for which at least one move generator `(i,
// j)` or `(j, i)` require an update.
virtual void execute(Solution& solution, const MoveGenerator& move, VertexSet& affected_vertices) = 0;
virtual void execute(Solution& solution, const MoveGenerator& move, SparseIntSet& affected_vertices) = 0;

// Performs some final cleanup at the end of the local search cycle, if necessary.
virtual void post_processing(Solution& solution) = 0;
Expand Down Expand Up @@ -415,7 +415,7 @@ namespace cobra {
};

auto depot = false;
for (auto i : affected_vertices.get_vertices()) {
for (auto i : affected_vertices.get_elements()) {

if constexpr (handle_partial_solutions) {
if (!solution.is_vertex_in_solution(i)) {
Expand Down Expand Up @@ -524,7 +524,7 @@ namespace cobra {
};

auto depot = false;
for (auto i : affected_vertices.get_vertices()) {
for (auto i : affected_vertices.get_elements()) {

if constexpr (handle_partial_solutions) {
if (!solution.is_vertex_in_solution(i)) {
Expand Down Expand Up @@ -776,7 +776,7 @@ namespace cobra {
}

// Reset the update bits associated with affected vertices.
for (auto i : affected_vertices.get_vertices()) {
for (auto i : affected_vertices.get_elements()) {
T::update_bits.at(i, UPDATE_BITS_FIRST, false);
T::update_bits.at(i, UPDATE_BITS_SECOND, false);
}
Expand All @@ -789,7 +789,7 @@ namespace cobra {

// Set of vertices affected by a move application. This set together with `update_bits` define the move generators requiring an
// update.
VertexSet affected_vertices;
SparseIntSet affected_vertices;
};


Expand Down
4 changes: 2 additions & 2 deletions localsearch/EjectionChain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ namespace cobra {
}

inline void execute(Solution &solution, __attribute__((unused)) const MoveGenerator &generating_move,
VertexSet &affected_vertices) override {
SparseIntSet &affected_vertices) override {

for (auto i : forbidden_i.get_set_entries_possibly_with_duplicates(feasible_rni)) {
affected_vertices.insert(i);
Expand All @@ -254,7 +254,7 @@ namespace cobra {
}

// Reset some cached deltas associated with affected vertices.
for (const int i : affected_vertices.get_vertices()) {
for (const int i : affected_vertices.get_elements()) {
for (const int move_index : moves.get_move_generator_indices_involving(i)) {
moves.get(move_index).set_computed_for_ejch(false);
moves.get(move_index + 1).set_computed_for_ejch(false);
Expand Down
2 changes: 1 addition & 1 deletion localsearch/LocalSearch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <map>
#include <set>

#include "../base/VertexSet.hpp"
#include "../base/SparseIntSet.hpp"
#include "../instance/Instance.hpp"
#include "../movegen/MoveGenerators.hpp"
#include "../solution/Solution.hpp"
Expand Down
2 changes: 1 addition & 1 deletion localsearch/OneOneExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace cobra {
(i_route == j_route && i != j_prev && j_prev != solution.get_next_vertex(i_route, i));
}

inline void execute(Solution& solution, const MoveGenerator& move, VertexSet& storage) override {
inline void execute(Solution& solution, const MoveGenerator& move, SparseIntSet& storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/OneZeroExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace cobra {
(iRoute == jRoute && j != solution.get_next_vertex(iRoute, i));
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/RevThreeOneExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace cobra {
jNext != solution.get_prev_vertex(iRoute, iPrevPrev));
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/RevThreeThreeExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace cobra {
jNextNextNext != iPrevPrev && jNextNextNext != solution.get_prev_vertex(iRoute, iPrevPrev));
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/RevThreeTwoExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace cobra {
jNextNext != solution.get_prev_vertex(iRoute, iPrevPrev));
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/RevThreeZeroExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace cobra {
(iRoute == jRoute && j != iPrev && j != iPrevPrev && j != solution.get_prev_vertex(iRoute, iPrevPrev));
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {
const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();

Expand Down
2 changes: 1 addition & 1 deletion localsearch/RevTwoOneExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace cobra {
(iRoute == jRoute && j != iPrev && j != iPrevPrev && jNext != iPrevPrev);
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/RevTwoTwoExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace cobra {
jNextNext != solution.get_prev_vertex(iRoute, iPrev));
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/RevTwoZeroExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace cobra {
(iRoute == jRoute && iPrev != j && j != solution.get_prev_vertex(iRoute, iPrev));
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/SplitExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace cobra {
this->instance.get_vehicle_capacity());
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/TailsExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace cobra {
this->instance.get_vehicle_capacity();
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/ThreeOneExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace cobra {
(iRoute == jRoute && i != jPrev && i != solution.get_prev_vertex(jRoute, jPrev) && j != iPrev && j != iPrevPrev);
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/ThreeThreeExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace cobra {
solution.get_next_vertex(iRoute, i) != jPrevPrevPrev && j != iPrev && j != iPrevPrev);
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/ThreeTwoExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace cobra {
(iRoute == jRoute && i != jPrev && i != jPrevPrev && i != jPrevPrevPrev && j != iPrev && j != iPrevPrev);
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/ThreeZeroExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace cobra {
(iRoute == jRoute && j != iPrev && j != iPrevPrev && j != solution.get_next_vertex(iRoute, i));
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {
const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();

Expand Down
2 changes: 1 addition & 1 deletion localsearch/TwoOneExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace cobra {
(iRoute == jRoute && i != jPrev && solution.get_next_vertex(iRoute, i) != jPrev && iPrev != j);
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/TwoOptExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace cobra {
return iRoute == jRoute;
}

inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/TwoTwoExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace cobra {
}


inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down
2 changes: 1 addition & 1 deletion localsearch/TwoZeroExchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace cobra {
}


inline void execute(Solution &solution, const MoveGenerator &move, VertexSet &storage) override {
inline void execute(Solution &solution, const MoveGenerator &move, SparseIntSet &storage) override {

const auto i = move.get_first_vertex();
const auto j = move.get_second_vertex();
Expand Down

0 comments on commit 2127972

Please sign in to comment.