<?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: solve optimization problem for each row of a dataset in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814909#M3713</link>
    <description>&lt;P&gt;This might help get you started with the syntax.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first row is infeasible due to the constraint. If all three decision variables are at their upper bounds, 10*y1 + 3*y2 + 5*y3 equals 188, less than the y value of 220. The solution status column in the output data set captures this information for all rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
/* declare an index set */
set &amp;lt;num&amp;gt; ROWS;

/* declare parameters for each row in the set */
num x1{ROWS};
num x2{ROWS};
num x3{ROWS};
num y{ROWS};
num y1g{ROWS};
num y2g{ROWS};
num y3g{ROWS};

/* read the have data set and parameters into OPTMODEL to populate */
read data have into ROWS=[id] x1 x2 x3 y y1g y2g y3g;

/* iteration scalar to loop through and solve each row */
num i;

/* decision variables for each row */
var y1{ROWS} &amp;gt;= 0 &amp;lt;= 12;
var y2{ROWS} &amp;gt;= 0 &amp;lt;= 6;
var y3{ROWS} &amp;gt;= 0 &amp;lt;= 10;

/* declare and later populate dec variables with optimal values */
num y1sol{ROWS};
num y2sol{ROWS};
num y3sol{ROWS};

/* declare and later populate solution status of the run */
str solstatus {ROWS};

con Equality: x1[i]*y1[i] + x2[i]*y2[i] + x3[i]*y3[i] = y[i];

min Obj = (y1[i]-y1g[i])**2+(y2[i]-y2g[i])**2+(y3[i]-y3g[i])**2;

/* loop to solve for each row */
do i = 1 to card(ROWS);
 solve;
  *print y1[i] y2[i] y3[i];
   y1sol[i] = y1[i].sol;
   y2sol[i] = y2[i].sol;
   y3sol[i] = y3[i].sol;
   solstatus[i] = _solution_status_;
end;

/* create output data set */
create data want from [id]=ROWS 
   x1 x2 x3 y y1g y2g y3g y1sol y2sol y3sol solstatus;
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 24 May 2022 16:30:55 GMT</pubDate>
    <dc:creator>ChanceTGardener</dc:creator>
    <dc:date>2022-05-24T16:30:55Z</dc:date>
    <item>
      <title>solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814878#M3710</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a dataset like the following:&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;id=1; x1=10; x2=3; x3=5; y=220;&lt;BR /&gt;y1g=12; y2g=6; y3g=10;&lt;BR /&gt;output;&lt;BR /&gt;id=2; x1=7; x2=0; x3=3; y=100;&lt;BR /&gt;y1g=12; y2g=0; y3g=10;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for each record I want to find values for y1,y2,y3&lt;/P&gt;
&lt;P&gt;such that&lt;/P&gt;
&lt;P&gt;x1*y1+x2*y2+x3*y3=y&lt;/P&gt;
&lt;P&gt;0=&amp;lt;y1&amp;lt;=12&lt;/P&gt;
&lt;P&gt;0=&amp;lt;y2&amp;lt;=6&lt;/P&gt;
&lt;P&gt;0=&amp;lt;y3&amp;lt;=10&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure of which can be the objective function, but to fix the ideas&lt;/P&gt;
&lt;P&gt;I might want that the solutions are as similar as possible to some initial guess, y1g,y2g,y3g.&lt;/P&gt;
&lt;P&gt;so for instance the objective might be:&lt;/P&gt;
&lt;P&gt;min (y1-y1g)**2+(y2-y2g)**2+(y3-y3g)**2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using SAS9 M5 and have to apply the program to a dataset of about 100000 records.&lt;/P&gt;
&lt;P&gt;I am very new to sas/or but wishing to learn it at least a little.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you very much in advance for any help or suggestions&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 May 2022 14:21:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814878#M3710</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2022-05-24T14:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814886#M3711</link>
      <description>&lt;P&gt;Sounds like you want to solve a least-squares problem, which is formulated as a quadratic objective function subject to a linear constraint and boundary constraints. &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/ormpug/ormpug_qpsolver_examples.htm" target="_self"&gt;See the documentation for the quadratic programming solver in PROC OPTMODEL&lt;/A&gt;, which contains several examples of how to encode the problem in PROC OPTMODEL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 May 2022 14:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814886#M3711</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-05-24T14:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814887#M3712</link>
      <description>Thank you very much Rick.&lt;BR /&gt;I tried to learn by my self, but a bit in a hurry.&lt;BR /&gt;I would appreciate if you could suggest me the code.&lt;BR /&gt;thank you in advance</description>
      <pubDate>Tue, 24 May 2022 14:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814887#M3712</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2022-05-24T14:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814909#M3713</link>
      <description>&lt;P&gt;This might help get you started with the syntax.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first row is infeasible due to the constraint. If all three decision variables are at their upper bounds, 10*y1 + 3*y2 + 5*y3 equals 188, less than the y value of 220. The solution status column in the output data set captures this information for all rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
