[Swift-commit] r7762 - SwiftApps/SimulatedAnnealing
wilde at ci.uchicago.edu
wilde at ci.uchicago.edu
Mon Apr 7 11:54:03 CDT 2014
Author: wilde
Date: 2014-04-07 11:54:02 -0500 (Mon, 07 Apr 2014)
New Revision: 7762
Modified:
SwiftApps/SimulatedAnnealing/annealing.swift
Log:
Converted to use generic objective function. Converted variable names to reflect more general terminology (eg loss -> result). Used simple avg() function to average N runs of objectve function.
Modified: SwiftApps/SimulatedAnnealing/annealing.swift
===================================================================
--- SwiftApps/SimulatedAnnealing/annealing.swift 2014-04-06 22:47:26 UTC (rev 7761)
+++ SwiftApps/SimulatedAnnealing/annealing.swift 2014-04-07 16:54:02 UTC (rev 7762)
@@ -1,30 +1,29 @@
import "math";
-import "colortext";
type file;
type Res
{
- float loss;
+ float result;
+/*
float sdev;
float tavg;
float tsdev;
+*/
}
type Checkpoint
{
int i;
int j;
- float dx0, dx1, dx2, dx3, dx4;
+ float dx0, dx1, dx2, dx3, dx4;
float rej0, rej1, rej2, rej3, rej4;
- float target_innov;
- float loss;
+ float result;
float alpha_i;
float alpha_m;
float beta;
float gamma;
float delta;
- float sdev;
}
global boolean restart = (@arg("restart","false") == "true");
@@ -50,31 +49,36 @@
// tracef("newx(%f,%f)=%f\n",x,dx,nx);
}
-app (/*file outfile,*/ file objout ) orig_objective ( string args[], file objdata )
-{
- # evolve @objout args /*stdout=@outfile*/; // FIXME: objdata file is passed implicitly
-}
-
/*
function y = simple_objective(x)
- y = (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;
+ y = (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;
*/
-(float y) objective ( float x[], file objdata )
+(float y) objective ( float x[] )
{
float x0 = x[0];
float x1 = x[1];
float x0s = x0 * x0;
float x1s = x1 * x1;
- y = (4.0 - 2.1*x0s + pow(x[0],4.0/3.0))*x0s + x0*x1 + (-4.0 + 4.0*x1s)*x1s;
+ float r = random() * .1;
+ tracef("r=%f\n",r);
+ y = ( (4.0 - 2.1*x0s + pow(x[0],4.0/3.0))*x0s + x0*x1 + (-4.0 + 4.0*x1s)*x1s ) + r;
}
-app (file outfile, file logfile ) sumobjective( file loss[] )
+(float s) sum (float a[])
{
- sumloss @filename(logfile) @filenames(loss) stdout=@filename(outfile);
+ string t[] = system("echo " + strjoin(a,"+") + "|bc");
+ s = toFloat(t[0]);
}
+(float a) avg (float f[])
+{
+ float n = toFloat( length(f) );
+ float t1 = sum(f);
+ float t2 = t1 / n;
+ a = t2;
+}
+
/* Program structure:
main
@@ -100,11 +104,11 @@
float rejection[][]; // [i][j] where i is cycle and j is evolve-parameter (alpha_i, alpha_m, beta, gamma, delta)
float trejection[][];
- float x[][], dx[][], curr_loss[], curr_sdev[];
+ float x[][], dx[][], curr_result[];
- Res mlres[][];
+ Res objres[][];
- file ckptFile <single_file_mapper; file=@strcat("ckpt.",target_innov)>;
+ file ckptFile <single_file_mapper; file=@strcat("ckpt")>;
Checkpoint ckpt;
int restartIndex;
@@ -114,16 +118,19 @@
}
else {
restartIndex = -1;
+/*
mlres[0][0] = multi_loss(T_start, T_end, annealing_cycles, Target_rejection,
starting_jump, 0, 0, params0, target_innov, evolve_reruns ); // FIXME: serves for all evolve-params ???
- tracef( "multi_annealing: AR: initial: %f +- %f\n", mlres[0][0].loss, mlres[0][0].sdev );
+*/
+ objres[0][0] = N_objective(params0, obj_runs ); // FIXME: serves for all evolve-params ???
+
+ tracef( "multi_annealing: initial: %f\n", objres[0][0].result );
foreach j in [0:NEVOPARAMS-1] {
x[0][j] = params0[j];
dx[0][j] = starting_jump;
rejection[0][j] = 0.0;
- curr_loss[j] = mlres[0][0].loss;
- curr_sdev[j] = mlres[0][0].sdev;
+ curr_result[j] = objres[0][0].result;
}
}
@@ -133,7 +140,7 @@
// set new temperature, rejection threshold, and dx values for this cycle
float temperature = T_start*exp( @tofloat( i-1 ) * ( jlog( T_end ) - jlog( T_start ) ) / @tofloat( annealing_cycles ) );
- tracef( @strcat( "multi_annealing: AR: i=%i ....T = ", color( 3, "%f" ),"\n" ), i, temperature );
+ tracef("multi_annealing: i=%i ....T =%f\n", i, temperature );
if ( i < restartIndex ) {
tracef( "skipping index %i - less than restart index %i\n", i, restartIndex );
@@ -146,8 +153,7 @@
x[i][2] = ckpt.beta;
x[i][3] = ckpt.gamma;
x[i][4] = ckpt.delta;
- curr_loss[((i+1)*NEVOPARAMS)-1] = ckpt.loss;
- curr_sdev[((i+1)*NEVOPARAMS)-1] = ckpt.sdev;
+ curr_result[((i+1)*NEVOPARAMS)-1] = ckpt.result;
}
else { // i > restartIndex: proceed as normal whether restarting or not
@@ -155,7 +161,7 @@
if (i %% cycle == 1 && i > 1) {
tracef( "multi_annealing: new cycle at i=%i\n", i );
- tracef( color( Pink, "multi_annealing: AR: New cycle at %i: prev dx[0-4]=[%f %f %f %f %f]\n" ),
+ tracef( "multi_annealing: New cycle at %i: prev dx[0-4]=[%f %f %f %f %f]\n",
i, dx[i-1][0], dx[i-1][1], dx[i-1][2], dx[i-1][3], dx[i-1][4] );
foreach k in [0:NEVOPARAMS-1] {
float newrejection = rejection[i-1][k] / @tofloat( cycle );
@@ -169,9 +175,9 @@
// FIXME: re-enable: rejection[i][k]=rejection[i-1][k];
trejection[i][k]=newrejection;
}
- // FIXME: HANGS? : tracef(color(Red,"Recomputed rejection: i=%d k=%d dx[i][k]=%f\n"), i, k, dx[i][k]);
+ // FIXME: HANGS? : tracef("Recomputed rejection: i=%d k=%d dx[i][k]=%f\n", i, k, dx[i][k]);
}
- tracef( color( Blue, "multi_annealing: AR: New cycle at %i: dx[0-4]=[%f %f %f %f %f]\n" ),
+ tracef( "multi_annealing: New cycle at %i: dx[0-4]=[%f %f %f %f %f]\n",
i, dx[i][0], dx[i][1], dx[i][2], dx[i][3], dx[i][4] );
}
else { // If not new cycle, set dx[i][*] from previous dx ([i-1]). rejection[i][j] is set later.
@@ -199,34 +205,24 @@
}
}
}
- tracef( @strcat( "multi_annealing: AR: ", color( 10,"%f" ), " ", color( 9,"%i" ),"\n" ), try_x[j], j );
+ tracef( "multi_annealing: %f %i\n", try_x[j], j );
# Call objective function here:
# mlres[i][j] = multi_loss( T_start, T_end, annealing_cycles, Target_rejection, starting_jump,
- i, j, try_x, target_innov, evolve_reruns ); // do the N evolve()'s, N=evolve_rerusn
- mlres[i][j] = multi_loss( try_x, target_innov, evolve_reruns ); // do the N evolve()'s, N=evolve_reruns
+# i, j, try_x, target_innov, evolve_reruns ); // do the N evolve()'s, N=evolve_rerusn
+ objres[i][j] = N_objective( try_x, obj_runs );
- tracef( "multi_annealing: AR: %f +- %f\n", mlres[i][j].loss, mlres[i][j].sdev );
+ tracef( "multi_annealing: %f\n", objres[i][j].result );
// Beyond this point, x[] and dx[] are being set for this i,j
- float ALOT = 100000000000.0; // 100,000,000,000. = 10^11
- if (mlres[i][j].loss < ALOT) {
- fprintf( best_filename, "N, %fs, %i, %i, %f, %f, |, %i, %f, [, %f, %f, %f, %f, %f, ], %f\n",
- mlres[i][j].tavg, i-1, j, dx[i][j], rejection[i][j], @toint(target_innov), mlres[i][j].loss,
- try_x[0], try_x[1], try_x[2], try_x[3], try_x[4], mlres[i][j].sdev );
+ fprintf( best_filename, "%i, %i, %f, %f, | %f [ %f, %f, %f, %f, %f ]\n",
+ i-1, j, dx[i][j], rejection[i][j], objres[i][j].result,
+ try_x[0], try_x[1], try_x[2], try_x[3], try_x[4] );
// Note i-1 field: print that way to match with C++ output
// fprintf( "max_dist_swift.txt", color( Red,"multi_annealing: AF: max_dist.txt - tbd\n" ) );
// FIXME: max_dist is global set in evolve()
- }
- else { // does this ever occur? if so did we want to still do the ratio computation above???
- fprintf( best_filename, "A, %fs, %i, %i, %f, %f, |, %i, %f, [, %f, %f, %f, %f, %f, ], %f\n",
- mlres[i][j].tavg, i-1, j, dx[i][j], rejection[i][j], @toint(target_innov), mlres[i][j].loss,
- try_x[0], try_x[1], try_x[2], try_x[3], try_x[4], mlres[i][j].sdev );
- // Note i-1 field: print that way to match with C++ output
- //tracef( "multi_annealing: Loss %f > ALOT at [i][j] = [%d][%d]\n", mlres[i][j].loss, i ,j );
- }
- float ratio = min( 1.0, exp( -( mlres[i][j].loss - curr_loss[prev] ) / temperature ) );
+ float ratio = min( 1.0, exp( -( objres[i][j].result - curr_result[prev] ) / temperature ) );
float r = (random()); //Java already returns a random float between [0.0-1.0]
tracef("multi_annealing: AR: %f vs %f\n", r, ratio);
if (r > ratio) { // Reject new parameter
@@ -237,11 +233,10 @@
else {
rejection[i][j] = rejection[i-1][j] + 1.0; // FIXME: AR: Is this correct? incr rejection?
}
- curr_loss[curr] = curr_loss[prev];
- curr_sdev[curr] = curr_sdev[prev];
+ curr_result[curr] = curr_result[prev];
// FIXME: AR: the following prints seem to replicate values in the .cpp version - please clarify.
- tracef( "multi_annealing: AR: %i,%i %i Did not accept: %f (%i)\n", i, j, i, try_x[j], j );
- tracef( "multi_annealing: AR: %f %f %f %f %f\n", try_x[0],try_x[1],try_x[2],try_x[3],try_x[4] );
+ tracef( "multi_annealing: %i,%i %i Did not accept: %f (%i)\n", i, j, i, try_x[j], j );
+ tracef( "multi_annealing: %f %f %f %f %f\n", try_x[0],try_x[1],try_x[2],try_x[3],try_x[4] );
}
else { // Accept new parameter
tracef( "multi_annealing: Accepting try_x[j], i=%i j=%i\n",i,j );
@@ -252,8 +247,7 @@
else {
rejection[i][j] = rejection[i-1][j]; // FIXME: AR: Is this correct? no incr of rejection?
}
- curr_loss[curr] = mlres[i][j].loss;
- curr_sdev[curr] = mlres[i][j].sdev;
+ curr_result[curr] = objres[i][j].result;
tracef( "multi_annealing: Accepting try_x[j], i=%i j=%i try_x[j]=%f\n", i, j, try_x[j] );
float rj[];
foreach k in [0:NEVOPARAMS-1] {
@@ -264,19 +258,16 @@
rj[k] = rejection[i-1][k]; // Not yet set, use previous
}
}
- tracef(@strcat("multi_annealing: AR: [%i][%i] ", color(8,"Rejection counts: "),
- color(1,"%f"), " ", color(7,"%f"), " ", color(5,"%f"), " ", color(9,"%f"), " ", color(6,"%f"), "\n\n"),
+ tracef("multi_annealing: [%i][%i] Rejection counts: %f %f %f %f %f\n\n",
i, j, rj[0], rj[1], rj[2], rj[3], rj[4]);
- tracef(@strcat("multi_annealing: AR: %i ", color(8,"***** Did accept! "),
- color(1,"%f"), " ", color(7,"%f"), " ", color(5,"%f"), " ", color(9,"%f"), " ", color(6,"%f"), "\n\n"),
+ tracef("multi_annealing: %i ***** Did accept! %f %f %f %f %f\n\n",
i, try_x[0], try_x[1], try_x[2], try_x[3], try_x[4]);
}
}
else {// Fixed Vars
x[i][j] = x[i-1][j];
rejection[i][j] = rejection[i-1][j];
- curr_loss[curr] = curr_loss[prev];
- curr_sdev[curr] = curr_sdev[prev];
+ curr_result[curr] = curr_result[prev];
// dx[i][j] not set for fixed vars
}
} until( j == (NEVOPARAMS-iterate_adjust) );
@@ -284,59 +275,19 @@
} until( iter_i == (annealing_cycles-iterate_adjust) );
}
-/*
-(Res r) multi_loss(float t_start, float t_end, int annealing_steps, float t_rejection, float starting_jump,
- int ci, int cj, float x[], float target_innov, int evolve_reruns)
-*/
-
(Res r) N_objective(float x[], int obj_runs)
{
tracef("%q\n", x);
- file rfile[];
- //file ofile[]; // FIXME: to obtain timings and other stats
+ float result[];
tracef( "N_objective: entered: obj_runs=%i x=%q\n", obj_runs, x );
- int appCalls = @toint(@tofloat(obj_runs)/@tofloat(runsPerApp)); // FIXME: handle fractional issues and rounding etc.
- // For now must divide evenly
- tracef("N_objective appCalls=%i\n", appCalls);
-
- foreach i in [1:appCalls] { // repeats of the objective() - same as n_reruns
- //file outfile; // FIXME: map and save in future
- string args[] = [ // FIXME: move this to a setargs() function
- // alpha_i alpha_m beta gamma delta
- @strcat(x[0]), @strcat(x[1]), @strcat(x[2]), @strcat(x[3]), @strcat(x[4]),
- // n_epochs n_steps n_runs range
- "40000", "20", @strcat(rerunsPerApp), "2",
- // verbose_level
- "1",
- // operation-code:(m,a) Nworkers seed
- "m", @strcat(Nworkers), @arg("seed", "0" ) ];
-
- file graph <"movie_graph.txt">;
-
- (/*outfile,*/ rfile[i]) = objective(args, graph);
- tracef("multi_loss: i=%i calling objective, args=%q\n", i, args);
- // tracef("multi_objective: after objective: i=%i %k %k\n", i, outfile, rfile[i]);
+ foreach i in [1:obj_runs] { // repeats of the objective() - same as n_reruns
+ result[i] = objective(x);
}
- string anneal_cycle;
- if(ci==0) {
- anneal_cycle="init";
- }
- else {
- anneal_cycle=@strcat(ci-1);
- }
-
- file sumfile<single_file_mapper; file=@strcat("sumloss/",anneal_cycle,".",cj,".sumloss")>;
- file logfile<single_file_mapper; file=@strcat("sumloss/",anneal_cycle,".",cj,".sumlog")>;
-
- (sumfile, logfile) = sumloss(rfile);
- r = readData(sumfile);
-
- tracef("multi_loss: returning: ci=%i cj=%i r.loss=%f r.sdev=%f\n",ci,cj,r.loss,r.sdev);
- // file statfile = sumstats(ofile); FIXME: to obtain timings and other stats
- // s = readStat(statsfile); FIXME: to obtain timings and other stats
+ r.result = avg(result);
+ tracef("multi_loss: returning: r.result=%f\n", r.result);
}
optimizer_serial_sweep() // Implements logic of python driver script
@@ -353,14 +304,14 @@
//file bestfile <single_file_mapper; file=@strcat("output/T",target,".R",rep,".best_opt_some")>;
//file maxfile <single_file_mapper; file=@strcat("output/T",target,".R",rep,".max_dist")>;
-# foreach target_innov in [minrange:maxrange:rangeinc] {
- iterate i {
- int target_innov = minrange + (i*rangeinc);
+# foreach target_innov in [minrange:maxrange:rangeinc] {
+# iterate i {
+# int target_innov = minrange + (i*rangeinc);
foreach rep in [1:nreps] {
- string best_filename = @strcat( "best.T", @strcat(target_innov), ".R", @strcat(rep), ".txt" );
- file outfile; // <single_file_mapper; file=@strcat("output/T",target_innov,".R",rep,".out")>;
- file lossfile; // <single_file_mapper; file=@strcat("output/T",target_innov,".R",rep,".loss_data")>;
+ string best_filename = @strcat( "best.R", @strcat(rep), ".txt" );
+ file outfile; // <single_file_mapper; file=@strcat("output/R",rep,".out")>;
+ file lossfile; // <single_file_mapper; file=@strcat("output/R",rep,".loss_data")>;
(outfile,lossfile) = multi_annealing(best_filename,
@tofloat(@arg("tstart", "2.0")),
@@ -374,10 +325,9 @@
@tofloat(@arg("gamma", "50.0")),
@tofloat(@arg("delta", "-1.0"))
],
- @tofloat(target_innov),
@toint(@arg("annealingcycles", "50")) );
}
- } until(target_innov >= (maxrange-rangeinc));
+ # } until(target_innov >= (maxrange-rangeinc));
}
optimizer_sweep() // Implements logic of python driver script
@@ -394,12 +344,12 @@
//file bestfile <single_file_mapper; file=@strcat("output/T",target,".R",rep,".best_opt_some")>;
//file maxfile <single_file_mapper; file=@strcat("output/T",target,".R",rep,".max_dist")>;
- foreach target_innov in [minrange:maxrange:rangeinc] {
+# foreach target_innov in [minrange:maxrange:rangeinc] {
foreach rep in [1:nreps] {
- string best_filename = @strcat( "best.T", @strcat(target_innov), ".R", @strcat(rep), ".txt" );
- file outfile; // <single_file_mapper; file=@strcat("output/T",target_innov,".R",rep,".out")>;
- file lossfile; // <single_file_mapper; file=@strcat("output/T",target_innov,".R",rep,".loss_data")>;
+ string best_filename = @strcat( "best.R", @strcat(rep), ".txt" );
+ file outfile; // <single_file_mapper; file=@strcat("output/R",rep,".out")>;
+ file lossfile; // <single_file_mapper; file=@strcat("output/R",rep,".loss_data")>;
(outfile,lossfile) = multi_annealing(best_filename,
@tofloat(@arg("tstart", "2.0")),
@@ -413,10 +363,9 @@
@tofloat(@arg("gamma", "50.0")),
@tofloat(@arg("delta", "-1.0"))
],
- @tofloat(target_innov),
@toint(@arg("annealingcycles", "50")) );
}
- }
+# }
}
main()
@@ -425,63 +374,3 @@
}
main();
-
-/*
-
- Program structure:
-
- main
- optimizer_sweep()
- multi_annealing()
- multi_loss()
- evolve()
- sumloss()
-
- Example parameter sets:
-
- for target in range(58,59,50):
- for i in range(1):
- args="./toptimizer 0 0 4 50 -1 "+target+" 40000 20 75 2 1 2. 0.01 2 0.3 2.3 1 1 1 0 0 m // > out.T"+str(target)+".i"+str(i)
- os.system(args);
-
- string fastargs1[] = [
- "0", "0", "4", "50", "-1", @strcat(target),
- "40000", "20", "1000", "2",
- "1",
- "2.", "0.01", "100", "0.3", "2.3",
- "1", "1", "0", "0", "0"];
- string fastargs2[] = [
- "0", "0", "4", "50", "-1", @strcat(target),
- "40000", "20", "1000", "2",
- "1",
- "2.", "0.01", "5", "0.3", "2.3",
- "1", "1", "0", "0", "0", "m"];
- string fastargs3[] = [
- "0", "0", "4", "50", "-1", @strcat(target),
- "40000", "20", @strcat(repeats), "2",
- "1",
- "2.", "0.01", "2", "0.3", "2.3",
- "1", "1", "0", "0", "0", "m"];
-*/
-
-(string args[]) setargs()
-{
- // string longargs[] = @strcat("0 0 4 50 -1 ",target," 40000 20 1000 2 1 2. 0.01 100 0.3 2.3 1 1 0 0 0 m");
-
- // [alpha_i alpha_m beta gamma delta target_innov
- // [n_epochs n_steps n_reruns] [range]
- // [verbose_level]
- // [T_start T_end Annealing_steps Target_rejection Starting_jump]
- // [FREEZE_alpha_i FREEZE_alpha_m FREEZE_beta FREEZE_gamma FREEZE_delta] [operation-code:(m,a) Nworkers]
-}
-
-////////////////// HOLD JUNK
-
-// tracef(@strcat("multi_annealing: AR: %i ", color(8,"Rejection counts: "),
-// color( /* 2 */ 1," %f"), "\n\n"),
-// i, rejection[i][j] ); // , rejection[i][1], rejection[i][2], rejection[i][3], rejection[i][4]);
-// FIXME: determine correct rejection[] values to avoid hanging:
-// tracef(@strcat("multi_annealing: AR: %i ", color(8,"Rejection counts: "),
-// color( /* 2 */ 1," %f"), color(7," %f"), color(5," %f"), color(9," %f"), color(6," %f"), "\n\n"),
-// rejection[i][0], rejection[i][1], rejection[i][2], rejection[i][3], rejection[i][4]);
-// END FIXME
More information about the Swift-commit
mailing list