<?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 Re: Defining a sub problem in proc Optmodel that is based on the aggregated solution in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763880#M3479</link>
    <description>&lt;P&gt;so, I may be misunderstanding how this works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The values in the soluion Matrix X[s,j] are just the binary matrix of values which correspond to the szone choice in matrix a? It appears that this solution assumes that the columns in X correspond to szones? when the columns in X are just representative of the variable order in a. So I was trying to figure out how summing X[s,j] is counting the distinct restaurant choices as demonstrated in the excel counting method as shown in the pic below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASAlex101_0-1629904992232.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62992iBE28046C94155A69/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASAlex101_0-1629904992232.png" alt="SASAlex101_0-1629904992232.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I think I can visualize how the szonelimit[szone[s]] is working. Seems to be a lookup based on the limit matrix we defined earlier.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks Rob&lt;/P&gt;</description>
    <pubDate>Wed, 25 Aug 2021 15:23:56 GMT</pubDate>
    <dc:creator>SASAlex101</dc:creator>
    <dc:date>2021-08-25T15:23:56Z</dc:date>
    <item>
      <title>Defining a sub problem in proc Optmodel that is based on the aggregated solution</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763783#M3477</link>
      <description>&lt;P&gt;I have code in proc optmodel that outputs a solution to a minimization problem. The solution matrix is a binary matrix which represents a column choice of restaurants szone value that best matches the ID's (first column s 1,2,3...) szone value. The following SAS code is where I am now.&amp;nbsp;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.RestaurantsBySzone;
Infile datalines delimiter="#";
length Restaurant $100.;
input Restaurant $ szone;
datalines;
McDonalds#1
Subway#1
Tim Hortons#1
Starbucks#1
KFC#2
Wendys#2
Taco Bell#2
Pizza Hut#2
Ruby Tuesday#3
Whataburger#3
Burger King#3
IHOP#3
;
run;
data WORK.OPEN_F1_FINAL;
Infile datalines delimiter='#';
input
S
szone
Var1
Var2
Var3
;
datalines;
1#3#2#2#3
2#1#2#3#3
3#2#2#1#1
4#2#3#1#3
5#3#1#1#2
6#1#3#1#3
7#2#3#1#1
8#3#3#3#1
9#1#3#2#2
10#2#1#2#2
11#2#3#2#6
12#2#2#2#2
13#2#2#2#1
14#1#2#1#3
15#1#1#2#3
16#3#2#2#1
17#3#1#1#3
18#1#1#3#1
19#1#3#2#3
20#1#3#1#3
;
Run;

data WORK.CoorespondingRestaurants;
Length	
Var1	$100.
Var2	$100.
Var3	$100.;
Infile datalines delimiter='#';
input
S
szone
Var1 $
Var2 $
Var3 $
;
datalines;
1#3#Pizza Hut#KFC#Ruby Tuesday
2#1#Pizza Hut#IHOP#Whataburger
3#2#Taco Bell#McDonalds#Subway
4#2#Burger King#McDonalds#Ruby Tuesday
5#3#Starbucks#Tim Hortons#Taco Bell
6#1#Whataburger#Subway#Burger King
7#2#Burger King#Subway#Tim Hortons
8#3#IHOP#Whataburger#Starbucks
9#1#IHOP#Pizza Hut#KFC
10#2#Starbucks#Pizza Hut#KFC
11#2#IHOP#Wendys#
12#2#Pizza Hut#Wendys#Taco Bell
13#2#Taco Bell#KFC#Tim Hortons
14#1#Pizza Hut#Subway#Ruby Tuesday
15#1#Starbucks#Taco Bell#Burger King
16#3#Pizza Hut#KFC#Starbucks
17#3#Starbucks#Subway#Whataburger
18#1#McDonalds#Burger King#Starbucks
19#1#Whataburger#Wendys#Burger King
20#1#Whataburger#Subway#IHOP
;
Run;
PROC SQL;
   CREATE TABLE WORK.CoorespondingRestaurants AS 
   SELECT t1.S, 
          t1.szone, 
          t1.Var1, 
          t1.Var2, 
          t1.Var3
      FROM WORK.COORESPONDINGRESTAURANTS t1;
QUIT;

proc optmodel;
ods output PrintTable#4=F1_Solution;
set SSET;
set JSET = 1..3;
num a {SSET, JSET};
num szone {SSET};

read data OPEN_F1_FINAL into SSET=[s] {j in JSET} &amp;lt;a[s,j] = col('Var'||(j))&amp;gt; szone;

var X {SSET, JSET} binary;
var Abs {SSET};

min Objective = (sum {s in SSET} Abs[s]) / (sum {s in SSET} szone[s]);

constraint OnceChoice {s in SSET}: sum {j in JSET} X[s,j] = 1;
constraint AbsCon1 {s in SSET}: Abs[s] &amp;gt;= sum {j in JSET} a[s,j]*X[s,j] - szone[s];
constraint AbsCon2 {s in SSET}: Abs[s] &amp;gt;= -sum {j in JSET} a[s,j]*X[s,j] + szone[s];

solve;

print a szone;
print Objective;
print X;

