-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_main.c
356 lines (297 loc) · 12.4 KB
/
color_main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
This file is part of exactcolors.
exactcolors is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
exactcolors is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with exactcolors. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include <getopt.h>
#include <string.h>
#include <assert.h>
#include "color_defs.h"
#include "color_parms.h"
#include "color_private.h"
static int get_problem_name(char* pname,const char* efname);
static void usage (char *f)
{
fprintf (stderr, "Usage %s: [-see below-] edge_file\n", f);
fprintf (stderr, " -d turn on debugging\n");
fprintf (stderr, " -b d write intermediate solutions to directory d\n");
fprintf (stderr, " -o f write coloring to file f\n");
fprintf (stderr, " -m write final stable set and clique instances\n");
fprintf (stderr, " -r f read initial stable sets from file f\n");
fprintf (stderr, " -w f write stable sets to file f\n");
fprintf (stderr, " -c f read initial coloring from file to track optimum path in the B&B-tree f\n");
fprintf (stderr, " -p start boss of parallel coloring\n");
fprintf (stderr, " -u int initial upper bound\n");
fprintf (stderr, " -a use B&B as coloring heuristic for upper bouns.\n");
fprintf (stderr, " -s int Branching strategy: 0 = none, 1 = minimum lower bound (default),"
" 2 = DFS, 3 = hybrid (2 followed by 1).\n");
fprintf (stderr, " -R int rounding style: 0 = neighbor (default), 1 = uniform, 2 = none\n");
fprintf (stderr, " -l dbl cpu time limit for branching.\n");
}
static int parseargs (int ac, char **av, COLORparms* parms)
{
int c;
int rval = 0;
int debug = COLORdbg_lvl();
while ((c = getopt (ac, av, "admpo:r:w:c:u:b:l:s:R:")) != EOF) {
switch (c) {
case 'd':
/* each -d increases the verbosity by one.*/
++(debug);
COLORset_dbg_lvl(debug);
break;
case 'o':
rval = COLORparms_set_outfile(parms,optarg);
COLORcheck_rval(rval,"Failed in COLORparms_set_outfile");
break;
case 'm':
COLORparms_set_write_mwis(parms,1);
break;
case 'r':
rval = COLORparms_set_cclasses_infile(parms,optarg);
COLORcheck_rval(rval,"Failed in COLORparms_set_cclasses_infile");
break;
case 'w':
rval = COLORparms_set_cclasses_outfile(parms,optarg);
COLORcheck_rval(rval,"Failed in COLORparms_set_cclasses_outfile");
break;
case 'c':
rval = COLORparms_set_color_infile(parms,optarg);
COLORcheck_rval(rval,"Failed in COLORparms_set_color_infile");
break;
case 'u':
rval = COLORparms_set_initial_upper_bound(parms,atoi(optarg));
COLORcheck_rval(rval,"Failed in COLORparms_set_initial_upper_bound");
break;
case 'a':
parms->upper_bounds_only = 1;
parms->branching_strategy = COLOR_hybrid_strategy;
break;
case 'p':
rval = COLORparms_set_parallel(parms,1);
COLORcheck_rval(rval,"Failed in COLORparms_set_initial_upper_bound");
break;
case 'b':
rval = COLORparms_set_backupdir(parms,optarg);
COLORcheck_rval(rval,"Failed in COLORparms_set_backupdir");
break;
case 'l':
rval = COLORparms_set_branching_cpu_limit(parms,atof(optarg));
COLORcheck_rval(rval,"Failed in COLORparms_set_branching_cpu_limit");
break;
case 's':
rval = COLORparms_set_branching_strategy(parms,atoi(optarg));
COLORcheck_rval(rval,"Failed in COLORparms_set_branching_strategy");
break;
case 'R':
rval = COLORparms_set_rounding_strategy(parms,atoi(optarg));
COLORcheck_rval(rval,"Failed in COLORparms_set_rounding_strategy");
break;
default:
usage (av[0]);
rval = 1; goto CLEANUP;
}
}
if (ac <= optind) {
rval = 1; goto CLEANUP;
} else {
rval = COLORparms_set_edgefile(parms,av[optind++]);
COLORcheck_rval(rval,"Failed in COLORparms_set_edgefile");
}
CLEANUP:
if (rval) usage (av[0]);
return (rval);
}
static int get_problem_name(char* pname,const char* efname)
{
int rval = 0;
int len = 0;
const char * fname = strrchr(efname,'/');
char * lastdot = strrchr(efname,'.');
if(!fname) {
/* no slashes in efname.*/
fname = efname;
} else {
fname++;
}
if (lastdot) {
len = lastdot - fname + 1;
} else {
len = strlen(fname);
}
if (snprintf(pname,len,"%s",fname) < 0) {
rval = 1;
}
printf("Extracted problem name %s\n",pname);
return rval;
}
COLOR_MAYBE_UNUSED
static int quick_lower_bound(COLORset **newsets, int *nnewsets, int ncount,
int ecount, int *elist, int cutoff, int *pval) {
int rval = 0;
int* all_one_nweights = (int*) NULL;
int i;
int nrbranches = ncount/10 + 1;
COLORset* cliques = (COLORset*) NULL;
int ncliques;
all_one_nweights = COLOR_SAFE_MALLOC(ncount, int);
COLORcheck_NULL(all_one_nweights, "Failed to allocate all_one_nweights");
for (i = 0; i < ncount; ++i) {all_one_nweights[i] = 1;}
rval = COLORclique_ostergard(&cliques, &ncliques,
ncount, ecount, elist,
all_one_nweights,cutoff,pval,
nrbranches);
COLORcheck_rval(rval,"Failed in COLORclique_ostergard.");
/** From the found clique we initialize a coloring for the
nodes in the cliques.
*/
*nnewsets = cliques[ncliques-1].count;
*newsets = COLOR_SAFE_MALLOC(*nnewsets,COLORset);
COLORcheck_NULL(*newsets,"Failed to allocate *newsets");
for (i = 0; i < cliques[ncliques-1].count; ++i) {
(*newsets)[i].count = 1;
(*newsets)[i].members = COLOR_SAFE_MALLOC(1,int);
(*newsets)[i].members[0] = cliques[ncliques-1].members[i];
}
CLEANUP:
COLORfree_sets(&cliques,&ncliques);
COLOR_IFFREE(all_one_nweights,int);
return rval;
}
int main (int ac, char **av)
{
int rval = 0;
int i;
double start_time = COLORcpu_time();
double tot_rtime;
COLORproblem colorproblem;
COLORparms* parms = &(colorproblem.parms);
colordata* cd = &(colorproblem.root_cd);
COLORset* debugcolors = (COLORset*) NULL;
int ndebugcolors = 0;
rval = COLORprogram_header (ac,av);
COLORcheck_rval(rval, "Failed in COLORprogram_header");
COLORproblem_init(&colorproblem);
cd->id = 0;
colorproblem.ncolordata = 1;
rval = parseargs (ac, av,parms);
if (rval) goto CLEANUP;
cd->upper_bound = parms->initial_upper_bound;
get_problem_name(cd->pname,parms->edgefile);
printf ("Rounding strategy: ");
switch (parms->rounding_strategy) {
case COLOR_neighbor_rounding:
printf("neighbor\n");
break;
case COLOR_uniform_rounding:
printf("uniform\n");
break;
case COLOR_no_rounding:
printf("none\n");
break;
}
if (COLORdbg_lvl() > 1) printf ("Debugging turned on\n");
fflush (stdout);
rval = COLORread_dimacs (parms->edgefile, &(cd->ncount), &(cd->ecount),
&(cd->elist), (int **) NULL);
COLORcheck_rval (rval, "COLORread_diamcs failed");
if (cd->upper_bound > cd->ncount) {cd->upper_bound = cd->ncount;}
if (colorproblem.parms.backupdir) {
recover_colordata(cd,&colorproblem);
}
if (cd->status == initialized) {
/** Using a restricted CLIQUER to obtain an initial lower bound
for the chromatic number and a good starting solution for
DSATUR didn't pay off.
Though many maximum clique problems are solved within a
second, I could not find a deterministic bound for the
running time. I don't want to impose a cpu time limit, as
this would be non-deterministic.
@author S. Held
*/
/* rval = quick_lower_bound(&(cd->cclasses),&(cd->ccount), cd->ncount, */
/* cd->ecount, cd->elist, cd->upper_bound, &(cd->lower_bound)); */
/* COLORcheck_rval(rval, "Failed in quick_lower_bound"); */
cd->orig_node_ids = (int*) COLOR_SAFE_MALLOC(cd->ncount,int);
COLORcheck_NULL(cd->orig_node_ids,"Failed to allocate cd->orig_node_ids");
for (i = 0; i < cd->ncount; ++i) { cd->orig_node_ids[i] = i; }
if (parms->cclasses_infile != (char*) NULL) {
printf ("Reading initial stable set cover from %s.\n",
parms->cclasses_infile);
rval = COLORstable_read_stable_sets(&(cd->cclasses),&(cd->ccount),
cd->ncount,parms->cclasses_infile,cd->pname);
COLORcheck_rval(rval,"Failed in COLORstable_read_stable_sets");
rval = COLORcopy_sets(&(cd->bestcolors),&(cd->nbestcolors),
cd->cclasses,cd->ccount);
COLORcheck_rval(rval,"Failed in COLORcopy_sets");
rval = COLORtransform_into_coloring(cd->ncount, &(cd->nbestcolors), &(cd->bestcolors));
COLORcheck_rval(rval,"Failed in COLORtransform_into_coloring");
rval = COLORcheck_coloring(cd->bestcolors,cd->nbestcolors,
cd->ncount, cd->ecount, cd->elist);
COLORcheck_rval(rval,"Failed in COLORcheck_coloring");
printf ("Extracted an initial coloring from stable set cover in %s to obtain an upper bound of %d.\n",
parms->cclasses_infile, cd->nbestcolors);
} else {
if (0) {
rval = COLORgreedy (cd->ncount, cd->ecount, cd->elist,
&(cd->ccount), &(cd->cclasses));
COLORcheck_rval (rval, "COLORgreedy failed");
} else {
rval = COLORdsatur (cd->ncount, cd->ecount, cd->elist,
&(cd->ccount), &(cd->cclasses));
COLORcheck_rval (rval, "COLORdsatur failed");
}
printf ("Greedy Colors: %d\n", cd->ccount); fflush (stdout);
print_colors(cd->cclasses,cd->ccount);
COLORcopy_sets(&(cd->bestcolors),&(cd->nbestcolors),
cd->cclasses,cd->ccount);
COLORcheck_coloring(cd->bestcolors,cd->nbestcolors,
cd->ncount, cd->ecount, cd->elist);
}
if (parms->color_infile != (char*) NULL) {
rval = COLORstable_read_stable_sets(&debugcolors,&ndebugcolors,
cd->ncount,parms->color_infile,cd->pname);
COLORcheck_rval(rval,"Failed in COLORstable_read_stable_sets");
rval = COLORcheck_coloring(debugcolors,ndebugcolors,cd->ncount,
cd->ecount,cd->elist);
COLORcheck_rval(rval,"Failed in COLORcheck_coloring");
cd->debugcolors = debugcolors;
cd->ndebugcolors = ndebugcolors;
cd->opt_track = 1;
}
cd->upper_bound = cd->nbestcolors < cd->upper_bound ? cd->nbestcolors : cd->upper_bound;
colorproblem.global_upper_bound = cd->upper_bound;
cd->gallocated = cd->ccount;
}
rval = compute_coloring(&colorproblem);
COLORcheck_rval(rval, "Failed to compute_coloring");
if (cd->nbestcolors == cd->upper_bound) {
printf ("Opt Colors: %d\n", cd->nbestcolors); fflush (stdout);
print_colors(cd->bestcolors,cd->nbestcolors);
} else if (cd->lower_bound == parms->initial_upper_bound) {
printf("Lower bound reached predefined upper bound %d.\n",
parms->initial_upper_bound);
} else {
printf("Finished with LB %d and UB %d.\n",
cd->lower_bound, cd->upper_bound);
}
tot_rtime = COLORcpu_time() - start_time;
printf("Computing coloring took %f seconds.\n",tot_rtime);
CLEANUP:
COLORproblem_free(&colorproblem);
COLOR_IFFREE(debugcolors,COLORset);
return rval;
}