update routing samples
This commit is contained in:
@@ -202,18 +202,17 @@ def add_time_window_constraints(routing, manager, data, time_evaluator_index):
|
||||
# [START solution_printer]
|
||||
def print_solution(data, manager, routing, assignment): # pylint:disable=too-many-locals
|
||||
"""Prints assignment on console."""
|
||||
print('Objective: {}'.format(assignment.ObjectiveValue()))
|
||||
print(f'Objective: {assignment.ObjectiveValue()}')
|
||||
|
||||
print('Breaks:')
|
||||
intervals = assignment.IntervalVarContainer()
|
||||
for i in range(intervals.Size()):
|
||||
brk = intervals.Element(i)
|
||||
if brk.PerformedValue() == 1:
|
||||
print('{}: Start({}) Duration({})'.format(brk.Var().Name(),
|
||||
brk.StartValue(),
|
||||
brk.DurationValue()))
|
||||
print(f'{brk.Var().Name()}:'
|
||||
f' Start({brk.StartValue()}) Duration({brk.DurationValue()})')
|
||||
else:
|
||||
print('{}: Unperformed'.format(brk.Var().Name()))
|
||||
print(f'{brk.Var().Name()}: Unperformed')
|
||||
|
||||
total_distance = 0
|
||||
total_load = 0
|
||||
@@ -222,37 +221,40 @@ def print_solution(data, manager, routing, assignment): # pylint:disable=too-ma
|
||||
time_dimension = routing.GetDimensionOrDie('Time')
|
||||
for vehicle_id in range(data['num_vehicles']):
|
||||
index = routing.Start(vehicle_id)
|
||||
plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
|
||||
plan_output = f'Route for vehicle {vehicle_id}:\n'
|
||||
distance = 0
|
||||
while not routing.IsEnd(index):
|
||||
load_var = capacity_dimension.CumulVar(index)
|
||||
time_var = time_dimension.CumulVar(index)
|
||||
slack_var = time_dimension.SlackVar(index)
|
||||
plan_output += ' {0} Load({1}) Time({2},{3}) Slack({4},{5}) ->'.format(
|
||||
manager.IndexToNode(index), assignment.Value(load_var),
|
||||
assignment.Min(time_var), assignment.Max(time_var),
|
||||
assignment.Min(slack_var), assignment.Max(slack_var))
|
||||
node = manager.IndexToNode(index)
|
||||
plan_output += (
|
||||
f' {node}'
|
||||
f' Load({assignment.Value(load_var)})'
|
||||
f' Time({assignment.Min(time_var)}, {assignment.Max(time_var)})'
|
||||
f' Slack({assignment.Min(slack_var)}, {assignment.Max(slack_var)})'
|
||||
' ->')
|
||||
previous_index = index
|
||||
index = assignment.Value(routing.NextVar(index))
|
||||
distance += routing.GetArcCostForVehicle(previous_index, index,
|
||||
vehicle_id)
|
||||
load_var = capacity_dimension.CumulVar(index)
|
||||
time_var = time_dimension.CumulVar(index)
|
||||
plan_output += ' {0} Load({1}) Time({2},{3})\n'.format(
|
||||
manager.IndexToNode(index), assignment.Value(load_var),
|
||||
assignment.Min(time_var), assignment.Max(time_var))
|
||||
plan_output += 'Distance of the route: {0}m\n'.format(distance)
|
||||
plan_output += 'Load of the route: {}\n'.format(
|
||||
assignment.Value(load_var))
|
||||
plan_output += 'Time of the route: {}\n'.format(
|
||||
assignment.Value(time_var))
|
||||
node = manager.IndexToNode(index)
|
||||
plan_output += (
|
||||
f' {node}'
|
||||
f' Load({assignment.Value(load_var)})'
|
||||
f' Time({assignment.Min(time_var)}, {assignment.Max(time_var)})\n')
|
||||
plan_output += f'Distance of the route: {distance}m\n'
|
||||
plan_output += f'Load of the route: {assignment.Value(load_var)}\n'
|
||||
plan_output += f'Time of the route: {assignment.Value(time_var)}\n'
|
||||
print(plan_output)
|
||||
total_distance += distance
|
||||
total_load += assignment.Value(load_var)
|
||||
total_time += assignment.Value(time_var)
|
||||
print('Total Distance of all routes: {0}m'.format(total_distance))
|
||||
print('Total Load of all routes: {}'.format(total_load))
|
||||
print('Total Time of all routes: {0}min'.format(total_time))
|
||||
print(f'Total Distance of all routes: {total_distance}m')
|
||||
print(f'Total Load of all routes: {total_load}')
|
||||
print(f'Total Time of all routes: {total_time}min')
|
||||
# [END solution_printer]
|
||||
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ void SolveJobShopExample() {
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
InitGoogle(argv[0], &argc, &argv, true);
|
||||
absl::SetFlag(&FLAGS_logtostderr, true);
|
||||
absl::SetFlag(&FLAGS_stderrthreshold, 0);
|
||||
operations_research::SolveJobShopExample();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ void SolveNursesExample() {
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
InitGoogle(argv[0], &argc, &argv, true);
|
||||
absl::SetFlag(&FLAGS_logtostderr, true);
|
||||
absl::SetFlag(&FLAGS_stderrthreshold, 0);
|
||||
operations_research::SolveNursesExample();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ void RunConstraintProgrammingExample() {
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
InitGoogle(argv[0], &argc, &argv, true);
|
||||
absl::SetFlag(&FLAGS_logtostderr, true);
|
||||
absl::SetFlag(&FLAGS_stderrthreshold, 0);
|
||||
operations_research::RunConstraintProgrammingExample();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -53,18 +53,18 @@ def main():
|
||||
solver.NewSearch(decision_builder)
|
||||
while solver.NextSolution():
|
||||
count += 1
|
||||
solution = 'Solution {}:\n'.format(count)
|
||||
solution = f'Solution {count}:\n'
|
||||
for var in [x, y, z]:
|
||||
solution += ' {} = {}'.format(var.Name(), var.Value())
|
||||
solution += f' {var.Name()} = {var.Value()}'
|
||||
print(solution)
|
||||
solver.EndSearch()
|
||||
print('Number of solutions found: ', count)
|
||||
print(f'Number of solutions found: {count}')
|
||||
# [END print_solution]
|
||||
|
||||
# [START advanced]
|
||||
print('Advanced usage:')
|
||||
print('Problem solved in ', solver.WallTime(), 'ms')
|
||||
print('Memory usage: ', pywrapcp.Solver.MemoryUsage(), 'bytes')
|
||||
print(f'Problem solved in {solver.WallTime()}ms')
|
||||
print(f'Memory usage: {pywrapcp.Solver.MemoryUsage()}bytes')
|
||||
# [END advanced]
|
||||
|
||||
|
||||
|
||||
@@ -29,13 +29,12 @@ class OneVarLns : public BaseLns {
|
||||
explicit OneVarLns(const std::vector<IntVar*>& vars)
|
||||
: BaseLns(vars), index_(0) {}
|
||||
|
||||
~OneVarLns() override {}
|
||||
~OneVarLns() override = default;
|
||||
|
||||
void InitFragments() override { index_ = 0; }
|
||||
|
||||
bool NextFragment() override {
|
||||
const int size = Size();
|
||||
if (index_ < size) {
|
||||
if (index_ < Size()) {
|
||||
AppendToFragment(index_);
|
||||
++index_;
|
||||
return true;
|
||||
@@ -55,7 +54,7 @@ class MoveOneVar : public IntVarLocalSearchOperator {
|
||||
variable_index_(0),
|
||||
move_up_(false) {}
|
||||
|
||||
~MoveOneVar() override {}
|
||||
~MoveOneVar() override = default;
|
||||
|
||||
protected:
|
||||
// Make a neighbor assigning one variable to its target value.
|
||||
@@ -88,7 +87,7 @@ class SumFilter : public IntVarLocalSearchFilter {
|
||||
explicit SumFilter(const std::vector<IntVar*>& vars)
|
||||
: IntVarLocalSearchFilter(vars), sum_(0) {}
|
||||
|
||||
~SumFilter() override {}
|
||||
~SumFilter() override = default;
|
||||
|
||||
void OnSynchronize(const Assignment* delta) override {
|
||||
sum_ = 0;
|
||||
@@ -200,7 +199,7 @@ void SolveProblem(SolveType solve_type) {
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
InitGoogle(argv[0], &argc, &argv, true);
|
||||
absl::SetFlag(&FLAGS_logtostderr, true);
|
||||
absl::SetFlag(&FLAGS_stderrthreshold, 0);
|
||||
operations_research::SolveProblem(operations_research::LNS);
|
||||
operations_research::SolveProblem(operations_research::LS);
|
||||
operations_research::SolveProblem(operations_research::LS_WITH_FILTER);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// [START import]
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
|
||||
#include "ortools/constraint_solver/routing.h"
|
||||
@@ -73,11 +74,11 @@ void SimpleRoutingProgram() {
|
||||
// Inspect solution.
|
||||
int64_t index = routing.Start(0);
|
||||
LOG(INFO) << "Route for Vehicle 0:";
|
||||
int64_t route_distance{0};
|
||||
int64_t route_distance = 0;
|
||||
std::ostringstream route;
|
||||
while (routing.IsEnd(index) == false) {
|
||||
while (!routing.IsEnd(index)) {
|
||||
route << manager.IndexToNode(index).value() << " -> ";
|
||||
int64_t previous_index = index;
|
||||
const int64_t previous_index = index;
|
||||
index = solution->Value(routing.NextVar(index));
|
||||
route_distance +=
|
||||
routing.GetArcCostForVehicle(previous_index, index, int64_t{0});
|
||||
|
||||
@@ -72,17 +72,17 @@ def main():
|
||||
|
||||
# Print solution on console.
|
||||
# [START print_solution]
|
||||
print('Objective: {}'.format(assignment.ObjectiveValue()))
|
||||
print(f'Objective: {assignment.ObjectiveValue()}')
|
||||
index = routing.Start(0)
|
||||
plan_output = 'Route for vehicle 0:\n'
|
||||
route_distance = 0
|
||||
while not routing.IsEnd(index):
|
||||
plan_output += '{} -> '.format(manager.IndexToNode(index))
|
||||
plan_output += f'{manager.IndexToNode(index)} -> '
|
||||
previous_index = index
|
||||
index = assignment.Value(routing.NextVar(index))
|
||||
route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
|
||||
plan_output += '{}\n'.format(manager.IndexToNode(index))
|
||||
plan_output += 'Distance of the route: {}m\n'.format(route_distance)
|
||||
plan_output += f'{manager.IndexToNode(index)}\n'
|
||||
plan_output += f'Distance of the route: {route_distance}m\n'
|
||||
print(plan_output)
|
||||
# [END print_solution]
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// [START import]
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
@@ -52,12 +53,12 @@ std::vector<std::vector<int64_t>> GenerateManhattanDistanceMatrix(
|
||||
std::vector<std::vector<int64_t>> distances =
|
||||
std::vector<std::vector<int64_t>>(
|
||||
locations.size(), std::vector<int64_t>(locations.size(), int64_t{0}));
|
||||
for (int fromNode = 0; fromNode < locations.size(); fromNode++) {
|
||||
for (int toNode = 0; toNode < locations.size(); toNode++) {
|
||||
if (fromNode != toNode)
|
||||
distances[fromNode][toNode] =
|
||||
int64_t{std::abs(locations[toNode][0] - locations[fromNode][0]) +
|
||||
std::abs(locations[toNode][1] - locations[fromNode][1])};
|
||||
for (int from_node = 0; from_node < locations.size(); from_node++) {
|
||||
for (int to_node = 0; to_node < locations.size(); to_node++) {
|
||||
if (from_node != to_node)
|
||||
distances[from_node][to_node] =
|
||||
int64_t{std::abs(locations[to_node][0] - locations[from_node][0]) +
|
||||
std::abs(locations[to_node][1] - locations[from_node][1])};
|
||||
}
|
||||
}
|
||||
return distances;
|
||||
@@ -77,9 +78,9 @@ void PrintSolution(const RoutingIndexManager& manager,
|
||||
LOG(INFO) << "Route for Vehicle 0:";
|
||||
int64_t distance{0};
|
||||
std::stringstream route;
|
||||
while (routing.IsEnd(index) == false) {
|
||||
while (!routing.IsEnd(index)) {
|
||||
route << manager.IndexToNode(index).value() << " -> ";
|
||||
int64_t previous_index = index;
|
||||
const int64_t previous_index = index;
|
||||
index = solution.Value(routing.NextVar(index));
|
||||
distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0});
|
||||
}
|
||||
@@ -112,8 +113,8 @@ void Tsp() {
|
||||
// [START transit_callback]
|
||||
const auto distance_matrix = GenerateManhattanDistanceMatrix(data.locations);
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&distance_matrix, &manager](int64_t from_index,
|
||||
int64_t to_index) -> int64_t {
|
||||
[&distance_matrix, &manager](const int64_t from_index,
|
||||
const int64_t to_index) -> int64_t {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
|
||||
@@ -78,17 +78,17 @@ def create_distance_callback(data, manager):
|
||||
# [START solution_printer]
|
||||
def print_solution(manager, routing, assignment):
|
||||
"""Prints assignment on console."""
|
||||
print('Objective: {}'.format(assignment.ObjectiveValue()))
|
||||
print(f'Objective: {assignment.ObjectiveValue()}')
|
||||
index = routing.Start(0)
|
||||
plan_output = 'Route for vehicle 0:\n'
|
||||
route_distance = 0
|
||||
while not routing.IsEnd(index):
|
||||
plan_output += ' {} ->'.format(manager.IndexToNode(index))
|
||||
plan_output += f' {manager.IndexToNode(index)} ->'
|
||||
previous_index = index
|
||||
index = assignment.Value(routing.NextVar(index))
|
||||
route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
|
||||
plan_output += ' {}\n'.format(manager.IndexToNode(index))
|
||||
plan_output += 'Distance of the route: {}m\n'.format(route_distance)
|
||||
plan_output += f' {manager.IndexToNode(index)}\n'
|
||||
plan_output += f'Distance of the route: {route_distance}m\n'
|
||||
print(plan_output)
|
||||
# [END solution_printer]
|
||||
|
||||
|
||||
@@ -88,12 +88,12 @@ std::vector<std::vector<int64_t>> ComputeEuclideanDistanceMatrix(
|
||||
std::vector<std::vector<int64_t>> distances =
|
||||
std::vector<std::vector<int64_t>>(
|
||||
locations.size(), std::vector<int64_t>(locations.size(), int64_t{0}));
|
||||
for (int fromNode = 0; fromNode < locations.size(); fromNode++) {
|
||||
for (int toNode = 0; toNode < locations.size(); toNode++) {
|
||||
if (fromNode != toNode)
|
||||
distances[fromNode][toNode] = static_cast<int64_t>(
|
||||
std::hypot((locations[toNode][0] - locations[fromNode][0]),
|
||||
(locations[toNode][1] - locations[fromNode][1])));
|
||||
for (int from_node = 0; from_node < locations.size(); from_node++) {
|
||||
for (int to_node = 0; to_node < locations.size(); to_node++) {
|
||||
if (from_node != to_node)
|
||||
distances[from_node][to_node] = static_cast<int64_t>(
|
||||
std::hypot((locations[to_node][0] - locations[from_node][0]),
|
||||
(locations[to_node][1] - locations[from_node][1])));
|
||||
}
|
||||
}
|
||||
return distances;
|
||||
@@ -113,9 +113,9 @@ void PrintSolution(const RoutingIndexManager& manager,
|
||||
LOG(INFO) << "Route:";
|
||||
int64_t distance{0};
|
||||
std::stringstream route;
|
||||
while (routing.IsEnd(index) == false) {
|
||||
while (!routing.IsEnd(index)) {
|
||||
route << manager.IndexToNode(index).value() << " -> ";
|
||||
int64_t previous_index = index;
|
||||
const int64_t previous_index = index;
|
||||
index = solution.Value(routing.NextVar(index));
|
||||
distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0});
|
||||
}
|
||||
@@ -147,8 +147,8 @@ void Tsp() {
|
||||
// [START transit_callback]
|
||||
const auto distance_matrix = ComputeEuclideanDistanceMatrix(data.locations);
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&distance_matrix, &manager](int64_t from_index,
|
||||
int64_t to_index) -> int64_t {
|
||||
[&distance_matrix, &manager](const int64_t from_index,
|
||||
const int64_t to_index) -> int64_t {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
|
||||
@@ -103,18 +103,18 @@ def compute_euclidean_distance_matrix(locations):
|
||||
# [START solution_printer]
|
||||
def print_solution(manager, routing, solution):
|
||||
"""Prints solution on console."""
|
||||
print('Objective: {}'.format(solution.ObjectiveValue()))
|
||||
print(f'Objective: {solution.ObjectiveValue()}')
|
||||
index = routing.Start(0)
|
||||
plan_output = 'Route:\n'
|
||||
route_distance = 0
|
||||
while not routing.IsEnd(index):
|
||||
plan_output += ' {} ->'.format(manager.IndexToNode(index))
|
||||
plan_output += f' {manager.IndexToNode(index)} ->'
|
||||
previous_index = index
|
||||
index = solution.Value(routing.NextVar(index))
|
||||
route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
|
||||
plan_output += ' {}\n'.format(manager.IndexToNode(index))
|
||||
plan_output += f' {manager.IndexToNode(index)}\n'
|
||||
print(plan_output)
|
||||
plan_output += 'Objective: {}m\n'.format(route_distance)
|
||||
plan_output += f'Objective: {route_distance}m\n'
|
||||
# [END solution_printer]
|
||||
|
||||
|
||||
|
||||
@@ -60,9 +60,9 @@ void PrintSolution(const RoutingIndexManager& manager,
|
||||
LOG(INFO) << "Route:";
|
||||
int64_t distance{0};
|
||||
std::stringstream route;
|
||||
while (routing.IsEnd(index) == false) {
|
||||
while (!routing.IsEnd(index)) {
|
||||
route << manager.IndexToNode(index).value() << " -> ";
|
||||
int64_t previous_index = index;
|
||||
const int64_t previous_index = index;
|
||||
index = solution.Value(routing.NextVar(index));
|
||||
distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0});
|
||||
}
|
||||
@@ -93,7 +93,8 @@ void Tsp() {
|
||||
|
||||
// [START transit_callback]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64_t from_index, int64_t to_index) -> int64_t {
|
||||
[&data, &manager](const int64_t from_index,
|
||||
const int64_t to_index) -> int64_t {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
|
||||
@@ -49,18 +49,18 @@ def create_data_model():
|
||||
# [START solution_printer]
|
||||
def print_solution(manager, routing, solution):
|
||||
"""Prints solution on console."""
|
||||
print('Objective: {} miles'.format(solution.ObjectiveValue()))
|
||||
print(f'Objective: {solution.ObjectiveValue()} miles')
|
||||
index = routing.Start(0)
|
||||
plan_output = 'Route for vehicle 0:\n'
|
||||
route_distance = 0
|
||||
while not routing.IsEnd(index):
|
||||
plan_output += ' {} ->'.format(manager.IndexToNode(index))
|
||||
plan_output += f' {manager.IndexToNode(index)} ->'
|
||||
previous_index = index
|
||||
index = solution.Value(routing.NextVar(index))
|
||||
route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
|
||||
plan_output += ' {}\n'.format(manager.IndexToNode(index))
|
||||
plan_output += f' {manager.IndexToNode(index)}\n'
|
||||
print(plan_output)
|
||||
plan_output += 'Route distance: {}miles\n'.format(route_distance)
|
||||
plan_output += f'Route distance: {route_distance}miles\n'
|
||||
# [END solution_printer]
|
||||
|
||||
|
||||
|
||||
@@ -92,7 +92,8 @@ void Tsp() {
|
||||
// Define cost of each arc.
|
||||
// [START arc_cost]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64_t from_index, int64_t to_index) -> int64_t {
|
||||
[&data, &manager](const int64_t from_index,
|
||||
const int64_t to_index) -> int64_t {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
|
||||
@@ -80,9 +80,9 @@ void PrintSolution(const RoutingIndexManager& manager,
|
||||
LOG(INFO) << "Route for Vehicle 0:";
|
||||
int64_t distance{0};
|
||||
std::stringstream route;
|
||||
while (routing.IsEnd(index) == false) {
|
||||
while (!routing.IsEnd(index)) {
|
||||
route << manager.IndexToNode(index).value() << " -> ";
|
||||
int64_t previous_index = index;
|
||||
const int64_t previous_index = index;
|
||||
index = solution.Value(routing.NextVar(index));
|
||||
distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0});
|
||||
}
|
||||
@@ -114,7 +114,8 @@ void Tsp() {
|
||||
// Create and register a transit callback.
|
||||
// [START transit_callback]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64_t from_index, int64_t to_index) -> int64_t {
|
||||
[&data, &manager](const int64_t from_index,
|
||||
const int64_t to_index) -> int64_t {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
|
||||
@@ -104,17 +104,17 @@ def create_data_model():
|
||||
# [START solution_printer]
|
||||
def print_solution(manager, routing, solution):
|
||||
"""Prints solution on console."""
|
||||
print('Objective: {}'.format(solution.ObjectiveValue()))
|
||||
print(f'Objective: {solution.ObjectiveValue()}')
|
||||
index = routing.Start(0)
|
||||
plan_output = 'Route for vehicle 0:\n'
|
||||
route_distance = 0
|
||||
while not routing.IsEnd(index):
|
||||
plan_output += ' {} ->'.format(manager.IndexToNode(index))
|
||||
plan_output += f' {manager.IndexToNode(index)} ->'
|
||||
previous_index = index
|
||||
index = solution.Value(routing.NextVar(index))
|
||||
route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
|
||||
plan_output += ' {}\n'.format(manager.IndexToNode(index))
|
||||
plan_output += 'Distance of the route: {}m\n'.format(route_distance)
|
||||
plan_output += f' {manager.IndexToNode(index)}\n'
|
||||
plan_output += f'Distance of the route: {route_distance}m\n'
|
||||
print(plan_output)
|
||||
# [END solution_printer]
|
||||
|
||||
|
||||
@@ -82,9 +82,9 @@ void PrintSolution(const DataModel& data, const RoutingIndexManager& manager,
|
||||
LOG(INFO) << "Route for Vehicle " << vehicle_id << ":";
|
||||
int64_t distance{0};
|
||||
std::stringstream route;
|
||||
while (routing.IsEnd(index) == false) {
|
||||
while (!routing.IsEnd(index)) {
|
||||
route << manager.IndexToNode(index).value() << " -> ";
|
||||
int64_t previous_index = index;
|
||||
const int64_t previous_index = index;
|
||||
index = solution.Value(routing.NextVar(index));
|
||||
distance += routing.GetArcCostForVehicle(previous_index, index,
|
||||
int64_t{vehicle_id});
|
||||
@@ -120,7 +120,8 @@ void Vrp() {
|
||||
// Create and register a transit callback.
|
||||
// [START transit_callback]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64_t from_index, int64_t to_index) -> int64_t {
|
||||
[&data, &manager](const int64_t from_index,
|
||||
const int64_t to_index) -> int64_t {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
auto from_node = manager.IndexToNode(from_index).value();
|
||||
auto to_node = manager.IndexToNode(to_index).value();
|
||||
|
||||
@@ -116,19 +116,19 @@ def print_solution(data, manager, routing, solution):
|
||||
total_distance = 0
|
||||
for vehicle_id in range(data['num_vehicles']):
|
||||
index = routing.Start(vehicle_id)
|
||||
plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
|
||||
plan_output = f'Route for vehicle {vehicle_id}:\n'
|
||||
route_distance = 0
|
||||
while not routing.IsEnd(index):
|
||||
plan_output += ' {} ->'.format(manager.IndexToNode(index))
|
||||
plan_output += f' {manager.IndexToNode(index)} ->'
|
||||
previous_index = index
|
||||
index = solution.Value(routing.NextVar(index))
|
||||
route_distance += routing.GetArcCostForVehicle(
|
||||
previous_index, index, vehicle_id)
|
||||
plan_output += ' {}\n'.format(manager.IndexToNode(index))
|
||||
plan_output += 'Distance of the route: {}m\n'.format(route_distance)
|
||||
plan_output += f' {manager.IndexToNode(index)}\n'
|
||||
plan_output += f'Distance of the route: {route_distance}m\n'
|
||||
print(plan_output)
|
||||
total_distance += route_distance
|
||||
print('Total Distance of all routes: {}m'.format(total_distance))
|
||||
print(f'Total Distance of all routes: {total_distance}m')
|
||||
|
||||
# [END solution_printer]
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ void PrintSolution(const RoutingIndexManager& manager,
|
||||
LOG(INFO) << "Route for Vehicle " << vehicle_id << ":";
|
||||
int64_t index = routing.Start(vehicle_id);
|
||||
std::stringstream route;
|
||||
while (routing.IsEnd(index) == false) {
|
||||
while (!routing.IsEnd(index)) {
|
||||
const IntVar* time_var = time_dimension.CumulVar(index);
|
||||
route << manager.IndexToNode(index).value() << " Time("
|
||||
<< solution.Value(time_var) << ") -> ";
|
||||
@@ -128,7 +128,8 @@ void VrpBreaks() {
|
||||
// Create and register a transit callback.
|
||||
// [START transit_callback]
|
||||
const int transit_callback_index = routing.RegisterTransitCallback(
|
||||
[&data, &manager](int64_t from_index, int64_t to_index) -> int64_t {
|
||||
[&data, &manager](const int64_t from_index,
|
||||
const int64_t to_index) -> int64_t {
|
||||
// Convert from routing variable Index to distance matrix NodeIndex.
|
||||
int from_node = manager.IndexToNode(from_index).value();
|
||||
int to_node = manager.IndexToNode(to_index).value();
|
||||
|
||||
Reference in New Issue
Block a user