Files
ortools-clone/docs/cpp/linear__expr_8h.html
2021-01-26 11:34:47 +01:00

187 lines
14 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.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>OR-Tools: linear_expr.h File Reference</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.2</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<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','.html');
/* @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('linear__expr_8h.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="summary">
<a href="#nested-classes">Classes</a> &#124;
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">linear_expr.h File Reference</div> </div>
</div><!--header-->
<div class="contents">
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>This file allows you to write natural code (like a mathematical equation) to model optimization problems with MPSolver. </p>
<p>It is syntatic sugar on top of the MPSolver API, it provides no additional functionality. Use of these APIs makes it much easier to write code that is both simple and big-O optimal for creating your model, at the cost of some additional constant factor overhead. If model creation is a bottleneck in your problem, consider using the MPSolver API directly instead.</p>
<p>This file contains two classes:</p><ol type="1">
<li>LinearExpr: models offset + sum_{i in S} a_i*x_i for decision var x_i,</li>
<li>LinearRange: models lb &lt;= sum_{i in S} a_i*x_i &lt;= ub, and it provides various operator overloads to build up "LinearExpr"s and then convert them to "LinearRange"s.</li>
</ol>
<p>Recommended use (avoids dangerous code):</p>
<div class="fragment"><div class="line">MPSolver solver = ...;</div>
<div class="line"><span class="keyword">const</span> LinearExpr x = solver.MakeVar(...); * Note: implicit conversion</div>
<div class="line"><span class="keyword">const</span> LinearExpr y = solver.MakeVar(...);</div>
<div class="line"><span class="keyword">const</span> LinearExpr z = solver.MakeVar(...);</div>
<div class="line"><span class="keyword">const</span> LinearExpr e1 = x + y;</div>
<div class="line"><span class="keyword">const</span> LinearExpr e2 = (e1 + 7.0 + z)/3.0;</div>
<div class="line"><span class="keyword">const</span> LinearRange r = e1 &lt;= e2;</div>
<div class="line">solver.MakeRowConstraint(r);</div>
</div><!-- fragment --><p><b>WARNING</b>, AVOID THIS TRAP:</p>
<div class="fragment"><div class="line">MPSolver solver = ...;</div>
<div class="line">MPVariable* x = solver.MakeVar(...);</div>
<div class="line">LinearExpr y = x + 5;</div>
</div><!-- fragment --><p>In evaluating "x+5" above, x is NOT converted to a LinearExpr before the addition, but rather is treated as a pointer, so x+5 gives a new pointer to garbage memory.</p>
<p>For this reason, when using LinearExpr, it is best practice to:</p><ol type="1">
<li>use double literals instead of ints (e.g. "x + 5.0", not "x + 5"),</li>
<li>Immediately convert all MPVariable* to LinearExpr on creation, and only hold references to the "LinearExpr"s.</li>
</ol>
<p>Likewise, the following code is NOT recommended: </p><div class="fragment"><div class="line">MPSolver solver = ...;</div>
<div class="line">MPVariable* x = solver.MakeVar(...);</div>
<div class="line">MPVariable* y = solver.MakeVar(...);</div>
<div class="line">LinearExpr e1 = LinearExpr(x) + y + 5;</div>
</div><!-- fragment --><p>While it is correct, it violates the natural assumption that the + operator is associative. Thus you are setting a trap for future modifications of the code, as any of the following changes would lead to the above failure mode:</p>
<ul>
<li><div class="fragment"><div class="line">LinearExpr e1 = LinearExpr(x) + (y + 5); </div>
</div><!-- fragment --></li>
<li><div class="fragment"><div class="line">LinearExpr e1 = y + 5 + LinearExpr(x); </div>
</div><!-- fragment --> </li>
</ul>
<p class="definition">Definition in file <a class="el" href="linear__expr_8h_source.html">linear_expr.h</a>.</p>
</div>
<p><a href="linear__expr_8h_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="nested-classes"></a>
Classes</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">class &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classoperations__research_1_1_linear_expr.html">LinearExpr</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight"><a class="el" href="classoperations__research_1_1_linear_expr.html" title="LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...">LinearExpr</a> models a quantity that is linear in the decision variables (<a class="el" href="classoperations__research_1_1_m_p_variable.html" title="The class for variables of a Mathematical Programming (MP) model.">MPVariable</a>) of an optimization problem, i.e. <a href="classoperations__research_1_1_linear_expr.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">class &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classoperations__research_1_1_linear_range.html">LinearRange</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">An expression of the form: <a href="classoperations__research_1_1_linear_range.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:namespaceoperations__research"><td class="memItemLeft" align="right" valign="top"> &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html">operations_research</a></td></tr>
<tr class="memdesc:namespaceoperations__research"><td class="mdescLeft">&#160;</td><td class="mdescRight">The vehicle routing library lets one model and solve generic vehicle routing problems ranging from the Traveling Salesman Problem to more complex problems such as the Capacitated Vehicle Routing Problem with Time Windows. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a4d39af6692e71ee2b0191f0a9d46b764"><td class="memItemLeft" align="right" valign="top">std::ostream &amp;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a4d39af6692e71ee2b0191f0a9d46b764">operator&lt;&lt;</a> (std::ostream &amp;stream, const LinearExpr &amp;linear_expr)</td></tr>
<tr class="separator:a4d39af6692e71ee2b0191f0a9d46b764"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a118de93231a6290e4f98ce5d981fd903"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a118de93231a6290e4f98ce5d981fd903">operator+</a> (LinearExpr lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:a118de93231a6290e4f98ce5d981fd903"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a9a57971e3ced4a836ed66de9dc3b657d"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a9a57971e3ced4a836ed66de9dc3b657d">operator-</a> (LinearExpr lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:a9a57971e3ced4a836ed66de9dc3b657d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a66066138340286e4386bbb3de7eafdf4"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a66066138340286e4386bbb3de7eafdf4">operator*</a> (LinearExpr lhs, double rhs)</td></tr>
<tr class="separator:a66066138340286e4386bbb3de7eafdf4"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a81b3f73c470d398ce42791b85964e90f"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a81b3f73c470d398ce42791b85964e90f">operator/</a> (LinearExpr lhs, double rhs)</td></tr>
<tr class="separator:a81b3f73c470d398ce42791b85964e90f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a4ad9d128501e5d521839ad16cdc82d39"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a4ad9d128501e5d521839ad16cdc82d39">operator*</a> (double lhs, LinearExpr rhs)</td></tr>
<tr class="separator:a4ad9d128501e5d521839ad16cdc82d39"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ad0e6185e1b4809a4edd6cc31ac00d7e2"><td class="memItemLeft" align="right" valign="top">LinearRange&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#ad0e6185e1b4809a4edd6cc31ac00d7e2">operator&lt;=</a> (const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:ad0e6185e1b4809a4edd6cc31ac00d7e2"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae161405d349af5d521fa0fd25c3b6f83"><td class="memItemLeft" align="right" valign="top">LinearRange&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#ae161405d349af5d521fa0fd25c3b6f83">operator==</a> (const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:ae161405d349af5d521fa0fd25c3b6f83"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ab7cf6c0298d3fa64034fe8d1eff683f6"><td class="memItemLeft" align="right" valign="top">LinearRange&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#ab7cf6c0298d3fa64034fe8d1eff683f6">operator&gt;=</a> (const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:ab7cf6c0298d3fa64034fe8d1eff683f6"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
</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="navelem"><a class="el" href="dir_a7cc1eeded8f693d0da6c729bc88c45a.html">ortools</a></li><li class="navelem"><a class="el" href="dir_4d3a5a688e4550f3d7725aaa5ab9c27b.html">linear_solver</a></li><li class="navelem"><a class="el" href="linear__expr_8h.html">linear_expr.h</a></li>
<li class="footer">Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
</ul>
</div>
</body>
</html>