| 1 |
/*===========================================================================*/ |
| 2 |
/* */ |
| 3 |
/* This file is part of a demonstration application for use with the */ |
| 4 |
/* SYMPHONY Branch, Cut, and Price Library. This application is a solver for */ |
| 5 |
/* the Set Partitioning Problem. */ |
| 6 |
/* */ |
| 7 |
/* (c) Copyright 2005-2007 Marta Eso and Ted Ralphs. All Rights Reserved. */ |
| 8 |
/* */ |
| 9 |
/* This application was originally developed by Marta Eso and was modified */ |
| 10 |
/* Ted Ralphs ([email protected]) */ |
| 11 |
/* */ |
| 12 |
/* This software is licensed under the Eclipse Public License. Please see */ |
| 13 |
/* accompanying file for terms. */ |
| 14 |
/* */ |
| 15 |
/*===========================================================================*/ |
| 16 |
|
| 17 |
/* system include files */ |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
/* SYMPHONY include files */ |
| 23 |
#include "sym_constants.h" |
| 24 |
#include "sym_macros.h" |
| 25 |
#include "sym_master_u.h" |
| 26 |
#include "sym_proccomm.h" |
| 27 |
|
| 28 |
/* SPP include files */ |
| 29 |
#include "spp.h" |
| 30 |
#include "spp_common.h" |
| 31 |
#include "spp_master_functions.h" |
| 32 |
#ifdef COMPILE_IN_TM |
| 33 |
#ifdef COMPILE_IN_LP |
| 34 |
#include "spp_lp.h" |
| 35 |
#include "spp_lp_functions.h" |
| 36 |
#ifdef COMPILE_IN_CG |
| 37 |
#include "spp_cg.h" |
| 38 |
#endif |
| 39 |
#endif |
| 40 |
#endif |
| 41 |
|
| 42 |
/*===========================================================================*\ |
| 43 |
* This file contains stubs for the user-written functions for the master |
| 44 |
* process. The primary function that has to be filled in here is user_io(), |
| 45 |
* where the data for the instance is read in and the user data structure |
| 46 |
* that stores the instance data filled out (this data structure is defined |
| 47 |
* in user.h). Other than that, the default routines should work fine. |
| 48 |
\*===========================================================================*/ |
| 49 |
|
| 50 |
/*===========================================================================*/ |
| 51 |
|
| 52 |
/*===========================================================================*\ |
| 53 |
* This function gives help on command-line switches defined by the user. |
| 54 |
* All user switches have capital letters by convention. |
| 55 |
\*===========================================================================*/ |
| 56 |
|
| 57 |
void user_usage(void){ |
| 58 |
printf("master [ -H ] [ -F file ] \n\t%s\n\t%s\n", |
| 59 |
"-H: help (user switches)", |
| 60 |
"-F file: problem instance data is in 'file'"); |
| 61 |
} |
| 62 |
|
| 63 |
/*===========================================================================*/ |
| 64 |
|
| 65 |
/*===========================================================================*\ |
| 66 |
* Initialize user-defined data structures. This basically consists of |
| 67 |
* allocating the memory. If you are using the default data structure, |
| 68 |
* nothing needs to be changed here. |
| 69 |
\*===========================================================================*/ |
| 70 |
|
| 71 |
int user_initialize(void **user) |
| 72 |
{ |
| 73 |
spp_problem *spp = (spp_problem *) calloc(1, sizeof(spp_problem)); |
| 74 |
|
| 75 |
*user = spp; |
| 76 |
|
| 77 |
spp->par = (spp_parameters *) calloc(1, sizeof(spp_parameters)); |
| 78 |
spp->stat = (statistics *) calloc(2, sizeof(statistics)); |
| 79 |
|
| 80 |
return(USER_SUCCESS); |
| 81 |
} |
| 82 |
|
| 83 |
/*===========================================================================*/ |
| 84 |
|
| 85 |
/*===========================================================================*\ |
| 86 |
* Parse the user options Read in parameters from the parameter file given on the command line |
| 87 |
\*===========================================================================*/ |
| 88 |
|
| 89 |
int user_readparams(void *user, char *filename, int argc, char **argv) |
| 90 |
{ |
| 91 |
FILE *f; |
| 92 |
char line[50], key[50], value[50], c, tmp; |
| 93 |
int i; |
| 94 |
/* This gives you access to the user data structure*/ |
| 95 |
spp_problem *spp = (spp_problem *) user; |
| 96 |
spp_parameters *par = spp->par; |
| 97 |
|
| 98 |
spp_read_params(spp, filename); |
| 99 |
|
| 100 |
spp_print_params(spp); |
| 101 |
|
| 102 |
/* Here you can parse the command line for options. By convention, the |
| 103 |
users options should be capital letters */ |
| 104 |
|
| 105 |
for (i = 1; i < argc; i++){ |
| 106 |
sscanf(argv[i], "%c %c", &tmp, &c); |
| 107 |
if (tmp != '-') |
| 108 |
continue; |
| 109 |
switch (c) { |
| 110 |
case 'H': |
| 111 |
user_usage(); |
| 112 |
exit(0); |
| 113 |
break; |
| 114 |
case 'F': |
| 115 |
strncpy(par->infile, argv[++i], MAX_FILE_NAME_LENGTH); |
| 116 |
break; |
| 117 |
case 'T': |
| 118 |
par->test = TRUE; |
| 119 |
if(i+1 < argc){ |
| 120 |
sscanf(argv[i+1], "%c", &tmp); |
| 121 |
if(tmp != '-'){ |
| 122 |
strncpy(par->test_dir, argv[++i],MAX_FILE_NAME_LENGTH); |
| 123 |
} |
| 124 |
} |
| 125 |
break; |
| 126 |
|
| 127 |
}; |
| 128 |
} |
| 129 |
|
| 130 |
return(USER_SUCCESS); |
| 131 |
} |
| 132 |
|
| 133 |
/*===========================================================================*/ |
| 134 |
|
| 135 |
/*===========================================================================*\ |
| 136 |
* Read in the data file, whose name was given in the parameter file. |
| 137 |
* This file contains instance data. Right now, this function is set up to |
| 138 |
* read in just the number of columns and number of rows from the file. |
| 139 |
* Add more data as needed to describe the instance and set up the LP |
| 140 |
* relaxation. |
| 141 |
\*===========================================================================*/ |
| 142 |
|
| 143 |
int user_io(void *user) |
| 144 |
{ |
| 145 |
/* This gives you access to the user data structure. */ |
| 146 |
spp_problem *spp = (spp_problem *) user; |
| 147 |
int colnum, rownum; |
| 148 |
|
| 149 |
spp_read_input(spp); |
| 150 |
|
| 151 |
colnum = spp->cmatrix->colnum; |
| 152 |
rownum = spp->cmatrix->rownum; |
| 153 |
spp->cmatrix->active_colnum = colnum; |
| 154 |
spp->cmatrix->col_deleted = (char *) calloc(colnum/BITSPERBYTE + 1, CSIZE); |
| 155 |
spp->feasibility = SPP_FEASIBILITY_NOT_KNOWN; |
| 156 |
spp->feas_sol = (int *) malloc(rownum * ISIZE); |
| 157 |
|
| 158 |
/* order cols into lex ascending order */ |
| 159 |
spp_fix_lex(spp); |
| 160 |
|
| 161 |
return(USER_SUCCESS); |
| 162 |
} |
| 163 |
|
| 164 |
/*===========================================================================*/ |
| 165 |
|
| 166 |
/*===========================================================================*\ |
| 167 |
* Here is where the heuristics are performed and an upper bound is calculated. |
| 168 |
* An upper bound can also be specified in the parameter file. This function |
| 169 |
* need not be filled in if no upper bounding is done. |
| 170 |
\*===========================================================================*/ |
| 171 |
|
| 172 |
int user_start_heurs(void *user, double *ub, double *ub_estimate) |
| 173 |
{ |
| 174 |
return(USER_DEFAULT); |
| 175 |
} |
| 176 |
|
| 177 |
/*===========================================================================*/ |
| 178 |
|
| 179 |
/*===========================================================================*\ |
| 180 |
* If graph drawing will be used, the user must initialize the drawing |
| 181 |
* window here. This function need not be filled in. |
| 182 |
\*===========================================================================*/ |
| 183 |
|
| 184 |
int user_init_draw_graph(void *user, int dg_id) |
| 185 |
{ |
| 186 |
return(USER_DEFAULT); |
| 187 |
} |
| 188 |
|
| 189 |
/*===========================================================================*/ |
| 190 |
|
| 191 |
/*===========================================================================*\ |
| 192 |
* This is the subroutine where the user specifies which variables are to be |
| 193 |
* in the base set and which variables are to be active in the root node but |
| 194 |
* not in the base set (these are called the "extra variables"). This is done |
| 195 |
* by listing the indices of the corresponding variables in arrays named |
| 196 |
* "basevars" and extravars below. |
| 197 |
* |
| 198 |
* The base set of variables form the core that is never removed from the LP |
| 199 |
* relaxation. Extra variables, on the other hand, can be removed if they are |
| 200 |
* fixed by reduced cost or by logic-based rules. Allowing the removal of |
| 201 |
* variables from the relaxation can lead to efficiencies, but there is a |
| 202 |
* price to pay in terms of extra bookkeeping. If possible, it is a good idea |
| 203 |
* to form a base set of variables consisting of those that are "likely" to be |
| 204 |
* present in some optimal solution. If this is not possible, the simplest |
| 205 |
* approach is just to put all the variables in the extra set, which allows |
| 206 |
* them all to be fixed by reduced cost if possible. This is implemented below |
| 207 |
* as an example. |
| 208 |
* |
| 209 |
* Note that each variable must have a unique user index by which the variable |
| 210 |
* can be identified later. Note also that it is possible to have variables |
| 211 |
* that are neither in the base set or active in the root node by using column |
| 212 |
* generation and filling out the function user_generate_column(). |
| 213 |
\*===========================================================================*/ |
| 214 |
|
| 215 |
int user_initialize_root_node(void *user, int *basevarnum, int **basevars, |
| 216 |
int *basecutnum, int *extravarnum, int **extravars, |
| 217 |
char *obj_sense, double *obj_offset, |
| 218 |
char ***colnames, int *colgen_strat) |
| 219 |
{ |
| 220 |
/* This gives you access to the user data structure. */ |
| 221 |
spp_problem *spp = (spp_problem *) user; |
| 222 |
int i; |
| 223 |
int *vars, varnum; |
| 224 |
|
| 225 |
/* Since we don't know how to form a good set of base variables, we'll put |
| 226 |
all the variables in the extra set */ |
| 227 |
|
| 228 |
/* Set the number of extra variables*/ |
| 229 |
varnum = *extravarnum = spp->cmatrix->colnum; |
| 230 |
|
| 231 |
/* Put all the variables in the extra set */ |
| 232 |
vars = *extravars = (int *) malloc(varnum * ISIZE); |
| 233 |
for (i = 0; i < varnum; i++){ |
| 234 |
vars[i] = i; |
| 235 |
} |
| 236 |
|
| 237 |
/* Set the number of rows in the initial formulation */ |
| 238 |
*basecutnum = spp->cmatrix->rownum; |
| 239 |
|
| 240 |
/* The set base variables will be empty */ |
| 241 |
*basevarnum = 0; |
| 242 |
*basevars = NULL; |
| 243 |
|
| 244 |
return(USER_NO_PP); |
| 245 |
} |
| 246 |
|
| 247 |
/*===========================================================================*/ |
| 248 |
|
| 249 |
/*===========================================================================*\ |
| 250 |
* Receive the feasible solution. Doesn't need to be filled in. |
| 251 |
\*===========================================================================*/ |
| 252 |
|
| 253 |
int user_receive_feasible_solution(void *user, int msgtag, double cost, |
| 254 |
int numvars, int *indices, double *values) |
| 255 |
{ |
| 256 |
spp_problem *spp = (spp_problem *)user; |
| 257 |
int *colnames = spp->cmatrix->colnames; |
| 258 |
int i; |
| 259 |
|
| 260 |
/* by default we are sent the user indices of nonzero variables. |
| 261 |
choose PACK_NONZEROS in user_pack_feasible_solution in LP. */ |
| 262 |
|
| 263 |
if (spp->feasibility == SPP_FEASIBLE && cost >= spp->feas_value) |
| 264 |
return(USER_SUCCESS); |
| 265 |
|
| 266 |
spp->feasibility = SPP_FEASIBLE; |
| 267 |
spp->feas_value = cost; |
| 268 |
spp->feas_sol_length = numvars; |
| 269 |
for (i = 0; i < numvars; i++) |
| 270 |
spp->feas_sol[i] = colnames[indices[i]]; |
| 271 |
|
| 272 |
return(USER_SUCCESS); |
| 273 |
} |
| 274 |
|
| 275 |
/*===========================================================================*/ |
| 276 |
|
| 277 |
/*===========================================================================*\ |
| 278 |
* Here, we send the necessary data to the LP process. Notice that |
| 279 |
* there are two cases to deal with. If the LP or the TM are running |
| 280 |
* as separate processes, then we have to send the data by |
| 281 |
* message-passing. Otherwise, we can allocate the user-defined LP data |
| 282 |
* structure here and simply copy the necessary information. This is the |
| 283 |
* only place the user has to sorry about this distinction between |
| 284 |
* configurations. If running sequentially and using the default data |
| 285 |
* structure, nothing needs to be modified in here. |
| 286 |
\*===========================================================================*/ |
| 287 |
|
| 288 |
int user_send_lp_data(void *user, void **user_lp) |
| 289 |
{ |
| 290 |
/* This gives you access to the user data structure. */ |
| 291 |
spp_problem *spp = (spp_problem *) user; |
| 292 |
col_ordered *m = spp->cmatrix; |
| 293 |
|
| 294 |
#if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) |
| 295 |
|
| 296 |
spp_lp_problem *spp_lp = (spp_lp_problem *) |
| 297 |
calloc(1, sizeof(spp_lp_problem)); |
| 298 |
*user_lp = (void *) spp_lp; |
| 299 |
|
| 300 |
spp_lp->par = spp->lp_par; |
| 301 |
spp_lp->cmatrix = m; |
| 302 |
|
| 303 |
/* initialize some data structures in spp */ |
| 304 |
spp_init_lp(spp_lp); |
| 305 |
|
| 306 |
#else |
| 307 |
/* Here, we send that data using message passing and the rest is |
| 308 |
done in user_receive_lp_data() in the LP process */ |
| 309 |
|
| 310 |
send_char_array((char *)spp->lp_par, sizeof(spp_lp_params)); |
| 311 |
send_int_array(&m->colnum, 1); |
| 312 |
send_int_array(&m->rownum, 1); |
| 313 |
send_int_array(&m->nzcnt, 1); |
| 314 |
send_int_array(m->colnames, m->colnum); |
| 315 |
send_dbl_array(m->obj, m->colnum); |
| 316 |
send_int_array(m->matbeg, (m->colnum + 1)); |
| 317 |
send_char_array((char *)m->matind, m->nzcnt * sizeof(row_ind_type)); |
| 318 |
|
| 319 |
#endif |
| 320 |
|
| 321 |
return(USER_SUCCESS); |
| 322 |
} |
| 323 |
|
| 324 |
/*===========================================================================*/ |
| 325 |
|
| 326 |
/*===========================================================================*\ |
| 327 |
* Here, we send the necessary data to the CG process. Notice that |
| 328 |
* there are two cases to deal with. If the CG, LP, or the TM are running |
| 329 |
* as separate processes, then we have to send the data by |
| 330 |
* message-passing. Otherwise, we can allocate the user-defined LP data |
| 331 |
* structure here and simply copy the necessary information. This is the |
| 332 |
* only place the user has to sorry about this distinction between |
| 333 |
* configurations. If running sequentially and using the default data |
| 334 |
* structure, nothing needs to be modified in here. |
| 335 |
\*===========================================================================*/ |
| 336 |
|
| 337 |
int user_send_cg_data(void *user, void **user_cg) |
| 338 |
{ |
| 339 |
spp_problem *spp = (spp_problem *)user; |
| 340 |
col_ordered *m = spp->cmatrix; |
| 341 |
int colnum = m->colnum; |
| 342 |
|
| 343 |
#if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) && defined (COMPILE_IN_CG) |
| 344 |
|
| 345 |
spp_cg_problem *spp_cg = (spp_cg_problem *)calloc(1,sizeof(spp_cg_problem)); |
| 346 |
*user_cg = (void *) spp_cg; |
| 347 |
|
| 348 |
spp_cg->par = spp->cg_par; |
| 349 |
spp_cg->cmatrix = m; |
| 350 |
|
| 351 |
/* allocate space for tmp arrays */ |
| 352 |
spp_cg->tmp = (spp_cg_tmp *) calloc(1, sizeof(spp_cg_tmp)); |
| 353 |
spp_cg->tmp->itmp_m = (int *) malloc(m->rownum * ISIZE); |
| 354 |
spp_cg->tmp->istartmp_m = (int **) malloc(m->rownum * sizeof(int *)); |
| 355 |
spp_cg->tmp->cuttmp = (cut_data *) calloc(1, sizeof(cut_data)); |
| 356 |
|
| 357 |
/* initialize cg data structures */ |
| 358 |
spp_cg->fgraph = (frac_graph *) calloc(1, sizeof(frac_graph)); |
| 359 |
spp_cg->cfgraph = (frac_graph *) calloc(1, sizeof(frac_graph)); |
| 360 |
spp_cg->cm_frac = (col_ordered *) calloc(1, sizeof(col_ordered)); |
| 361 |
spp_cg->rm_frac = (row_ordered *) calloc(1, sizeof(row_ordered)); |
| 362 |
spp_cg->rm_frac->rmatbeg = (int *) malloc((m->rownum+1) * ISIZE); |
| 363 |
spp_cg->lgraph = (level_graph *) calloc(1, sizeof(level_graph)); |
| 364 |
|
| 365 |
allocate_var_length_structures(spp_cg, spp_cg->max_sol_length); |
| 366 |
|
| 367 |
/* cut collection is a local cut pool that contains the cuts that have |
| 368 |
been sent back to the lp */ |
| 369 |
spp_cg->cut_coll = (cut_collection *) calloc(1, sizeof(cut_collection)); |
| 370 |
spp_cg->cut_coll->max_size = 1000; |
| 371 |
spp_cg->cut_coll->cuts = (cut_data **) calloc(spp_cg->cut_coll->max_size, |
| 372 |
sizeof(cut_data *)); |
| 373 |
spp_cg->cut_coll->violation = (double *) |
| 374 |
malloc(spp_cg->cut_coll->max_size * DSIZE); |
| 375 |
spp_cg->cut_coll->mult = (int *) |
| 376 |
malloc(spp_cg->cut_coll->max_size * ISIZE); |
| 377 |
|
| 378 |
#else |
| 379 |
|
| 380 |
int info; |
| 381 |
|
| 382 |
send_char_array((char *)spp->cg_par, sizeof(spp_cg_params)); |
| 383 |
send_int_array(&colnum, 1); |
| 384 |
send_int_array(&m->rownum, 1); |
| 385 |
send_int_array(&m->nzcnt, 1); |
| 386 |
send_int_array(m->colnames, colnum); |
| 387 |
send_dbl_array(m->obj, colnum); |
| 388 |
send_int_array(m->matbeg, colnum + 1); |
| 389 |
send_char_array((char *)m->matind, m->nzcnt * sizeof(row_ind_type)); |
| 390 |
|
| 391 |
#endif |
| 392 |
|
| 393 |
return(USER_SUCCESS); |
| 394 |
} |
| 395 |
|
| 396 |
/*===========================================================================*/ |
| 397 |
|
| 398 |
/*===========================================================================*\ |
| 399 |
* Here, we send the necessary data to the CP process. Notice that |
| 400 |
* there are two cases to deal with. If the CP, LP, or the TM are running |
| 401 |
* as separate processes, then we have to send the data by |
| 402 |
* message-passing. Otherwise, we can allocate the user-defined LP data |
| 403 |
* structure here and simply copy the necessary information. This is the |
| 404 |
* only place the user has to sorry about this distinction between |
| 405 |
* configurations. If running sequentially and using the default data |
| 406 |
* structure, nothing needs to be modified in here. |
| 407 |
\*===========================================================================*/ |
| 408 |
|
| 409 |
int user_send_cp_data(void *user, void **user_cp) |
| 410 |
{ |
| 411 |
return(USER_DEFAULT); |
| 412 |
} |
| 413 |
|
| 414 |
/*===========================================================================*/ |
| 415 |
|
| 416 |
/*===========================================================================*\ |
| 417 |
* Generally, this function is not needed but you might find some use |
| 418 |
* for it. Someone did :). |
| 419 |
\*===========================================================================*/ |
| 420 |
|
| 421 |
int user_process_own_messages(void *user, int msgtag) |
| 422 |
{ |
| 423 |
return(USER_DEFAULT); |
| 424 |
} |
| 425 |
|
| 426 |
/*===========================================================================*/ |
| 427 |
|
| 428 |
/*===========================================================================*\ |
| 429 |
* This is the user's chance to display the solution in whatever |
| 430 |
* manner desired. |
| 431 |
\*===========================================================================*/ |
| 432 |
|
| 433 |
int user_display_solution(void *user, double lpetol, int varnum, |
| 434 |
int *indices, double *values, double objval) |
| 435 |
{ |
| 436 |
spp_problem *spp = (spp_problem *)user; |
| 437 |
int *colnames = spp->cmatrix->colnames; |
| 438 |
int i; |
| 439 |
|
| 440 |
printf("\nBest Solution Found:\n"); |
| 441 |
for (i = 0; i < varnum; i++) |
| 442 |
printf("%i \n", colnames[indices[i]]); |
| 443 |
printf("\n\n\n"); |
| 444 |
|
| 445 |
return(USER_SUCCESS); |
| 446 |
} |
| 447 |
|
| 448 |
/*===========================================================================*/ |
| 449 |
|
| 450 |
/*===========================================================================*\ |
| 451 |
* This is a debugging feature which might |
| 452 |
* allow you to find out why a known feasible solution is being cut off. |
| 453 |
\*===========================================================================*/ |
| 454 |
|
| 455 |
int user_send_feas_sol(void *user, int *feas_sol_size, int **feas_sol) |
| 456 |
{ |
| 457 |
#ifdef TRACE_PATH |
| 458 |
|
| 459 |
#endif |
| 460 |
return(USER_DEFAULT); |
| 461 |
} |
| 462 |
|
| 463 |
/*===========================================================================*/ |
| 464 |
|
| 465 |
/*===========================================================================*\ |
| 466 |
* This function frees everything. |
| 467 |
\*===========================================================================*/ |
| 468 |
|
| 469 |
int user_free_master(void **user) |
| 470 |
{ |
| 471 |
spp_problem *spp = (spp_problem *) (*user); |
| 472 |
|
| 473 |
FREE(spp->feas_sol); |
| 474 |
spp_free_cmatrix(spp->cmatrix); |
| 475 |
FREE(spp->cmatrix); |
| 476 |
|
| 477 |
if(!spp->par->test){ |
| 478 |
FREE(spp->par); |
| 479 |
FREE(spp->stat); |
| 480 |
FREE(*user); |
| 481 |
} |
| 482 |
return(USER_SUCCESS); |
| 483 |
} |
| 484 |
/*===========================================================================*/ |
| 485 |
|
| 486 |
/*===========================================================================*\ |
| 487 |
* This function is used to lift the user created cuts during warm starting * |
| 488 |
/*===========================================================================*/ |
| 489 |
|
| 490 |
int user_ws_update_cuts (void *user, int *size, char **coef, double * rhs, |
| 491 |
char *sense, char type, int new_col_num, |
| 492 |
int change_type) |
| 493 |
{ |
| 494 |
return(USER_DEFAULT); |
| 495 |
} |
| 496 |
|
| 497 |
/*===========================================================================*/ |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|