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

77 lines
43 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="headertitle">
<div class="title">linear_expr.h</div> </div>
</div><!--header-->
<div class="contents">
<a href="linear__expr_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="comment">// Copyright 2010-2018 Google LLC</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;<span class="comment">// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;<span class="comment">// you may not use this file except in compliance with the License.</span></div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;<span class="comment">// You may obtain a copy of the License at</span></div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span>&#160;<span class="comment">// http://www.apache.org/licenses/LICENSE-2.0</span></div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span>&#160;<span class="comment">// Unless required by applicable law or agreed to in writing, software</span></div><div class="line"><a name="l00009"></a><span class="lineno"> 9</span>&#160;<span class="comment">// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span></div><div class="line"><a name="l00010"></a><span class="lineno"> 10</span>&#160;<span class="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span>&#160;<span class="comment">// See the License for the specific language governing permissions and</span></div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span>&#160;<span class="comment">// limitations under the License.</span></div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span>&#160;</div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span>&#160;<span class="preprocessor">#ifndef OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_</span></div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160;<span class="preprocessor">#define OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_</span></div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160;</div><div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160;<span class="comment">// This file allows you to write natural code (like a mathematical equation) to</span></div><div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160;<span class="comment">// model optimization problems with MPSolver. It is syntatic sugar on top of</span></div><div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160;<span class="comment">// the MPSolver API, it provides no additional functionality. Use of these APIs</span></div><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160;<span class="comment">// makes it much easier to write code that is both simple and big-O optimal for</span></div><div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160;<span class="comment">// creating your model, at the cost of some additional constant factor</span></div><div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160;<span class="comment">// overhead. If model creation is a bottleneck in your problem, consider using</span></div><div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160;<span class="comment">// the MPSolver API directly instead.</span></div><div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160;<span class="comment">// This file contains two classes:</span></div><div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160;<span class="comment">// 1. LinearExpr: models offset + sum_{i in S} a_i*x_i for decision var x_i,</span></div><div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160;<span class="comment">// 2. LinearRange: models lb &lt;= sum_{i in S} a_i*x_i &lt;= ub,</span></div><div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160;<span class="comment">// and it provides various operator overloads to build up &quot;LinearExpr&quot;s and</span></div><div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160;<span class="comment">// then convert them to &quot;LinearRange&quot;s.</span></div><div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160;<span class="comment">// Recommended use (avoids dangerous code):</span></div><div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160;<span class="comment">// MPSolver solver = ...;</span></div><div class="line"><a name="l00034"></a><span class="lineno"> 34</span>&#160;<span class="comment">// const LinearExpr x = solver.MakeVar(...); // Note: implicit conversion</span></div><div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160;<span class="comment">// const LinearExpr y = solver.MakeVar(...);</span></div><div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160;<span class="comment">// const LinearExpr z = solver.MakeVar(...);</span></div><div class="line"><a name="l00037"></a><span class="lineno"> 37</span>&#160;<span class="comment">// const LinearExpr e1 = x + y;</span></div><div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160;<span class="comment">// const LinearExpr e2 = (e1 + 7.0 + z)/3.0;</span></div><div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160;<span class="comment">// const LinearRange r = e1 &lt;= e2;</span></div><div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160;<span class="comment">// solver.MakeRowConstraint(r);</span></div><div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160;<span class="comment">// WARNING, AVOID THIS TRAP:</span></div><div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160;<span class="comment">// MPSolver solver = ...;</span></div><div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160;<span class="comment">// MPVariable* x = solver.MakeVar(...);</span></div><div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160;<span class="comment">// LinearExpr y = x + 5;</span></div><div class="line"><a name="l00047"></a><span class="lineno"> 47</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160;<span class="comment">// In evaluating &quot;x+5&quot; above, x is NOT converted to a LinearExpr before the</span></div><div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160;<span class="comment">// addition, but rather is treated as a pointer, so x+5 gives a new pointer to</span></div><div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160;<span class="comment">// garbage memory.</span></div><div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160;<span class="comment">// For this reason, when using LinearExpr, it is best practice to:</span></div><div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160;<span class="comment">// 1. use double literals instead of ints (e.g. &quot;x + 5.0&quot;, not &quot;x + 5&quot;),</span></div><div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160;<span class="comment">// 2. Immediately convert all MPVariable* to LinearExpr on creation, and only</span></div><div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160;<span class="comment">// hold references to the &quot;LinearExpr&quot;s.</span></div><div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160;<span class="comment">// Likewise, the following code is NOT recommended:</span></div><div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160;<span class="comment">// MPSolver solver = ...;</span></div><div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160;<span class="comment">// MPVariable* x = solver.MakeVar(...);</span></div><div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160;<span class="comment">// MPVariable* y = solver.MakeVar(...);</span></div><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160;<span class="comment">// LinearExpr e1 = LinearExpr(x) + y + 5;</span></div><div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160;<span class="comment">// While it is correct, it violates the natural assumption that the + operator</span></div><div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160;<span class="comment">// is associative. Thus you are setting a trap for future modifications of the</span></div><div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160;<span class="comment">// code, as any of the following changes would lead to the above failure mode:</span></div><div class="line"><a name="l00066"></a><span class="lineno"><a class="line" href="linear__expr_8h.html#a6cb9e1af7c42b3893aa112dd4de84f18"> 66</a></span>&#160;<span class="comment">// * LinearExpr e1 = LinearExpr(x) + (y + 5);</span></div><div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160;<span class="comment">// * LinearExpr e1 = y + 5 + LinearExpr(x);</span></div><div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160;</div><div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160;<span class="preprocessor">#include &quot;absl/container/flat_hash_map.h&quot;</span></div><div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160;</div><div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespaceoperations__research.html">operations_research</a> {</div><div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160;</div><div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160;<span class="comment">// NOTE(user): forward declaration is necessary due to cyclic dependencies,</span></div><div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160;<span class="comment">// MPVariable is defined in linear_solver.h, which depends on LinearExpr.</span></div><div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160;<span class="keyword">class </span>MPVariable;</div><div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160;</div><div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160;<span class="comment">// LinearExpr models a quantity that is linear in the decision variables</span></div><div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160;<span class="comment">// (MPVariable) of an optimization problem, i.e.</span></div><div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160;<span class="comment">// offset + sum_{i in S} a_i*x_i,</span></div><div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160;<span class="comment">// where the a_i and offset are constants and the x_i are MPVariables. You can</span></div><div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160;<span class="comment">// use a LinearExpr &quot;linear_expr&quot; with an MPSolver &quot;solver&quot; to:</span></div><div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160;<span class="comment">// * Set as the objective of your optimization problem, e.g.</span></div><div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160;<span class="comment">// solver.MutableObjective()-&gt;MaximizeLinearExpr(linear_expr);</span></div><div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160;<span class="comment">// * Create a constraint in your optimization, e.g.</span></div><div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160;<span class="comment">// solver.MakeRowConstraint(linear_expr1 &lt;= linear_expr2);</span></div><div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160;<span class="comment">// * Get the value of the quantity after solving, e.g.</span></div><div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160;<span class="comment">// solver.Solve();</span></div><div class="line"><a name="l00095"></a><span class="lineno"> 95</span>&#160;<span class="comment">// linear_expr.SolutionValue();</span></div><div class="line"><a name="l00096"></a><span class="lineno"> 96</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160;<span class="comment">// LinearExpr is allowed to delete variables with coefficient zero from the map,</span></div><div class="line"><a name="l00098"></a><span class="lineno"> 98</span>&#160;<span class="comment">// but is not obligated to do so.</span></div><div class="line"><a name="l00099"></a><span class="lineno"><a class="line" href="classoperations__research_1_1LinearExpr.html"> 99</a></span>&#160;<span class="keyword">class </span><a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a> {</div><div class="line"><a name="l00100"></a><span class="lineno"> 100</span>&#160; <span class="keyword">public</span>:</div><div class="line"><a name="l00101"></a><span class="lineno"> 101</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html#a3245b8350f417872f1f638ecd22a832f">LinearExpr</a>();</div><div class="line"><a name="l00102"></a><span class="lineno"> 102</span>&#160; <span class="comment">// Possible implicit conversions are intentional.</span></div><div class="line"><a name="l00103"></a><span class="lineno"> 103</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html#a3245b8350f417872f1f638ecd22a832f">LinearExpr</a>(<span class="keywordtype">double</span> constant); <span class="comment">// NOLINT</span></div><div class="line"><a name="l00104"></a><span class="lineno"> 104</span>&#160; <span class="comment">// Possible implicit conversions are intentional.</span></div><div class="line"><a name="l00105"></a><span class="lineno"> 105</span>&#160; <span class="comment">// Warning: var is not owned.</span></div><div class="line"><a name="l00106"></a><span class="lineno"> 106</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html#a3245b8350f417872f1f638ecd22a832f">LinearExpr</a>(<span class="keyword">const</span> MPVariable* var); <span class="comment">// NOLINT</span></div><div class="line"><a name="l00107"></a><span class="lineno"> 107</span>&#160;</div><div class="line"><a name="l00108"></a><span class="lineno"> 108</span>&#160; <span class="comment">// Returns 1-var.</span></div><div class="line"><a name="l00109"></a><span class="lineno"> 109</span>&#160; <span class="comment">// NOTE(user): if var is binary variable, this corresponds to the logical</span></div><div class="line"><a name="l00110"></a><span class="lineno"> 110</span>&#160; <span class="comment">// negation of var.</span></div><div class="line"><a name="l00111"></a><span class="lineno"> 111</span>&#160; <span class="comment">// Passing by value is intentional, see the discussion on binary ops.</span></div><div class="line"><a name="l00112"></a><span class="lineno"> 112</span>&#160; <span class="keyword">static</span> <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a> <a class="code" href="classoperations__research_1_1LinearExpr.html#a46b677de8b46c72ffdff9e16bc2e3745">NotVar</a>(<a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a> var);</div><div class="line"><a name="l00113"></a><span class="lineno"> 113</span>&#160;</div><div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a>&amp; <a class="code" href="classoperations__research_1_1LinearExpr.html#a5f501758e469b8718b8410bb87100f51">operator+=</a>(<span class="keyword">const</span> <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a>&amp; rhs);</div><div class="line"><a name="l00115"></a><span class="lineno"> 115</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a>&amp; <a class="code" href="classoperations__research_1_1LinearExpr.html#a359bec8d7b3221d7549c98b0825970d4">operator-=</a>(<span class="keyword">const</span> <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a>&amp; rhs);</div><div class="line"><a name="l00116"></a><span class="lineno"> 116</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a>&amp; <a class="code" href="classoperations__research_1_1LinearExpr.html#a757085e5b10be1bde2ff049a3dbc51c7">operator*=</a>(<span class="keywordtype">double</span> rhs);</div><div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a>&amp; <a class="code" href="classoperations__research_1_1LinearExpr.html#a14e5dbbcd5618dc42a8799181490742e">operator/=</a>(<span class="keywordtype">double</span> rhs);</div><div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a> <a class="code" href="classoperations__research_1_1LinearExpr.html#a3f94eb1c0b25852c0ff0910b83d363d9">operator-</a>() <span class="keyword">const</span>;</div><div class="line"><a name="l00119"></a><span class="lineno"> 119</span>&#160;</div><div class="line"><a name="l00120"></a><span class="lineno"><a class="line" href="classoperations__research_1_1LinearExpr.html#aff2f953067d8d8854273fa973496466e"> 120</a></span>&#160; <span class="keywordtype">double</span> <a class="code" href="classoperations__research_1_1LinearExpr.html#aff2f953067d8d8854273fa973496466e">offset</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> offset_; }</div><div class="line"><a name="l00121"></a><span class="lineno"><a class="line" href="classoperations__research_1_1LinearExpr.html#a37d87e22cd7125839fab636219336df8"> 121</a></span>&#160; <span class="keyword">const</span> absl::flat_hash_map&lt;const MPVariable*, double&gt;&amp; <a class="code" href="classoperations__research_1_1LinearExpr.html#a37d87e22cd7125839fab636219336df8">terms</a>()<span class="keyword"> const </span>{</div><div class="line"><a name="l00122"></a><span class="lineno"> 122</span>&#160; <span class="keywordflow">return</span> terms_;</div><div class="line"><a name="l00123"></a><span class="lineno"> 123</span>&#160; }</div><div class="line"><a name="l00124"></a><span class="lineno"> 124</span>&#160;</div><div class="line"><a name="l00125"></a><span class="lineno"> 125</span>&#160; <span class="comment">// Call only after calling MPSolver::Solve. Evaluates the value of this</span></div><div class="line"><a name="l00126"></a><span class="lineno"> 126</span>&#160; <span class="comment">// expression at the solution found.</span></div><div class="line"><a name="l00127"></a><span class="lineno"> 127</span>&#160; <span class="keywordtype">double</span> <a class="code" href="classoperations__research_1_1LinearExpr.html#a07afbba5788651a38be83e959bbbc92c">SolutionValue</a>() <span class="keyword">const</span>;</div><div class="line"><a name="l00128"></a><span class="lineno"> 128</span>&#160;</div><div class="line"><a name="l00129"></a><span class="lineno"> 129</span>&#160; <span class="keyword">private</span>:</div><div class="line"><a name="l00130"></a><span class="lineno"> 130</span>&#160; <span class="keywordtype">double</span> offset_;</div><div class="line"><a name="l00131"></a><span class="lineno"> 131</span>&#160; absl::flat_hash_map&lt;const MPVariable*, double&gt; terms_;</div><div class="line"><a name="l00132"></a><span class="lineno"> 132</span>&#160;};</div><div class="line"><a name="l00133"></a><span class="lineno"> 133</span>&#160;</div><div class="line"><a name="l00134"></a><span class="lineno"> 134</span>&#160;<span class="comment">// NOTE(user): in the ops below, the non-&quot;const LinearExpr&amp;&quot; are intentional.</span></div><div class="line"><a name="l00135"></a><span class="lineno"> 135</span>&#160;<span class="comment">// We need to create a new LinearExpr for the result, so we lose nothing by</span></div><div class="line"><a name="l00136"></a><span class="lineno"> 136</span>&#160;<span class="comment">// passing one argument by value, mutating it, and then returning it. In</span></div><div class="line"><a name="l00137"></a><span class="lineno"> 137</span>&#160;<span class="comment">// particular, this allows (with move semantics and RVO) an optimized</span></div><div class="line"><a name="l00138"></a><span class="lineno"> 138</span>&#160;<span class="comment">// evaluation of expressions such as</span></div><div class="line"><a name="l00139"></a><span class="lineno"> 139</span>&#160;<span class="comment">// a + b + c + d</span></div><div class="line"><a name="l00140"></a><span class="lineno"> 140</span>&#160;<span class="comment">// (see http://en.cppreference.com/w/cpp/language/operators).</span></div><div class="line"><a name="l00141"></a><span class="lineno"> 141</span>&#160;LinearExpr <a class="code" href="namespaceoperations__research.html#a97f9b83239285f5fdfcac1b8e8b4f162">operator+</a>(LinearExpr lhs, <span class="keyword">const</span> LinearExpr&amp; rhs);</div><div class="line"><a name="l00142"></a><span class="lineno"> 142</span>&#160;LinearExpr <a class="code" href="namespaceoperations__research.html#a515cdaf4f9c4000bb3482a0c450e23c3">operator-</a>(LinearExpr lhs, <span class="keyword">const</span> LinearExpr&amp; rhs);</div><div class="line"><a name="l00143"></a><span class="lineno"> 143</span>&#160;LinearExpr <a class="code" href="namespaceoperations__research.html#a741104fe08089fe3520676487f7a685d">operator*</a>(LinearExpr lhs, <span class="keywordtype">double</span> rhs);</div><div class="line"><a name="l00144"></a><span class="lineno"> 144</span>&#160;LinearExpr <a class="code" href="namespaceoperations__research.html#abebdd7f40e90df8dc7d557b6e26da942">operator/</a>(LinearExpr lhs, <span class="keywordtype">double</span> rhs);</div><div class="line"><a name="l00145"></a><span class="lineno"> 145</span>&#160;LinearExpr <a class="code" href="namespaceoperations__research.html#a741104fe08089fe3520676487f7a685d">operator*</a>(<span class="keywordtype">double</span> lhs, LinearExpr rhs);</div><div class="line"><a name="l00146"></a><span class="lineno"> 146</span>&#160;</div><div class="line"><a name="l00147"></a><span class="lineno"> 147</span>&#160;<span class="comment">// An expression of the form:</span></div><div class="line"><a name="l00148"></a><span class="lineno"> 148</span>&#160;<span class="comment">// lower_bound &lt;= sum_{i in S} a_i*x_i &lt;= upper_bound.</span></div><div class="line"><a name="l00149"></a><span class="lineno"> 149</span>&#160;<span class="comment">// The sum is represented as a LinearExpr with offset 0.</span></div><div class="line"><a name="l00150"></a><span class="lineno"> 150</span>&#160;<span class="comment">//</span></div><div class="line"><a name="l00151"></a><span class="lineno"> 151</span>&#160;<span class="comment">// Must be added to model with</span></div><div class="line"><a name="l00152"></a><span class="lineno"> 152</span>&#160;<span class="comment">// MPSolver::AddRowConstraint(const LinearRange&amp; range[, const std::string&amp;</span></div><div class="line"><a name="l00153"></a><span class="lineno"> 153</span>&#160;<span class="comment">// name]);</span></div><div class="line"><a name="l00154"></a><span class="lineno"><a class="line" href="classoperations__research_1_1LinearRange.html"> 154</a></span>&#160;<span class="keyword">class </span><a class="code" href="classoperations__research_1_1LinearRange.html">LinearRange</a> {</div><div class="line"><a name="l00155"></a><span class="lineno"> 155</span>&#160; <span class="keyword">public</span>:</div><div class="line"><a name="l00156"></a><span class="lineno"><a class="line" href="classoperations__research_1_1LinearRange.html#a9ce7b1b40dc5135ed9ca27cfedeca64e"> 156</a></span>&#160; <a class="code" href="classoperations__research_1_1LinearRange.html#a9ce7b1b40dc5135ed9ca27cfedeca64e">LinearRange</a>() : lower_bound_(0), upper_bound_(0) {}</div><div class="line"><a name="l00157"></a><span class="lineno"> 157</span>&#160; <span class="comment">// The bounds of the linear range are updated so that they include the offset</span></div><div class="line"><a name="l00158"></a><span class="lineno"> 158</span>&#160; <span class="comment">// from &quot;linear_expr&quot;, i.e., we form the range:</span></div><div class="line"><a name="l00159"></a><span class="lineno"> 159</span>&#160; <span class="comment">// lower_bound - offset &lt;= linear_expr - offset &lt;= upper_bound - offset.</span></div><div class="line"><a name="l00160"></a><span class="lineno"> 160</span>&#160; <a class="code" href="classoperations__research_1_1LinearRange.html#a9ce7b1b40dc5135ed9ca27cfedeca64e">LinearRange</a>(<span class="keywordtype">double</span> <a class="code" href="classoperations__research_1_1LinearRange.html#ac9dc29ebeaf4a37d108c0094ca89eb9d">lower_bound</a>, <span class="keyword">const</span> <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a>&amp; <a class="code" href="classoperations__research_1_1LinearRange.html#aa75204d544c4e4fc91c3ee8a091dd690">linear_expr</a>,</div><div class="line"><a name="l00161"></a><span class="lineno"> 161</span>&#160; <span class="keywordtype">double</span> <a class="code" href="classoperations__research_1_1LinearRange.html#a527995fddd9d1781d996ff6f50e48041">upper_bound</a>);</div><div class="line"><a name="l00162"></a><span class="lineno"> 162</span>&#160;</div><div class="line"><a name="l00163"></a><span class="lineno"><a class="line" href="classoperations__research_1_1LinearRange.html#ac9dc29ebeaf4a37d108c0094ca89eb9d"> 163</a></span>&#160; <span class="keywordtype">double</span> <a class="code" href="classoperations__research_1_1LinearRange.html#ac9dc29ebeaf4a37d108c0094ca89eb9d">lower_bound</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> lower_bound_; }</div><div class="line"><a name="l00164"></a><span class="lineno"><a class="line" href="classoperations__research_1_1LinearRange.html#aa75204d544c4e4fc91c3ee8a091dd690"> 164</a></span>&#160; <span class="keyword">const</span> <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a>&amp; <a class="code" href="classoperations__research_1_1LinearRange.html#aa75204d544c4e4fc91c3ee8a091dd690">linear_expr</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> linear_expr_; }</div><div class="line"><a name="l00165"></a><span class="lineno"><a class="line" href="classoperations__research_1_1LinearRange.html#a527995fddd9d1781d996ff6f50e48041"> 165</a></span>&#160; <span class="keywordtype">double</span> <a class="code" href="classoperations__research_1_1LinearRange.html#a527995fddd9d1781d996ff6f50e48041">upper_bound</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> upper_bound_; }</div><div class="line"><a name="l00166"></a><span class="lineno"> 166</span>&#160;</div><div class="line"><a name="l00167"></a><span class="lineno"> 167</span>&#160; <span class="keyword">private</span>:</div><div class="line"><a name="l00168"></a><span class="lineno"> 168</span>&#160; <span class="keywordtype">double</span> lower_bound_;</div><div class="line"><a name="l00169"></a><span class="lineno"> 169</span>&#160; <span class="comment">// invariant: linear_expr_.offset() == 0.</span></div><div class="line"><a name="l00170"></a><span class="lineno"> 170</span>&#160; <a class="code" href="classoperations__research_1_1LinearExpr.html">LinearExpr</a> linear_expr_;</div><div class="line"><a name="l00171"></a><span class="lineno"> 171</span>&#160; <span class="keywordtype">double</span> upper_bound_;</div><div class="line"><a name="l00172"></a><span class="lineno"> 172</span>&#160;};</div><div class="line"><a name="l00173"></a><span class="lineno"> 173</span>&#160;</div><div class="line"><a name="l00174"></a><span class="lineno"> 174</span>&#160;LinearRange <a class="code" href="namespaceoperations__research.html#a6d1fa20f9c9faf7027c0b16f97139e80">operator&lt;=</a>(<span class="keyword">const</span> LinearExpr&amp; lhs, <span class="keyword">const</span> LinearExpr&amp; rhs);</div><div class="line"><a name="l00175"></a><span class="lineno"> 175</span>&#160;LinearRange <a class="code" href="namespaceoperations__research.html#a08146f196bd9c3f492ee108732449ced">operator==</a>(<span class="keyword">const</span> LinearExpr&amp; lhs, <span class="keyword">const</span> LinearExpr&amp; rhs);</div><div class="line"><a name="l00176"></a><span class="lineno"> 176</span>&#160;LinearRange <a class="code" href="namespaceoperations__research.html#ac4052f92af6a7fbb1d45e17befcb68e0">operator&gt;=</a>(<span class="keyword">const</span> LinearExpr&amp; lhs, <span class="keyword">const</span> LinearExpr&amp; rhs);</div><div class="line"><a name="l00177"></a><span class="lineno"> 177</span>&#160;</div><div class="line"><a name="l00178"></a><span class="lineno"> 178</span>&#160;<span class="comment">// TODO(user,user): explore defining more overloads to support:</span></div><div class="line"><a name="l00179"></a><span class="lineno"> 179</span>&#160;<span class="comment">// solver.AddRowConstraint(0.0 &lt;= x + y + z &lt;= 1.0);</span></div><div class="line"><a name="l00180"></a><span class="lineno"> 180</span>&#160;</div><div class="line"><a name="l00181"></a><span class="lineno"> 181</span>&#160;} <span class="comment">// namespace operations_research</span></div><div class="line"><a name="l00182"></a><span class="lineno"> 182</span>&#160;</div><div class="line"><a name="l00183"></a><span class="lineno"> 183</span>&#160;<span class="preprocessor">#endif // OR_TOOLS_LINEAR_SOLVER_LINEAR_EXPR_H_</span></div><div class="ttc" id="classoperations__research_1_1LinearRange_html_aa75204d544c4e4fc91c3ee8a091dd690"><div class="ttname"><a href="classoperations__research_1_1LinearRange.html#aa75204d544c4e4fc91c3ee8a091dd690">operations_research::LinearRange::linear_expr</a></div><div class="ttdeci">const LinearExpr &amp; linear_expr() const</div><div class="ttdef"><b>Definition:</b> <a href="linear__expr_8h_source.html#l00164">linear_expr.h:164</a></div></div>
<div class="ttc" id="namespaceoperations__research_html_a97f9b83239285f5fdfcac1b8e8b4f162"><div class="ttname"><a href="namespaceoperations__research.html#a97f9b83239285f5fdfcac1b8e8b4f162">operations_research::operator+</a></div><div class="ttdeci">LinearExpr operator+(LinearExpr lhs, const LinearExpr &amp;rhs)</div><div class="ttdoc">NOTE(user): in the ops below, the non-&quot;const LinearExpr&amp;&quot; are intentional.</div></div>
<div class="ttc" id="namespaceoperations__research_html_a741104fe08089fe3520676487f7a685d"><div class="ttname"><a href="namespaceoperations__research.html#a741104fe08089fe3520676487f7a685d">operations_research::operator *</a></div><div class="ttdeci">LinearExpr operator *(LinearExpr lhs, double rhs)</div></div>
<div class="ttc" id="namespaceoperations__research_html_a6d1fa20f9c9faf7027c0b16f97139e80"><div class="ttname"><a href="namespaceoperations__research.html#a6d1fa20f9c9faf7027c0b16f97139e80">operations_research::operator&lt;=</a></div><div class="ttdeci">LinearRange operator&lt;=(const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</div></div>
<div class="ttc" id="classoperations__research_1_1LinearRange_html"><div class="ttname"><a href="classoperations__research_1_1LinearRange.html">operations_research::LinearRange</a></div><div class="ttdoc">An expression of the form: lower_bound &lt;= sum_{i in S} a_i*x_i &lt;= upper_bound.</div><div class="ttdef"><b>Definition:</b> <a href="linear__expr_8h_source.html#l00154">linear_expr.h:154</a></div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a07afbba5788651a38be83e959bbbc92c"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a07afbba5788651a38be83e959bbbc92c">operations_research::LinearExpr::SolutionValue</a></div><div class="ttdeci">double SolutionValue() const</div><div class="ttdoc">Call only after calling MPSolver::Solve.</div></div>
<div class="ttc" id="namespaceoperations__research_html_a08146f196bd9c3f492ee108732449ced"><div class="ttname"><a href="namespaceoperations__research.html#a08146f196bd9c3f492ee108732449ced">operations_research::operator==</a></div><div class="ttdeci">LinearRange operator==(const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a757085e5b10be1bde2ff049a3dbc51c7"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a757085e5b10be1bde2ff049a3dbc51c7">operations_research::LinearExpr::operator *=</a></div><div class="ttdeci">LinearExpr &amp; operator *=(double rhs)</div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a3245b8350f417872f1f638ecd22a832f"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a3245b8350f417872f1f638ecd22a832f">operations_research::LinearExpr::LinearExpr</a></div><div class="ttdeci">LinearExpr()</div></div>
<div class="ttc" id="classoperations__research_1_1LinearRange_html_ac9dc29ebeaf4a37d108c0094ca89eb9d"><div class="ttname"><a href="classoperations__research_1_1LinearRange.html#ac9dc29ebeaf4a37d108c0094ca89eb9d">operations_research::LinearRange::lower_bound</a></div><div class="ttdeci">double lower_bound() const</div><div class="ttdef"><b>Definition:</b> <a href="linear__expr_8h_source.html#l00163">linear_expr.h:163</a></div></div>
<div class="ttc" id="classoperations__research_1_1LinearRange_html_a527995fddd9d1781d996ff6f50e48041"><div class="ttname"><a href="classoperations__research_1_1LinearRange.html#a527995fddd9d1781d996ff6f50e48041">operations_research::LinearRange::upper_bound</a></div><div class="ttdeci">double upper_bound() const</div><div class="ttdef"><b>Definition:</b> <a href="linear__expr_8h_source.html#l00165">linear_expr.h:165</a></div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html">operations_research::LinearExpr</a></div><div class="ttdoc">LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...</div><div class="ttdef"><b>Definition:</b> <a href="linear__expr_8h_source.html#l00099">linear_expr.h:99</a></div></div>
<div class="ttc" id="classoperations__research_1_1LinearRange_html_a9ce7b1b40dc5135ed9ca27cfedeca64e"><div class="ttname"><a href="classoperations__research_1_1LinearRange.html#a9ce7b1b40dc5135ed9ca27cfedeca64e">operations_research::LinearRange::LinearRange</a></div><div class="ttdeci">LinearRange()</div><div class="ttdef"><b>Definition:</b> <a href="linear__expr_8h_source.html#l00156">linear_expr.h:156</a></div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a37d87e22cd7125839fab636219336df8"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a37d87e22cd7125839fab636219336df8">operations_research::LinearExpr::terms</a></div><div class="ttdeci">const absl::flat_hash_map&lt; const MPVariable *, double &gt; &amp; terms() const</div><div class="ttdef"><b>Definition:</b> <a href="linear__expr_8h_source.html#l00121">linear_expr.h:121</a></div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a5f501758e469b8718b8410bb87100f51"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a5f501758e469b8718b8410bb87100f51">operations_research::LinearExpr::operator+=</a></div><div class="ttdeci">LinearExpr &amp; operator+=(const LinearExpr &amp;rhs)</div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a14e5dbbcd5618dc42a8799181490742e"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a14e5dbbcd5618dc42a8799181490742e">operations_research::LinearExpr::operator/=</a></div><div class="ttdeci">LinearExpr &amp; operator/=(double rhs)</div></div>
<div class="ttc" id="namespaceoperations__research_html_ac4052f92af6a7fbb1d45e17befcb68e0"><div class="ttname"><a href="namespaceoperations__research.html#ac4052f92af6a7fbb1d45e17befcb68e0">operations_research::operator&gt;=</a></div><div class="ttdeci">LinearRange operator&gt;=(const LinearExpr &amp;lhs, const LinearExpr &amp;rhs)</div></div>
<div class="ttc" id="namespaceoperations__research_html_a515cdaf4f9c4000bb3482a0c450e23c3"><div class="ttname"><a href="namespaceoperations__research.html#a515cdaf4f9c4000bb3482a0c450e23c3">operations_research::operator-</a></div><div class="ttdeci">LinearExpr operator-(LinearExpr lhs, const LinearExpr &amp;rhs)</div></div>
<div class="ttc" id="namespaceoperations__research_html"><div class="ttname"><a href="namespaceoperations__research.html">operations_research</a></div><div class="ttdoc">Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in c...</div><div class="ttdef"><b>Definition:</b> <a href="constraint__solver_8h_source.html#l00093">constraint_solver.h:93</a></div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a46b677de8b46c72ffdff9e16bc2e3745"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a46b677de8b46c72ffdff9e16bc2e3745">operations_research::LinearExpr::NotVar</a></div><div class="ttdeci">static LinearExpr NotVar(LinearExpr var)</div><div class="ttdoc">Returns 1-var.</div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_aff2f953067d8d8854273fa973496466e"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#aff2f953067d8d8854273fa973496466e">operations_research::LinearExpr::offset</a></div><div class="ttdeci">double offset() const</div><div class="ttdef"><b>Definition:</b> <a href="linear__expr_8h_source.html#l00120">linear_expr.h:120</a></div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a359bec8d7b3221d7549c98b0825970d4"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a359bec8d7b3221d7549c98b0825970d4">operations_research::LinearExpr::operator-=</a></div><div class="ttdeci">LinearExpr &amp; operator-=(const LinearExpr &amp;rhs)</div></div>
<div class="ttc" id="classoperations__research_1_1LinearExpr_html_a3f94eb1c0b25852c0ff0910b83d363d9"><div class="ttname"><a href="classoperations__research_1_1LinearExpr.html#a3f94eb1c0b25852c0ff0910b83d363d9">operations_research::LinearExpr::operator-</a></div><div class="ttdeci">LinearExpr operator-() const</div></div>
<div class="ttc" id="namespaceoperations__research_html_abebdd7f40e90df8dc7d557b6e26da942"><div class="ttname"><a href="namespaceoperations__research.html#abebdd7f40e90df8dc7d557b6e26da942">operations_research::operator/</a></div><div class="ttdeci">LinearExpr operator/(LinearExpr lhs, double rhs)</div></div>
</div><!-- fragment --></div><!-- contents -->
</div>
<div id="footer-container">
<div id="footer">
</div>
</div>
</body>
</html>