2019-07-15 17:42:24 -07:00
<!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"/> -->
< script type = "text/javascript" src = "jquery.js" > < / script >
< script type = "text/javascript" src = "dynsections.js" > < / script >
2020-09-01 16:00:33 +02:00
< link href = "doxygen.css" rel = "stylesheet" type = "text/css" / >
< link href = "styleSheet.tmp.css" rel = "stylesheet" type = "text/css" / >
2019-07-15 17:42:24 -07:00
< / head >
< body >
< div id = "banner-container" >
< div id = "banner" >
2021-04-27 23:00:03 +02:00
< span id = "sfml" > Google OR-Tools 9.0< / span >
2019-07-15 17:42:24 -07:00
< / div >
< / div >
2019-08-07 18:01:08 -07:00
< div id = "content" style = "width: 100%; overflow: hidden;" >
< div style = "margin-left: 15px; margin-top: 5px; float: left; color: #145A32;" >
2019-08-12 09:16:03 -07:00
< h2 > C++ Reference< / h2 >
2019-08-07 18:01:08 -07:00
< ul >
< li > < a href = "../cpp_algorithms/annotated.html" > Algorithms< / a > < / li >
2019-08-12 09:16:03 -07:00
< li > < a href = "../cpp_sat/annotated.html" > CP-SAT< / a > < / li >
2019-08-07 18:01:08 -07:00
< 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 >
2019-07-15 17:42:24 -07:00
< div id = "content" >
2019-08-07 18:01:08 -07:00
< div align = "center" >
< h1 style = "color: #145A32;" > C++ Reference: Linear solver< / h1 >
< / div >
2021-01-26 11:28:50 +01:00
<!-- Generated by Doxygen 1.9.1 -->
2020-09-01 16:00:33 +02:00
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
2021-01-26 11:28:50 +01:00
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
2020-09-01 16:00:33 +02:00
/* @license-end */
< / script >
< script type = "text/javascript" src = "menudata.js" > < / script >
< script type = "text/javascript" src = "menu.js" > < / script >
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */< / script >
< div id = "main-nav" > < / 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">
2019-07-15 17:42:24 -07:00
< / div >
< / div >
2020-09-01 16:00:33 +02:00
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('linear__expr_8h.html',''); initResizable(); });
/* @license-end */
< / script >
< div id = "doc-content" >
<!-- window showing the filter options -->
< div id = "MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
< / div >
<!-- iframe showing the search results (closed by default) -->
< div id = "MSearchResultsWindow" >
< iframe src = "javascript:void(0)" frameborder = "0"
name="MSearchResults" id="MSearchResults">
< / iframe >
< / div >
2019-07-15 17:42:24 -07:00
< div class = "header" >
< div class = "summary" >
< a href = "#nested-classes" > Classes< / a > |
< a href = "#namespaces" > Namespaces< / a > |
< 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" >
2019-08-12 23:15:11 -07:00
< a name = "details" id = "details" > < / a > < h2 class = "groupheader" > Detailed Description< / h2 >
2020-10-09 12:00:09 +02:00
< 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 >
2019-08-12 23:15:11 -07:00
< 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 < = 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 >
2019-11-14 13:52:51 -08:00
< 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 < = 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 >
2019-08-12 23:15:11 -07:00
< 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 >
2019-11-14 13:52:51 -08:00
< 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 >
2019-08-12 23:15:11 -07:00
< ul >
2019-11-14 13:52:51 -08:00
< 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 >
2019-08-12 23:15:11 -07:00
< / ul >
2019-07-15 17:42:24 -07:00
2019-08-12 23:15:11 -07:00
< p class = "definition" > Definition in file < a class = "el" href = "linear__expr_8h_source.html" > linear_expr.h< / a > .< / p >
2019-07-15 17:42:24 -07:00
< / 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 >
2020-10-09 12:00:09 +02:00
< tr class = "memitem:" > < td class = "memItemLeft" align = "right" valign = "top" > class   < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "classoperations__research_1_1_linear_expr.html" > LinearExpr< / a > < / td > < / tr >
< tr class = "memdesc:" > < td class = "mdescLeft" >   < / td > < td class = "mdescRight" > < a class = "el" href = "classoperations__research_1_1_linear_expr.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_1_m_p_variable.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_1_linear_expr.html#details" > More...< / a > < br / > < / td > < / tr >
2019-07-15 17:42:24 -07:00
< tr class = "separator:" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2020-10-09 12:00:09 +02:00
< tr class = "memitem:" > < td class = "memItemLeft" align = "right" valign = "top" > class   < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "classoperations__research_1_1_linear_range.html" > LinearRange< / a > < / td > < / tr >
< tr class = "memdesc:" > < td class = "mdescLeft" >   < / td > < td class = "mdescRight" > An expression of the form: < a href = "classoperations__research_1_1_linear_range.html#details" > More...< / a > < br / > < / td > < / tr >
2019-07-15 17:42:24 -07:00
< tr class = "separator:" > < td class = "memSeparator" colspan = "2" >   < / 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" >   < / 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" >   < / td > < / tr >
< / table > < table class = "memberdecls" >
< tr class = "heading" > < td colspan = "2" > < h2 class = "groupheader" > < a name = "func-members" > < / a >
Functions< / h2 > < / td > < / tr >
2019-08-07 18:01:08 -07:00
< tr class = "memitem:ad3390eca60a1042a3d81452cea3863aa" > < td class = "memItemLeft" align = "right" valign = "top" > std::ostream &   < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#ad3390eca60a1042a3d81452cea3863aa" > operator< < < / a > (std::ostream & stream, const LinearExpr & linear_expr)< / td > < / tr >
< tr class = "separator:ad3390eca60a1042a3d81452cea3863aa" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< tr class = "memitem:a97f9b83239285f5fdfcac1b8e8b4f162" > < td class = "memItemLeft" align = "right" valign = "top" > LinearExpr  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#a97f9b83239285f5fdfcac1b8e8b4f162" > operator+< / a > (LinearExpr lhs, const LinearExpr & rhs)< / td > < / tr >
2019-07-15 17:42:24 -07:00
< tr class = "separator:a97f9b83239285f5fdfcac1b8e8b4f162" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2019-08-07 18:01:08 -07:00
< tr class = "memitem:a515cdaf4f9c4000bb3482a0c450e23c3" > < td class = "memItemLeft" align = "right" valign = "top" > LinearExpr  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#a515cdaf4f9c4000bb3482a0c450e23c3" > operator-< / a > (LinearExpr lhs, const LinearExpr & rhs)< / td > < / tr >
2019-07-15 17:42:24 -07:00
< tr class = "separator:a515cdaf4f9c4000bb3482a0c450e23c3" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2019-11-14 13:52:51 -08:00
< tr class = "memitem:a138bb0f103cd9d68e4d13fa773901186" > < td class = "memItemLeft" align = "right" valign = "top" > LinearExpr  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#a138bb0f103cd9d68e4d13fa773901186" > operator*< / a > (LinearExpr lhs, double rhs)< / td > < / tr >
< tr class = "separator:a138bb0f103cd9d68e4d13fa773901186" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2019-08-07 18:01:08 -07:00
< tr class = "memitem:abebdd7f40e90df8dc7d557b6e26da942" > < td class = "memItemLeft" align = "right" valign = "top" > LinearExpr  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#abebdd7f40e90df8dc7d557b6e26da942" > operator/< / a > (LinearExpr lhs, double rhs)< / td > < / tr >
2019-07-15 17:42:24 -07:00
< tr class = "separator:abebdd7f40e90df8dc7d557b6e26da942" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2019-11-14 13:52:51 -08:00
< tr class = "memitem:a62b131d2829f1cdacd2414d2d7bc6c7c" > < td class = "memItemLeft" align = "right" valign = "top" > LinearExpr  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#a62b131d2829f1cdacd2414d2d7bc6c7c" > operator*< / a > (double lhs, LinearExpr rhs)< / td > < / tr >
< tr class = "separator:a62b131d2829f1cdacd2414d2d7bc6c7c" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2019-08-07 18:01:08 -07:00
< tr class = "memitem:a6d1fa20f9c9faf7027c0b16f97139e80" > < td class = "memItemLeft" align = "right" valign = "top" > LinearRange  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#a6d1fa20f9c9faf7027c0b16f97139e80" > operator< =< / a > (const LinearExpr & lhs, const LinearExpr & rhs)< / td > < / tr >
2019-07-15 17:42:24 -07:00
< tr class = "separator:a6d1fa20f9c9faf7027c0b16f97139e80" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2019-08-07 18:01:08 -07:00
< tr class = "memitem:a08146f196bd9c3f492ee108732449ced" > < td class = "memItemLeft" align = "right" valign = "top" > LinearRange  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#a08146f196bd9c3f492ee108732449ced" > operator==< / a > (const LinearExpr & lhs, const LinearExpr & rhs)< / td > < / tr >
2019-07-15 17:42:24 -07:00
< tr class = "separator:a08146f196bd9c3f492ee108732449ced" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
2019-08-07 18:01:08 -07:00
< tr class = "memitem:ac4052f92af6a7fbb1d45e17befcb68e0" > < td class = "memItemLeft" align = "right" valign = "top" > LinearRange  < / td > < td class = "memItemRight" valign = "bottom" > < a class = "el" href = "namespaceoperations__research.html#ac4052f92af6a7fbb1d45e17befcb68e0" > operator> =< / a > (const LinearExpr & lhs, const LinearExpr & rhs)< / td > < / tr >
2019-07-15 17:42:24 -07:00
< tr class = "separator:ac4052f92af6a7fbb1d45e17befcb68e0" > < td class = "memSeparator" colspan = "2" >   < / td > < / tr >
< / table >
2019-08-12 23:15:11 -07:00
< / div > <!-- contents -->
2020-09-01 16:00:33 +02:00
< / div > <!-- doc - content -->
2019-08-07 18:01:08 -07:00
< / div >
2019-07-15 17:42:24 -07:00
< / div >
< div id = "footer-container" >
< div id = "footer" >
< / div >
< / div >
< / body >
< / html >