[Swift-commit] r7791 - SwiftApps/SimulatedAnnealing

wilde at ci.uchicago.edu wilde at ci.uchicago.edu
Thu Apr 24 11:32:20 CDT 2014


Author: wilde
Date: 2014-04-24 11:32:20 -0500 (Thu, 24 Apr 2014)
New Revision: 7791

Modified:
   SwiftApps/SimulatedAnnealing/sa.swift
Log:
Commit changes to sa example from Fri Apr 11 session with Jonathan Ozik

Modified: SwiftApps/SimulatedAnnealing/sa.swift
===================================================================
--- SwiftApps/SimulatedAnnealing/sa.swift	2014-04-24 15:32:21 UTC (rev 7790)
+++ SwiftApps/SimulatedAnnealing/sa.swift	2014-04-24 16:32:20 UTC (rev 7791)
@@ -377,13 +377,37 @@
   float y[];
   y[0] = objective(a[0]);
 
-  iterate c {
-    int cy = c + 1;
-    a[cy] = [ newx(a[0][0],0.5), newx(a[0][1],0.5), newx(a[0][2],0.5), newx(a[0][3],0.5) ];
-    y[cy] = objective(a[cy]);
+  iterate i {
+    int c = i + 1;
+    a[c] = [ newx(a[i][0],0.5), newx(a[i][1],0.5), newx(a[i][2],0.5), newx(a[i][3],0.5) ];
+    float ny = objective(a[c]);
+    if ( accept(ny,c,y[c-1]) ) {
+      y[c] = ny;
+    }
+    else {
+      y[c] = y[c-1];
+    }
     trace(c,y[c]);
-  } until (c==10);
+  } until (i==10);
 
 }
 
+(boolean a) accept (float cur, int cy, float prev)
+{
+  float T_start = 1.9;
+  float T_end   = 1.0;
+  int cycles = 10;
+
+  float t =  T_start*exp( @tofloat( cy-1 ) * ( jlog(T_end) - jlog(T_start) ) / @tofloat(cycles) );
+
+  float ratio = min( 1.0, exp( - (cur-prev) / t ) );
+  float r = (random()); // a random float between [0.0-1.0]
+  if (r > ratio) { // Reject new parameter
+    a = false;
+  }
+  else {
+    a = true;
+  }
+}
+
 main();




More information about the Swift-commit mailing list