<?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: Find numbers whose summation is specified using proc optmodel in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934340#M4175</link>
    <description>&lt;P&gt;Hello Rob,&lt;/P&gt;&lt;P&gt;How to create the result as a dataset including variable x and UseItem other than just printing them out?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 01 Jul 2024 20:26:42 GMT</pubDate>
    <dc:creator>t75wez1</dc:creator>
    <dc:date>2024-07-01T20:26:42Z</dc:date>
    <item>
      <title>Find numbers whose summation is specified using proc optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934230#M4171</link>
      <description>&lt;P&gt;I think is an OR question.&lt;/P&gt;
&lt;P&gt;Assume there are 18 numbers:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tab1;
  input x;
  cards;
22.57
62.43
56.01
52.71
24.44
20.34
16
14.73
17.52
20.15
22.76
18.59
30.14
14.63
23.13
11
26.39
149.18
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is the combination of these numbers whose summation is most close to 500?&lt;/P&gt;
&lt;P&gt;That is to say, you are asked to find out combinations of these numbers, to make the summation of these numbers as close to 500 as possible.&lt;/P&gt;
&lt;P&gt;Here is what I do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n=18;
%let target=500;

data tab2;
  array d[&amp;amp;n.]_temporary_;
  array nx[&amp;amp;n.];
  do i=1 by 1 until(eof);
    set tab1 end=eof;
    d[i]=x;
  end;

  do i=1 to dim(d);
    ncomb=comb(dim(d),i);
    do j=1 to ncomb;
      call allcomb(j,i,of d[*]);    *All Combinations;

      *Calculate sum and distance to target of this combination;
      sum=0;
      do k=1 to i;
        sum+d[k];
      end;
      abs_dif=abs(sum-&amp;amp;target.);
    
      *Get minimum distance to target and responding combination;
      if abs_dif&amp;lt;min_abs_dif or min_abs_dif=. then do;
        min_abs_dif=abs_dif;
        do k=1 to i;
          nx[k]=d[k];
        end;
      end;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;The result is located at array NX.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question is: How to slove this kind of problem(maybe it is &lt;I data-bm="181"&gt;&lt;I class="01"&gt;Knapsack&lt;/I&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I class="01"&gt;problem&lt;/I&gt;&lt;/I&gt;) using proc optmodel? Well, an&amp;nbsp;&lt;SPAN&gt;elegant&amp;nbsp;&lt;/SPAN&gt;data step method is welcomed, too.&lt;/P&gt;
&lt;P&gt;PS: I have checked&amp;nbsp;&lt;I data-bm="181"&gt;&lt;I class="01"&gt;Knapsack&amp;nbsp;&lt;/I&gt;&lt;I class="01"&gt;problem&amp;nbsp;&lt;/I&gt;&lt;/I&gt;example in&amp;nbsp;SAS doc, but it is too complex to understand.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2024 09:04:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934230#M4171</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2024-07-01T09:04:24Z</dc:date>
    </item>
    <item>
      <title>Re: Find numbers whose summation is specified using proc optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934241#M4172</link>
      <description>&lt;P&gt;It's called the&amp;nbsp;&lt;STRONG&gt;Subset Sum Problem&lt;/STRONG&gt;!&lt;/P&gt;
&lt;UL class="lia-list-style-type-square"&gt;
&lt;LI&gt;A brute force solution using IML ...&lt;BR /&gt;Home &amp;gt; Analytics &amp;gt; SAS/IML &amp;gt; Subset Sum Problem&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/td-p/227127" target="_blank"&gt;https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/td-p/227127&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;What is the Subset Sum Problem?&lt;BR /&gt;&lt;A href="https://www.cs.dartmouth.edu/~ac/Teach/CS105-Winter05/Notes/nanda-scribe-3.pdf" target="_blank"&gt;https://www.cs.dartmouth.edu/~ac/Teach/CS105-Winter05/Notes/nanda-scribe-3.pdf&lt;/A&gt;&lt;BR /&gt;(it's indeed a special case of the Knapsack problem.)&lt;/LI&gt;
&lt;LI&gt;I cannot give you the dynamic programming (DP) solution using PROC OPTMODEL , but I moved this topic to the appropriate board (where you will get a solution I believe).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2024 09:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934241#M4172</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2024-07-01T09:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: Find numbers whose summation is specified using proc optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934266#M4173</link>
      <description>&lt;P&gt;Here are two ways, both using the MILP solver through PROC OPTMODEL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* automated linearization in SAS Viya */
