<?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: Assign object to a place constrained to totals in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/Assign-object-to-a-place-constrained-to-totals/m-p/842370#M3815</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114"&gt;@ciro&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an elementary approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Expand list of places */

data temp / view=temp;
call streaminit('MT64',27182818);
set have2;
do n=1 to n;
  r=rand('uniform');
  output;
end;
run;

/* Sort list of places randomly within groups */

proc sort data=temp out=places(keep=place);
by group r;
run;

/* Assign objects to groups (HAVE1 must be sorted by GROUP!) */

data want;
merge have1 places;
run;

/* Optional: Verify the constraint */

proc freq data=want noprint;
tables place*group / out=chk(drop=percent) sparse;
run;

proc compare data=have2(rename=(n=count)) c=chk method=absolute;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that 20 of the 48 values of variable N in dataset HAVE2 are not exact integers (but close enough so that the DO loop in the first step works correctly).&lt;/P&gt;</description>
    <pubDate>Thu, 03 Nov 2022 17:21:07 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2022-11-03T17:21:07Z</dc:date>
    <item>
      <title>Assign object to a place constrained to totals</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Assign-object-to-a-place-constrained-to-totals/m-p/842318#M3813</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am not sure whether this is the right forum.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to assign to each object in dataset 'have1' a place of dataset 'have2' such that in the output dataset, the total number of place x group is equal to the variable 'n' in dataset 'have2'.&lt;/P&gt;
&lt;P&gt;for each object the assignation should be random within the given constraint.&lt;/P&gt;
&lt;P&gt;the output dataset should have the same number of observations of dataset 'have1' and be like:&lt;/P&gt;
&lt;P&gt;object group place&lt;/P&gt;
&lt;P&gt;172&amp;nbsp; &amp;nbsp; IF&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;173&amp;nbsp; &amp;nbsp; IF&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;........&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any suggestion, and especially code, is greatly appreciated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even more appreciated would be different options (OR, datasetp, IML...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you very much in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 13:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Assign-object-to-a-place-constrained-to-totals/m-p/842318#M3813</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2022-11-03T13:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: Assign object to a place constrained to totals</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Assign-object-to-a-place-constrained-to-totals/m-p/842348#M3814</link>
      <description>&lt;P&gt;Here's one approach that uses the MILP solver in PROC OPTMODEL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
   /* declare parameters and read data */
   set OBJECTS;
   str group {OBJECTS};
   read data have1 into OBJECTS=[object] group;

   set &amp;lt;str,num&amp;gt; GROUP_PLACE;
   num n {GROUP_PLACE};
   read data have2 into GROUP_PLACE=[group place] n;

   set OBJECT_GROUP_PLACE = {o in OBJECTS, &amp;lt;g,p&amp;gt; in GROUP_PLACE: group[o] = g};

   /* X[o,g,p] = 1 if object o is assigned to group g and place p; 0 otherwise */
   var X {OBJECT_GROUP_PLACE} binary;

   /* assign each object to exactly one group-place */
   con AssignOnce {o in OBJECTS}:
      sum {&amp;lt;(o),g,p&amp;gt; in OBJECT_GROUP_PLACE} X[o,g,p] = 1;

   /* assign correct number of objects to each group-place */
   con Cardinality {&amp;lt;g,p&amp;gt; in GROUP_PLACE}:
      sum {&amp;lt;o,(g),(p)&amp;gt; in OBJECT_GROUP_PLACE} X[o,g,p] = n[g,p];

   /* call MILP solver with no objective */
   solve noobj;

   /* create output data set */
   create data want from [object group place]={&amp;lt;o,g,p&amp;gt; in OBJECT_GROUP_PLACE: X[o,g,p].sol &amp;gt; 0.5};
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you had some measure of desirability of placing object o in group g and place p, you could instead maximize the total desirability rather than assigning arbitrarily.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 17:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Assign-object-to-a-place-constrained-to-totals/m-p/842348#M3814</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2022-11-03T17:16:56Z</dc:date>
    </item>
    <item>
      <title>Re: Assign object to a place constrained to totals</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Assign-object-to-a-place-constrained-to-totals/m-p/842370#M3815</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114"&gt;@ciro&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an elementary approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Expand list of places */

data temp / view=temp;
call streaminit('MT64',27182818);
set have2;
do n=1 to n;
  r=rand('uniform');
  output;
end;
run;

/* Sort list of places randomly within groups */

proc sort data=temp out=places(keep=place);
by group r;
run;

/* Assign objects to groups (HAVE1 must be sorted by GROUP!) */

data want;
merge have1 places;
run;

/* Optional: Verify the constraint */

proc freq data=want noprint;
tables place*group / out=chk(drop=percent) sparse;
run;

proc compare data=have2(rename=(n=count)) c=chk method=absolute;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that 20 of the 48 values of variable N in dataset HAVE2 are not exact integers (but close enough so that the DO loop in the first step works correctly).&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 17:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Assign-object-to-a-place-constrained-to-totals/m-p/842370#M3815</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-11-03T17:21:07Z</dc:date>
    </item>
  </channel>
</rss>