quit;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the solution matrix:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASAlex101_4-1629866980883.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62975iDCC033EA9EB3293B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASAlex101_4-1629866980883.png" alt="SASAlex101_4-1629866980883.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can be translated to: for S = 1 the best matched restaurant is 'Ruby Tuesday', and so forth for the other rows which can be determined by pairing the corresponding column with a 1 to the&amp;nbsp;COORESPONDINGRESTAURANTS table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The subproblem is that I want to apply a limitation to how many of each restaurant can be chosen during the optimization. i.e I don't want the optimizer to choose&amp;nbsp;McDonalds or KFC more often than another restaurant of the same szone based on the rule that&lt;/P&gt;&lt;P&gt;an szone 1 restaurant can only be chosen at most once in the solution&lt;/P&gt;&lt;P&gt;an szone 2 restaurant can only be chosen 2 times in the solution&lt;/P&gt;&lt;P&gt;an szone 3 restaurant can only be chosed at most 2 times in the solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are 3 szones and 4 restaurants in each szone as shown in the table RESTAURANTBYSZONE.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in excel, I would count using countifs or something the number of time a restaurant has been chosen and compare that to my szone rules:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;szone&lt;/TD&gt;&lt;TD&gt;numof choices in solution&lt;/TD&gt;&lt;TD&gt;comment&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;McDonalds&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;WRONG - too many&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Subway&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;WRONG - too many&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&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R&gt;&lt;TD&gt;IHOP&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;OK&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;on lines 2 and 3 above) Perhaps the optimizer could have chosen Tim Hortons instead of Subway when it had the choice of szone 1's. How does this correspond to a subproblem in my current proc optmodel?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Aug 2021 05:02:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763783#M3477</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2021-08-25T05:02:49Z</dc:date>
    </item>
    <item>
      <title>Re: Defining a sub problem in proc Optmodel that is based on the aggregated solution</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763880#M3479</link>
      <description>&lt;P&gt;so, I may be misunderstanding how this works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The values in the soluion Matrix X[s,j] are just the binary matrix of values which correspond to the szone choice in matrix a? It appears that this solution assumes that the columns in X correspond to szones? when the columns in X are just representative of the variable order in a. So I was trying to figure out how summing X[s,j] is counting the distinct restaurant choices as demonstrated in the excel counting method as shown in the pic below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASAlex101_0-1629904992232.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62992iBE28046C94155A69/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASAlex101_0-1629904992232.png" alt="SASAlex101_0-1629904992232.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I think I can visualize how the szonelimit[szone[s]] is working. Seems to be a lookup based on the limit matrix we defined earlier.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks Rob&lt;/P&gt;</description>
      <pubDate>Wed, 25 Aug 2021 15:23:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763880#M3479</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2021-08-25T15:23:56Z</dc:date>
    </item>
    <item>
      <title>Re: Defining a sub problem in proc Optmodel that is based on the aggregated solution</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763923#M3480</link>
      <description>&lt;P&gt;Sorry for the false start, which I have now deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can omit the PROC SQL call, which doesn't change anything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add this DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data szoneData;
   input szone szoneLimit;
   datalines;
1 1
2 2
3 2
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Then add these OPTMODEL statements before the solver call:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   set &amp;lt;str&amp;gt; RESTAURANTS;
   num szone_r {RESTAURANTS};
   read data RestaurantsBySzone into RESTAURANTS=[Restaurant] szone_r=szone;

   str restaurant_sj {SSET, JSET};
   read data CoorespondingRestaurants into [s] {j in JSET} &amp;lt;restaurant_sj[s,j] = col('Var'||(j))&amp;gt;;

   set SZONES;
   num szoneLimit {SZONES};
   read data szoneData into SZONES=[szone] szoneLimit;

   con SzoneCon {r in RESTAURANTS}:
      sum {s in SSET, j in JSET: restaurant_sj[s,j] = r} X[s,j] &amp;lt;= szoneLimit[szone_r[r]];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;After the solve, you can verify that the new constraint is satisfied by adding this statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   print SzoneCon.body SzoneCon.ub;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;On my machine, the results are:&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Optmodel: PrintTable" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt;&lt;/COLGROUP&gt; &lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l b header" scope="col"&gt;[1]&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;SzoneCon.BODY&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;SzoneCon.UB&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Burger King&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;IHOP&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;KFC&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;McDonalds&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Pizza Hut&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Ruby Tuesday&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Starbucks&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Subway&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Taco Bell&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Tim Hortons&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Wendys&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;Whataburger&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 25 Aug 2021 17:23:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763923#M3480</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2021-08-25T17:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: Defining a sub problem in proc Optmodel that is based on the aggregated solution</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763935#M3481</link>
      <description>&lt;P&gt;ok I see. the call for&amp;nbsp;&lt;BR /&gt;sum {s in SSET, j in JSET: restaurant_sj[s,j] = r} X[s,j]&lt;/P&gt;&lt;P&gt;is equivalent to the COUNTIFS like in excel - taking the choice of Szone as indicated by X.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;elegant solution. The problem becomes more fickle if I play with the limit per szone in the szonedata; but, this definitely answers the question.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks Rob!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Aug 2021 17:47:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Defining-a-sub-problem-in-proc-Optmodel-that-is-based-on-the/m-p/763935#M3481</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2021-08-25T17:47:58Z</dc:date>
    </item>
  </channel>
</rss>

