<?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: [GA Optimization] Convergence Graph in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/GA-Optimization-Convergence-Graph/m-p/231847#M1146</link>
    <description>&lt;P&gt;Hi Davi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC GA provides a hook for an update routine, called each iteration. &amp;nbsp;You can use this to do things like report the iteration count and current best objective. &amp;nbsp;Below is an example the prints the best objective each iteration to Output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--Josh&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;OPTIONS NODATE NONUMBER;
TITLE "Iteration history";
TITLE "Iteration history output";

proc ga seed = 555;
call SetEncoding('R3R2');
npoints = 3;
array cvxhull[3,2] /nosym ( -2 0
                             0 2
                             2 -2 );
/* Objective function */
function sixhump(selected[*],cvxhull[*,*],npoints);
   /* Function has global minimum value of -1.0316 
    * at x = {-0.0898  0.7126} and  
    *    x = { 0.0898 -0.7126} 
    */ 
   array w[1] /nosym;
   call dynamic_array(w,npoints);
   array x[2] /nosym;
   call ReadMember(selected,1,w);
   /* make sure that weights add up to 1 */
   sum = 0;
   do i = 1 to npoints;
      sum + w[i];
   end;
   /* if all weights 0, then reinitialize */
   if sum=0 then do;
      sum = npoints;
      do i = 1 to npoints;
         w[i] = 1;
      end;
   end;
   /* re-normalize weights */
   do i = 1 to npoints;
      w[i] = w[i] / sum;
   end;
   call WriteMember(selected,1,w);
   /* convert weights to x-coordinate form */
   x[1] = 0;
   x[2] = 0;
   do i = 1 to npoints;
      x[1] + w[i] * cvxhull[i,1];
      x[2] + w[i] * cvxhull[i,2];
   end;
   /* write out x coordinates to second segment */
   call WriteMember(selected,2,x);
   
   /* compute objective value */ 
   r = (4 - 2.1*x[1]**2 + x[1]**4/3)*x[1]**2 + x[1]*x[2] + 
       (-4 + 4*x[2]**2)*x[2]**2;
   return(r);
endsub;
/*************************************/
subroutine gethistory(iteration, maxiter, popsize, iterArray[*], bestArray[*]);
   outargs iteration, iterArray, bestArray;
   iteration = iteration+1; 
   array objValues[1] /nosym;

   /* dynamically allocate array to fit populationSize */
   call dynamic_array(objValues, popsize);

   /* read in current objective values */
   call GetObjValues(objValues, popsize);

   /* find best value */
   current_best = objValues[1];
   do i = 2 to popsize;
     /* for a minimization problem, use &amp;lt; here */
     if(objValues[i] &amp;lt; current_best) then 
        current_best = objValues[i];
   end;
   *put iteration current_best;
   iterArray[iteration]=iteration;
   bestArray[iteration]=current_best; 
   if (iteration = maxiter) then do; 
      put +1; 
      do i = 1 to maxiter;
         put "iter=" iterArray[i] ",  bestObj=" bestArray[i]  +1;
      end; 
   end;
endsub;
/************************************/
call SetObjFunc('sixhump',0);
array lower[1] /nosym;
array upper[1] /nosym;
call dynamic_array(lower, npoints);
call dynamic_array(upper, npoints);
do i = 1 to npoints;
   lower[i] = 0;
   upper[i] = 1;
end;
call SetBounds(lower, upper, 1);
array delta[3] /nosym (0.01 0.01 0.01);
call SetMut('delta', 'nchange', 1, 'delta', delta);
call SetMutProb(0.05);
call SetCross('Twopoint', 'alpha', 0.9);
call SetCrossProb(0.8);
call SetSel('tournament', 'size', 2);
call SetElite(3);
iteration=0;
popsize=20;
maxiter=40;
array iterArray[1] /nosym;
array bestArray[1] /nosym;
call dynamic_array(iterArray, maxiter+1);
call dynamic_array(bestArray, maxiter+1);
call SetUpdateRoutine('gethistory');
call Initialize('DEFAULT', popsize);
call ContinueFor(maxiter); 
run;
 &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Oct 2015 15:45:45 GMT</pubDate>
    <dc:creator>joshgriffin</dc:creator>
    <dc:date>2015-10-27T15:45:45Z</dc:date>
    <item>
      <title>[GA Optimization] Convergence Graph</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/GA-Optimization-Convergence-Graph/m-p/230072#M1128</link>
      <description>&lt;P&gt;Dear Sir or Madam, &lt;BR /&gt; &lt;/P&gt;
