Files
ortools-clone/docs/cpp/tsp_8cc-example.html
Mizux Seiha 3b7ffd12b0 Update doc
2020-10-09 12:01:52 +02:00

258 lines
21 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.20"/>
<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>
<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
&#160;<span id="projectnumber">8.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.20 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;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&amp;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&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('tsp_8cc-example.html',''); initResizable(); });
/* @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-2018 Google LLC</span></div>
<div class="line"><span class="comment">// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</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 &quot;AS IS&quot; 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 &lt;cmath&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;vector&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="routing_8h.html">ortools/constraint_solver/routing.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="routing__enums_8pb_8h.html">ortools/constraint_solver/routing_enums.pb.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="routing__index__manager_8h.html">ortools/constraint_solver/routing_index_manager.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="routing__parameters_8h.html">ortools/constraint_solver/routing_parameters.h</a>&quot;</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&lt;std::vector&lt;int&gt;&gt; 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>&amp; it : <span class="keyword">const_cast&lt;</span>std::vector&lt;std::vector&lt;int&gt;<span class="keyword">&gt;</span>&amp;&gt;(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&lt;std::vector&lt;int64&gt;&gt; GenerateManhattanDistanceMatrix(</div>
<div class="line"> <span class="keyword">const</span> std::vector&lt;std::vector&lt;int&gt;&gt;&amp; locations) {</div>
<div class="line"> std::vector&lt;std::vector&lt;int64&gt;&gt; distances = std::vector&lt;std::vector&lt;int64&gt;&gt;(</div>
<div class="line"> locations.size(), std::vector&lt;int64&gt;(locations.size(), <a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a>{0}));</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> fromNode = 0; fromNode &lt; locations.size(); fromNode++) {</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> toNode = 0; toNode &lt; 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"> <a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a>{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&amp; manager,</div>
<div class="line"> <span class="keyword">const</span> RoutingModel&amp; routing, <span class="keyword">const</span> Assignment&amp; solution) {</div>
<div class="line"> LOG(INFO) &lt;&lt; <span class="stringliteral">&quot;Objective: &quot;</span> &lt;&lt; solution.ObjectiveValue();</div>
<div class="line"> <span class="comment">// Inspect solution.</span></div>
<div class="line"> <a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> <a name="a1"></a><a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a> = routing.Start(0);</div>
<div class="line"> LOG(INFO) &lt;&lt; <span class="stringliteral">&quot;Route for Vehicle 0:&quot;</span>;</div>
<div class="line"> <a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> distance{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 &lt;&lt; manager.IndexToNode(<a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>).value() &lt;&lt; <span class="stringliteral">&quot; -&gt; &quot;</span>;</div>
<div class="line"> <a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> 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"> distance += routing.GetArcCostForVehicle(previous_index, <a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>, <a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a>{0});</div>
<div class="line"> }</div>
<div class="line"> LOG(INFO) &lt;&lt; route.str() &lt;&lt; manager.IndexToNode(<a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a>).value();</div>
<div class="line"> LOG(INFO) &lt;&lt; <span class="stringliteral">&quot;Distance of the route: &quot;</span> &lt;&lt; distance &lt;&lt; <span class="stringliteral">&quot;m&quot;</span>;</div>
<div class="line"> LOG(INFO) &lt;&lt; <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line"> LOG(INFO) &lt;&lt; <span class="stringliteral">&quot;Advanced usage:&quot;</span>;</div>
<div class="line"> LOG(INFO) &lt;&lt; <span class="stringliteral">&quot;Problem solved in &quot;</span> &lt;&lt; routing.solver()-&gt;wall_time() &lt;&lt; <span class="stringliteral">&quot;ms&quot;</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"> [&amp;distance_matrix, &amp;manager](<a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> from_index, <a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> to_index) -&gt; <a class="code" href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> {</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).<a name="a2"></a><a class="code" href="demon__profiler_8cc.html#a21edc7ca4cc5802c8779d68556bc09cf">value</a>();</div>
<div class="line"> <span class="keyword">auto</span> to_node = manager.IndexToNode(to_index).<a class="code" href="demon__profiler_8cc.html#a21edc7ca4cc5802c8779d68556bc09cf">value</a>();</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="a3"></a><a class="code" href="namespaceoperations__research.html#ab59d4ce2b572e1ae576d2aa181433187">DefaultRoutingSearchParameters</a>();</div>
<div class="line"> searchParameters.set_first_solution_strategy(</div>
<div class="line"> FirstSolutionStrategy::PATH_CHEAPEST_ARC);</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="a4"></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 -->
<div class="ttc" id="afz_8cc_html_a3c04138a5bfe5d72780bb7e82a18e627"><div class="ttname"><a href="fz_8cc.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a></div><div class="ttdeci">int main(int argc, char **argv)</div><div class="ttdef"><b>Definition:</b> <a href="fz_8cc_source.html#l00163">fz.cc:163</a></div></div>
<div class="ttc" id="arouting_8h_html"><div class="ttname"><a href="routing_8h.html">routing.h</a></div></div>
<div class="ttc" id="ademon__profiler_8cc_html_a21edc7ca4cc5802c8779d68556bc09cf"><div class="ttname"><a href="demon__profiler_8cc.html#a21edc7ca4cc5802c8779d68556bc09cf">value</a></div><div class="ttdeci">int64 value</div><div class="ttdef"><b>Definition:</b> <a href="demon__profiler_8cc_source.html#l00043">demon_profiler.cc:43</a></div></div>
<div class="ttc" id="anamespaceoperations__research_html"><div class="ttname"><a href="namespaceoperations__research.html">operations_research</a></div><div class="ttdoc">The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...</div><div class="ttdef"><b>Definition:</b> <a href="dense__doubly__linked__list_8h_source.html#l00021">dense_doubly_linked_list.h:21</a></div></div>
<div class="ttc" id="aintegral__types_8h_html_a7cde0074dfd288f2d70c0e035dacb28a"><div class="ttname"><a href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a></div><div class="ttdeci">int64_t int64</div><div class="ttdef"><b>Definition:</b> <a href="integral__types_8h_source.html#l00034">integral_types.h:34</a></div></div>
<div class="ttc" id="arouting__enums_8pb_8h_html"><div class="ttname"><a href="routing__enums_8pb_8h.html">routing_enums.pb.h</a></div></div>
<div class="ttc" id="anamespaceoperations__research_html_ab59d4ce2b572e1ae576d2aa181433187"><div class="ttname"><a href="namespaceoperations__research.html#ab59d4ce2b572e1ae576d2aa181433187">operations_research::DefaultRoutingSearchParameters</a></div><div class="ttdeci">RoutingSearchParameters DefaultRoutingSearchParameters()</div><div class="ttdef"><b>Definition:</b> <a href="routing__parameters_8cc_source.html#l00045">routing_parameters.cc:45</a></div></div>
<div class="ttc" id="apack_8cc_html_a750b5d744c39a06bfb13e6eb010e35d0"><div class="ttname"><a href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a></div><div class="ttdeci">int index</div><div class="ttdef"><b>Definition:</b> <a href="pack_8cc_source.html#l00508">pack.cc:508</a></div></div>
<div class="ttc" id="aclassoperations__research_1_1_routing_index_manager_html_aeb44c17e71161ec3a77d4a87e358c84b"><div class="ttname"><a href="classoperations__research_1_1_routing_index_manager.html#aeb44c17e71161ec3a77d4a87e358c84b">operations_research::RoutingIndexManager::NodeIndex</a></div><div class="ttdeci">RoutingNodeIndex NodeIndex</div><div class="ttdef"><b>Definition:</b> <a href="routing__index__manager_8h_source.html#l00050">routing_index_manager.h:50</a></div></div>
<div class="ttc" id="arouting__parameters_8h_html"><div class="ttname"><a href="routing__parameters_8h.html">routing_parameters.h</a></div></div>
<div class="ttc" id="arouting__index__manager_8h_html"><div class="ttname"><a href="routing__index__manager_8h.html">routing_index_manager.h</a></div></div>
<!-- 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.20 </li>
</ul>
</div>
</body>
</html>