chiark / gitweb /
wip lp, results, compiles
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 7 Mar 2014 15:32:54 +0000 (15:32 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 7 Mar 2014 15:32:54 +0000 (15:32 +0000)
Makefile
main.c

index 166f614380eaf5cf6406a7bfa3ef96716111aaa4..e91ae036b62f91a997301fd356dd94441b52c8c0 100644 (file)
--- 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 22fa3437bfd7b7b7d3e43a2a25d69ea33a6ef26b..bdcb18bfbb2c2fa54582aa14b383539e92a28e49 100644 (file)
--- a/main.c
+++ b/main.c
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <string.h>
 #include <assert.h>
 #include <inttypes.h>
 
@@ -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) {