<divclass="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><oltype="1">
<li>LinearExpr: models offset + sum_{i in S} a_i*x_i for decision var x_i,</li>
<li>LinearRange: models lb <= sum_{i in S} a_i*x_i <= 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>
<divclass="fragment"><divclass="line">MPSolver solver = ...;</div><divclass="line"><spanclass="keyword">const</span> LinearExpr x = solver.MakeVar(...); * Note: implicit conversion</div><divclass="line"><spanclass="keyword">const</span> LinearExpr y = solver.MakeVar(...);</div><divclass="line"><spanclass="keyword">const</span> LinearExpr z = solver.MakeVar(...);</div><divclass="line"><spanclass="keyword">const</span> LinearExpr e1 = x + y;</div><divclass="line"><spanclass="keyword">const</span> LinearExpr e2 = (e1 + 7.0 + z)/3.0;</div><divclass="line"><spanclass="keyword">const</span> LinearRange r = e1 <= e2;</div><divclass="line">solver.MakeRowConstraint(r);</div></div><!-- fragment --><p><b>WARNING</b>, AVOID THIS TRAP:</p>
<divclass="fragment"><divclass="line">MPSolver solver = ...;</div><divclass="line">MPVariable* x = solver.MakeVar(...);</div><divclass="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><oltype="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><divclass="fragment"><divclass="line">MPSolver solver = ...;</div><divclass="line">MPVariable* x = solver.MakeVar(...);</div><divclass="line">MPVariable* y = solver.MakeVar(...);</div><divclass="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>
<trclass="memdesc:"><tdclass="mdescLeft"> </td><tdclass="mdescRight"><aclass="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 (<aclass="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. <ahref="classoperations__research_1_1LinearExpr.html#details">More...</a><br/></td></tr>
<trclass="memdesc:"><tdclass="mdescLeft"> </td><tdclass="mdescRight">An expression of the form: <ahref="classoperations__research_1_1LinearRange.html#details">More...</a><br/></td></tr>