Files
ortools-clone/docs/cpp_linear/linear__expr_8h.html
Laurent Perron f36ea62bbc new doc
2019-08-12 23:15:11 -07:00

123 lines
12 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="ortools.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">Google OR-Tools 7.3</span>
</div>
</div>
<div id="content" style="width: 100%; overflow: hidden;">
<div style="margin-left: 15px; margin-top: 5px; float: left; color: #145A32;">
<h2>C++ Reference</h2>
<ul>
<li><a href="../cpp_algorithms/annotated.html">Algorithms</a></li>
<li><a href="../cpp_sat/annotated.html">CP-SAT</a></li>
<li><a href="../cpp_graph/annotated.html">Graph</a></li>
<li><a href="../cpp_routing/annotated.html">Routing</a></li>
<li><a href="../cpp_linear/annotated.html">Linear solver</a></li>
</ul>
</div>
<div id="content">
<div align="center">
<h1 style="color: #145A32;">C++ Reference: Linear solver</h1>
</div>
<!-- Generated by Doxygen 1.8.15 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<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="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> </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_1LinearExpr.html">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 (<a class="el" href="classoperations__research_1_1MPVariable.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_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">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_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="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:ad3390eca60a1042a3d81452cea3863aa"><td class="memItemLeft" align="right" valign="top">std::ostream &amp;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceoperations__research.html#ad3390eca60a1042a3d81452cea3863aa">operator&lt;&lt;</a> (std::ostream &amp;stream, const LinearExpr &amp;linear_expr)</td></tr>
<tr class="separator:ad3390eca60a1042a3d81452cea3863aa"><td class="memSeparator" colspan="2">&#160;</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">operator+</a> (LinearExpr lhs, const LinearExpr &amp;rhs)</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">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">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">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">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">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">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">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>
</div><!-- contents -->
</div>
</div>
<div id="footer-container">
<div id="footer">
</div>
</div>
</body>
</html>