/* declare an index set */
set &amp;lt;num&amp;gt; ROWS;

/* declare parameters for each row in the set */
num x1{ROWS};
num x2{ROWS};
num x3{ROWS};
num y{ROWS};
num y1g{ROWS};
num y2g{ROWS};
num y3g{ROWS};

/* read the have data set and parameters into OPTMODEL to populate */
read data have into ROWS=[id] x1 x2 x3 y y1g y2g y3g;

/* iteration scalar to loop through and solve each row */
num i;

/* decision variables for each row */
var y1{ROWS} &amp;gt;= 0 &amp;lt;= 12;
var y2{ROWS} &amp;gt;= 0 &amp;lt;= 6;
var y3{ROWS} &amp;gt;= 0 &amp;lt;= 10;

/* declare and later populate dec variables with optimal values */
num y1sol{ROWS};
num y2sol{ROWS};
num y3sol{ROWS};

/* declare and later populate solution status of the run */
str solstatus {ROWS};

con Equality: x1[i]*y1[i] + x2[i]*y2[i] + x3[i]*y3[i] = y[i];

min Obj = (y1[i]-y1g[i])**2+(y2[i]-y2g[i])**2+(y3[i]-y3g[i])**2;

/* loop to solve for each row */
do i = 1 to card(ROWS);
 solve;
  *print y1[i] y2[i] y3[i];
   y1sol[i] = y1[i].sol;
   y2sol[i] = y2[i].sol;
   y3sol[i] = y3[i].sol;
   solstatus[i] = _solution_status_;
end;

/* create output data set */
create data want from [id]=ROWS 
   x1 x2 x3 y y1g y2g y3g y1sol y2sol y3sol solstatus;
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 May 2022 16:30:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/814909#M3713</guid>
      <dc:creator>ChanceTGardener</dc:creator>
      <dc:date>2022-05-24T16:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815012#M3714</link>
      <description>Thank you very much  ChanceTGardener.&lt;BR /&gt;I appreciated it a lot and especially the comments that helps me to understand the code.&lt;BR /&gt;Ciro</description>
      <pubDate>Wed, 25 May 2022 12:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815012#M3714</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2022-05-25T12:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815187#M3715</link>
      <description>I have a problem when applying the procedure to my dataset of about 100k observations.&lt;BR /&gt;when I tried with a subset of 10k observations it took about 6 minutes.&lt;BR /&gt;so I imagined that 1-2 hours was a sufficient time to process the whole dataset.&lt;BR /&gt;Instead after a night the procedure was not finished.&lt;BR /&gt;I closed all output destination and routed the log with proc printto. &lt;BR /&gt;I also tried to suppress the the notes to log, but with no success.&lt;BR /&gt;Any idea of how can I complete the task? Should I partition the problem, dividing the dataset, or is there a smarter solution?&lt;BR /&gt;Thank you very much in advance</description>
      <pubDate>Thu, 26 May 2022 09:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815187#M3715</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2022-05-26T09:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815235#M3716</link>
      <description>&lt;P&gt;The original code works correctly but has many unused variables (three times the number of rows when you need only three).&amp;nbsp; You will get much better performance by replacing the three VAR statements as shown below.&amp;nbsp; When calling the solver in a loop, I often use PRINTLEVEL=0 and OPTION NONOTES and also a PUT statement to show the progress, as shown.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel printlevel=0;
   /* declare an index set */
   set &amp;lt;num&amp;gt; ROWS;

   /* declare parameters for each row in the set */
   num x1{ROWS};
   num x2{ROWS};
   num x3{ROWS};
   num y{ROWS};
   num y1g{ROWS};
   num y2g{ROWS};
   num y3g{ROWS};

   /* read the have data set and parameters into OPTMODEL to populate */
   read data have into ROWS=[id] x1 x2 x3 y y1g y2g y3g;

   /* iteration scalar to loop through and solve each row */
   num i;

   /* decision variables for each row */
