summaryrefslogtreecommitdiff
path: root/packages/glpk/src/C/glpk.c
blob: 86b12773e2d8d27813c355bce6b52f6497a1604d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#define DVEC(A) int A##n, double*A##p
#define DMAT(A) int A##r, int A##c, double*A##p

#define AT(M,r,co) (M##p[(r)*M##c+(co)])

#include <stdlib.h>
#include <stdio.h>
#include <glpk.h>
#include <math.h>

/*-----------------------------------------------------*/

#define C_X_SPARSE(X)                                           \
  int c_##X##_sparse(int m, int n, DMAT(c), DMAT(b), DVEC(s)) { \
    glp_prob *lp;                                               \
    lp = glp_create_prob();                                     \
    glp_set_obj_dir(lp, GLP_MAX);                               \
    int i,j,k;                                                  \
    int tot = cr - n;                                           \
    glp_add_rows(lp, m);                                        \
    glp_add_cols(lp, n);                                        \
                                                                \
    /*printf("%d %d\n",m,n);*/                                  \
                                                                \
    /* the first n values */                                    \
    for (k=1;k<=n;k++) {                                        \
      glp_set_obj_coef(lp, k, AT(c, k-1, 2));                   \
      /*printf("%d %f\n",k,AT(c, k-1, 2)); */                   \
    }                                                           \
                                                                \
    int * ia = malloc((1+tot)*sizeof(int));                     \
    int * ja = malloc((1+tot)*sizeof(int));                     \
    double * ar = malloc((1+tot)*sizeof(double));               \
                                                                \
    for (k=1; k<= tot; k++) {                                   \
      ia[k] = rint(AT(c,k-1+n,0));                              \
      ja[k] = rint(AT(c,k-1+n,1));                              \
      ar[k] =      AT(c,k-1+n,2);                               \
      /*printf("%d %d %f\n",ia[k],ja[k],ar[k]);*/               \
    }                                                           \
    glp_load_matrix(lp, tot, ia, ja, ar);                       \
                                                                \
    int t;                                                      \
    for (i=1;i<=m;i++) {                                        \
      switch((int)rint(AT(b,i-1,0))) {                          \
      case 0: { t = GLP_FR; break; }                            \
      case 1: { t = GLP_LO; break; }                            \
      case 2: { t = GLP_UP; break; }                            \
      case 3: { t = GLP_DB; break; }                            \
      default: { t = GLP_FX; break; }                           \
      }                                                         \
      glp_set_row_bnds(lp, i, t , AT(b,i-1,1), AT(b,i-1,2));    \
    }                                                           \
    for (j=1;j<=n;j++) {                                        \
      switch((int)rint(AT(b,m+j-1,0))) {                        \
      case 0: { t = GLP_FR; break; }                            \
      case 1: { t = GLP_LO; break; }                            \
      case 2: { t = GLP_UP; break; }                            \
      case 3: { t = GLP_DB; break; }                            \
      default: { t = GLP_FX; break; }                           \
      }                                                         \
      glp_set_col_bnds(lp, j, t , AT(b,m+j-1,1), AT(b,m+j-1,2));        \
    }                                                                   \
    glp_term_out(0);                                                    \
    glp_##X(lp, NULL);                                                  \
      sp[0] = glp_get_status(lp);                                       \
      sp[1] = glp_get_obj_val(lp);                                      \
      for (k=1; k<=n; k++) {                                            \
        sp[k+1] = glp_get_col_prim(lp, k);                              \
      }                                                                 \
      glp_delete_prob(lp);                                              \
      free(ia);                                                         \
      free(ja);                                                         \
      free(ar);                                                         \
                                                                        \
      return 0;                                                         \
  }                                                                     \

C_X_SPARSE(simplex);
C_X_SPARSE(exact);