Files
ortools-clone/docs/cpp_linear/linear__expr_8h.html
Laurent Perron 0ac3fa17eb more linear doc
2019-07-15 17:42:24 -07:00

121 lines
11 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">OR-Tools 7.2</span>
</div>
</div>
<link rel="icon" href="https://developers.google.com/optimization/images/orLogo.png">
<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="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><!-- 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','');});
/* @license-end */
</script>
<div id="doc-content">
<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>WARNING, 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><code>LinearExpr</code> e1 = LinearExpr(x) + (y + 5);</li>
<li><code>LinearExpr</code> e1 = y + 5 + LinearExpr(x); \endpage </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">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 (<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">operations_research::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: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="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>
</div><!-- contents -->
</div><!-- doc-content -->
</div>
<div id="footer-container">
<div id="footer">
</div>
</div>
</body>
</html>