| 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 |
/* system include files */ |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
/* SYMPHONY include files */ |
| 21 |
#include "sym_constants.h" |
| 22 |
#include "sym_macros.h" |
| 23 |
#include "sym_master_u.h" |
| 24 |
|
| 25 |
/* User include files */ |
| 26 |
#include "user.h" |
| 27 |
#ifdef COMPILE_IN_TM |
| 28 |
#ifdef COMPILE_IN_LP |
| 29 |
/* fill these in for sequential compilation if needed. */ |
| 30 |
#ifdef COMPILE_IN_CG |
| 31 |
/* fill these in for sequential compilation if needed. */ |
| 32 |
#endif |
| 33 |
#ifdef COMPILE_IN_CP |
| 34 |
/* fill these in for sequential compilation if needed. */ |
| 35 |
#endif |
| 36 |
#endif |
| 37 |
#endif |
| 38 |
|
| 39 |
/*===========================================================================*\ |
| 40 |
* This file contains stubs for the user-written functions for the master |
| 41 |
* process. The primary function that has to be filled in here is user_io(), |
| 42 |
* where the data for the instance is read in and the user data structure |
| 43 |
* that stores the instance data filled out (this data structure is defined |
| 44 |
* in user.h). Other than that, the default routines should work fine. |
| 45 |
\*===========================================================================*/ |
| 46 |
|
| 47 |
/*===========================================================================*/ |
| 48 |
|
| 49 |
/*===========================================================================*\ |
| 50 |
* This function gives help on command-line switches defined by the user. |
| 51 |
* All user switches have capital letters by convention. |
| 52 |
\*===========================================================================*/ |
| 53 |
|
| 54 |
void user_usage(void){ |
| 55 |
printf("master [ -H ] [ -F file ] \n\n\t%s\n\t%s\n\t%s\n\t%s\n\n", |
| 56 |
"-H: help (solver-specific switches)", |
| 57 |
"-F model: model should be read in from file 'model'", |
| 58 |
" (MPS format is assumed unless -D is also present)", |
| 59 |
"-D data: model is in AMPL format and data is in file 'data'"); |
| 60 |
} |
| 61 |
|
| 62 |
/*===========================================================================*/ |
| 63 |
|
| 64 |
/*===========================================================================*\ |
| 65 |
* Initialize user-defined data structures. This basically consists of |
| 66 |
* allocating the memory. If you are using the default data structure, |
| 67 |
* nothing needs to be changed here. |
| 68 |
\*===========================================================================*/ |
| 69 |
|
| 70 |
int user_initialize(void **user) |
| 71 |
{ |
| 72 |
/* Create the user's data structure and pass a pointer back to SYMPHONY. */ |
| 73 |
user_problem *prob = (user_problem *) calloc(1, sizeof(user_problem)); |
| 74 |
|
| 75 |
*user = prob; |
| 76 |
|
| 77 |
return(USER_SUCCESS); |
| 78 |
} |
| 79 |
|
| 80 |
/*===========================================================================*/ |
| 81 |
|
| 82 |
/*===========================================================================*\ |
| 83 |
* Parse the user options and read in parameters from the parameter file |
| 84 |
* given on the command line |
| 85 |
\*===========================================================================*/ |
| 86 |
|
| 87 |
int user_readparams(void *user, char *filename, int argc, char **argv) |
| 88 |
{ |
| 89 |
FILE *f; |
| 90 |
char line[50], key[50], value[50], c, tmp; |
| 91 |
int i; |
| 92 |
|
| 93 |
/* This gives you access to the user data structure*/ |
| 94 |
user_problem *prob = (user_problem *) user; |
| 95 |
user_parameters *par = &(prob->par); |
| 96 |
/* This code is just a template for customization. Uncomment to use.*/ |
| 97 |
#if 0 |
| 98 |
FILE *f; |
| 99 |
char line[50], key[50], value[50], c, tmp; |
| 100 |
int i; |
| 101 |
/* This gives you access to the user data structure*/ |
| 102 |
user_problem *prob = (user_problem *) user; |
| 103 |
user_parameters *par = &(prob->par); |
| 104 |
|
| 105 |
if (strcmp(filename, "")){ |
| 106 |
if ((f = fopen(filename, "r")) == NULL){ |
| 107 |
printf("SYMPHONY: file %s can't be opened\n", filename); |
| 108 |
return(USER_ERROR); /*error check for existence of parameter file*/ |
| 109 |
} |
| 110 |
|
| 111 |
/* Here you can read in the parameter settings from the file. See the |
| 112 |
function bc_readparams() for an example of how this is done. */ |
| 113 |
while(NULL != fgets(line, MAX_LINE_LENGTH, f)){ /*read in parameters*/ |
| 114 |
strcpy(key, ""); |
| 115 |
sscanf(line, "%s%s", key, value); |
| 116 |
|
| 117 |
if (strcmp(key, "input_file") == 0){ |
| 118 |
par->infile[MAX_FILE_NAME_LENGTH] = 0; |
| 119 |
strncpy(par->infile, value, MAX_FILE_NAME_LENGTH); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
fclose(f); |
| 124 |
} |
| 125 |
#endif |
| 126 |
/* Here you can parse the command line for options. By convention, the |
| 127 |
users options should be capital letters */ |
| 128 |
|
| 129 |
for (i = 1; i < argc; i++){ |
| 130 |
sscanf(argv[i], "%c %c", &tmp, &c); |
| 131 |
if (tmp != '-') |
| 132 |
continue; |
| 133 |
switch (c) { |
| 134 |
case 'H': |
| 135 |
user_usage(); |
| 136 |
exit(0); |
| 137 |
break; |
| 138 |
case 'F': |
| 139 |
strncpy(par->infile, argv[++i], MAX_FILE_NAME_LENGTH); |
| 140 |
break; |
| 141 |
}; |
| 142 |
} |
| 143 |
return(USER_SUCCESS); |
| 144 |
} |
| 145 |
|
| 146 |
/*===========================================================================*/ |
| 147 |
|
| 148 |
/*===========================================================================*\ |
| 149 |
* Read in the data file, whose name was given in the parameter file. |
| 150 |
* This file contains instance data. Right now, this function is set up to |
| 151 |
* read in just the number of columns and number of rows from the file. |
| 152 |
* Add more data as needed to describe the instance and set up the LP |
| 153 |
* relaxation. |
| 154 |
\*===========================================================================*/ |
| 155 |
|
| 156 |
int user_io(void *user) |
| 157 |
{ |
| 158 |
#if 0 |
| 159 |
/* This gives you access to the user data structure. */ |
| 160 |
user_problem *prob = (user_problem *) user; |
| 161 |
user_parameters *par = &(prob->par); |
| 162 |
char *infile = par->infile; |
| 163 |
FILE *f = NULL; |
| 164 |
char line[MAX_LINE_LENGTH], key[50], value[50]; |
| 165 |
|
| 166 |
/* Make sure the file exists and can be opened */ |
| 167 |
if (!strcmp(infile, "")){ |
| 168 |
printf("\nMpp I/O: No problem data file specified\n\n"); |
| 169 |
exit(1); |
| 170 |
} |
| 171 |
|
| 172 |
if ((f = fopen(infile, "r")) == NULL){ |
| 173 |
printf("Readparams: file %s can't be opened\n", infile); |
| 174 |
exit(1); /*error check for existence of parameter file*/ |
| 175 |
} |
| 176 |
|
| 177 |
/* Here you can read in the data for the problem instance. For the default |
| 178 |
setup, the user should set the colnum and rownum here. */ |
| 179 |
while(NULL != fgets(line, MAX_LINE_LENGTH, f)){ /*read in problem data*/ |
| 180 |
strcpy(key, ""); |
| 181 |
sscanf(line, "%s%s", key, value); |
| 182 |
if (strcmp(key, "colnum") == 0){ /* Read in the number of columns */ |
| 183 |
READ_INT_PAR(prob->colnum); |
| 184 |
} |
| 185 |
else if (strcmp(key, "rownum") == 0){ /* Read in the number of rows */ |
| 186 |
READ_INT_PAR(prob->rownum); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
fclose(f); |
| 191 |
#endif |
| 192 |
|
| 193 |
return(USER_DEFAULT); |
| 194 |
} |
| 195 |
|
| 196 |
/*===========================================================================*/ |
| 197 |
|
| 198 |
/*===========================================================================*\ |
| 199 |
* Here is where the heuristics are performed and an upper bound is calculated. |
| 200 |
* An upper bound can also be specified in the parameter file. This function |
| 201 |
* need not be filled in if no upper bounding is done. |
| 202 |
\*===========================================================================*/ |
| 203 |
|
| 204 |
int user_start_heurs(void *user, double *ub, double *ub_estimate) |
| 205 |
{ |
| 206 |
/* This gives you access to the user data structure. */ |
| 207 |
user_problem *prob = (user_problem *) user; |
| 208 |
|
| 209 |
return(USER_DEFAULT); |
| 210 |
} |
| 211 |
|
| 212 |
/*===========================================================================*/ |
| 213 |
|
| 214 |
/*===========================================================================*\ |
| 215 |
* If graph drawing will be used, the user must initialize the drawing |
| 216 |
* window here. This function need not be filled in. |
| 217 |
\*===========================================================================*/ |
| 218 |
|
| 219 |
int user_init_draw_graph(void *user, int dg_id) |
| 220 |
{ |
| 221 |
/* This gives you access to the user data structure. */ |
| 222 |
user_problem *prob = (user_problem *) user; |
| 223 |
|
| 224 |
return(USER_DEFAULT); |
| 225 |
} |
| 226 |
|
| 227 |
/*===========================================================================*/ |
| 228 |
|
| 229 |
/*===========================================================================*\ |
| 230 |
* This is the subroutine where the user specifies which variables are to be |
| 231 |
* in the base set and which variables are to be active in the root node but |
| 232 |
* not in the base set (these are called the "extra variables"). This is done |
| 233 |
* by listing the indices of the corresponding variables in arrays named |
| 234 |
* "basevars" and extravars below. |
| 235 |
* |
| 236 |
* The base set of variables form the core that is never removed from the LP |
| 237 |
* relaxation. Extra variables, on the other hand, can be removed if they are |
| 238 |
* fixed by reduced cost or by logic-based rules. Allowing the removal of |
| 239 |
* variables from the relaxation can lead to efficiencies, but there is a |
| 240 |
* price to pay in terms of extra bookkeeping. If possible, it is a good idea |
| 241 |
* to form a base set of variables consisting of those that are "likely" to be |
| 242 |
* present in some optimal solution. If this is not possible, the simplest |
| 243 |
* approach is just to put all the variables in the extra set, which allows |
| 244 |
* them all to be fixed by reduced cost if possible. This is implemented below |
| 245 |
* as an example. |
| 246 |
* |
| 247 |
* Note that each variable must have a unique user index by which the variable |
| 248 |
* can be identified later. Note also that it is possible to have variables |
| 249 |
* that are neither in the base set or active in the root node by using column |
| 250 |
* generation and filling out the function user_generate_column(). |
| 251 |
\*===========================================================================*/ |
| 252 |
|
| 253 |
int user_initialize_root_node(void *user, int *basevarnum, int **basevars, |
| 254 |
int *basecutnum, int *extravarnum, |
| 255 |
int **extravars, char *obj_sense, |
| 256 |
double *obj_offset, char ***colnames, |
| 257 |
int *colgen_strat) |
| 258 |
{ |
| 259 |
/* This gives you access to the user data structure. */ |
| 260 |
user_problem *prob = (user_problem *) user; |
| 261 |
|
| 262 |
/* Since we don't know how to form a good set of base variables, we'll put all |
| 263 |
the variables in the extra set */ |
| 264 |
|
| 265 |
/* Set the number of extra variables*/ |
| 266 |
*extravarnum = prob->colnum; |
| 267 |
|
| 268 |
#if 0 |
| 269 |
/* This code is not really needed because this is the default, so it is |
| 270 |
commented out and left for illustration. */ |
| 271 |
|
| 272 |
/* Put all the variables in the extra set */ |
| 273 |
vars = *extravars = (int *) malloc(varnum * ISIZE); |
| 274 |
for (i = 0; i < varnum; i++){ |
| 275 |
vars[i] = i; |
| 276 |
} |
| 277 |
#endif |
| 278 |
|
| 279 |
/* Set the number of rows in the initial formulation */ |
| 280 |
*basecutnum = prob->rownum; |
| 281 |
|
| 282 |
/* The set of base variables will be empty */ |
| 283 |
*basevarnum = 0; |
| 284 |
*basevars = NULL; |
| 285 |
|
| 286 |
return(USER_DEFAULT); |
| 287 |
} |
| 288 |
|
| 289 |
/*===========================================================================*/ |
| 290 |
|
| 291 |
/*===========================================================================*\ |
| 292 |
* Receive the feasible solution. Doesn't need to be filled in. |
| 293 |
\*===========================================================================*/ |
| 294 |
|
| 295 |
int user_receive_feasible_solution(void *user, int msgtag, double cost, |
| 296 |
int numvars, int *indices, double *values) |
| 297 |
{ |
| 298 |
/* This gives you access to the user data structure. */ |
| 299 |
user_problem *prob = (user_problem *) user; |
| 300 |
|
| 301 |
return(USER_DEFAULT); |
| 302 |
} |
| 303 |
|
| 304 |
/*===========================================================================*/ |
| 305 |
|
| 306 |
/*===========================================================================*\ |
| 307 |
* Here, we send the necessary data to the LP process. Notice that |
| 308 |
* there are two cases to deal with. If the LP or the TM are running |
| 309 |
* as separate processes, then we have to send the data by |
| 310 |
* message-passing. Otherwise, we can allocate the user-defined LP data |
| 311 |
* structure here and simply copy the necessary information. This is the |
| 312 |
* only place the user has to sorry about this distinction between |
| 313 |
* configurations. If running sequentially and using the default data |
| 314 |
* structure, nothing needs to be modified in here. |
| 315 |
\*===========================================================================*/ |
| 316 |
|
| 317 |
int user_send_lp_data(void *user, void **user_lp) |
| 318 |
{ |
| 319 |
/* This gives you access to the user data structure. */ |
| 320 |
user_problem *prob = (user_problem *) user; |
| 321 |
|
| 322 |
#if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) |
| 323 |
/* This is is the case when we are copying data directly because the LP is |
| 324 |
not running separately. The easiest thing to do here is just to use the |
| 325 |
same user data structure in both the master and the LP. Then this |
| 326 |
subroutine would simply consist of the line |
| 327 |
|
| 328 |
*user_lp = user; |
| 329 |
|
| 330 |
Otherwise, this code should be virtually |
| 331 |
identical to that of user_receive_lp_data() in the LP process.*/ |
| 332 |
|
| 333 |
*user_lp = user; |
| 334 |
#else |
| 335 |
/* Here, we send that data using message passing and the rest is |
| 336 |
done in user_receive_lp_data() in the LP process */ |
| 337 |
#endif |
| 338 |
return(USER_SUCCESS); |
| 339 |
} |
| 340 |
|
| 341 |
/*===========================================================================*/ |
| 342 |
|
| 343 |
/*===========================================================================*\ |
| 344 |
* Here, we send the necessary data to the CG process. Notice that |
| 345 |
* there are two cases to deal with. If the CG, LP, or the TM are running |
| 346 |
* as separate processes, then we have to send the data by |
| 347 |
* message-passing. Otherwise, we can allocate the user-defined LP data |
| 348 |
* structure here and simply copy the necessary information. This is the |
| 349 |
* only place the user has to sorry about this distinction between |
| 350 |
* configurations. If running sequentially and using the default data |
| 351 |
* structure, nothing needs to be modified in here. |
| 352 |
\*===========================================================================*/ |
| 353 |
|
| 354 |
int user_send_cg_data(void *user, void **user_cg) |
| 355 |
{ |
| 356 |
/* This gives you access to the user data structure. */ |
| 357 |
user_problem *prob = (user_problem *) user; |
| 358 |
|
| 359 |
#if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) && defined (COMPILE_IN_CG) |
| 360 |
/* This is is the case when we are copying data directly because |
| 361 |
the CG is not running separately. The easiest thing to do here is just |
| 362 |
to use the same user data structure in both the master and the cut |
| 363 |
generator. Then this subroutine would simply consist of |
| 364 |
|
| 365 |
*user_cg = user; |
| 366 |
|
| 367 |
Otherwise, this code should be virtually |
| 368 |
identical to that of user_receive_cg_data() in the CG process.*/ |
| 369 |
|
| 370 |
*user_cg = user; |
| 371 |
#ifdef CHECK_CUT_VALIDITY |
| 372 |
/* Send the feasible solution here */ |
| 373 |
#endif |
| 374 |
#else |
| 375 |
/* Here, we send that data using message passing and the rest is |
| 376 |
done in user_receive_cg_data() in the CG process */ |
| 377 |
#ifdef CHECK_CUT_VALIDITY |
| 378 |
/* Send the feasible solution here */ |
| 379 |
#endif |
| 380 |
#endif |
| 381 |
return(USER_SUCCESS); |
| 382 |
} |
| 383 |
|
| 384 |
/*===========================================================================*/ |
| 385 |
|
| 386 |
/*===========================================================================*\ |
| 387 |
* Here, we send the necessary data to the CP process. Notice that |
| 388 |
* there are two cases to deal with. If the CP, LP, or the TM are running |
| 389 |
* as separate processes, then we have to send the data by |
| 390 |
* message-passing. Otherwise, we can allocate the user-defined LP data |
| 391 |
* structure here and simply copy the necessary information. This is the |
| 392 |
* only place the user has to sorry about this distinction between |
| 393 |
* configurations. If running sequentially and using the default data |
| 394 |
* structure, nothing needs to be modified in here. |
| 395 |
\*===========================================================================*/ |
| 396 |
|
| 397 |
int user_send_cp_data(void *user, void **user_cp) |
| 398 |
{ |
| 399 |
/* This gives you access to the user data structure. */ |
| 400 |
user_problem *prob = (user_problem *) user; |
| 401 |
|
| 402 |
#if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) && defined (COMPILE_IN_CP) |
| 403 |
/* This is is the case when we are copying data directly because |
| 404 |
the CP is not running separately. The easiest thing to do here is just |
| 405 |
to use the same user data structure in both the master and the cut |
| 406 |
pool. Then this subroutine would simply consist of |
| 407 |
|
| 408 |
*user_cp = user; |
| 409 |
|
| 410 |
Otherwise, this code should be virtually |
| 411 |
identical to that of user_receive_cp_data() in the CP process.*/ |
| 412 |
|
| 413 |
*user_cp = user; |
| 414 |
#else |
| 415 |
/* Here, we send that data using message passing and the rest is |
| 416 |
done in user_receive_cp_data() in the CP process */ |
| 417 |
#endif |
| 418 |
return(USER_SUCCESS); |
| 419 |
} |
| 420 |
|
| 421 |
/*===========================================================================*/ |
| 422 |
|
| 423 |
/*===========================================================================*\ |
| 424 |
* Generally, this function is not needed but you might find some use |
| 425 |
* for it. Someone did :). |
| 426 |
\*===========================================================================*/ |
| 427 |
|
| 428 |
int user_process_own_messages(void *user, int msgtag) |
| 429 |
{ |
| 430 |
/* This gives you access to the user data structure. */ |
| 431 |
user_problem *prob = (user_problem *) user; |
| 432 |
|
| 433 |
switch (msgtag){ |
| 434 |
case 0: |
| 435 |
default: |
| 436 |
fprintf(stderr, "\nMaster: unknown message type %i!!!\n\n", msgtag); |
| 437 |
exit(1); |
| 438 |
} |
| 439 |
|
| 440 |
return(USER_DEFAULT); |
| 441 |
} |
| 442 |
|
| 443 |
/*===========================================================================*/ |
| 444 |
|
| 445 |
/*===========================================================================*\ |
| 446 |
* This is the user's chance to display the solution in whatever |
| 447 |
* manner desired. A return value of USER_DEFAULT will cause the |
| 448 |
* default solution display routine to be executed, even if the user displays |
| 449 |
* the solution as well. |
| 450 |
\*===========================================================================*/ |
| 451 |
|
| 452 |
int user_display_solution(void *user, double lpetol, int varnum, int *indices, |
| 453 |
double *values, double objval) |
| 454 |
{ |
| 455 |
return(USER_DEFAULT); |
| 456 |
} |
| 457 |
|
| 458 |
/*===========================================================================*/ |
| 459 |
|
| 460 |
/*===========================================================================*\ |
| 461 |
* This is a debugging feature which might |
| 462 |
* allow you to find out why a known feasible solution is being cut off. |
| 463 |
\*===========================================================================*/ |
| 464 |
|
| 465 |
int user_send_feas_sol(void *user, int *feas_sol_size, int **feas_sol) |
| 466 |
{ |
| 467 |
#ifdef TRACE_PATH |
| 468 |
|
| 469 |
#endif |
| 470 |
return(USER_DEFAULT); |
| 471 |
} |
| 472 |
|
| 473 |
/*===========================================================================*/ |
| 474 |
|
| 475 |
/*===========================================================================*\ |
| 476 |
* This function frees everything. |
| 477 |
\*===========================================================================*/ |
| 478 |
|
| 479 |
int user_free_master(void **user) |
| 480 |
{ |
| 481 |
user_problem *prob = (user_problem *) (*user); |
| 482 |
int i; |
| 483 |
for (i=0; i<prob->num_cones; ++i) { |
| 484 |
free(prob->cone_members[i]); |
| 485 |
} |
| 486 |
free(prob->cone_type); |
| 487 |
free(prob->cone_size); |
| 488 |
free(prob->cone_members); |
| 489 |
free(prob); |
| 490 |
return(USER_SUCCESS); |
| 491 |
} |
| 492 |
/*===========================================================================*/ |
| 493 |
|
| 494 |
/*===========================================================================*\ |
| 495 |
* This function is used to lift the user created cuts during warm starting * |
| 496 |
/*===========================================================================*/ |
| 497 |
|
| 498 |
int user_ws_update_cuts (void *user, int *size, char **coef, double * rhs, |
| 499 |
char *sense, char type, int new_col_num, |
| 500 |
int change_type) |
| 501 |
{ |
| 502 |
return(USER_DEFAULT); |
| 503 |
} |
| 504 |
/*===========================================================================*/ |
| 505 |
|