From 63f39a339d46152875081ec5772868b23deba821 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 7 Mar 2014 15:32:54 +0000 Subject: [PATCH] wip lp, results, compiles --- Makefile | 2 +- main.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 166f614..e91ae03 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS += -Wall -Wwrite-strings -Wstrict-prototypes +CFLAGS += -Wall -Wwrite-strings -Wstrict-prototypes -g LC_CTYPE=C LDLIBS = -lpub -lglpk diff --git a/main.c b/main.c index 22fa343..bdcb18b 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -16,10 +17,16 @@ static AdjWord *adjmatrix; static AdjWord adjall; static double best; +static glp_prob *best_prob; +static AdjWord *best_adjmatrix; + +static AdjWord *xalloc_adjmatrix(void) { + return xmalloc(sizeof(*adjmatrix)*n); +} static void prep(void) { adjall = ~((~(AdjWord)0) << m); - adjmatrix = xmalloc(sizeof(*adjmatrix)*n); + adjmatrix = xalloc_adjmatrix(); } static AdjWord one_adj_bit(int bitnum) { @@ -83,7 +90,7 @@ static void optimise(void) { int matrix_entries_XY[2][matrix_entries_size]; #define ADD_MATRIX_ENTRY(Y,X) ({ \ - assert(matrix_entries_size < next_matrix_entry); \ + assert(next_matrix_entry < matrix_entries_size); \ matrix_entries_XY[0][next_matrix_entry] = X; \ matrix_entries_XY[1][next_matrix_entry] = Y; \ matrix_entries[next_matrix_entry] = 0; \ @@ -136,7 +143,50 @@ static void optimise(void) { matrix_entries_XY[1], matrix_entries_XY[0], matrix_entries); - printf("nyi\n"); + int r = glp_simplex(prob, NULL); + switch (r) { + case 0:; + + double got = glp_get_obj_val(prob); + printf("%g", got); + if (got <= best) { + printf("\n"); + break; + } + + best = got; + + if (best_prob) glp_delete_prob(best_prob); + best_prob = prob; + + free(best_adjmatrix); + best_adjmatrix = xalloc_adjmatrix(); + memcpy(best_adjmatrix, adjmatrix, sizeof(*adjmatrix)*n); + + printf(" <--\n"); + return; + +#define OKERR(e) \ + case e: printf(" " #e "\n"); break; + OKERR(GLP_ESING); + OKERR(GLP_ECOND); + OKERR(GLP_EBOUND); + OKERR(GLP_EFAIL); + OKERR(GLP_ENOPFS); + OKERR(GLP_ENODFS); + +#define BADERR(e) \ + case e: printf(" " #e " CRASHING\n"); exit(-1); + BADERR(GLP_EBADB); + BADERR(GLP_EOBJLL); + BADERR(GLP_EOBJUL); + BADERR(GLP_EITLIM); + BADERR(GLP_ETMLIM); + + default: printf(" r=%d! CRASHING\n", r); exit(-1); + } + + glp_delete_prob(prob); } static void iterate_recurse(int i, AdjWord min) { -- 2.30.2