&lt;P&gt;I’m using PROC GA as a candidate tool for a model optimization task, going through the procedure documentation I wasn’t able to find any reference for optimization convergence graphs. Is there a way to output the best solution for each optimization step so that I can generate the graph myself?&lt;BR /&gt;Thanks in advance. &lt;BR /&gt; &lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Davi Laraia&lt;/P&gt;</description>
      <pubDate>Thu, 15 Oct 2015 12:58:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/GA-Optimization-Convergence-Graph/m-p/230072#M1128</guid>
      <dc:creator>DaviLaraia</dc:creator>
      <dc:date>2015-10-15T12:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: [GA Optimization] Convergence Graph</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/GA-Optimization-Convergence-Graph/m-p/231847#M1146</link>
      <description>&lt;P&gt;Hi Davi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC GA provides a hook for an update routine, called each iteration. &amp;nbsp;You can use this to do things like report the iteration count and current best objective. &amp;nbsp;Below is an example the prints the best objective each iteration to Output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--Josh&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;OPTIONS NODATE NONUMBER;
TITLE "Iteration history";
TITLE "Iteration history output";

proc ga seed = 555;
call SetEncoding('R3R2');
npoints = 3;
array cvxhull[3,2] /nosym ( -2 0
                             0 2
                             2 -2 );
/* Objective function */
function sixhump(selected[*],cvxhull[*,*],npoints);
   /* Function has global minimum value of -1.0316 
    * at x = {-0.0898  0.7126} and  
    *    x = { 0.0898 -0.7126} 
    */ 
   array w[1] /nosym;
   call dynamic_array(w,npoints);
   array x[2] /nosym;
   call ReadMember(selected,1,w);
   /* make sure that weights add up to 1 */
   sum = 0;
   do i = 1 to npoints;
      sum + w[i];
   end;
   /* if all weights 0, then reinitialize */
   if sum=0 then do;
      sum = npoints;
      do i = 1 to npoints;
         w[i] = 1;
      end;
   end;
   /* re-normalize weights */
   do i = 1 to npoints;
      w[i] = w[i] / sum;
   end;
   call WriteMember(selected,1,w);
   /* convert weights to x-coordinate form */
   x[1] = 0;
   x[2] = 0;
   do i = 1 to npoints;
      x[1] + w[i] * cvxhull[i,1];
      x[2] + w[i] * cvxhull[i,2];
   end;
   /* write out x coordinates to second segment */
   call WriteMember(selected,2,x);
   
   /* compute objective value */ 
   r = (4 - 2.1*x[1]**2 + x[1]**4/3)*x[1]**2 + x[1]*x[2] + 
       (-4 + 4*x[2]**2)*x[2]**2;
   return(r);
endsub;
/*************************************/
subroutine gethistory(iteration, maxiter, popsize, iterArray[*], bestArray[*]);
   outargs iteration, iterArray, bestArray;
   iteration = iteration+1; 
   array objValues[1] /nosym;

   /* dynamically allocate array to fit populationSize */
   call dynamic_array(objValues, popsize);

   /* read in current objective values */
   call GetObjValues(objValues, popsize);

   /* find best value */
   current_best = objValues[1];
   do i = 2 to popsize;
     /* for a minimization problem, use &amp;lt; here */
     if(objValues[i] &amp;lt; current_best) then 
        current_best = objValues[i];
   end;
   *put iteration current_best;
   iterArray[iteration]=iteration;
   bestArray[iteration]=current_best; 
   if (iteration = maxiter) then do; 
      put +1; 
      do i = 1 to maxiter;
         put "iter=" iterArray[i] ",  bestObj=" bestArray[i]  +1;
      end; 
   end;
endsub;
/************************************/
call SetObjFunc('sixhump',0);
array lower[1] /nosym;
array upper[1] /nosym;
call dynamic_array(lower, npoints);
call dynamic_array(upper, npoints);
do i = 1 to npoints;
   lower[i] = 0;
   upper[i] = 1;
end;
call SetBounds(lower, upper, 1);
array delta[3] /nosym (0.01 0.01 0.01);
call SetMut('delta', 'nchange', 1, 'delta', delta);
call SetMutProb(0.05);
call SetCross('Twopoint', 'alpha', 0.9);
call SetCrossProb(0.8);
call SetSel('tournament', 'size', 2);
call SetElite(3);
iteration=0;
popsize=20;
maxiter=40;
array iterArray[1] /nosym;
array bestArray[1] /nosym;
call dynamic_array(iterArray, maxiter+1);
call dynamic_array(bestArray, maxiter+1);
call SetUpdateRoutine('gethistory');
call Initialize('DEFAULT', popsize);
call ContinueFor(maxiter); 
run;
 &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 15:45:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/GA-Optimization-Convergence-Graph/m-p/231847#M1146</guid>
      <dc:creator>joshgriffin</dc:creator>
      <dc:date>2015-10-27T15:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: [GA Optimization] Convergence Graph</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/GA-Optimization-Convergence-Graph/m-p/232328#M1149</link>
      <description>&lt;P&gt;Hi Josh,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've already modified my code with your solution and it works perfectly.&lt;BR /&gt;Thanks for your help!&lt;BR /&gt;&lt;BR /&gt;Best Regards,&lt;BR /&gt;Davi&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2015 12:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/GA-Optimization-Convergence-Graph/m-p/232328#M1149</guid>
      <dc:creator>DaviLaraia</dc:creator>
      <dc:date>2015-10-30T12:52:36Z</dc:date>
    </item>
  </channel>
</rss>

