/* solve LP problem coefficients are read from a file matrices are allocated dynamically sample data files lp-t-0.dat lp-t-1.dat lp-t-2.dat Usage: lp-t1.out or lp-t1.out filename */ #include #include "opt.h" main(int argc, char **argv) { FILE *fd; char *filename = "lp-t-0.dat"; int m, n; double *a, *b, *c, *d, *ox, *ov; double eps; status st; if (argc >= 2) { filename = argv[1]; } if ((fd = fopen(filename,"r")) == NULL) { fprintf(stderr, "cannot open file %s\n", filename); exit(1); } fprintf(stderr, "\n Solving LP in file %s\n\n", filename); fscanf(fd, "%d %d", &m, &n); matrixallocfscan(fd, m, n, &a); /* a : m x n matrix */ matrixallocfscan(fd, m, 1, &b); /* b : m x 1 matrix */ matrixallocfscan(fd, 1, n, &c); /* c : 1 x n matrix */ matrixallocfscan(fd, 1, 1, &d); /* d : 1 x 1 matrix */ fclose(fd); matrixfprint(stdout, m, n, a); matrixfprint(stdout, m, 1, b); matrixfprint(stdout, 1, n, c); matrixfprint(stdout, 1, 1, d); ox = allc(dbl,n*1); ov = allc(dbl,1*1); eps = 1e-5; st = LPsimplex(m, n, a, b, c, d, eps, ox, ov); if (st == success) { puts("--- converge ---"); printf("optimal solution\n"); matrixfprint(stdout, n, 1, ox); printf("optimal value = %lf\n", ov[0]); } else if (st == failure) { puts("--- diverge ---"); } else { puts("--- error ---"); } }