Skip to content

Commit

Permalink
Remove some template complexity.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoupey committed Oct 16, 2024
1 parent e731291 commit 15d832b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Refactor `Matrix` template class (#1089)
- Refactor to use `std::format` whenever possible (#1081)
- Reduce complexity for recreation process (#1155)
- Refactor `SolutionIndicators` (#1169)

#### CI

Expand Down
51 changes: 25 additions & 26 deletions src/algorithms/local_search/local_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void LocalSearch<Route,
steps.push_back({utils::now(),
log::EVENT::JOB_ADDITION,
OperatorName::MAX,
utils::SolutionIndicators<Route>(_input, _sol),
utils::SolutionIndicators(_input, _sol),
std::nullopt});
}
#endif
Expand Down Expand Up @@ -1767,7 +1767,7 @@ void LocalSearch<Route,
steps.push_back({utils::now(),
log::EVENT::OPERATOR,
best_ops[best_source][best_target]->get_name(),
utils::SolutionIndicators<Route>(_input, _sol),
utils::SolutionIndicators(_input, _sol),
std::nullopt});
#endif

Expand Down Expand Up @@ -1936,8 +1936,7 @@ void LocalSearch<Route,
run_ls_step();

// Comparison with indicators for current solution.
if (const utils::SolutionIndicators<Route> current_sol_indicators(_input,
_sol);
if (const utils::SolutionIndicators current_sol_indicators(_input, _sol);
current_sol_indicators < _best_sol_indicators) {
_best_sol_indicators = current_sol_indicators;
_best_sol = _sol;
Expand Down Expand Up @@ -2002,7 +2001,7 @@ void LocalSearch<Route,
steps.push_back({utils::now(),
log::EVENT::RUIN,
OperatorName::MAX,
utils::SolutionIndicators<Route>(_input, _sol),
utils::SolutionIndicators(_input, _sol),
utils::format_solution(_input, _sol)});
#endif

Expand Down Expand Up @@ -2030,7 +2029,7 @@ void LocalSearch<Route,
steps.push_back({utils::now(),
log::EVENT::RECREATE,
OperatorName::MAX,
utils::SolutionIndicators<Route>(_input, _sol),
utils::SolutionIndicators(_input, _sol),
utils::format_solution(_input, _sol)});
#endif
}
Expand Down Expand Up @@ -2450,26 +2449,26 @@ template <class Route,
class RouteSplit,
class PriorityReplace,
class TSPFix>
utils::SolutionIndicators<Route> LocalSearch<Route,
UnassignedExchange,
CrossExchange,
MixedExchange,
TwoOpt,
ReverseTwoOpt,
Relocate,
OrOpt,
IntraExchange,
IntraCrossExchange,
IntraMixedExchange,
IntraRelocate,
IntraOrOpt,
IntraTwoOpt,
PDShift,
RouteExchange,
SwapStar,
RouteSplit,
PriorityReplace,
TSPFix>::indicators() const {
utils::SolutionIndicators LocalSearch<Route,
UnassignedExchange,
CrossExchange,
MixedExchange,
TwoOpt,
ReverseTwoOpt,
Relocate,
OrOpt,
IntraExchange,
IntraCrossExchange,
IntraMixedExchange,
IntraRelocate,
IntraOrOpt,
IntraTwoOpt,
PDShift,
RouteExchange,
SwapStar,
RouteSplit,
PriorityReplace,
TSPFix>::indicators() const {
return _best_sol_indicators;
}

Expand Down
8 changes: 4 additions & 4 deletions src/algorithms/local_search/local_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LocalSearch {
std::vector<Route> _sol;

std::vector<Route>& _best_sol;
utils::SolutionIndicators<Route> _best_sol_indicators;
utils::SolutionIndicators _best_sol_indicators;

#ifdef LOG_LS_OPERATORS
// Store operator usage stats.
Expand All @@ -64,7 +64,7 @@ class LocalSearch {
#endif

#ifdef LOG_LS
std::vector<log::Step<Route>> steps;
std::vector<log::Step> steps;
#endif

void recreate(const std::vector<Index>& routes
Expand Down Expand Up @@ -95,7 +95,7 @@ class LocalSearch {
unsigned depth,
const Timeout& timeout);

utils::SolutionIndicators<Route> indicators() const;
utils::SolutionIndicators indicators() const;

void run();

Expand All @@ -104,7 +104,7 @@ class LocalSearch {
#endif

#ifdef LOG_LS
std::vector<log::Step<Route>> get_steps() {
std::vector<log::Step> get_steps() {
return steps;
}
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/algorithms/local_search/log_local_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ enum class EVENT {
ROLLBACK
};

template <class Route> struct Step {
struct Step {
TimePoint time_point;
EVENT event;
OperatorName operator_name;
vroom::utils::SolutionIndicators<Route> indicators;
vroom::utils::SolutionIndicators indicators;
std::optional<Solution> solution;
};

template <class Route> struct Dump {
struct Dump {
HeuristicParameters heuristic_parameters;
std::vector<Step<Route>> steps;
std::vector<Step> steps;
};
} // namespace vroom::ls::log

Expand Down
6 changes: 3 additions & 3 deletions src/problems/vrp.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class VRP {
std::vector<std::vector<Route>> solutions(nb_searches, empty_sol);

#ifdef LOG_LS
std::vector<ls::log::Dump<Route>> ls_dumps;
std::vector<ls::log::Dump> ls_dumps;
ls_dumps.reserve(nb_searches);
#endif

Expand Down Expand Up @@ -195,7 +195,7 @@ class VRP {
}

// Filter out duplicate heuristics solutions.
std::set<utils::SolutionIndicators<Route>> unique_indicators;
std::set<utils::SolutionIndicators> unique_indicators;
std::vector<unsigned> to_remove;
to_remove.reserve(solutions.size());

Expand All @@ -217,7 +217,7 @@ class VRP {

// Split local searches across threads.
unsigned nb_solutions = solutions.size();
std::vector<utils::SolutionIndicators<Route>> sol_indicators(nb_solutions);
std::vector<utils::SolutionIndicators> sol_indicators(nb_solutions);
#ifdef LOG_LS_OPERATORS
std::vector<std::array<ls::OperatorStats, OperatorName::MAX>> ls_stats(
nb_solutions);
Expand Down
3 changes: 2 additions & 1 deletion src/structures/vroom/solution_indicators.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ All rights reserved (see LICENSE).

namespace vroom::utils {

template <class Route> struct SolutionIndicators {
struct SolutionIndicators {
Priority priority_sum{0};
unsigned assigned{0};
Eval eval;
unsigned used_vehicles{0};

SolutionIndicators() = default;

template <class Route>
SolutionIndicators(const Input& input, const std::vector<Route>& sol)
: SolutionIndicators() {
Index v_rank = 0;
Expand Down
15 changes: 3 additions & 12 deletions src/utils/output_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ void write_to_json(const Solution& sol,
}

#ifdef LOG_LS
template <class Route>
rapidjson::Value to_json(const std::vector<ls::log::Step<Route>>& steps,
rapidjson::Value to_json(const std::vector<ls::log::Step>& steps,
rapidjson::Document::AllocatorType& allocator) {
rapidjson::Value json_LS_steps(rapidjson::kArrayType);

Expand Down Expand Up @@ -480,8 +479,7 @@ rapidjson::Value to_json(const std::vector<ls::log::Step<Route>>& steps,
return json_LS_steps;
}

template <class Route>
rapidjson::Value to_json(const ls::log::Dump<Route>& dump,
rapidjson::Value to_json(const ls::log::Dump& dump,
rapidjson::Document::AllocatorType& allocator) {
rapidjson::Value json_parameters(rapidjson::kObjectType);

Expand Down Expand Up @@ -553,8 +551,7 @@ rapidjson::Value to_json(const ls::log::Dump<Route>& dump,
return json_parameters;
}

template <class Route>
void write_LS_logs_to_json(const std::vector<ls::log::Dump<Route>>& dumps) {
void write_LS_logs_to_json(const std::vector<ls::log::Dump>& dumps) {
rapidjson::Document json_log;
json_log.SetArray();
rapidjson::Document::AllocatorType& allocator = json_log.GetAllocator();
Expand All @@ -566,12 +563,6 @@ void write_LS_logs_to_json(const std::vector<ls::log::Dump<Route>>& dumps) {
write_to_output(json_log, "vroom_ls_log.json");
}

template void
write_LS_logs_to_json(const std::vector<ls::log::Dump<RawRoute>>& dumps);

template void
write_LS_logs_to_json(const std::vector<ls::log::Dump<TWRoute>>& dumps);

#endif

} // namespace vroom::io
3 changes: 1 addition & 2 deletions src/utils/output_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ void write_to_json(const Solution& sol,
bool report_distances = false);

#ifdef LOG_LS
template <class Route>
void write_LS_logs_to_json(const std::vector<ls::log::Dump<Route>>& dumps);
void write_LS_logs_to_json(const std::vector<ls::log::Dump>& dumps);
#endif
} // namespace vroom::io

Expand Down

0 comments on commit 15d832b

Please sign in to comment.