109 lines
18 KiB
HTML
109 lines
18 KiB
HTML
<!-- HTML header for doxygen 1.8.18-->
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.8.15"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>OR-Tools: tsp.cc</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="jquery.js"></script>
|
|
<script type="text/javascript" src="dynsections.js"></script>
|
|
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="resize.js"></script>
|
|
<script type="text/javascript" src="navtreedata.js"></script>
|
|
<script type="text/javascript" src="navtree.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(document).ready(initResizable);
|
|
/* @license-end */</script>
|
|
<link href="search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="search/searchdata.js"></script>
|
|
<script type="text/javascript" src="search/search.js"></script>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
|
<link href="styleSheet.tmp.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectlogo"><img alt="Logo" src="orLogo.png"/></td>
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">OR-Tools
|
|
 <span id="projectnumber">9.2</span>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.8.15 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
var searchBox = new SearchBox("searchBox", "search",false,'Search');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="menudata.js"></script>
|
|
<script type="text/javascript" src="menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(function() {
|
|
initMenu('',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(document).ready(function(){initNavTree('tsp_8cc-example.html','');});
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">tsp.cc</div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="fragment"><div class="line"><span class="comment">// Copyright 2010-2021 Google LLC</span></div><div class="line"><span class="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span></div><div class="line"><span class="comment">// you may not use this file except in compliance with the License.</span></div><div class="line"><span class="comment">// You may obtain a copy of the License at</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// http://www.apache.org/licenses/LICENSE-2.0</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Unless required by applicable law or agreed to in writing, software</span></div><div class="line"><span class="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span></div><div class="line"><span class="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div><div class="line"><span class="comment">// See the License for the specific language governing permissions and</span></div><div class="line"><span class="comment">// limitations under the License.</span></div><div class="line"></div><div class="line"><span class="comment">// [START program]</span></div><div class="line"><span class="comment">// [START import]</span></div><div class="line"><span class="preprocessor">#include <cmath></span></div><div class="line"><span class="preprocessor">#include <cstdint></span></div><div class="line"><span class="preprocessor">#include <vector></span></div><div class="line"></div><div class="line"><span class="preprocessor">#include "<a class="code" href="routing_8h.html">ortools/constraint_solver/routing.h</a>"</span></div><div class="line"><span class="preprocessor">#include "<a class="code" href="routing__enums_8pb_8h.html">ortools/constraint_solver/routing_enums.pb.h</a>"</span></div><div class="line"><span class="preprocessor">#include "<a class="code" href="routing__index__manager_8h.html">ortools/constraint_solver/routing_index_manager.h</a>"</span></div><div class="line"><span class="preprocessor">#include "<a class="code" href="routing__parameters_8h.html">ortools/constraint_solver/routing_parameters.h</a>"</span></div><div class="line"><span class="comment">// [END import]</span></div><div class="line"></div><div class="line"><span class="keyword">namespace </span><a class="code" href="namespaceoperations__research.html">operations_research</a> {</div><div class="line"><span class="comment">// [START data_model]</span></div><div class="line"><span class="keyword">struct </span>DataModel {</div><div class="line"> <span class="keyword">const</span> std::vector<std::vector<int>> locations{</div><div class="line"> {4, 4}, {2, 0}, {8, 0}, {0, 1}, {1, 1}, {5, 2}, {7, 2}, {3, 3}, {6, 3},</div><div class="line"> {5, 5}, {8, 5}, {1, 6}, {2, 6}, {3, 7}, {6, 7}, {0, 8}, {7, 8},</div><div class="line"> };</div><div class="line"> <span class="keyword">const</span> <span class="keywordtype">int</span> num_vehicles = 1;</div><div class="line"> <span class="keyword">const</span> <a name="a0"></a><a class="code" href="classoperations__research_1_1_routing_index_manager.html#aeb44c17e71161ec3a77d4a87e358c84b">RoutingIndexManager::NodeIndex</a> depot{0};</div><div class="line"> DataModel() {</div><div class="line"> <span class="comment">// Convert locations in meters using a city block dimension of 114m x 80m.</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">auto</span>& it : <span class="keyword">const_cast<</span>std::vector<std::vector<int><span class="keyword">></span>&>(locations)) {</div><div class="line"> it[0] *= 114;</div><div class="line"> it[1] *= 80;</div><div class="line"> }</div><div class="line"> }</div><div class="line">};</div><div class="line"><span class="comment">// [END data_model]</span></div><div class="line"></div><div class="line"><span class="comment">// [START manhattan_distance_matrix]</span></div><div class="line"><span class="comment"></span>std::vector<std::vector<int64_t>> GenerateManhattanDistanceMatrix(</div><div class="line"> <span class="keyword">const</span> std::vector<std::vector<int>>& locations) {</div><div class="line"> std::vector<std::vector<int64_t>> distances =</div><div class="line"> std::vector<std::vector<int64_t>>(</div><div class="line"> locations.size(), std::vector<int64_t>(locations.size(), int64_t{0}));</div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> fromNode = 0; fromNode < locations.size(); fromNode++) {</div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> toNode = 0; toNode < locations.size(); toNode++) {</div><div class="line"> <span class="keywordflow">if</span> (fromNode != toNode)</div><div class="line"> distances[fromNode][toNode] =</div><div class="line"> int64_t{std::abs(locations[toNode][0] - locations[fromNode][0]) +</div><div class="line"> std::abs(locations[toNode][1] - locations[fromNode][1])};</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keywordflow">return</span> distances;</div><div class="line">}</div><div class="line"><span class="comment">// [END manhattan_distance_matrix]</span></div><div class="line"></div><div class="line"><span class="comment">// [START solution_printer]</span></div><div class="line"><span class="comment"></span><span class="keywordtype">void</span> PrintSolution(<span class="keyword">const</span> RoutingIndexManager& manager,</div><div class="line"> <span class="keyword">const</span> RoutingModel& routing, <span class="keyword">const</span> Assignment& solution) {</div><div class="line"> <a name="a1"></a><a class="code" href="base_2logging_8h.html#accad43a85d781d53381cd53a9894b6ae">LOG</a>(<a name="a2"></a><a class="code" href="log__severity_8h.html#ab4a2cbab234914b320b7fae11b6e8cb9">INFO</a>) << <span class="stringliteral">"Objective: "</span> << solution.ObjectiveValue();</div><div class="line"> <span class="comment">// Inspect solution.</span></div><div class="line"> int64_t <a name="a3"></a><a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a> = routing.Start(0);</div><div class="line"> <a class="code" href="base_2logging_8h.html#accad43a85d781d53381cd53a9894b6ae">LOG</a>(<a class="code" href="log__severity_8h.html#ab4a2cbab234914b320b7fae11b6e8cb9">INFO</a>) << <span class="stringliteral">"Route for Vehicle 0:"</span>;</div><div class="line"> int64_t <a name="a4"></a><a class="code" href="routing__search_8cc.html#a79b8e036dca6911e3295a47d99f21f43">distance</a>{0};</div><div class="line"> std::stringstream route;</div><div class="line"> <span class="keywordflow">while</span> (routing.IsEnd(<a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>) == <span class="keyword">false</span>) {</div><div class="line"> route << manager.IndexToNode(<a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>).value() << <span class="stringliteral">" -> "</span>;</div><div class="line"> int64_t previous_index = <a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>;</div><div class="line"> <a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a> = solution.Value(routing.NextVar(<a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>));</div><div class="line"> <a class="code" href="routing__search_8cc.html#a79b8e036dca6911e3295a47d99f21f43">distance</a> += routing.GetArcCostForVehicle(previous_index, <a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>, int64_t{0});</div><div class="line"> }</div><div class="line"> <a class="code" href="base_2logging_8h.html#accad43a85d781d53381cd53a9894b6ae">LOG</a>(<a class="code" href="log__severity_8h.html#ab4a2cbab234914b320b7fae11b6e8cb9">INFO</a>) << route.str() << manager.IndexToNode(<a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>).value();</div><div class="line"> <a class="code" href="base_2logging_8h.html#accad43a85d781d53381cd53a9894b6ae">LOG</a>(<a class="code" href="log__severity_8h.html#ab4a2cbab234914b320b7fae11b6e8cb9">INFO</a>) << <span class="stringliteral">"Distance of the route: "</span> << <a class="code" href="routing__search_8cc.html#a79b8e036dca6911e3295a47d99f21f43">distance</a> << <span class="stringliteral">"m"</span>;</div><div class="line"> <a class="code" href="base_2logging_8h.html#accad43a85d781d53381cd53a9894b6ae">LOG</a>(<a class="code" href="log__severity_8h.html#ab4a2cbab234914b320b7fae11b6e8cb9">INFO</a>) << <span class="stringliteral">""</span>;</div><div class="line"> <a class="code" href="base_2logging_8h.html#accad43a85d781d53381cd53a9894b6ae">LOG</a>(<a class="code" href="log__severity_8h.html#ab4a2cbab234914b320b7fae11b6e8cb9">INFO</a>) << <span class="stringliteral">"Advanced usage:"</span>;</div><div class="line"> <a class="code" href="base_2logging_8h.html#accad43a85d781d53381cd53a9894b6ae">LOG</a>(<a class="code" href="log__severity_8h.html#ab4a2cbab234914b320b7fae11b6e8cb9">INFO</a>) << <span class="stringliteral">"Problem solved in "</span> << routing.solver()->wall_time() << <span class="stringliteral">"ms"</span>;</div><div class="line">}</div><div class="line"><span class="comment">// [END solution_printer]</span></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> Tsp() {</div><div class="line"> <span class="comment">// Instantiate the data problem.</span></div><div class="line"> <span class="comment">// [START data]</span></div><div class="line"> DataModel data;</div><div class="line"> <span class="comment">// [END data]</span></div><div class="line"></div><div class="line"> <span class="comment">// Create Routing Index Manager</span></div><div class="line"> <span class="comment">// [START index_manager]</span></div><div class="line"> RoutingIndexManager manager(data.locations.size(), data.num_vehicles,</div><div class="line"> data.depot);</div><div class="line"> <span class="comment">// [END index_manager]</span></div><div class="line"></div><div class="line"> <span class="comment">// Create Routing Model.</span></div><div class="line"> <span class="comment">// [START routing_model]</span></div><div class="line"> RoutingModel routing(manager);</div><div class="line"> <span class="comment">// [END routing_model]</span></div><div class="line"></div><div class="line"> <span class="comment">// Create and register a transit callback.</span></div><div class="line"> <span class="comment">// [START transit_callback]</span></div><div class="line"> <span class="keyword">const</span> <span class="keyword">auto</span> distance_matrix = GenerateManhattanDistanceMatrix(data.locations);</div><div class="line"> <span class="keyword">const</span> <span class="keywordtype">int</span> transit_callback_index = routing.RegisterTransitCallback(</div><div class="line"> [&distance_matrix, &manager](int64_t from_index,</div><div class="line"> int64_t to_index) -> int64_t {</div><div class="line"> <span class="comment">// Convert from routing variable Index to distance matrix NodeIndex.</span></div><div class="line"> <span class="keyword">auto</span> from_node = manager.IndexToNode(from_index).value();</div><div class="line"> <span class="keyword">auto</span> to_node = manager.IndexToNode(to_index).value();</div><div class="line"> <span class="keywordflow">return</span> distance_matrix[from_node][to_node];</div><div class="line"> });</div><div class="line"> <span class="comment">// [END transit_callback]</span></div><div class="line"></div><div class="line"> <span class="comment">// Define cost of each arc.</span></div><div class="line"> <span class="comment">// [START arc_cost]</span></div><div class="line"> routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);</div><div class="line"> <span class="comment">// [END arc_cost]</span></div><div class="line"></div><div class="line"> <span class="comment">// Setting first solution heuristic.</span></div><div class="line"> <span class="comment">// [START parameters]</span></div><div class="line"> RoutingSearchParameters searchParameters = <a name="a5"></a><a class="code" href="namespaceoperations__research.html#ab59d4ce2b572e1ae576d2aa181433187">DefaultRoutingSearchParameters</a>();</div><div class="line"> searchParameters.set_first_solution_strategy(</div><div class="line"> <a name="a6"></a><a class="code" href="classoperations__research_1_1_first_solution_strategy.html#af9514bec629ba99306ed93ab08252708">FirstSolutionStrategy::PATH_CHEAPEST_ARC</a>);</div><div class="line"> <span class="comment">// [END parameters]</span></div><div class="line"></div><div class="line"> <span class="comment">// Solve the problem.</span></div><div class="line"> <span class="comment">// [START solve]</span></div><div class="line"> <span class="keyword">const</span> Assignment* solution = routing.SolveWithParameters(searchParameters);</div><div class="line"> <span class="comment">// [END solve]</span></div><div class="line"></div><div class="line"> <span class="comment">// Print solution on console.</span></div><div class="line"> <span class="comment">// [START print_solution]</span></div><div class="line"> PrintSolution(manager, routing, *solution);</div><div class="line"> <span class="comment">// [END print_solution]</span></div><div class="line">}</div><div class="line"></div><div class="line">} <span class="comment">// namespace operations_research</span></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> <a name="a7"></a><a class="code" href="fz_8cc.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv) {</div><div class="line"> operations_research::Tsp();</div><div class="line"> <span class="keywordflow">return</span> EXIT_SUCCESS;</div><div class="line">}</div><div class="line"><span class="comment">// [END program]</span></div></div><!-- fragment --> </div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<!-- HTML footer for doxygen 1.8.18-->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="footer">Generated by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.15 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|