<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Properly add binary variable (to examples like 5.18) in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/Properly-add-binary-variable-to-examples-like-5-18/m-p/200399#M1028</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am playing around with the optmodel-network-examples a bit, and would like to add a binary variable. This is, you have to ship a fixed minimum amount from the sources to the sinks (or ship nothing).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The example below works in principle, but I am worried about the "conditionally optimal" message. The critical restrictions are "balance" (the "&amp;lt;" sign) and "Lower_Upper_Flow". Is the syntax I am using all right? (The rationale I think is.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let Fixed_Const=1e6;&lt;/P&gt;&lt;P&gt;data garcs;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; input _from_ $ _to_ $ _cost_ FIXED_QTY FIXED_COST Lower Upper;&lt;BR /&gt;datalines;&lt;BR /&gt;s1 d1 1 20 10 0 150&lt;BR /&gt;s1 d2 8 20 70 0 150&lt;BR /&gt;s2 d1 4 20 50 0 150&lt;BR /&gt;s2 d2 2 20 20 0 150&lt;BR /&gt;s2 d3 1 20 10 0 150&lt;BR /&gt;s3 d2 5 20 50 0 150&lt;BR /&gt;s3 d3 4 20 40 0 150&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data gnodes;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; input _node_ $ _sd_ ;&lt;BR /&gt;datalines;&lt;BR /&gt;s1&amp;nbsp; 53&lt;BR /&gt;s2&amp;nbsp; 21&lt;BR /&gt;s3&amp;nbsp; 73&lt;BR /&gt;d1&amp;nbsp; -52&lt;BR /&gt;d2&amp;nbsp; -16&lt;BR /&gt;d3&amp;nbsp; -23&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set &amp;lt;str&amp;gt; NODES;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num _sd_ {NODES} init 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; read data gnodes into NODES=[_node_] _sd_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set &amp;lt;str,str&amp;gt; ARCS;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num Lower {ARCS} init 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num Upper {ARCS};&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num _capac_ {ARCS} init .;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num _cost_ {ARCS};&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num FIXED_QTY {ARCS}, FIXED_COST {ARCS};&lt;BR /&gt;&amp;nbsp;&amp;nbsp; read data garcs nomiss into ARCS=[_from_ _to_] _cost_ FIXED_QTY FIXED_COST Lower Upper;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Print FIXED_QTY;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; NODES = NODES union (union {&amp;lt;i,j&amp;gt; in ARCS} {i,j});&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; var Flow {&amp;lt;i,j&amp;gt; in ARCS}&amp;gt;=0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; var Flow_Fixed {&amp;lt;i,j&amp;gt; in ARCS} Binary;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; min obj = sum {&amp;lt;i,j&amp;gt; in ARCS} (_cost_[i,j] * Flow[i,j] + FIXED_COST[i,j]*FIXED_QTY[i,j]*Flow_Fixed[i,j]);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; con balance {i in NODES}: sum {&amp;lt;(i),j&amp;gt; in ARCS} (Flow[i,j] + FIXED_QTY[i,j]*Flow_Fixed[i,j])&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - sum {&amp;lt;j,(i)&amp;gt; in ARCS} (Flow[j,i] + FIXED_QTY[j,i]*Flow_Fixed[j,i]) &lt;STRONG style="color: #ff0000;"&gt;&amp;lt;&lt;/STRONG&gt;= _sd_&lt;I&gt;; * don't need to take all supply;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #ff0000;"&gt;con Lower_Upper_Flow {&amp;lt;i,j&amp;gt; in ARCS}:Lower[i,j]&amp;lt;=Flow[i,j]+Flow_Fixed[i,j]*FIXED_QTY[i,j]&amp;lt;=Upper[i,j]; * is this ok?;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; * or would something like this be better? and why?;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; * -----------------------------------------------------------------;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; * num excess = sum {i in NODES} _sd_&lt;I&gt;;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; *if (excess &amp;gt; 0) then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * change equality constraint to le constraint for supply nodes;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * for {i in NODES: _sd_&lt;I&gt; &amp;gt; 0} balance&lt;I&gt;.lb = -infinity;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; *end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; *else if (excess &amp;lt; 0) then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; * change equality constraint to ge constraint for demand nodes;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *for {i in NODES: _sd_&lt;I&gt; &amp;lt; 0} balance&lt;I&gt;.ub = infinity;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; *end;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Con Con_Binary {&amp;lt;i,j&amp;gt; in ARCS}:Flow[i,j]&amp;lt;=&amp;amp;Fixed_Const.*Flow_Fixed[i,j]; * Flow has to be at least Fixed_Const (or nothing);&lt;BR /&gt; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; solve;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Print obj; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; Print Flow Flow_Fixed;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&amp;amp;kind regards&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 17 Aug 2015 14:34:43 GMT</pubDate>
    <dc:creator>user24feb</dc:creator>
    <dc:date>2015-08-17T14:34:43Z</dc:date>
    <item>
      <title>Properly add binary variable (to examples like 5.18)</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Properly-add-binary-variable-to-examples-like-5-18/m-p/200399#M1028</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am playing around with the optmodel-network-examples a bit, and would like to add a binary variable. This is, you have to ship a fixed minimum amount from the sources to the sinks (or ship nothing).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The example below works in principle, but I am worried about the "conditionally optimal" message. The critical restrictions are "balance" (the "&amp;lt;" sign) and "Lower_Upper_Flow". Is the syntax I am using all right? (The rationale I think is.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let Fixed_Const=1e6;&lt;/P&gt;&lt;P&gt;data garcs;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; input _from_ $ _to_ $ _cost_ FIXED_QTY FIXED_COST Lower Upper;&lt;BR /&gt;datalines;&lt;BR /&gt;s1 d1 1 20 10 0 150&lt;BR /&gt;s1 d2 8 20 70 0 150&lt;BR /&gt;s2 d1 4 20 50 0 150&lt;BR /&gt;s2 d2 2 20 20 0 150&lt;BR /&gt;s2 d3 1 20 10 0 150&lt;BR /&gt;s3 d2 5 20 50 0 150&lt;BR /&gt;s3 d3 4 20 40 0 150&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data gnodes;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; input _node_ $ _sd_ ;&lt;BR /&gt;datalines;&lt;BR /&gt;s1&amp;nbsp; 53&lt;BR /&gt;s2&amp;nbsp; 21&lt;BR /&gt;s3&amp;nbsp; 73&lt;BR /&gt;d1&amp;nbsp; -52&lt;BR /&gt;d2&amp;nbsp; -16&lt;BR /&gt;d3&amp;nbsp; -23&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set &amp;lt;str&amp;gt; NODES;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num _sd_ {NODES} init 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; read data gnodes into NODES=[_node_] _sd_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set &amp;lt;str,str&amp;gt; ARCS;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num Lower {ARCS} init 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num Upper {ARCS};&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num _capac_ {ARCS} init .;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num _cost_ {ARCS};&lt;BR /&gt;&amp;nbsp;&amp;nbsp; num FIXED_QTY {ARCS}, FIXED_COST {ARCS};&lt;BR /&gt;&amp;nbsp;&amp;nbsp; read data garcs nomiss into ARCS=[_from_ _to_] _cost_ FIXED_QTY FIXED_COST Lower Upper;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Print FIXED_QTY;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; NODES = NODES union (union {&amp;lt;i,j&amp;gt; in ARCS} {i,j});&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; var Flow {&amp;lt;i,j&amp;gt; in ARCS}&amp;gt;=0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; var Flow_Fixed {&amp;lt;i,j&amp;gt; in ARCS} Binary;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; min obj = sum {&amp;lt;i,j&amp;gt; in ARCS} (_cost_[i,j] * Flow[i,j] + FIXED_COST[i,j]*FIXED_QTY[i,j]*Flow_Fixed[i,j]);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; con balance {i in NODES}: sum {&amp;lt;(i),j&amp;gt; in ARCS} (Flow[i,j] + FIXED_QTY[i,j]*Flow_Fixed[i,j])&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - sum {&amp;lt;j,(i)&amp;gt; in ARCS} (Flow[j,i] + FIXED_QTY[j,i]*Flow_Fixed[j,i]) &lt;STRONG style="color: #ff0000;"&gt;&amp;lt;&lt;/STRONG&gt;= _sd_&lt;I&gt;; * don't need to take all supply;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #ff0000;"&gt;con Lower_Upper_Flow {&amp;lt;i,j&amp;gt; in ARCS}:Lower[i,j]&amp;lt;=Flow[i,j]+Flow_Fixed[i,j]*FIXED_QTY[i,j]&amp;lt;=Upper[i,j]; * is this ok?;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; * or would something like this be better? and why?;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; * -----------------------------------------------------------------;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; * num excess = sum {i in NODES} _sd_&lt;I&gt;;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; *if (excess &amp;gt; 0) then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * change equality constraint to le constraint for supply nodes;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * for {i in NODES: _sd_&lt;I&gt; &amp;gt; 0} balance&lt;I&gt;.lb = -infinity;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; *end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; *else if (excess &amp;lt; 0) then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; * change equality constraint to ge constraint for demand nodes;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *for {i in NODES: _sd_&lt;I&gt; &amp;lt; 0} balance&lt;I&gt;.ub = infinity;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; *end;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Con Con_Binary {&amp;lt;i,j&amp;gt; in ARCS}:Flow[i,j]&amp;lt;=&amp;amp;Fixed_Const.*Flow_Fixed[i,j]; * Flow has to be at least Fixed_Const (or nothing);&lt;BR /&gt; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; solve;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Print obj; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; Print Flow Flow_Fixed;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&amp;amp;kind regards&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Aug 2015 14:34:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Properly-add-binary-variable-to-examples-like-5-18/m-p/200399#M1028</guid>
      <dc:creator>user24feb</dc:creator>
      <dc:date>2015-08-17T14:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: Properly add binary variable (to examples like 5.18)</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Properly-add-binary-variable-to-examples-like-5-18/m-p/200400#M1029</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Arbitrarily large numbers like 1e6 often introduce numerical instability, and that is why you are seeing conditionally optimal.&amp;nbsp; But your formulation also does not quite match your problem description.&amp;nbsp; If I understand correctly, the following does what you want:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; var Flow {&amp;lt;i,j&amp;gt; in ARCS} &amp;gt;= 0 &amp;lt;= Upper[i,j];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; var Flow_Fixed {ARCS} Binary;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; min obj = sum {&amp;lt;i,j&amp;gt; in ARCS} (_cost_[i,j] * Flow[i,j] + FIXED_COST[i,j]*Flow_Fixed[i,j]);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; con balance {i in NODES}: sum {&amp;lt;(i),j&amp;gt; in ARCS} Flow[i,j]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - sum {&amp;lt;j,(i)&amp;gt; in ARCS} Flow[j,i] &amp;lt;= _sd_&lt;I&gt;; * don't need to take all supply;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; con Upper_Flow {&amp;lt;i,j&amp;gt; in ARCS}: Flow[i,j] &amp;lt;= Flow[i,j].ub * Flow_Fixed[i,j];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Con Con_Binary {&amp;lt;i,j&amp;gt; in ARCS}: Flow[i,j] &amp;gt;= FIXED_QTY[i,j] * Flow_Fixed[i,j]; * Flow has to be at least FIXED_QTY[i,j] (or nothing);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The resulting optimal solution has Flow[s1,d1] = 52, Flow[s2,d2] = 20, and Flow[s3,d3] = 23, for a total cost of 254.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that you do not need Lower or _capac_ because you don't use them anywhere.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See also this related doc example that models fixed charges in a similar way:&lt;/P&gt;&lt;P&gt;&lt;A class="active_link" href="http://support.sas.com/documentation/cdl/en/ormpug/68156/HTML/default/viewer.htm#ormpug_milpsolver_examples02.htm" title="http://support.sas.com/documentation/cdl/en/ormpug/68156/HTML/default/viewer.htm#ormpug_milpsolver_examples02.htm"&gt;SAS/OR(R) 14.1 User's Guide: Mathematical Programming&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Finally, you might also be interested in this book of examples, especially Depot Location (Distribution 2) and Car Rental 2:&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/ormpex/68157/HTML/default/viewer.htm#titlepage.htm" title="http://support.sas.com/documentation/cdl/en/ormpex/68157/HTML/default/viewer.htm#titlepage.htm"&gt;SAS/OR(R) 14.1 User's Guide: Mathematical Programming Examples&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Aug 2015 15:12:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Properly-add-binary-variable-to-examples-like-5-18/m-p/200400#M1029</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2015-08-17T15:12:10Z</dc:date>
    </item>
  </channel>
</rss>

