JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for OX-USERS Archives


OX-USERS Archives

OX-USERS Archives


OX-USERS@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

OX-USERS Home

OX-USERS Home

OX-USERS  October 1999

OX-USERS October 1999

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

simulated annealing

From:

Michael Creel <[log in to unmask]>

Reply-To:

Michael Creel <[log in to unmask]>

Date:

Fri, 15 Oct 1999 16:08:09 +0200

Content-Type:

multipart/mixed

Parts/Attachments:

Parts/Attachments

text/plain (19 lines) , anneal.ox (376 lines)

The attached file is an improved version of the simulated annealing
program I posted some time ago. This one eliminates two inner loops, and
is about 20% faster.

-- 
Michael Creel              [log in to unmask]
Tel. 93-581-1696           Fax. 93-581-2012
Dept. of Economics and Economic History
Universitat Autonoma de Barcelona

"Life is too short to waste your time doing much of anything"
-- 
Michael Creel              [log in to unmask]
Tel. 93-581-1696           Fax. 93-581-2012
Dept. of Economics and Economic History
Universitat Autonoma de Barcelona

"Life is too short to waste your time doing much of anything"


// New and Improved (~20% faster) // inner loop was inefficient, now eliminated // See line 132 and 268 for the changes // This version 15 Oct. 1999 //--------------------------- anneal.ox ---------------------------------- // // This is a quick and dirty translation and modification of Goffe et. al.'s //simulated annealing algorithm. The source was Gauss code obtained from // // http://gurukul.ucc.american.edu/econ/gaussres/GAUSSIDX.HTM // // You will find proper credits and references there // // The structure of the program has been modified in minor ways. In particular it // only maximizes, so objective functions should be written accordingly. // // The Gauss code is by E.G. Tsionas, who deserves all credit. // Translated to Ox by Michael Creel [log in to unmask] /* Example format of objective function for maximization obj(const theta) { < your calculations here >     return(value_of_obj); } */ /* Example of how to call the routine: the following need to be defined.    Then the obj. function is maximized. See Goffe et. al documentation for details.     // SA controls t=1000; rt=.25; ns=20; nt=5; neps=5; eps=1e-5; maxeval=1e10; iprint=1; lb=-2*ones(rows(theta),1); ub=-lb; c=ub; vm=ub./ub; error_code=anneal(obj,&theta, rt, eps, ns, nt, neps, maxeval, lb, ub, c, iprint, t, vm); */ //-------------- The annealing algorithm -------------- anneal(const func, const aParam, const rt, const eps, const ns, const nt, const neps, const maxevl, const lb, const ub, const c, const iprint, t, vm) {     decl n, x, xopt, xp, nacp, nacc, nobds, nfuncev, ier; decl fstar, f, fopt, nup, nrej, nnew, ndown, lnobds; decl m, j, h, i, fp, p, pp, ratio, converge; decl test;      x=aParam[0];     n = rows(x);     xopt = zeros(n, 1);     xp = zeros(n, 1);     nacp = zeros(n, 1);          // Set initial values          nacc = 0;     nobds = 0;     nfuncev = 0;     ier = 99;     xopt = x;     nacp = zeros(n, 1);     fstar = 1e20 * ones(neps, 1);          // If the initial temperature is not positive, notify the user and abort          if(t <= 0.0)     {         print(" The initial temperature is not positive. Reset the variable t");         return 4;     }          /** If the initial value is out of bounds, notify the user and abort. **/     if((sumc(x .> ub) + sumc(x .< lb) > 0))     {         print("initial condition out of bounds");         return 3;     }          /** Evaluate the function with input param and return value as f. **/          f = func(x);          nfuncev = nfuncev + 1;     fopt = f;     fstar[0][] = f;     if(iprint > 1) { print(""); print("initial x", x'); print("initial f", f); }           /**   Start the main loop. Note that it terminates if (i) the algorithm   successfully optimizes the function or (ii) there are too many   function evaluations (more than MAXEVL). **/      converge=0; while(converge==0) { nup = 0; nrej = 0; nnew = 0; ndown = 0; lnobds = 0;      for(m = 0;m < nt;++m) { for(j = 0;j < ns;++j) { for(h = 0;h < n;++h) { // Creel's code to replace the commented loop below xp = x; xp[h][] = x[h][] + (2*ranu(1, 1) - 1) * vm[h][]; if((xp[h][] < lb[h][]) || (xp[h][] > ub[h][])) { xp[h][] = lb[h][] + (ub[h][] - lb[h][]) * ranu(1, 1); lnobds = lnobds + 1; nobds = nobds + 1; if ((iprint >= 3)) { print("");      print("\ncurrent x");print(x', "\n"); print("current f", f); print("trial x", xp'); print("point rejected since out of bounds"); } } // End of Creel's code // This is the replaced code // /** Generate xp, the trial value of param. Note use of vm to choose xp. **/ // for(i = 0;i < n;++i) // { // if(i == h) // { // xp[i][] = x[i][] + (2*ranu(1, 1) - 1) * vm[i][]; // } // else // { // xp[i][] = x[i][]; // } // // /** If xp is out of bounds, select a point in bounds for the trial. **/ // if((xp[i][] < lb[i][]) || (xp[i][] > ub[i][])) // { // xp[i][] = lb[i][] + (ub[i][] - lb[i][]) * ranu(1, 1); // lnobds = lnobds + 1; // nobds = nobds + 1; // if ((iprint >= 3)) // { // print(""); // print("current x");print(x', "\n"); // print("current f", f); // print("trial x", xp'); // print("point rejected since out of bounds"); // } // } // }                  /** Evaluate the function with the trial point xp and return as fp. **/                  fp = func(xp); nfuncev = nfuncev + 1; if ((iprint >= 3)) { print("");      print("\ncurrent x", x'); print("current f", f); print("trial x", xp'); print("resulting f", fp); }                  /** If too many function evaluations occur, terminate the algorithm. **/                  if(nfuncev >= maxevl) { print("Too many function evaluations; consider"); print("increasing maxevl or eps, or decreasing"); print("nt or rt. These results are likely to be poor"); return 2; }                  /** Accept the new point if the function value increases. **/                  if(fp >= f) { if ((iprint >= 3)) { print("point accepted"); } x = xp; f = fp; nacc = nacc + 1; nacp[h][] = nacp[h][] + 1; nup = nup + 1;                      /** If greater than any other point, record as new optimum. **/                      if(fp > fopt) { if(iprint >= 3) { print("\nnew optimum"); } xopt = xp; fopt = fp; nnew = nnew + 1; } } /** If the point is lower, use the Metropolis criteria to decide on acceptance or rejection. **/                    else { p = exp((fp - f) / t); pp = ranu(1, 1);                      if(pp < p) { if(iprint >= 3) { print("though lower, point accepted"); } x = xp; f = fp; nacc = nacc + 1; nacp[h][] = nacp[h][] + 1; ndown = ndown + 1; } else { nrej = nrej + 1; if(iprint >= 3) { print("lower point rejected"); } } } } }          /** Adjust vm so that approximately half of all evaluations are accepted. **/ // Creel's code to replace the commented loop below ratio = nacp / ns; test = ratio .> 0.6; vm = (1 - test) .* vm + test .* vm .* (1. + c .* (ratio - 0.6) / 0.4); test = ratio .< 0.4; vm = (1 - test) .* vm + test .* vm .* (1. + c .* (0.4 - ratio) / 0.4); test = vm .> (ub - lb); vm = (1 - test) .* vm + test .* (ub - lb); // end of Creel's code          // this is the replaced code // for(i = 0;i < n;++i) // { // ratio = nacp[i][] / ns; // if(ratio > .6) // { // vm[i][] = vm[i][] * (1. + c[i][] * (ratio - .6) / .4); // } // else if(ratio < .4) // { // vm[i][] = vm[i][] / (1. + c[i][] * ((.4 - ratio) / .4)); // } // if(vm[i][] > (ub[i][] - lb[i][])) // { // vm[i][] = ub[i][] - lb[i][]; // } // } if(iprint >= 2) { print("intermediate results after step length adjustment", "\n");      print("new step length (vm)", vm', "\n"); print("current optimal x", "\n"); print(xopt', "\n"); print("current x", "\n"); print(x', "\n"); print(""); } nacp = zeros(n, 1); }      if(iprint >= 1) { print("Intermediate results before next temperature reduction\n"); print("\ncurrent temperature ", t); print("\nmax function value so far", fopt); print("\ntotal moves ", nup + ndown + nrej); print("\nuphill ", nup); print("\naccepted downhill ", ndown); print("\nrejected downhill ", nrej); print("\nout of bounds trials ", lnobds); print("\nnew maxima this temperature ", nnew);         print("\ncurrent optimal x", "\n"); print(xopt', "\n"); print("\nstrength (vm)", "\n"); print(vm', "\n"); print(""); }      /** Check termination criteria. **/      fstar[0][] = f; if( ((fopt - fstar[0][]) <= eps) && (max(fabs(f - fstar) .> eps) == 0) ) { converge = 1; }      /** Terminate SA if appropriate. **/      if(converge) { aParam[0] = xopt; ier=1; if(iprint >= 1) print("\nSuccessful normal termination\n"); print("function value\n",fopt); print("\ntotal accepted function evaluations\n",nacc); print("\ntotal trial function evaluation\n",nfuncev); print("\nnumber of out-of-bounds arguments\n",nobds); print("\nfinal temperature\n",t); return ier; // return breaks out of the program }      /** If termination criteria are not met, prepare for another loop. **/      t = rt * t; fstar[1:neps-1][] = fstar[0:neps-2][]; f = fopt; x = xopt;      } } /* Now that we're at the end, the temperature is low, so here's a joke from a cold climate: One polar bear to another, discussing igloos: "Man, I love these things - crunchy on the outside, chewy on the inside" */

Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

April 2024
March 2024
February 2024
January 2024
November 2023
October 2023
September 2023
June 2023
May 2023
March 2023
February 2023
January 2023
May 2022
March 2022
January 2022
December 2021
July 2021
February 2021
November 2020
October 2020
September 2020
May 2020
April 2020
March 2020
January 2020
November 2019
October 2019
September 2019
August 2019
May 2019
February 2019
November 2018
October 2018
August 2018
July 2018
May 2018
April 2018
March 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
February 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
December 2005
November 2005
October 2005
September 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
December 2002
November 2002
October 2002
September 2002
August 2002
July 2002
June 2002
May 2002
April 2002
March 2002
February 2002
January 2002
December 2001
November 2001
October 2001
September 2001
August 2001
July 2001
June 2001
May 2001
April 2001
March 2001
February 2001
January 2001
December 2000
November 2000
October 2000
September 2000
August 2000
July 2000
June 2000
May 2000
April 2000
March 2000
February 2000
January 2000
December 1999
November 1999
October 1999
September 1999
August 1999
July 1999
June 1999
May 1999
April 1999
March 1999
February 1999
January 1999
December 1998
November 1998
October 1998
September 1998


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

For help and support help@jisc.ac.uk

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager