chiark
/
gitweb
/
~ianmdlvl
/
matchsticks-search.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Python code to compute upper bounds on the solution.
[matchsticks-search.git]
/
main.c
diff --git
a/main.c
b/main.c
index 0967442c5a425f5d7c414c39be6b971b571753fc..3ee58c59760729eba27ae8b88123caa59d9062b0 100644
(file)
--- a/
main.c
+++ b/
main.c
@@
-11,6
+11,7
@@
*
* Options:
* -j<jobs> run in parallel on <jobs> cores
*
* Options:
* -j<jobs> run in parallel on <jobs> cores
+ * -b<best> search only for better than <best>
*/
/*
*/
/*
@@
-114,7
+115,7
@@
static double best;
static glp_prob *best_prob;
static AdjWord *best_adjmatrix;
static glp_prob *best_prob;
static AdjWord *best_adjmatrix;
-static int n_max_frags
, m_max_frags
;
+static int n_max_frags
=INT_MAX, m_max_frags=INT_MAX
;
static int *weight;
static unsigned printcounter;
static int *weight;
static unsigned printcounter;
@@
-146,8
+147,10
@@
static void set_best(double new_best) {
* subtract a fudge factor from our target.
*/
double near_best = best * 0.98 - 0.02;
* subtract a fudge factor from our target.
*/
double near_best = best * 0.98 - 0.02;
- n_max_frags = ceil(n / near_best) - 1;
- m_max_frags = ceil(m / near_best) - 1;
+ if (near_best > 0) {
+ n_max_frags = ceil(n / near_best) - 1;
+ m_max_frags = ceil(m / near_best) - 1;
+ }
}
/*----- multicore support -----*/
}
/*----- multicore support -----*/
@@
-262,6
+265,8
@@
static void multicore_outer_iteration(int i, AdjWord min) {
}
static void mc_iterate_worker(void) {
}
static void mc_iterate_worker(void) {
+ static time_t lastprint;
+
for (;;) {
mc_rwvsetup_outer();
ssize_t r = readv(mc_work[0], mc_iov, mc_niovs);
for (;;) {
mc_rwvsetup_outer();
ssize_t r = readv(mc_work[0], mc_iov, mc_niovs);
@@
-271,8
+276,12
@@
static void mc_iterate_worker(void) {
bool ok = maxhamweight_ok();
if (!ok) continue;
bool ok = maxhamweight_ok();
if (!ok) continue;
- ok = preconsider_ok(multicore_iteration_boundary, 1);
- progress_eol();
+ time_t now = time(0);
+ bool doprint = now != lastprint;
+ lastprint = now;
+
+ ok = preconsider_ok(multicore_iteration_boundary, doprint);
+ if (doprint) progress_eol();
if (!ok) continue;
/* stop iterate_recurse from trying to run multicore_outer_iteration */
if (!ok) continue;
/* stop iterate_recurse from trying to run multicore_outer_iteration */
@@
-383,8
+392,6
@@
static void prep(void) {
glp_term_out(GLP_OFF);
setlinebuf(stderr);
weight = calloc(sizeof(*weight), m); assert(weight);
glp_term_out(GLP_OFF);
setlinebuf(stderr);
weight = calloc(sizeof(*weight), m); assert(weight);
- n_max_frags = INT_MAX;
- m_max_frags = INT_MAX;
}
#if 0
}
#if 0
@@
-659,7
+666,7
@@
static void iterate_recurse(int i, AdjWord min) {
if (adjmatrix[i] & jbit)
weight[j]++;
for (int j = 0; j < m; j++)
if (adjmatrix[i] & jbit)
weight[j]++;
for (int j = 0; j < m; j++)
- if (weight[j] >
=
n_max_frags)
+ if (weight[j] > n_max_frags)
goto takeout;
iterate_recurse(i+1, adjmatrix[i]);
goto takeout;
iterate_recurse(i+1, adjmatrix[i]);
@@
-686,6
+693,14
@@
static void iterate(void) {
static void report(void) {
fprintf(stderr, "\n");
static void report(void) {
fprintf(stderr, "\n");
+ if (best_adjmatrix) {
+ int i;
+ fprintf(stderr," ");
+ for (i=0; i<n; i++) fprintf(stderr, " %"PRADJ, best_adjmatrix[i]);
+ }
+ fprintf(stderr, " best=%-12.8f nf<=%d mf<=%d\n",
+ best, n_max_frags, m_max_frags);
+ printf("%d into %d: ", n, m);
if (best_prob) {
double min = glp_get_obj_val(best_prob);
double a[n][m];
if (best_prob) {
double min = glp_get_obj_val(best_prob);
double a[n][m];
@@
-700,7
+715,7
@@
static void report(void) {
continue;
a[x][y] = min + glp_get_col_prim(best_prob, i);
}
continue;
a[x][y] = min + glp_get_col_prim(best_prob, i);
}
- printf("
%d into %d: min fragment %g [%s]\n", n, m
, min, VERSION);
+ printf("
min fragment %g [%s]\n"
, min, VERSION);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a[i][j])
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a[i][j])
@@
-710,15
+725,19
@@
static void report(void) {
}
printf("\n");
}
}
printf("\n");
}
+ } else {
+ printf(" none better than %9.3f [%s]\n", best, VERSION);
}
if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(-1); }
}
int main(int argc, char **argv) {
int opt;
}
if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(-1); }
}
int main(int argc, char **argv) {
int opt;
- while ((opt = getopt(argc,argv,"j:")) >= 0) {
+ double best_to_set = -1.0; /* means 'don't' */
+ while ((opt = getopt(argc,argv,"j:b:")) >= 0) {
switch (opt) {
case 'j': ncpus = atoi(optarg); break;
switch (opt) {
case 'j': ncpus = atoi(optarg); break;
+ case 'b': best_to_set = atof(optarg); break;
case '+': assert(!"bad option");
default: abort();
}
case '+': assert(!"bad option");
default: abort();
}
@@
-729,6
+748,7
@@
int main(int argc, char **argv) {
n = atoi(argv[1]);
m = atoi(argv[2]);
assert(n > m);
n = atoi(argv[1]);
m = atoi(argv[2]);
assert(n > m);
+ if (best_to_set > 0) set_best(best_to_set);
prep();
prep();