relax4 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +145 -0
- data/ext/extconf.rb +11 -0
- data/ext/relax4.c +3935 -0
- data/ext/relax4.h +187 -0
- data/ext/relax4_f2c.h +247 -0
- data/ext/relax4_wrap.c +2332 -0
- data/lib/relax4/version.rb +3 -0
- data/lib/relax4.rb +223 -0
- metadata +77 -0
data/ext/relax4.h
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
#ifndef RELAX4_H_
|
2
|
+
#define RELAX4_H_
|
3
|
+
|
4
|
+
/* Constants */
|
5
|
+
#define RELAX4_OK 0
|
6
|
+
#define RELAX4_INFEASIBLE 1
|
7
|
+
#define RELAX4_FAIL_OUT_OF_MEMORY 2
|
8
|
+
#define RELAX4_FAIL_BAD_SIZE 3
|
9
|
+
#define RELAX4_FAIL_BAD_NODE 4
|
10
|
+
#define RELAX4_FAIL_BAD_COST 5
|
11
|
+
#define RELAX4_FAIL_BAD_CAPACITY 6
|
12
|
+
#define RELAX4_OUTPUT_FAIL_NONZERO_DEMAND 101
|
13
|
+
#define RELAX4_OUTPUT_FAIL_COMPLEMENTARY_SLACKNESS 102
|
14
|
+
|
15
|
+
/**
|
16
|
+
* The default value used for the <tt>large</tt> parameter. It is set to 500
|
17
|
+
* million, which is the value in the original relax4 source.
|
18
|
+
*
|
19
|
+
* Further guidance on how to set <tt>large</tt> is given in NOTE 3 in the
|
20
|
+
* original source:
|
21
|
+
* ALL PROBLEM DATA SHOULD BE LESS THAN LARGE IN MAGNITUDE, AND LARGE SHOULD BE
|
22
|
+
* LESS THAN, SAY, 1/4 THE LARGEST INTEGER*4 OF THE MACHINE USED. THIS WILL
|
23
|
+
* GUARD PRIMARILY AGAINST OVERFLOW IN UNCAPACITATED PROBLEMS WHERE THE ARC
|
24
|
+
* CAPACITIES ARE TAKEN FINITE BUT VERY LARGE. NOTE, HOWEVER, THAT AS IN ALL
|
25
|
+
* CODES OPERATING WITH INTEGERS, OVERFLOW MAY OCCUR IF SOME OF THE PROBLEM DATA
|
26
|
+
* TAKES VERY LARGE VALUES.
|
27
|
+
*
|
28
|
+
* Edge capacities must be non-negative and less than or equal to
|
29
|
+
* <tt>large</tt>, and edge costs should not exceed <tt>large/10</tt> in
|
30
|
+
* absolute value, in order to avoid overflow (see RELAX4_DEFAULT_MAX_COST).
|
31
|
+
*/
|
32
|
+
#define RELAX4_DEFAULT_LARGE 500000000
|
33
|
+
|
34
|
+
/**
|
35
|
+
* The guidance in the original relax4 source is that edge costs should not
|
36
|
+
* exceed <tt>large/10</tt> in absolute value, in order to avoid overflow.
|
37
|
+
*
|
38
|
+
* This can be enforced by the relax4_check_inputs function.
|
39
|
+
*/
|
40
|
+
#define RELAX4_DEFAULT_MAX_COST (RELAX4_DEFAULT_LARGE/10)
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Set global state and allocate memory for internal arrays.
|
44
|
+
*
|
45
|
+
* The input arrays are not copied. You can solve several instances of the same
|
46
|
+
* size (same number of nodes and arcs) by modifying the input arrays and then
|
47
|
+
* restarting the call sequence from relax4_check_inputs (or relax4_init_phase1,
|
48
|
+
* if you trust your inputs).
|
49
|
+
*
|
50
|
+
* There can be only one relax4 problem active at a time, because it currently
|
51
|
+
* uses global state.
|
52
|
+
*
|
53
|
+
* The caller is responsible for allocating (and later freeing) the arrays
|
54
|
+
* passed to this function. The relax4_free method should be called to free the
|
55
|
+
* internal arrays. If this call fails, you do not have to call relax4_free to
|
56
|
+
* clean up the internal arrays.
|
57
|
+
*
|
58
|
+
* @param[in] num_nodes number of nodes in the graph; strictly positive
|
59
|
+
*
|
60
|
+
* @param[in] num_arcs number of arcs (edges) in the graph; strictly positive
|
61
|
+
*
|
62
|
+
* @param[in] start_nodes index of node at the start of each arc; the first node
|
63
|
+
* is numbered 1.
|
64
|
+
*
|
65
|
+
* @param[in] end_nodes index of node at the end of each arc; the first node
|
66
|
+
* is numbered 1.
|
67
|
+
*
|
68
|
+
* @param[in] costs cost for each arc; negative costs are allowed; negative cost
|
69
|
+
* cycles are allowed (flows on edges involved in such a cycle are set to
|
70
|
+
* <tt>large</tt>); absolute values of costs must be less than <tt>large</tt>
|
71
|
+
* and should be less than <tt>large/10</tt> to avoid overflow (see
|
72
|
+
* RELAX4_DEFAULT_MAX_COST).
|
73
|
+
*
|
74
|
+
* @param[in] capacities capacity for each arc; for an uncapacitated problem,
|
75
|
+
* set these to a suitably large value (such as <tt>large</tt>); capacities
|
76
|
+
* must be in [0, <tt>large</tt>].
|
77
|
+
*
|
78
|
+
* @param[in] demands demand for each node; a node with negative demand is a
|
79
|
+
* surplus node; demands should balance (sum to zero), or else the problem will
|
80
|
+
* be infeasible.
|
81
|
+
*
|
82
|
+
* @param[in] flows flow on each arc; the solution from relax4_run is stored
|
83
|
+
* in this array.
|
84
|
+
*
|
85
|
+
* @param[in] large a very large integer to represent infinity; see
|
86
|
+
* RELAX4_LARGE_DEFAULT for more information.
|
87
|
+
*
|
88
|
+
* @return RELAX4_OK if allocations succeeded, or RELAX4_FAIL_OUT_OF_MEMORY if
|
89
|
+
* any failed.
|
90
|
+
*/
|
91
|
+
int relax4_init(integer num_nodes, integer num_arcs,
|
92
|
+
integer start_nodes[],
|
93
|
+
integer end_nodes[],
|
94
|
+
integer costs[],
|
95
|
+
integer capacities[],
|
96
|
+
integer demands[],
|
97
|
+
integer flows[],
|
98
|
+
integer large);
|
99
|
+
|
100
|
+
/**
|
101
|
+
* Basic checks on the parameters given to relax4_init.
|
102
|
+
*
|
103
|
+
* You do not have to call this method, but it is recommended (avoid segfaults).
|
104
|
+
*
|
105
|
+
* @param max_cost return RELAX4_FAIL_BAD_COST if any arc cost exceeds
|
106
|
+
* max_cost in absolute value; see RELAX4_DEFAULT_MAX_COST.
|
107
|
+
*
|
108
|
+
* @return RELAX4_OK if checks pass; RELAX4_FAIL_BAD_SIZE (zero arcs or nodes),
|
109
|
+
* RELAX4_FAIL_BAD_NODE (in start_nodes or end_nodes), RELAX4_FAIL_BAD_COST or
|
110
|
+
* RELAX4_FAIL_BAD_CAPACITY otherwise.
|
111
|
+
*/
|
112
|
+
int relax4_check_inputs(int max_cost);
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Reduce arc capacities by as much as possible without changing the problem.
|
116
|
+
*
|
117
|
+
* @return RELAX4_OK if successful; RELAX4_INFEASIBLE if problem was found
|
118
|
+
* to be infeasible during arc capacity reduction.
|
119
|
+
*/
|
120
|
+
int relax4_init_phase_1();
|
121
|
+
|
122
|
+
/**
|
123
|
+
* Initialize the arc flows to satisfy complementary slackness with the node
|
124
|
+
* prices.
|
125
|
+
*
|
126
|
+
* You must call relax4_init_phase_1 first.
|
127
|
+
* Call either this method or relax4_auction, but do not call both.
|
128
|
+
*
|
129
|
+
* @return RELAX4_OK if successful; RELAX4_INFEASIBLE if problem was found to be
|
130
|
+
* infeasible.
|
131
|
+
*/
|
132
|
+
int relax4_init_phase_2();
|
133
|
+
|
134
|
+
/**
|
135
|
+
* Uses a version of the auction algorithm for min cost network flow to compute
|
136
|
+
* a good initial flow and prices for the problem.
|
137
|
+
*
|
138
|
+
* You must call relax4_init_phase_1 first.
|
139
|
+
* Call either this method or relax4_init_phase_2, but do not call both.
|
140
|
+
*
|
141
|
+
* You can get an approximate solution using the auction algorithm by just
|
142
|
+
* calling this method (and not calling relax4_run). The flows will be stored in
|
143
|
+
* the <tt>flows</tt> array passed to relax4_init.
|
144
|
+
*
|
145
|
+
* @return RELAX4_OK if successful; RELAX4_INFEASIBLE if problem was found to be
|
146
|
+
* infeasible.
|
147
|
+
*/
|
148
|
+
int relax4_auction();
|
149
|
+
|
150
|
+
/**
|
151
|
+
* The main solve routine.
|
152
|
+
*
|
153
|
+
* If the problem is feasible, the optimal flows will be stored in
|
154
|
+
* the <tt>flows</tt> array passed to relax4_init. Otherwise, RELAX4_INFEASIBLE
|
155
|
+
* is returned. See also the notes in relax4_init regarding negative cost
|
156
|
+
* cycles.
|
157
|
+
*
|
158
|
+
* You must call (exactly) one of relax4_init_phase_2 or relax4_auction first.
|
159
|
+
*
|
160
|
+
* @return RELAX4_OK if successful; RELAX4_INFEASIBLE if problem was found to be
|
161
|
+
* infeasible.
|
162
|
+
*/
|
163
|
+
int relax4_run();
|
164
|
+
|
165
|
+
/**
|
166
|
+
* Check that there are no unsatisfied demands and that complementary slackness
|
167
|
+
* conditions are satisfied.
|
168
|
+
*
|
169
|
+
* You must call relax4_run first.
|
170
|
+
* You do not have to call this method; it just provides an independent check on
|
171
|
+
* the correctness of relax4_run.
|
172
|
+
*
|
173
|
+
* @return RELAX4_OK if output passes checks; otherwise one of
|
174
|
+
* RELAX4_OUTPUT_FAIL_NONZERO_DEMAND or
|
175
|
+
* RELAX4_OUTPUT_FAIL_COMPLEMENTARY_SLACKNESS.
|
176
|
+
*/
|
177
|
+
int relax4_check_output();
|
178
|
+
|
179
|
+
/**
|
180
|
+
* Free internal working arrays.
|
181
|
+
*
|
182
|
+
* The caller is responsible for freeing the input and output arrays passed in
|
183
|
+
* via relax4_init.
|
184
|
+
*/
|
185
|
+
void relax4_free();
|
186
|
+
|
187
|
+
#endif /* guard */
|
data/ext/relax4_f2c.h
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
/* RELAX4 includes its own copy of f2c.h in order to avoid depending on f2c,
|
2
|
+
* since we really just need this header.
|
3
|
+
*/
|
4
|
+
|
5
|
+
/* f2c.h -- Standard Fortran to C header file */
|
6
|
+
|
7
|
+
/** barf [ba:rf] 2. "He suggested using FORTRAN, and everybody barfed."
|
8
|
+
|
9
|
+
- From The Shogakukan DICTIONARY OF NEW ENGLISH (Second edition) */
|
10
|
+
|
11
|
+
#ifndef RELAX4_F2C_INCLUDE
|
12
|
+
#define RELAX4_F2C_INCLUDE
|
13
|
+
|
14
|
+
#if defined(__alpha__) || defined(__sparc64__) || defined(__x86_64__) || defined(__ia64__)
|
15
|
+
typedef int integer;
|
16
|
+
typedef unsigned int uinteger;
|
17
|
+
#else
|
18
|
+
typedef long int integer;
|
19
|
+
typedef unsigned long int uinteger;
|
20
|
+
#endif
|
21
|
+
typedef char *address;
|
22
|
+
typedef short int shortint;
|
23
|
+
typedef float real;
|
24
|
+
typedef double doublereal;
|
25
|
+
typedef struct { real r, i; } complex;
|
26
|
+
typedef struct { doublereal r, i; } doublecomplex;
|
27
|
+
#if defined(__alpha__) || defined(__sparc64__) || defined(__x86_64__) || defined(__ia64__)
|
28
|
+
typedef int logical;
|
29
|
+
#else
|
30
|
+
typedef long int logical;
|
31
|
+
#endif
|
32
|
+
typedef short int shortlogical;
|
33
|
+
typedef char logical1;
|
34
|
+
typedef char integer1;
|
35
|
+
#ifdef INTEGER_STAR_8 /* Adjust for integer*8. */
|
36
|
+
#if defined(__alpha__) || defined(__sparc64__) || defined(__x86_64__) || defined(__ia64__)
|
37
|
+
typedef long longint; /* system-dependent */
|
38
|
+
typedef unsigned long ulongint; /* system-dependent */
|
39
|
+
#else
|
40
|
+
typedef long long longint; /* system-dependent - oh yeah*/
|
41
|
+
typedef unsigned long long ulongint; /* system-dependent - oh yeah*/
|
42
|
+
#endif
|
43
|
+
#define qbit_clear(a,b) ((a) & ~((ulongint)1 << (b)))
|
44
|
+
#define qbit_set(a,b) ((a) | ((ulongint)1 << (b)))
|
45
|
+
#endif
|
46
|
+
|
47
|
+
#define TRUE_ (1)
|
48
|
+
#define FALSE_ (0)
|
49
|
+
|
50
|
+
/* Extern is for use with -E */
|
51
|
+
#ifndef Extern
|
52
|
+
#define Extern extern
|
53
|
+
#endif
|
54
|
+
|
55
|
+
/* I/O stuff */
|
56
|
+
|
57
|
+
#ifdef f2c_i2
|
58
|
+
/* for -i2 */
|
59
|
+
typedef short flag;
|
60
|
+
typedef short ftnlen;
|
61
|
+
typedef short ftnint;
|
62
|
+
#else
|
63
|
+
#if defined(__alpha__) || defined(__sparc64__) || defined(__x86_64__) || defined(__ia64__)
|
64
|
+
typedef int flag;
|
65
|
+
typedef int ftnlen;
|
66
|
+
typedef int ftnint;
|
67
|
+
#else
|
68
|
+
typedef long int flag;
|
69
|
+
typedef long int ftnlen;
|
70
|
+
typedef long int ftnint;
|
71
|
+
#endif
|
72
|
+
#endif
|
73
|
+
|
74
|
+
/*external read, write*/
|
75
|
+
typedef struct
|
76
|
+
{ flag cierr;
|
77
|
+
ftnint ciunit;
|
78
|
+
flag ciend;
|
79
|
+
char *cifmt;
|
80
|
+
ftnint cirec;
|
81
|
+
} cilist;
|
82
|
+
|
83
|
+
/*internal read, write*/
|
84
|
+
typedef struct
|
85
|
+
{ flag icierr;
|
86
|
+
char *iciunit;
|
87
|
+
flag iciend;
|
88
|
+
char *icifmt;
|
89
|
+
ftnint icirlen;
|
90
|
+
ftnint icirnum;
|
91
|
+
} icilist;
|
92
|
+
|
93
|
+
/*open*/
|
94
|
+
typedef struct
|
95
|
+
{ flag oerr;
|
96
|
+
ftnint ounit;
|
97
|
+
char *ofnm;
|
98
|
+
ftnlen ofnmlen;
|
99
|
+
char *osta;
|
100
|
+
char *oacc;
|
101
|
+
char *ofm;
|
102
|
+
ftnint orl;
|
103
|
+
char *oblnk;
|
104
|
+
} olist;
|
105
|
+
|
106
|
+
/*close*/
|
107
|
+
typedef struct
|
108
|
+
{ flag cerr;
|
109
|
+
ftnint cunit;
|
110
|
+
char *csta;
|
111
|
+
} cllist;
|
112
|
+
|
113
|
+
/*rewind, backspace, endfile*/
|
114
|
+
typedef struct
|
115
|
+
{ flag aerr;
|
116
|
+
ftnint aunit;
|
117
|
+
} alist;
|
118
|
+
|
119
|
+
/* inquire */
|
120
|
+
typedef struct
|
121
|
+
{ flag inerr;
|
122
|
+
ftnint inunit;
|
123
|
+
char *infile;
|
124
|
+
ftnlen infilen;
|
125
|
+
ftnint *inex; /*parameters in standard's order*/
|
126
|
+
ftnint *inopen;
|
127
|
+
ftnint *innum;
|
128
|
+
ftnint *innamed;
|
129
|
+
char *inname;
|
130
|
+
ftnlen innamlen;
|
131
|
+
char *inacc;
|
132
|
+
ftnlen inacclen;
|
133
|
+
char *inseq;
|
134
|
+
ftnlen inseqlen;
|
135
|
+
char *indir;
|
136
|
+
ftnlen indirlen;
|
137
|
+
char *infmt;
|
138
|
+
ftnlen infmtlen;
|
139
|
+
char *inform;
|
140
|
+
ftnint informlen;
|
141
|
+
char *inunf;
|
142
|
+
ftnlen inunflen;
|
143
|
+
ftnint *inrecl;
|
144
|
+
ftnint *innrec;
|
145
|
+
char *inblank;
|
146
|
+
ftnlen inblanklen;
|
147
|
+
} inlist;
|
148
|
+
|
149
|
+
#define VOID void
|
150
|
+
|
151
|
+
union Multitype { /* for multiple entry points */
|
152
|
+
integer1 g;
|
153
|
+
shortint h;
|
154
|
+
integer i;
|
155
|
+
/* longint j; */
|
156
|
+
real r;
|
157
|
+
doublereal d;
|
158
|
+
complex c;
|
159
|
+
doublecomplex z;
|
160
|
+
};
|
161
|
+
|
162
|
+
typedef union Multitype Multitype;
|
163
|
+
|
164
|
+
/*typedef long int Long;*/ /* No longer used; formerly in Namelist */
|
165
|
+
|
166
|
+
struct Vardesc { /* for Namelist */
|
167
|
+
char *name;
|
168
|
+
char *addr;
|
169
|
+
ftnlen *dims;
|
170
|
+
int type;
|
171
|
+
};
|
172
|
+
typedef struct Vardesc Vardesc;
|
173
|
+
|
174
|
+
struct Namelist {
|
175
|
+
char *name;
|
176
|
+
Vardesc **vars;
|
177
|
+
int nvars;
|
178
|
+
};
|
179
|
+
typedef struct Namelist Namelist;
|
180
|
+
|
181
|
+
#define abs(x) ((x) >= 0 ? (x) : -(x))
|
182
|
+
#define dabs(x) (doublereal)abs(x)
|
183
|
+
#define min(a,b) ((a) <= (b) ? (a) : (b))
|
184
|
+
#define max(a,b) ((a) >= (b) ? (a) : (b))
|
185
|
+
#define dmin(a,b) (doublereal)min(a,b)
|
186
|
+
#define dmax(a,b) (doublereal)max(a,b)
|
187
|
+
#define bit_test(a,b) ((a) >> (b) & 1)
|
188
|
+
#define bit_clear(a,b) ((a) & ~((uinteger)1 << (b)))
|
189
|
+
#define bit_set(a,b) ((a) | ((uinteger)1 << (b)))
|
190
|
+
|
191
|
+
/* procedure parameter types for -A and -C++ */
|
192
|
+
|
193
|
+
#define F2C_proc_par_types 1
|
194
|
+
#ifdef __cplusplus
|
195
|
+
typedef int /* Unknown procedure type */ (*U_fp)(...);
|
196
|
+
typedef shortint (*J_fp)(...);
|
197
|
+
typedef integer (*I_fp)(...);
|
198
|
+
typedef real (*R_fp)(...);
|
199
|
+
typedef doublereal (*D_fp)(...), (*E_fp)(...);
|
200
|
+
typedef /* Complex */ VOID (*C_fp)(...);
|
201
|
+
typedef /* Double Complex */ VOID (*Z_fp)(...);
|
202
|
+
typedef logical (*L_fp)(...);
|
203
|
+
typedef shortlogical (*K_fp)(...);
|
204
|
+
typedef /* Character */ VOID (*H_fp)(...);
|
205
|
+
typedef /* Subroutine */ int (*S_fp)(...);
|
206
|
+
#else
|
207
|
+
typedef int /* Unknown procedure type */ (*U_fp)();
|
208
|
+
typedef shortint (*J_fp)();
|
209
|
+
typedef integer (*I_fp)();
|
210
|
+
typedef real (*R_fp)();
|
211
|
+
typedef doublereal (*D_fp)(), (*E_fp)();
|
212
|
+
typedef /* Complex */ VOID (*C_fp)();
|
213
|
+
typedef /* Double Complex */ VOID (*Z_fp)();
|
214
|
+
typedef logical (*L_fp)();
|
215
|
+
typedef shortlogical (*K_fp)();
|
216
|
+
typedef /* Character */ VOID (*H_fp)();
|
217
|
+
typedef /* Subroutine */ int (*S_fp)();
|
218
|
+
#endif
|
219
|
+
/* E_fp is for real functions when -R is not specified */
|
220
|
+
typedef VOID C_f; /* complex function */
|
221
|
+
typedef VOID H_f; /* character function */
|
222
|
+
typedef VOID Z_f; /* double complex function */
|
223
|
+
typedef doublereal E_f; /* real function with -R not specified */
|
224
|
+
|
225
|
+
/* undef any lower-case symbols that your C compiler predefines, e.g.: */
|
226
|
+
|
227
|
+
#ifndef Skip_f2c_Undefs
|
228
|
+
#undef cray
|
229
|
+
#undef gcos
|
230
|
+
#undef mc68010
|
231
|
+
#undef mc68020
|
232
|
+
#undef mips
|
233
|
+
#undef pdp11
|
234
|
+
#undef sgi
|
235
|
+
#undef sparc
|
236
|
+
#undef sun
|
237
|
+
#undef sun2
|
238
|
+
#undef sun3
|
239
|
+
#undef sun4
|
240
|
+
#undef u370
|
241
|
+
#undef u3b
|
242
|
+
#undef u3b2
|
243
|
+
#undef u3b5
|
244
|
+
#undef unix
|
245
|
+
#undef vax
|
246
|
+
#endif
|
247
|
+
#endif
|