/*   var y1{ROWS} &amp;gt;= 0 &amp;lt;= 12;*/
/*   var y2{ROWS} &amp;gt;= 0 &amp;lt;= 6;*/
/*   var y3{ROWS} &amp;gt;= 0 &amp;lt;= 10;*/
   var y1 &amp;gt;= 0 &amp;lt;= 12;
   var y2 &amp;gt;= 0 &amp;lt;= 6;
   var y3 &amp;gt;= 0 &amp;lt;= 10;

   /* declare and later populate dec variables with optimal values */
   num y1sol{ROWS};
   num y2sol{ROWS};
   num y3sol{ROWS};

   /* declare and later populate solution status of the run */
   str solstatus {ROWS};

/*   con Equality: x1[i]*y1[i] + x2[i]*y2[i] + x3[i]*y3[i] = y[i];*/
   con Equality: x1[i]*y1 + x2[i]*y2 + x3[i]*y3 = y[i];

/*   min Obj = (y1[i]-y1g[i])**2+(y2[i]-y2g[i])**2+(y3[i]-y3g[i])**2;*/
   min Obj = (y1-y1g[i])**2+(y2-y2g[i])**2+(y3-y3g[i])**2;

   /* loop to solve for each row */
   option nonotes;
   do i = 1 to card(ROWS);
      if mod(i,1000) = 0 then put i=;
      solve;
      *print y1[i] y2[i] y3[i];
/*      y1sol[i] = y1[i].sol;*/
/*      y2sol[i] = y2[i].sol;*/
/*      y3sol[i] = y3[i].sol;*/
      y1sol[i] = y1.sol;
      y2sol[i] = y2.sol;
      y3sol[i] = y3.sol;
      solstatus[i] = _solution_status_;
   end;
   option notes;

   /* create output data set */
   create data want from [id]=ROWS 
      x1 x2 x3 y y1g y2g y3g y1sol y2sol y3sol solstatus;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 May 2022 15:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815235#M3716</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2022-05-26T15:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815243#M3717</link>
      <description>&lt;P&gt;And in SAS Viya, you can use the &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_027/casactmopt/cas-optimization-runoptmodel.htm" target="_self"&gt;runOptmodel action&lt;/A&gt; with &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_027/casactmopt/casactmopt_optimization_details05.htm" target="_self"&gt;BY-group processing&lt;/A&gt; to solve the independent instances concurrently, without having to write any explicit loops:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sascas1.have;
   set have;
run;

proc cas;
   action setsessopt / messageLevel="warning";
   run;

   source pgm;
   /* declare parameters for each row in the set */
   num x1;
   num x2;
   num x3;
   num y;
   num y1g;
   num y2g;
   num y3g;

   /* read the have data set and parameters into OPTMODEL to populate */
   read data have into x1 x2 x3 y y1g y2g y3g;

   /* decision variables for each row */
   var y1 &amp;gt;= 0 &amp;lt;= 12;
   var y2 &amp;gt;= 0 &amp;lt;= 6;
   var y3 &amp;gt;= 0 &amp;lt;= 10;

   con Equality: x1*y1 + x2*y2 + x3*y3 = y;

   min Obj = (y1-y1g)**2+(y2-y2g)**2+(y3-y3g)**2;

   /* solve one row */
   solve;

   /* create output data set */
   create data want from 
      x1 x2 x3 y y1g y2g y3g y1sol=y1 y2sol=y2 y3sol=y3 solstatus=_solution_status_;
   endsource;
   action optimization.runOptmodel / code=pgm groupBy={"id"} printlevel=0;
   run;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 May 2022 15:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815243#M3717</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2022-05-26T15:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: solve optimization problem for each row of a dataset</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815404#M3718</link>
      <description>&lt;P&gt;Thank you very much Rob. the code on the entire dataset run in only about 10 minutes!&lt;/P&gt;
&lt;P&gt;The biggest improvement is probably the reduction of the number of variables, However a role is likely to be played also by printlevel=0.&lt;/P&gt;
&lt;P&gt;I realized that without it Sas kept on printing on the html output making it grow continuously and congesting the system.&lt;/P&gt;
&lt;P&gt;The strange thing is that I had on my code: ods html close; and ods html exclude all. So there is something that I misunderstand about these statements.&lt;/P&gt;
&lt;P&gt;However with printlevel=0, sas stopped to print the output.&lt;/P&gt;</description>
      <pubDate>Fri, 27 May 2022 13:03:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/solve-optimization-problem-for-each-row-of-a-dataset/m-p/815404#M3718</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2022-05-27T13:03:33Z</dc:date>
    </item>
  </channel>
</rss>