proc optmodel;
   set ITEMS;
   num x {ITEMS};
   read data tab1 into ITEMS=[_N_] x;

   var UseItem {ITEMS} binary;

   min Error = abs(sum {j in ITEMS} x[j]*UseItem[j] - &amp;amp;target);

   solve linearize;
   print x UseItem;
quit;


/* manual linearization */
proc optmodel;
   set ITEMS;
   num x {ITEMS};
   read data tab1 into ITEMS=[_N_] x;

   var UseItem {ITEMS} binary;

   var Over  &amp;gt;= 0;
   var Under &amp;gt;= 0;
   min Error = Over + Under;
   con sum {j in ITEMS} x[j]*UseItem[j] - Over + Under = &amp;amp;target;

   solve;
   print x UseItem;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Both yield optimal objective value 0 with the following solution:&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Optmodel: Print Table" frame="box" rules="all" cellspacing="0" cellpadding="5"&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;x&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;UseItem&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="r data"&gt;22.57&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="r data"&gt;62.43&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;3&lt;/TH&gt;
&lt;TD class="r data"&gt;56.01&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;4&lt;/TH&gt;
&lt;TD class="r data"&gt;52.71&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;5&lt;/TH&gt;
&lt;TD class="r data"&gt;24.44&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;6&lt;/TH&gt;
&lt;TD class="r data"&gt;20.34&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;7&lt;/TH&gt;
&lt;TD class="r data"&gt;16.00&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;8&lt;/TH&gt;
&lt;TD class="r data"&gt;14.73&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;9&lt;/TH&gt;
&lt;TD class="r data"&gt;17.52&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;10&lt;/TH&gt;
&lt;TD class="r data"&gt;20.15&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;11&lt;/TH&gt;
&lt;TD class="r data"&gt;22.76&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;12&lt;/TH&gt;
&lt;TD class="r data"&gt;18.59&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;13&lt;/TH&gt;
&lt;TD class="r data"&gt;30.14&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;14&lt;/TH&gt;
&lt;TD class="r data"&gt;14.63&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;15&lt;/TH&gt;
&lt;TD class="r data"&gt;23.13&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;16&lt;/TH&gt;
&lt;TD class="r data"&gt;11.00&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;17&lt;/TH&gt;
&lt;TD class="r data"&gt;26.39&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="l rowheader" scope="row"&gt;18&lt;/TH&gt;
&lt;TD class="r data"&gt;149.18&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 01 Jul 2024 14:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934266#M4173</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2024-07-01T14:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: Find numbers whose summation is specified using proc optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934340#M4175</link>
      <description>&lt;P&gt;Hello Rob,&lt;/P&gt;&lt;P&gt;How to create the result as a dataset including variable x and UseItem other than just printing them out?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2024 20:26:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934340#M4175</guid>
      <dc:creator>t75wez1</dc:creator>
      <dc:date>2024-07-01T20:26:42Z</dc:date>
    </item>
    <item>
      <title>Re: Find numbers whose summation is specified using proc optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934343#M4176</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   create data want from [item] x UseItem;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jul 2024 20:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-numbers-whose-summation-is-specified-using-proc-optmodel/m-p/934343#M4176</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2024-07-01T20:51:05Z</dc:date>
    </item>
  </channel>
</rss>

