-
Notifications
You must be signed in to change notification settings - Fork 6
/
do_lp.c
158 lines (153 loc) · 4.02 KB
/
do_lp.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#define REAL 0
#define PROJECTIVE 1
#include "lp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_lp(FLOAT **halves, int *d, int *m, FLOAT **n_vec, FLOAT **d_vec, int *itype);
int lp_d_unit(int d, FLOAT a[], FLOAT b[]);
int main(int argc,char *argv[])
{
int *next, *prev;
FLOAT *halves, *n_vec, *opt, *d_vec;
int *perm, r;
int m, d, i, j, k;
int status = AMBIGUOUS, type;
FLOAT *work;
int repeat;
if(argc>1) {
repeat = atoi(argv[1]);
} else {
repeat = 1;
}
if(read_lp(&halves,&d,&m,&n_vec, &d_vec, &type)) {
perm = (int*)malloc((unsigned)(m-1)*sizeof(int));
next = (int*)malloc((unsigned)(m)*sizeof(int));
prev = (int*)malloc((unsigned)(m)*sizeof(int));
printf("dimension: %d, number of planes: %d, repeat: %d\n",
d,m,repeat);
opt = (FLOAT *)malloc((unsigned)(d+1)*sizeof(FLOAT));
work = (FLOAT *)malloc((unsigned)(m+3)*(d+2)*(d-1)/2*sizeof(FLOAT));
for(r=0; r<repeat; r++) {
/* randomize the input planes */
randperm(m-1,perm);
/* previous to 0 should never be used */
prev[0] = 1234567890;
/* link the zero position in at the beginning */
next[0] = perm[0]+1;
prev[perm[0]+1] = 0;
/* link the other planes */
for(i=0; i<m-2; i++) {
next[perm[i]+1] = perm[i+1]+1;
prev[perm[i+1]+1] = perm[i]+1;
}
/* flag the last plane */
next[perm[m-2]+1] = m;
status = linprog(halves,0,m,n_vec,d_vec,d,opt,work,
next,prev,m);
}
switch(status) {
case INFEASIBLE:
(void)printf("no feasible solution\n");
break;
case MINIMUM:
(void)printf("minimum attained at\n");
break;
case UNBOUNDED:
(void)printf("region is unbounded: last vertex is\n");
break;
case AMBIGUOUS:
(void)printf("region is bounded by plane orthogonal\n");
(void)printf("to minimization vector: one vertex is\n");
break;
default:
(void)printf("unknown case returned from linprog\n");
}
if(status!=INFEASIBLE) {
if(type == PROJECTIVE) {
(void)printf("(");
for(i=0; i<d; i++) {
if(opt[d]==0.0) {
(void)printf("%f",opt[i]);
} else {
(void)printf("%f",opt[i]/opt[d]);
}
if(i!=d-1)(void)printf(",");
}
(void)printf(")\n");
if(opt[d]==0.0) (void)printf("at infinite\n");
for(j=0; j!=m; j=next[j]) {
FLOAT val = 0.0;
for(k=0; k<=d; k++) val += opt[k]*halves[j*(d+1)+k];
if(val <-d*EPS) {
printf("error\n");
exit(1);
}
}
} else {
(void)printf("(");
for(i=0; i<=d; i++) {
(void)printf("%f",opt[i]);
if(i!=d)(void)printf(",");
}
(void)printf(")\n");
}
}
free((char *)halves);
free((char *)next);
free((char *)prev);
free((char *)perm);
free((char *)n_vec);
free((char *)opt);
free((char *)work);
} else {
(void)printf("parse error\n");
}
return 0;
}
#ifdef DOUBLE
#define CONVERSION "%lf"
#else
#define CONVERSION "%f"
#endif
int read_lp(FLOAT **halves, int *d, int *m, FLOAT **n_vec, FLOAT **d_vec, int *itype)
{
int i, j;
char type[100];
if(scanf("dimension: %d, number of planes: %d\n",d,m) !=2)return(0);
if((i=scanf("%s",type)) !=1)return(0);
*n_vec = (FLOAT *)malloc((unsigned)(*d+1)*sizeof(FLOAT));
*d_vec = (FLOAT *)malloc((unsigned)(*d+1)*sizeof(FLOAT));
*halves = (FLOAT *)malloc((unsigned)*m*(*d+1)*sizeof(FLOAT));
for(i=0; i<=*d; i++) {
(*d_vec)[i] = 0.0;
}
if(!strcmp(type,"projective")) {
*itype = PROJECTIVE;
for(i=0; i<*d; i++) {
if(scanf(CONVERSION,(*n_vec)+i)!=1) goto err;
}
(*d_vec)[*d] = 1.0;
(*n_vec)[*d] = 0.0;
} else if(!strcmp(type,"real")){
*itype = REAL;
for(i=0; i<=*d; i++) {
if(scanf(CONVERSION,(*n_vec)+i)!=1) goto err;
}
} else {
goto err;
}
for(i=0; i<*m; i++) {
for(j=0; j<=*d; j++) {
if(scanf(CONVERSION,(*halves)+i*(*d+1)+j)!=1) {
goto err;
}
}
(void)lp_d_unit(*d,(*halves) + i*(*d+1),(*halves) + i*(*d+1));
}
return(1);
err: free((char *)*d_vec);
free((char *)*n_vec);
free((char *)*halves);
return(0);
}