Files
ortools-clone/docs/cpp/linear__expr_8h.html
Laurent Perron 37c1af7690 add more C++ doc
2019-06-13 15:36:14 +02:00

158 lines
14 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>OR-Tools</title>
<meta http-equiv="Content-Type" content="text/html;"/>
<meta charset="utf-8"/>
<!--<link rel='stylesheet' type='text/css' href="https://fonts.googleapis.com/css?family=Ubuntu:400,700,400italic"/>-->
<link rel="stylesheet" type="text/css" href="doxygen.css" title="default" media="screen,print" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
</head>
<body>
<div id="banner-container">
<div id="banner">
<span id="sfml">OR-Tools 7.1@</span>
</div>
</div>
<div id="content">
<!-- Generated by Doxygen 1.8.15 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div id="navrow2" class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File&#160;List</span></a></li>
<li><a href="globals.html"><span>File&#160;Members</span></a></li>
</ul>
</div>
<div id="nav-path" class="navpath">
<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> </ul>
</div>
</div><!-- top -->
<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> &#124;
<a href="#var-members">Variables</a> </div>
<div class="headertitle">
<div class="title">linear_expr.h File Reference</div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><code>#include &quot;absl/container/flat_hash_map.h&quot;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for linear_expr.h:</div>
<div class="dyncontent">
<div class="center"><img src="linear__expr_8h__incl.png" border="0" usemap="#linear__expr_8h" alt=""/></div>
<map name="linear__expr_8h" id="linear__expr_8h">
<area shape="rect" title=" " alt="" coords="19,5,117,31"/>
<area shape="rect" title=" " alt="" coords="5,79,131,117"/>
</map>
</div>
</div><div class="textblock"><div class="dynheader">
This graph shows which files directly or indirectly include this file:</div>
<div class="dyncontent">
<div class="center"><img src="linear__expr_8h__dep__incl.png" border="0" usemap="#linear__expr_8hdep" alt=""/></div>
<map name="linear__expr_8hdep" id="linear__expr_8hdep">
<area shape="rect" title=" " alt="" coords="41,5,139,31"/>
<area shape="rect" href="linear__solver_8h.html" title=" " alt="" coords="36,79,143,104"/>
<area shape="rect" href="christofides_8h.html" title=" " alt="" coords="40,152,140,177"/>
<area shape="rect" href="one__tree__lower__bound_8h.html" title=" " alt="" coords="5,225,174,251"/>
</map>
</div>
</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_1LinearExpr.html">operations_research::LinearExpr</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight"><a class="el" href="classoperations__research_1_1LinearExpr.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 (MPVariable) of an optimization problem, i.e. <a href="classoperations__research_1_1LinearExpr.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_1LinearRange.html">operations_research::LinearRange</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">An expression of the form: lower_bound &lt;= sum_{i in S} a_i*x_i &lt;= upper_bound. <a href="classoperations__research_1_1LinearRange.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">Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. <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:a97f9b83239285f5fdfcac1b8e8b4f162"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a97f9b83239285f5fdfcac1b8e8b4f162">operations_research::operator+</a> (LinearExpr lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="memdesc:a97f9b83239285f5fdfcac1b8e8b4f162"><td class="mdescLeft">&#160;</td><td class="mdescRight">NOTE(user): in the ops below, the non-"const LinearExpr&amp;" are intentional. <a href="namespaceoperations__research.html#a97f9b83239285f5fdfcac1b8e8b4f162">More...</a><br /></td></tr>
<tr class="separator:a97f9b83239285f5fdfcac1b8e8b4f162"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a515cdaf4f9c4000bb3482a0c450e23c3"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a515cdaf4f9c4000bb3482a0c450e23c3">operations_research::operator-</a> (LinearExpr lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:a515cdaf4f9c4000bb3482a0c450e23c3"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a741104fe08089fe3520676487f7a685d"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a741104fe08089fe3520676487f7a685d">operations_research::operator *</a> (LinearExpr lhs, double rhs)</td></tr>
<tr class="separator:a741104fe08089fe3520676487f7a685d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:abebdd7f40e90df8dc7d557b6e26da942"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#abebdd7f40e90df8dc7d557b6e26da942">operations_research::operator/</a> (LinearExpr lhs, double rhs)</td></tr>
<tr class="separator:abebdd7f40e90df8dc7d557b6e26da942"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a99590470c6ad2d59331b6fcc56609877"><td class="memItemLeft" align="right" valign="top">LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a99590470c6ad2d59331b6fcc56609877">operations_research::operator *</a> (double lhs, LinearExpr rhs)</td></tr>
<tr class="separator:a99590470c6ad2d59331b6fcc56609877"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a6d1fa20f9c9faf7027c0b16f97139e80"><td class="memItemLeft" align="right" valign="top">LinearRange&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a6d1fa20f9c9faf7027c0b16f97139e80">operations_research::operator&lt;=</a> (const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:a6d1fa20f9c9faf7027c0b16f97139e80"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a08146f196bd9c3f492ee108732449ced"><td class="memItemLeft" align="right" valign="top">LinearRange&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#a08146f196bd9c3f492ee108732449ced">operations_research::operator==</a> (const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:a08146f196bd9c3f492ee108732449ced"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac4052f92af6a7fbb1d45e17befcb68e0"><td class="memItemLeft" align="right" valign="top">LinearRange&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#ac4052f92af6a7fbb1d45e17befcb68e0">operations_research::operator&gt;=</a> (const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</td></tr>
<tr class="separator:ac4052f92af6a7fbb1d45e17befcb68e0"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem:a6cb9e1af7c42b3893aa112dd4de84f18"><td class="memItemLeft" align="right" valign="top">*LinearExpr&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="linear__expr_8h.html#a6cb9e1af7c42b3893aa112dd4de84f18">e1</a> = LinearExpr(x) + (y + 5)</td></tr>
<tr class="memdesc:a6cb9e1af7c42b3893aa112dd4de84f18"><td class="mdescLeft">&#160;</td><td class="mdescRight">Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. <a href="#a6cb9e1af7c42b3893aa112dd4de84f18">More...</a><br /></td></tr>
<tr class="separator:a6cb9e1af7c42b3893aa112dd4de84f18"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<h2 class="groupheader">Variable Documentation</h2>
<a id="a6cb9e1af7c42b3893aa112dd4de84f18"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a6cb9e1af7c42b3893aa112dd4de84f18">&#9670;&nbsp;</a></span>e1</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">*LinearExpr e1 = LinearExpr(x) + (y + 5)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. </p>
<p>You may obtain a copy of the License at </p><div class="fragment"><div class="line">http:<span class="comment">//www.apache.org/licenses/LICENSE-2.0</span></div></div><!-- fragment --><p> 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. This file allows you to write natural code (like a mathematical equation) to model optimization problems with MPSolver. 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>
<p>MPSolver solver = ...; const LinearExpr x = solver.MakeVar(...); ///&lt; </p><dl class="section note"><dt>Note</dt><dd>implicit conversion const LinearExpr y = solver.MakeVar(...); const LinearExpr z = solver.MakeVar(...); const LinearExpr e1 = x + y; const LinearExpr e2 = (e1 + 7.0 + z)/3.0; const LinearRange r = e1 &lt;= e2; solver.MakeRowConstraint(r);</dd></dl>
<p>WARNING, AVOID THIS TRAP:</p>
<p>MPSolver solver = ...; MPVariable* x = solver.MakeVar(...); LinearExpr y = x + 5;</p>
<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: MPSolver solver = ...; MPVariable* x = solver.MakeVar(...); MPVariable* y = solver.MakeVar(...); LinearExpr e1 = LinearExpr(x) + y + 5;</p>
<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>
<p class="definition">Definition at line <a class="el" href="linear__expr_8h_source.html#l00066">66</a> of file <a class="el" href="linear__expr_8h_source.html">linear_expr.h</a>.</p>
</div>
</div>
</div><!-- contents -->
</div>
<div id="footer-container">
<div id="footer">
</div>
</div>
</body>
</html>