Files
ortools-clone/ortools/constraint_solver/samples/simple_routing_program.cc

98 lines
3.2 KiB
C++
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
2018-12-27 14:18:19 +01:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START program]
// [START import]
2019-01-11 16:49:07 +01:00
#include <cmath>
2021-03-01 11:45:21 +01:00
#include <cstdint>
2023-04-03 18:23:32 +02:00
#include <cstdlib>
2022-07-22 14:35:40 +02:00
#include <sstream>
2018-12-27 14:18:19 +01:00
#include "ortools/constraint_solver/routing.h"
2019-01-10 10:43:43 +01:00
#include "ortools/constraint_solver/routing_enums.pb.h"
2018-12-27 14:18:19 +01:00
#include "ortools/constraint_solver/routing_index_manager.h"
#include "ortools/constraint_solver/routing_parameters.h"
// [END import]
namespace operations_research {
2019-01-10 10:43:43 +01:00
void SimpleRoutingProgram() {
// Instantiate the data problem.
// [START data]
int num_location = 5;
int num_vehicles = 1;
RoutingIndexManager::NodeIndex depot{0};
// [END data]
2018-12-27 14:18:19 +01:00
2019-01-10 10:43:43 +01:00
// Create Routing Index Manager
// [START index_manager]
RoutingIndexManager manager(num_location, num_vehicles, depot);
// [END index_manager]
2018-12-27 14:18:19 +01:00
2019-01-10 10:43:43 +01:00
// Create Routing Model.
// [START routing_model]
RoutingModel routing(manager);
// [END routing_model]
2018-12-27 14:18:19 +01:00
2019-01-10 10:43:43 +01:00
// Define cost of each arc.
// [START arc_cost]
2019-01-11 16:49:07 +01:00
int distance_call_index = routing.RegisterTransitCallback(
2021-04-01 12:13:35 +02:00
[&manager](int64_t from_index, int64_t to_index) -> int64_t {
2019-01-11 16:49:07 +01:00
// Convert from routing variable Index to user NodeIndex.
auto from_node = manager.IndexToNode(from_index).value();
auto to_node = manager.IndexToNode(to_index).value();
return std::abs(to_node - from_node);
});
routing.SetArcCostEvaluatorOfAllVehicles(distance_call_index);
2019-01-10 10:43:43 +01:00
// [END arc_cost]
2018-12-27 14:18:19 +01:00
2019-01-10 10:43:43 +01:00
// Setting first solution heuristic.
// [START parameters]
RoutingSearchParameters searchParameters = DefaultRoutingSearchParameters();
searchParameters.set_first_solution_strategy(
FirstSolutionStrategy::PATH_CHEAPEST_ARC);
// [END parameters]
2018-12-27 14:18:19 +01:00
2019-01-10 10:43:43 +01:00
// Solve the problem.
// [START solve]
const Assignment* solution = routing.SolveWithParameters(searchParameters);
// [END solve]
2018-12-27 14:18:19 +01:00
2019-01-10 10:43:43 +01:00
// Print solution on console.
// [START print_solution]
LOG(INFO) << "Objective: " << solution->ObjectiveValue();
// Inspect solution.
2021-03-01 11:45:21 +01:00
int64_t index = routing.Start(0);
2019-01-10 10:43:43 +01:00
LOG(INFO) << "Route for Vehicle 0:";
2023-04-03 18:23:32 +02:00
int64_t route_distance = 0;
2019-01-10 10:43:43 +01:00
std::ostringstream route;
2023-04-03 18:23:32 +02:00
while (!routing.IsEnd(index)) {
2019-01-10 10:43:43 +01:00
route << manager.IndexToNode(index).value() << " -> ";
2023-04-03 18:23:32 +02:00
const int64_t previous_index = index;
2019-01-10 10:43:43 +01:00
index = solution->Value(routing.NextVar(index));
route_distance +=
2021-03-01 11:45:21 +01:00
routing.GetArcCostForVehicle(previous_index, index, int64_t{0});
2018-12-27 14:18:19 +01:00
}
2019-01-10 10:43:43 +01:00
LOG(INFO) << route.str() << manager.IndexToNode(index).value();
LOG(INFO) << "Distance of the route: " << route_distance << "m";
// [END print_solution]
}
2018-12-27 14:18:19 +01:00
} // namespace operations_research
2022-10-24 11:56:09 +02:00
int main(int /*argc*/, char* /*argv*/[]) {
2018-12-27 14:18:19 +01:00
operations_research::SimpleRoutingProgram();
return EXIT_SUCCESS;
}
// [END program]