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

Remove some template complexity. #1170

Merged
merged 2 commits into from
Oct 16, 2024
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
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
113 changes: 56 additions & 57 deletions src/algorithms/local_search/local_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ void LocalSearch<Route,

#ifdef LOG_LS
if (log_addition_step) {
steps.push_back({utils::now(),
log::EVENT::JOB_ADDITION,
OperatorName::MAX,
utils::SolutionIndicators<Route>(_input, _sol),
std::nullopt});
steps.emplace_back(utils::now(),
log::EVENT::JOB_ADDITION,
OperatorName::MAX,
utils::SolutionIndicators(_input, _sol),
std::nullopt);
}
#endif
}
Expand Down Expand Up @@ -1764,11 +1764,11 @@ void LocalSearch<Route,
#endif

#ifdef LOG_LS
steps.push_back({utils::now(),
log::EVENT::OPERATOR,
best_ops[best_source][best_target]->get_name(),
utils::SolutionIndicators<Route>(_input, _sol),
std::nullopt});
steps.emplace_back(utils::now(),
log::EVENT::OPERATOR,
best_ops[best_source][best_target]->get_name(),
utils::SolutionIndicators(_input, _sol),
std::nullopt);
#endif

#ifndef NDEBUG
Expand Down Expand Up @@ -1924,30 +1924,29 @@ void LocalSearch<Route,
bool try_ls_step = true;

#ifdef LOG_LS
steps.push_back({utils::now(),
log::EVENT::START,
OperatorName::MAX,
_best_sol_indicators,
utils::format_solution(_input, _best_sol)});
steps.emplace_back(utils::now(),
log::EVENT::START,
OperatorName::MAX,
_best_sol_indicators,
utils::format_solution(_input, _best_sol));
#endif

while (try_ls_step) {
// A round of local search.
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;

#ifdef LOG_LS
steps.push_back({utils::now(),
log::EVENT::LOCAL_MINIMA,
OperatorName::MAX,
_best_sol_indicators,
utils::format_solution(_input, _best_sol)});
steps.emplace_back(utils::now(),
log::EVENT::LOCAL_MINIMA,
OperatorName::MAX,
_best_sol_indicators,
utils::format_solution(_input, _best_sol));
#endif
} else {
// No improvement so back to previous best known for further
Expand All @@ -1957,11 +1956,11 @@ void LocalSearch<Route,
_sol_state.setup(_sol);
}
#ifdef LOG_LS
steps.push_back({utils::now(),
log::EVENT::ROLLBACK,
OperatorName::MAX,
_best_sol_indicators,
std::nullopt});
steps.emplace_back(utils::now(),
log::EVENT::ROLLBACK,
OperatorName::MAX,
_best_sol_indicators,
std::nullopt);
#endif

if (_completed_depth.has_value()) {
Expand Down Expand Up @@ -1999,11 +1998,11 @@ void LocalSearch<Route,
}

#ifdef LOG_LS
steps.push_back({utils::now(),
log::EVENT::RUIN,
OperatorName::MAX,
utils::SolutionIndicators<Route>(_input, _sol),
utils::format_solution(_input, _sol)});
steps.emplace_back(utils::now(),
log::EVENT::RUIN,
OperatorName::MAX,
utils::SolutionIndicators(_input, _sol),
utils::format_solution(_input, _sol));
#endif

// Update insertion ranks ranges.
Expand All @@ -2027,11 +2026,11 @@ void LocalSearch<Route,
}

#ifdef LOG_LS
steps.push_back({utils::now(),
log::EVENT::RECREATE,
OperatorName::MAX,
utils::SolutionIndicators<Route>(_input, _sol),
utils::format_solution(_input, _sol)});
steps.emplace_back(utils::now(),
log::EVENT::RECREATE,
OperatorName::MAX,
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() const {
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