Commits

Anonymous committed 15a8932

bunch of messy experiements for target length, first pass at exponential damping with small list

Comments (0)

Files changed (1)

 		return solved;
 	if ((!all_sol && p2_len + p1_len < len) || (all_sol && p2_len + p1_len <= len)) {
 		if (interval && (iv.it_value.tv_sec || iv.it_value.tv_usec))
-			if (setitimer(ITIMER_VIRTUAL, &iv, NULL) < 0)
+			if (setitimer(ITIMER_REAL, &iv, NULL) < 0)
 				printf("setitimer failed\n");
 		len = p2_len + p1_len;
 		if (one_sol) sprint_solution(sol_str);
 	}
 }
 
+struct cbuf {
+	int beg, end, size;
+	char nums[1001];
+};
+void add_num(struct cbuf *nums, int n)
+{
+	nums->end = (nums->end + 1) % nums->size;
+	if (nums->end == nums->beg)
+		nums->beg = (nums->beg + 1) % nums->size;
+	nums->nums[nums->end] = n;
+}
+
+double get_avg(struct cbuf *nums, int size)
+{
+	int i, j, sum = 0;
+
+	for (i = nums->end, j = size; j; i--, j--) {
+		if (i < 0) i = nums->size - 1;
+		if (i == nums->beg) break;
+		sum += nums->nums[i];
+	}
+	return (double)sum / (double)(size - j);
+}
+
+#define member_size(x,y) sizeof(((struct x*)0)->y)
 int main(int argc, char **argv)
 {
 	struct sigaction s = { .sa_handler = signal_handler };
 
 	char buf[128];
-	int i;
+	int i, oold_len;
 	int total_solves = 0;
 	int total_moves = 0;
-	double tiny_avg = 0;
-	double small_avg = 0;
-	double big_avg = 0;
-	double avg_len = 0;
+	double tiny_avg, small_avg, big_avg, avg_len;
+	double otiny_avg, osmall_avg, obig_avg;
+	double tiny_avgd, small_avgd, big_avgd;
+	struct cbuf lens = { 0, 0, member_size(cbuf, nums) };
+	struct cbuf dlens = { 0, 0, member_size(cbuf, nums) };
 
 	while ((i = getopt(argc, argv, "1ad:gil:m:n:or:s:t:v")) != -1) {
 		switch (i) {
 	if (target) {
 		one_sol = 1;
 		timeout = 100;
-		avg_len = target;
-		small_avg = target;
-		big_avg = target;
+		avg_len = tiny_avg = small_avg = big_avg = target;
+		otiny_avg = osmall_avg = obig_avg = oold_len = target;
+		tiny_avgd = small_avgd = big_avgd = 0;
+		if (sigaction(SIGVTALRM, &s, NULL) < 0) {
+			printf("sigaction failed\n");
+			exit(EXIT_FAILURE);
+		}
 	}
 	if (timeout || num_cps) {
 		//if (sigaction(SIGVTALRM, &s, NULL) < 0) {
 		sigtstp = 0;
 		copy_cube(0, co_, eo_, cp_, ep_);
 
-		if (!num_cps && (iv.it_value.tv_sec || iv.it_value.tv_usec))
-			if (setitimer(ITIMER_VIRTUAL, &iv, NULL) < 0)
-				printf("setitimer failed\n");
+		if (!num_cps && (iv.it_value.tv_sec || iv.it_value.tv_usec)) {
+			if (target) {
+				if (setitimer(ITIMER_VIRTUAL, &iv, NULL) < 0)
+					printf("setitimer failed\n");
+			} else {
+				if (setitimer(ITIMER_REAL, &iv, NULL) < 0)
+					printf("setitimer failed\n");
+			}
+		}
 
 		for (p1_len = 0; p1_len <= len && (!max_p1d || p1_len <= max_p1d); p1_len++) {
 			if (solve(PHASE1, get_eo_coord(), get_co_coord(), get_ud1_coord(), p1_len, -1))
 		if (one_sol) print_solution(1);
 		if (target) {
 			struct timeval dif, res;
-			int us;
+			double td, sd, bd, od;
+			int	us = 0;
 
-			// average of last 100 solves
-			tiny_avg = (tiny_avg * 9 + old_len) / 10.0;
-			small_avg = (small_avg * 99 + old_len) / 100.0;
-			big_avg = (big_avg * 999 + old_len) / 1000.0;
+			// all of our statistics to work with
+			add_num(&lens, old_len);
+			tiny_avg  = get_avg(&lens,   10);
+			small_avg = get_avg(&lens,  100);
+			big_avg   = get_avg(&lens, 1000);
 
-			total_solves++;
-			total_moves += old_len;
-			avg_len = (double) total_moves / (double) total_solves;
+			/*
+			add_num(&dlens, old_len - oold_len);
+			tiny_avgd  = get_avg(&dlens,   10);
+			small_avgd = get_avg(&dlens,  100);
+			big_avgd   = get_avg(&dlens, 1000);
+			*/
 
-			us = big_avg * 1000000 - target * 1000000;
-			us /= 10;
-			if ((big_avg < target && tiny_avg > target) || (big_avg > target && tiny_avg < target)) {
-				us = us / 10;// / 10;
+			// scale exponentially
+			if (++total_solves % 10 == 0) {
+				us = tiny_avg * 10 - target * 10;
+				if (us < 0) us = - (1 << -((us < -10) ? -10 : us));
+				else        us =    1 <<  ((us >  10) ?  10 : us) ;
+				fprintf(stderr, "*");
 			} else
-			if ((big_avg < target && tiny_avg < target && tiny_avg > big_avg) ||
-			    (big_avg > target && tiny_avg > target && tiny_avg < big_avg)) {
-				us = 0;
-			}
+				fprintf(stderr, " ");
+
+			// scale to 0-25% of timeout
+			int to;
+			to = iv.it_value.tv_sec * 1000000 + iv.it_value.tv_usec;
+			us = (double)to/4.0/1024.0 * (double)us;
+
+
+			// apply the difference to the timeout
 			if (us < 0) timersub(&iv.it_value, &((struct timeval){-us / 1000000,-us % 1000000 }), &res);
 			else        timeradd(&iv.it_value, &((struct timeval){ us / 1000000, us % 1000000 }), &res);
 			if (timercmp(&res, &((struct timeval){ 0, 0 }), <=)) res = (struct timeval){ 0, 1 };
 			iv.it_value = res;
-			if (verbose) {
-				printf("total: solves: %d moves: %d\n", total_solves, total_moves);
-				printf("average length: %f target %d\n", avg_len, target);
-				printf("adjustment %d us\n", us);
-			}
-			fprintf(stderr, "now: %d avg: %f sml: %f big: %f dt: % 7.7d timeout: %d.%.6d\n",
-					old_len, avg_len, tiny_avg, big_avg, us, iv.it_value.tv_sec, iv.it_value.tv_usec);
+			fprintf(stderr, "now: %d(% d) tny: %f sml: %f big: %f dt: % 7.7d timeout: %d.%.6d\n",
+					old_len, old_len - oold_len, tiny_avg, small_avg, big_avg, us, iv.it_value.tv_sec, iv.it_value.tv_usec);
+			/*
+			fprintf(stderr, "now: %d(% d) tny: %f(% f) sml: %f(% f) big: %f(% f) dt: % 7.7d timeout: %d.%.6d\n",
+					old_len, old_len - oold_len, tiny_avg, tiny_avgd, small_avg, small_avgd, big_avg, big_avgd, us, iv.it_value.tv_sec, iv.it_value.tv_usec);
+					*/
 			fflush(stderr);
+
+			oold_len = old_len;
 		}
 		if (verbose) printf("----------\n");
 	}
-
 	return 0;
 }