Debian Codegraph @

SYMPHONY/Applications/MATCH/src/Master/user_main.c

raw
1 /*===========================================================================*/
2 /* */
3 /* This file is part of the SYMPHONY Branch, Cut, and Price Library. */
4 /* */
5 /* SYMPHONY was jointly developed by Ted Ralphs ([email protected]) and */
6 /* Laci Ladanyi ([email protected]). */
7 /* */
8 /* (c) Copyright 2000-2007 Ted Ralphs. All Rights Reserved. */
9 /* */
10 /* This software is licensed under the Eclipse Public License. Please see */
11 /* accompanying file for terms. */
12 /* */
13 /*===========================================================================*/
14 /*===========================================================================*/
15
16 #define CALL_FUNCTION(f) \
17 if ((termcode = f) < 0){ \
18 printf("Error detected: termcode = %i\n", termcode); \
19 printf("Exiting...\n\n"); \
20 exit(termcode); \
21 }
22
23 /*===========================================================================*\
24 This file contains the main() for the master process.
25
26 Note that, if you want to use the OSI SYMPHONY interface, you should set the
27 USE_OSI_INTERFACE flag and define the COINROOT path in the SYMPHONY
28 Makefile. Otherwise, the C callable library functions will be used by
29 default. See below for the usage.
30 \*===========================================================================*/
31
32 #include "symphony.h"
33 #include "sym_master.h"
34 #include "user.h"
35
36 #include <stdlib.h>
37
38
39 int match_test(sym_environment *env);
40
41 int main(int argc, char **argv)
42 {
43
44 int termcode;
45 char * infile;
46
47 /* Create a SYMPHONY environment */
48 sym_environment *env = sym_open_environment();
49
50 /* Print version info */
51 sym_version();
52
53 /* Create the data structure for storing the problem instance.*/
54 user_problem *prob = (user_problem *)calloc(1, sizeof(user_problem));
55
56 CALL_FUNCTION( sym_set_user_data(env, (void *)prob) );
57
58 CALL_FUNCTION( sym_parse_command_line(env, argc, argv) );
59
60 if(prob->par.test){
61
62 match_test (env);
63
64 } else {
65
66 CALL_FUNCTION( match_read_data(prob, prob->par.infile) );
67
68 CALL_FUNCTION( match_load_problem(env, prob) );
69
70 # if 0
71 int colsNum=-1;
72 sym_get_num_cols(env, &colsNum);
73 for(int j=0; j<=colsNum; ++j) {
74 int isInt=666;
75 int code=sym_is_integer(env, j, &isInt);
76 printf("j=%d code=%d isInt=%d\n", j, code, isInt);
77 } // j
78 #endif
79
80 CALL_FUNCTION( sym_solve(env) );
81 }
82
83 CALL_FUNCTION( sym_close_environment(env) );
84
85 return(0);
86
87 }
88
89 /*===========================================================================*\
90 \*===========================================================================*/
91
92 int match_read_data(user_problem *prob, char *infile)
93 {
94 int i, j;
95 FILE *f = NULL;
96
97 if ((f = fopen(infile, "r")) == NULL){
98 printf("main(): user file %s can't be opened\n", infile);
99 return(ERROR__USER);
100 }
101
102 /* Read in the costs */
103 fscanf(f,"%d",&(prob->numnodes));
104 for (i = 0; i < prob->numnodes; i++)
105 for (j = 0; j < prob->numnodes; j++)
106 fscanf(f, "%d", &(prob->cost[i][j]));
107
108 return (FUNCTION_TERMINATED_NORMALLY);
109 }
110
111 /*===========================================================================*\
112 \*===========================================================================*/
113
114 int match_load_problem(sym_environment *env, user_problem *prob){
115
116 int i, j, index, n, m, nz, *column_starts, *matrix_indices;
117 double *matrix_values, *lb, *ub, *obj, *rhs, *rngval;
118 char *sense, *is_int;
119
120 /* set up the inital LP data */
121 n = prob->numnodes*(prob->numnodes-1)/2;
122 m = prob->numnodes;
123 nz = 2 * n;
124
125 /* Allocate the arrays */
126 column_starts = (int *) malloc((n + 1) * ISIZE);
127 matrix_indices = (int *) malloc((nz) * ISIZE);
128 matrix_values = (double *) malloc((nz) * DSIZE);
129 obj = (double *) malloc(n * DSIZE);
130 lb = (double *) calloc(n, DSIZE);
131 ub = (double *) malloc(n * DSIZE);
132 rhs = (double *) malloc(m * DSIZE);
133 sense = (char *) malloc(m * CSIZE);
134 rngval = (double *) calloc(m, DSIZE);
135 is_int = (char *) malloc(n * CSIZE);
136
137 /* Fill out the appropriate data structures -- each column has
138 exactly two entries */
139 index = 0;
140 for (i = 0; i < prob->numnodes; i++) {
141 for (j = i+1; j < prob->numnodes; j++) {
142 prob->match1[index] = i; /*The first component of assignment 'index'*/
143 prob->match2[index] = j; /*The second componet of assignment 'index'*/
144 /* So we can recover the index later */
145 prob->index[i][j] = prob->index[j][i] = index;
146 obj[index] = prob->cost[i][j]; /* Cost of assignment (i, j) */
147 is_int[index] = TRUE;
148 column_starts[index] = 2*index;
149 matrix_values[2*index] = 1;
150 matrix_values[2*index+1] = 1;
151 matrix_indices[2*index] = i;
152 matrix_indices[2*index+1] = j;
153 ub[index] = 1.0;
154 index++;
155 }
156 }
157 column_starts[n] = 2 * n;
158
159 /* set the initial right hand side */
160 for (i = 0; i < m; i++) {
161 rhs[i] = 1;
162 sense[i] = 'E';
163 }
164
165 /* Load the problem to SYMPHONY */
166 sym_explicit_load_problem(env, n, m, column_starts, matrix_indices,
167 matrix_values, lb, ub, is_int, obj, 0, sense, rhs,
168 rngval, true);
169
170 FREE(column_starts);
171 FREE(matrix_indices);
172 FREE(matrix_values);
173 FREE(lb);
174 FREE(ub);
175 FREE(obj);
176 FREE(sense);
177 FREE(rhs);
178 FREE(rngval);
179 FREE(is_int);
180
181 return (FUNCTION_TERMINATED_NORMALLY);
182
183 }
184
185 /*===========================================================================*\
186 \*===========================================================================*/
187
188 int match_test(sym_environment *env)
189 {
190
191 int termcode, i, file_num = 1;
192 char input_files[1][MAX_FILE_NAME_LENGTH +1] = {"sample.mat"};
193
194 double sol[1] = {5.00};
195
196 char *input_dir = (char*)malloc(CSIZE*(MAX_FILE_NAME_LENGTH+1));
197 char *infile = (char*)malloc(CSIZE*(MAX_FILE_NAME_LENGTH+1));
198 double *obj_val = (double *)calloc(DSIZE,file_num);
199 double tol = 1e-03;
200 user_problem *prob = (user_problem *) env->user;
201
202 if (strcmp(env->par.test_dir, "") == 0){
203 strcpy(input_dir, ".");
204 } else{
205 strcpy(input_dir, env->par.test_dir);
206 }
207
208 sym_set_int_param(env, "verbosity", -10);
209
210 for(i = 0; i<file_num; i++){
211
212 strcpy(infile, "");
213 sprintf(infile, "%s%s%s", input_dir, "/", input_files[i]);
214
215 CALL_FUNCTION( match_read_data(prob, infile) );
216
217 CALL_FUNCTION( match_load_problem(env, prob) );
218
219 printf("Solving %s...\n", input_files[i]);
220
221 CALL_FUNCTION( sym_solve(env) );
222
223 sym_get_obj_val(env, &obj_val[i]);
224
225 if((obj_val[i] < sol[i] + tol) &&
226 (obj_val[i] > sol[i] - tol)){
227 printf("Success!\n");
228 } else {
229 printf("Failure!(%f, %f) \n", obj_val[i], sol[i]);
230 }
231
232 if(env->mip->n && i + 1 < file_num){
233 free_master_u(env);
234 strcpy(env->par.infile, "");
235 env->mip = (MIPdesc *) calloc(1, sizeof(MIPdesc));
236 }
237 }
238
239 FREE(input_dir);
240 FREE(infile);
241 FREE(obj_val);
242
243 return(0);
244
245 }
246