Files
ortools-clone/docs/cpp/vrp_8cc-example.html
Laurent Perron a2ad6677e1 regenerate doc
2022-01-16 18:02:36 +01:00

109 lines
19 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: vrp.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&amp;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
&#160;<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&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('vrp_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">vrp.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 &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;cstdint&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;int64_t&gt;&gt; distance_matrix{</div><div class="line"> {0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502, 388, 354, 468,</div><div class="line"> 776, 662},</div><div class="line"> {548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674,</div><div class="line"> 1016, 868, 1210},</div><div class="line"> {776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164, 1130,</div><div class="line"> 788, 1552, 754},</div><div class="line"> {696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822,</div><div class="line"> 1164, 560, 1358},</div><div class="line"> {582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708,</div><div class="line"> 1050, 674, 1244},</div><div class="line"> {274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628, 514,</div><div class="line"> 1050, 708},</div><div class="line"> {502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856, 514,</div><div class="line"> 1278, 480},</div><div class="line"> {194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320, 662,</div><div class="line"> 742, 856},</div><div class="line"> {308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810, 696, 662, 320,</div><div class="line"> 1084, 514},</div><div class="line"> {194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 536, 422, 388, 274,</div><div class="line"> 810, 468},</div><div class="line"> {536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878, 764, 730,</div><div class="line"> 388, 1152, 354},</div><div class="line"> {502, 594, 1278, 514, 400, 776, 1004, 468, 810, 536, 878, 0, 114, 308,</div><div class="line"> 650, 274, 844},</div><div class="line"> {388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194, 536,</div><div class="line"> 388, 730},</div><div class="line"> {354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0, 342,</div><div class="line"> 422, 536},</div><div class="line"> {468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536, 342,</div><div class="line"> 0, 764, 194},</div><div class="line"> {776, 868, 1552, 560, 674, 1050, 1278, 742, 1084, 810, 1152, 274, 388,</div><div class="line"> 422, 764, 0, 798},</div><div class="line"> {662, 1210, 754, 1358, 1244, 708, 480, 856, 514, 468, 354, 844, 730, 536,</div><div class="line"> 194, 798, 0},</div><div class="line"> };</div><div class="line"> <span class="keyword">const</span> <span class="keywordtype">int</span> num_vehicles = 4;</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">};</div><div class="line"><span class="comment">// [END data_model]</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> DataModel&amp; data, <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"> <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>) &lt;&lt; <span class="stringliteral">&quot;Objective: &quot;</span> &lt;&lt; solution.ObjectiveValue();</div><div class="line"> int64_t total_distance{0};</div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> vehicle_id = 0; vehicle_id &lt; data.num_vehicles; ++vehicle_id) {</div><div class="line"> int64_t <a name="a3"></a><a class="code" href="pack_8cc.html#a750b5d744c39a06bfb13e6eb010e35d0">index</a> = routing.Start(vehicle_id);</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>) &lt;&lt; <span class="stringliteral">&quot;Route for Vehicle &quot;</span> &lt;&lt; vehicle_id &lt;&lt; <span class="stringliteral">&quot;:&quot;</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 &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"> 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>,</div><div class="line"> int64_t{vehicle_id});</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>) &lt;&lt; route.str() &lt;&lt; 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>) &lt;&lt; <span class="stringliteral">&quot;Distance of the route: &quot;</span> &lt;&lt; <a class="code" href="routing__search_8cc.html#a79b8e036dca6911e3295a47d99f21f43">distance</a> &lt;&lt; <span class="stringliteral">&quot;m&quot;</span>;</div><div class="line"> total_distance += <a class="code" href="routing__search_8cc.html#a79b8e036dca6911e3295a47d99f21f43">distance</a>;</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>) &lt;&lt; <span class="stringliteral">&quot;Total distance of all routes: &quot;</span> &lt;&lt; total_distance &lt;&lt; <span class="stringliteral">&quot;m&quot;</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>) &lt;&lt; <span class="stringliteral">&quot;&quot;</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>) &lt;&lt; <span class="stringliteral">&quot;Advanced usage:&quot;</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>) &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> Vrp() {</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.distance_matrix.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="keywordtype">int</span> transit_callback_index = routing.RegisterTransitCallback(</div><div class="line"> [&amp;data, &amp;manager](int64_t from_index, int64_t to_index) -&gt; 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> data.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(data, manager, routing, *solution);</div><div class="line"> <span class="comment">// [END print_solution]</span></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::Vrp();</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>