| 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 <memory.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
/* SYMPHONY include files */ |
| 24 |
#include "sym_timemeas.h" |
| 25 |
#include "sym_macros.h" |
| 26 |
#include "sym_constants.h" |
| 27 |
|
| 28 |
/* SPP include files */ |
| 29 |
#include "spp.h" |
| 30 |
#include "spp_common.h" |
| 31 |
#include "spp_macros.h" |
| 32 |
#include "spp_master_functions.h" |
| 33 |
|
| 34 |
/*****************************************************************************/ |
| 35 |
|
| 36 |
void spp_read_params(spp_problem *spp, char *filename) |
| 37 |
{ |
| 38 |
char line[MAX_LINE_LENGTH], key[50], value[50]; |
| 39 |
FILE *f; |
| 40 |
|
| 41 |
spp->lp_par = (spp_lp_params *) calloc(1, sizeof(spp_lp_params)); |
| 42 |
spp->cg_par = (spp_cg_params *) calloc(1, sizeof(spp_cg_params)); |
| 43 |
|
| 44 |
/* defaults for the parameters in spp_master */ |
| 45 |
spp->par->dupc_at_loadtime = FALSE; |
| 46 |
spp->par->our_format_file_counter = -1; |
| 47 |
spp->par->matlab_format_file_counter = -1; |
| 48 |
spp->par->granularity = 0; |
| 49 |
|
| 50 |
/* defaults for the parameters in spp_lp */ |
| 51 |
spp->lp_par->do_lift_in_lp = 0; /* false */ |
| 52 |
spp->lp_par->lp_dj_threshold_frac = 0; |
| 53 |
spp->lp_par->lp_dj_threshold_abs = 0; /* ==> threshold = lp_etol */ |
| 54 |
spp->lp_par->lanmax = 30; |
| 55 |
spp->lp_par->which_atilde = 1; |
| 56 |
|
| 57 |
/* defaults for the parameters in spp_cg */ |
| 58 |
spp->cg_par->min_violation_clique = .00001; |
| 59 |
spp->cg_par->min_violation_oddhole = .00001; |
| 60 |
spp->cg_par->min_violation_oddantihole = .00001; |
| 61 |
spp->cg_par->min_violation_wheel = .00001; |
| 62 |
spp->cg_par->min_violation_orthocut = .00001; |
| 63 |
spp->cg_par->min_violation_othercut = .00001; |
| 64 |
spp->cg_par->starcl_degree_threshold = 16; |
| 65 |
if (!strcmp(filename, "")) |
| 66 |
return; |
| 67 |
|
| 68 |
if ((f = fopen(filename, "r")) == NULL){ |
| 69 |
printf("SYMPHONY: file %s can't be opened\n", filename); |
| 70 |
exit(1); /*error check for existence of parameter file*/ |
| 71 |
} |
| 72 |
|
| 73 |
/* read in parameters from file */ |
| 74 |
while ( fgets(line, MAX_LINE_LENGTH, f) != NULL ) { |
| 75 |
strcpy(key,""); |
| 76 |
sscanf(line, "%s%s", key, value); |
| 77 |
|
| 78 |
/* SPP Master params */ |
| 79 |
|
| 80 |
if ( !strcmp(key, "input_file") || !strcmp(key, "infile") ) { |
| 81 |
READ_STR_PAR(spp->par->infile); |
| 82 |
|
| 83 |
} else if ( !strcmp(key, "dupc_at_loadtime") ) { |
| 84 |
READ_INT_PAR(spp->par->dupc_at_loadtime); |
| 85 |
|
| 86 |
} else if ( !strcmp(key, "our_format_file") ) { |
| 87 |
READ_STR_PAR(spp->par->our_format_file); |
| 88 |
|
| 89 |
} else if ( !strcmp(key, "our_format_file_counter") ) { |
| 90 |
READ_INT_PAR(spp->par->our_format_file_counter); |
| 91 |
|
| 92 |
} else if ( !strcmp(key, "matlab_format_file") ) { |
| 93 |
READ_STR_PAR(spp->par->matlab_format_file); |
| 94 |
|
| 95 |
} else if ( !strcmp(key, "matlab_format_file_counter") ) { |
| 96 |
READ_INT_PAR(spp->par->matlab_format_file_counter); |
| 97 |
|
| 98 |
} else if ( !strcmp(key, "granularity") ) { |
| 99 |
READ_DBL_PAR(spp->par->granularity); |
| 100 |
|
| 101 |
/* SPP LP params */ |
| 102 |
|
| 103 |
} else if ( !strcmp(key, "do_lift_in_lp") ) { |
| 104 |
READ_INT_PAR(spp->lp_par->do_lift_in_lp); |
| 105 |
|
| 106 |
} else if ( !strcmp(key, "lp_dj_threshold_frac") ) { |
| 107 |
READ_DBL_PAR(spp->lp_par->lp_dj_threshold_frac); |
| 108 |
|
| 109 |
} else if ( !strcmp(key, "lp_dj_threshold_abs") ) { |
| 110 |
READ_DBL_PAR(spp->lp_par->lp_dj_threshold_abs); |
| 111 |
|
| 112 |
} else if ( !strcmp(key, "lanmax") ) { |
| 113 |
READ_INT_PAR(spp->lp_par->lanmax); |
| 114 |
|
| 115 |
} else if ( !strcmp(key, "which_atilde") ) { |
| 116 |
READ_INT_PAR(spp->lp_par->which_atilde); |
| 117 |
|
| 118 |
/* SPP CG params */ |
| 119 |
|
| 120 |
} else if ( !strcmp(key, "min_violation_clique") ) { |
| 121 |
READ_DBL_PAR(spp->cg_par->min_violation_clique); |
| 122 |
|
| 123 |
} else if ( !strcmp(key, "min_violation_oddhole") ) { |
| 124 |
READ_DBL_PAR(spp->cg_par->min_violation_oddhole); |
| 125 |
|
| 126 |
} else if ( !strcmp(key, "min_violation_oddantihole") ) { |
| 127 |
READ_DBL_PAR(spp->cg_par->min_violation_oddantihole); |
| 128 |
|
| 129 |
} else if ( !strcmp(key, "min_violation_wheel") ) { |
| 130 |
READ_DBL_PAR(spp->cg_par->min_violation_wheel); |
| 131 |
|
| 132 |
} else if ( !strcmp(key, "min_violation_orthocut") ) { |
| 133 |
READ_DBL_PAR(spp->cg_par->min_violation_orthocut); |
| 134 |
|
| 135 |
} else if ( !strcmp(key, "min_violation_othercut") ) { |
| 136 |
READ_DBL_PAR(spp->cg_par->min_violation_othercut); |
| 137 |
|
| 138 |
} else if ( !strcmp(key, "starcl_degree_threshold") ) { |
| 139 |
READ_INT_PAR(spp->cg_par->starcl_degree_threshold); |
| 140 |
|
| 141 |
} else if ( !strcmp(key, "starcl_which_node") ) { |
| 142 |
READ_INT_PAR(spp->cg_par->starcl_which_node); |
| 143 |
|
| 144 |
} else if ( !strcmp(key, "rowcl_degree_threshold") ) { |
| 145 |
READ_INT_PAR(spp->cg_par->rowcl_degree_threshold); |
| 146 |
|
| 147 |
} else if ( !strcmp(key, "max_hub_num") ) { |
| 148 |
READ_INT_PAR(spp->cg_par->max_hub_num); |
| 149 |
|
| 150 |
} else if ( !strcmp(key, "eval_oh_during_lifting") ) { |
| 151 |
READ_INT_PAR(spp->cg_par->eval_oh_during_lifting); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/* set some parameters */ |
| 156 |
spp->lp_par->granularity = spp->par->granularity; |
| 157 |
fclose(f); |
| 158 |
} |
| 159 |
|
| 160 |
/*****************************************************************************/ |
| 161 |
|
| 162 |
void spp_print_params(spp_problem *spp) |
| 163 |
{ |
| 164 |
spp_parameters *par = spp->par; |
| 165 |
|
| 166 |
printf("###########################################################\n"); |
| 167 |
printf("Parameter settings:\n"); |
| 168 |
printf(" infile: %s\n", par->infile); |
| 169 |
printf(" dupc_at_loadtime: %i\n", par->dupc_at_loadtime); |
| 170 |
printf(" our_format_file: %s\n", par->our_format_file); |
| 171 |
printf(" our_format_file_counter: %i\n",par->our_format_file_counter); |
| 172 |
printf(" matlab_format_file: %s\n", par->matlab_format_file); |
| 173 |
printf(" matlab_format_file_counter: %i\n", |
| 174 |
par->matlab_format_file_counter); |
| 175 |
printf(" granularity: %f\n", par->granularity); |
| 176 |
printf("\n\n"); |
| 177 |
} |
| 178 |
|
| 179 |
/*****************************************************************************/ |
| 180 |
|
| 181 |
/*===========================================================================* |
| 182 |
* The following routine reads in the data from the input file. The input * |
| 183 |
* file has to be in the following format: * |
| 184 |
* First row of the input file: * |
| 185 |
* <t> <m> <n> <nzcnt> * |
| 186 |
* where <t>: type of input file. (currently: OUR_FORMAT only) * |
| 187 |
* <m>: # of rows in the matrix of the problem. * |
| 188 |
* Rows are assumed to be numbered from 1 to m. * |
| 189 |
* <n>: # of columns in the matrix of the problem. * |
| 190 |
* <nzcnt>: # of nonzeros (=ones) in the matrix. * |
| 191 |
* * |
| 192 |
* A general row of the input file describing a COLUMN of the matrix: * |
| 193 |
* <col_name_j> <c_j> <nzcnt_j> <pos_1> ... <pos_nzcnt_j> * |
| 194 |
* where <col_name_j>: the name of the column (integer). * |
| 195 |
* <c_j>: objective function coefficient. Integral in HP * |
| 196 |
* problems, double in telebus problems. * |
| 197 |
* <nzcnt_j>: number of nonzeros in this column. * |
| 198 |
* <pos_1> ... : indices of rows where the nonzeros are. * |
| 199 |
* Note that in the case of the set partitioning * |
| 200 |
* problem, the matrix is 0-1. * |
| 201 |
* * |
| 202 |
* our0 format: same as above but rows are numbered from 0 to rownum-1. |
| 203 |
*===========================================================================*/ |
| 204 |
|
| 205 |
void spp_read_input(spp_problem *spp) |
| 206 |
{ |
| 207 |
FILE *f; |
| 208 |
col_ordered *matrix; |
| 209 |
int deleted_cols; /* number of columns deleted */ |
| 210 |
int colnum; |
| 211 |
double T = 0; |
| 212 |
|
| 213 |
used_time(&T); |
| 214 |
|
| 215 |
if ((f = fopen(spp->par->infile, "r")) == NULL) |
| 216 |
OPEN_READ_ERROR(spp->par->infile); |
| 217 |
|
| 218 |
if(spp->par->verbosity > -1){ |
| 219 |
printf("########################################################\n"); |
| 220 |
printf("Reading input file %s...\n", spp->par->infile); |
| 221 |
} |
| 222 |
matrix = spp->cmatrix = (col_ordered *) calloc(1, sizeof(col_ordered)); |
| 223 |
|
| 224 |
/* read in the type of the problem, rownum, colnum and nzcnt */ |
| 225 |
if (fscanf(f, "%i", &spp->input_type) != 1) IO_ERROR; |
| 226 |
if (fscanf(f, "%i%i%i", &matrix->rownum, &matrix->colnum, |
| 227 |
&matrix->nzcnt) != 3) IO_ERROR; |
| 228 |
colnum = matrix->colnum; |
| 229 |
printf(" input type: %i, rownum: %i, colnum: %i, nzcnt: %i\n", |
| 230 |
spp->input_type, matrix->rownum, colnum, matrix->nzcnt); |
| 231 |
|
| 232 |
matrix->colnames = (int *) malloc(colnum * ISIZE); |
| 233 |
matrix->obj = (double *) malloc(colnum * DSIZE); |
| 234 |
matrix->matbeg = (int *) malloc((colnum+1) * ISIZE); |
| 235 |
matrix->matind = (row_ind_type *) |
| 236 |
malloc(matrix->nzcnt * sizeof(row_ind_type)); |
| 237 |
|
| 238 |
switch (spp->input_type){ |
| 239 |
|
| 240 |
case OUR_FORMAT: |
| 241 |
|
| 242 |
if ( spp->par->dupc_at_loadtime ) { |
| 243 |
if ( (deleted_cols = read_our_del_dupl(matrix, f)) >= 0 ) { |
| 244 |
printf(" Deleted %i columns at loadtime,\n",deleted_cols); |
| 245 |
printf(" rownum: %i, colnum: %i, nzcnt: %i\n", |
| 246 |
matrix->rownum, matrix->colnum, matrix->nzcnt); |
| 247 |
} |
| 248 |
} else { |
| 249 |
read_our_no_del(matrix, f); |
| 250 |
} |
| 251 |
break; |
| 252 |
|
| 253 |
case OUR_FORMAT_0: |
| 254 |
|
| 255 |
if ( spp->par->dupc_at_loadtime ) { |
| 256 |
if ( (deleted_cols = read_our0_del_dupl(matrix, f)) >= 0 ) { |
| 257 |
printf(" Deleted %i columns at loadtime,\n",deleted_cols); |
| 258 |
printf(" rownum: %i, colnum: %i, nzcnt: %i\n", |
| 259 |
matrix->rownum, matrix->colnum, matrix->nzcnt); |
| 260 |
} |
| 261 |
} else { |
| 262 |
read_our0_no_del(matrix, f); |
| 263 |
} |
| 264 |
break; |
| 265 |
|
| 266 |
default: |
| 267 |
IO_ERROR; |
| 268 |
|
| 269 |
} /* end switch */ |
| 270 |
|
| 271 |
matrix->col_deleted = (char *) calloc(matrix->colnum/BITSPERBYTE +1, CSIZE); |
| 272 |
|
| 273 |
fclose(f); |
| 274 |
printf("\n\n"); |
| 275 |
|
| 276 |
spp->stat[READ_INPUT].time += used_time(&T); |
| 277 |
spp->stat[READ_INPUT].freq++; |
| 278 |
} |
| 279 |
|
| 280 |
/*===========================================================================* |
| 281 |
* read our format, no deletion of duplicate columns at loadtime. |
| 282 |
*===========================================================================*/ |
| 283 |
|
| 284 |
void read_our_no_del(col_ordered *matrix, FILE *f) |
| 285 |
{ |
| 286 |
int colnum = matrix->colnum; |
| 287 |
int rownum = matrix->rownum; |
| 288 |
int *colnames = matrix->colnames; |
| 289 |
double *obj = matrix->obj; |
| 290 |
int *matbeg = matrix->matbeg; |
| 291 |
row_ind_type *matind = matrix->matind; |
| 292 |
|
| 293 |
int j; /* runs thru columns */ |
| 294 |
int i; /* runs thru rows intersecting a column */ |
| 295 |
int nz_count; /* count nonzeros */ |
| 296 |
int col_nzcnt; /* number of nonzeros in a column */ |
| 297 |
int value; /* a temporary variable */ |
| 298 |
|
| 299 |
|
| 300 |
for ( j = 0, nz_count = 0, matbeg[0] = 0; j < colnum; j++ ) { |
| 301 |
|
| 302 |
/* read in the name of the column, obj fn coeff and |
| 303 |
num of rows intersecting this column */ |
| 304 |
if (fscanf(f, "%i%lf%i", &colnames[j], &obj[j], &col_nzcnt) != 3) |
| 305 |
IO_ERROR; |
| 306 |
|
| 307 |
/* read in row indices */ |
| 308 |
for ( i = 0; i < col_nzcnt; i++){ |
| 309 |
if (fscanf(f, "%i", &value) != 1) |
| 310 |
IO_ERROR; |
| 311 |
if ( !value || (value > rownum)) |
| 312 |
IO_ERROR; |
| 313 |
matind[nz_count++] = value-1; |
| 314 |
} |
| 315 |
matbeg[j+1] = matbeg[j] + col_nzcnt; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/*===========================================================================* |
| 320 |
* read our format, delete duplicate columns (that are next to |
| 321 |
* each other) at loadtime. returns the number of columns deleted, or |
| 322 |
* -1 in case of failure. |
| 323 |
*===========================================================================*/ |
| 324 |
|
| 325 |
int read_our_del_dupl(col_ordered *matrix, FILE *f) |
| 326 |
{ |
| 327 |
int colnum = matrix->colnum; |
| 328 |
int rownum = matrix->rownum; |
| 329 |
int *colnames = matrix->colnames; |
| 330 |
double *obj = matrix->obj; |
| 331 |
int *matbeg = matrix->matbeg; |
| 332 |
row_ind_type *matind = matrix->matind; |
| 333 |
|
| 334 |
int j; /* runs thru columns */ |
| 335 |
int i; /* runs thru rows intersecting a column */ |
| 336 |
int col_count, nz_count; /* count columns and nonzeros */ |
| 337 |
int col_nzcnt; /* number of nonzeros in a column */ |
| 338 |
int value; /* a temporary variable */ |
| 339 |
int deleted_cols; /* number of columns deleted */ |
| 340 |
|
| 341 |
|
| 342 |
for ( j = 0, nz_count = 0, col_count = 0, deleted_cols = 0, |
| 343 |
matbeg[0] = 0; j < colnum; j++ ) { |
| 344 |
|
| 345 |
/* read in the name of the column, obj fn coeff and |
| 346 |
num of rows intersecting this column */ |
| 347 |
if (fscanf(f, "%i%lf%i", |
| 348 |
&colnames[col_count], &obj[col_count], &col_nzcnt) != 3) |
| 349 |
IO_ERROR; |
| 350 |
|
| 351 |
/* read in row indices */ |
| 352 |
for ( i = 0; i < col_nzcnt; i++){ |
| 353 |
if (fscanf(f, "%i", &value) != 1) |
| 354 |
IO_ERROR; |
| 355 |
if ( !value || (value > rownum)) |
| 356 |
IO_ERROR; |
| 357 |
matind[nz_count++] = value-1; |
| 358 |
} |
| 359 |
matbeg[col_count+1] = matbeg[col_count] + col_nzcnt; |
| 360 |
|
| 361 |
/* if this this and the previous column are of the same length */ |
| 362 |
if ( col_count > 0 && |
| 363 |
matbeg[col_count] - matbeg[col_count-1] == col_nzcnt ) { |
| 364 |
/* if the two columns ARE the same */ |
| 365 |
if (!memcmp( matind+matbeg[col_count-1], matind+matbeg[col_count], |
| 366 |
col_nzcnt * sizeof(row_ind_type)) ) { |
| 367 |
/* if new column has a better obj fn coeff then keep this; |
| 368 |
no change in matind or matbeg since cols are same */ |
| 369 |
if ( obj[col_count] < obj[col_count-1] ) { |
| 370 |
obj[col_count-1] = obj[col_count]; |
| 371 |
colnames[col_count-1] = colnames[col_count]; |
| 372 |
} |
| 373 |
deleted_cols++; |
| 374 |
nz_count -= col_nzcnt; |
| 375 |
} else { |
| 376 |
col_count++; |
| 377 |
} |
| 378 |
} else { |
| 379 |
col_count++; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
if ( deleted_cols ) { |
| 384 |
matrix->colnum = col_count; |
| 385 |
matrix->rownum = rownum; |
| 386 |
matrix->nzcnt = nz_count; |
| 387 |
matrix->colnames = (int *) realloc((char *)matrix->colnames, |
| 388 |
col_count * ISIZE); |
| 389 |
matrix->obj = (double *) realloc((char *)matrix->obj, |
| 390 |
col_count * DSIZE ); |
| 391 |
matrix->matbeg = (int *) realloc((char *)matrix->matbeg, |
| 392 |
(col_count+1) * ISIZE ); |
| 393 |
matrix->matind = (row_ind_type *) |
| 394 |
realloc((char *)matrix->matind, nz_count * sizeof(row_ind_type) ); |
| 395 |
} |
| 396 |
return(deleted_cols); |
| 397 |
} |
| 398 |
|
| 399 |
/*===========================================================================* |
| 400 |
* read our0 format, no deletion of duplicate columns at loadtime. |
| 401 |
*===========================================================================*/ |
| 402 |
|
| 403 |
void read_our0_no_del(col_ordered *matrix, FILE *f) |
| 404 |
{ |
| 405 |
int colnum = matrix->colnum; |
| 406 |
int rownum = matrix->rownum; |
| 407 |
int *colnames = matrix->colnames; |
| 408 |
double *obj = matrix->obj; |
| 409 |
int *matbeg = matrix->matbeg; |
| 410 |
row_ind_type *matind = matrix->matind; |
| 411 |
|
| 412 |
int j; /* runs thru columns */ |
| 413 |
int i; /* runs thru rows intersecting a column */ |
| 414 |
int nz_count; /* count nonzeros */ |
| 415 |
int col_nzcnt; /* number of nonzeros in a column */ |
| 416 |
int value; /* a temporary variable */ |
| 417 |
int rownum_minus_1 = rownum - 1; |
| 418 |
|
| 419 |
|
| 420 |
for ( j = 0, nz_count = 0, matbeg[0] = 0; j < colnum; j++ ) { |
| 421 |
|
| 422 |
/* read in the name of the column, obj fn coeff and |
| 423 |
num of rows intersecting this column */ |
| 424 |
if (fscanf(f, "%i%lf%i", &colnames[j], &obj[j], &col_nzcnt) != 3) |
| 425 |
IO_ERROR; |
| 426 |
|
| 427 |
/* read in row indices */ |
| 428 |
for ( i = 0; i < col_nzcnt; i++){ |
| 429 |
if (fscanf(f, "%i", &value) != 1) |
| 430 |
IO_ERROR; |
| 431 |
if ( value < 0 || value > rownum_minus_1 ) |
| 432 |
IO_ERROR; |
| 433 |
matind[nz_count++] = value; |
| 434 |
} |
| 435 |
matbeg[j+1] = matbeg[j] + col_nzcnt; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
/*===========================================================================* |
| 440 |
* read our0 format, delete duplicate columns (that are next to |
| 441 |
* each other) at loadtime. returns the number of columns deleted, or |
| 442 |
* -1 in case of failure. |
| 443 |
*===========================================================================*/ |
| 444 |
|
| 445 |
int read_our0_del_dupl(col_ordered *matrix, FILE *f) |
| 446 |
{ |
| 447 |
int colnum = matrix->colnum; |
| 448 |
int rownum = matrix->rownum; |
| 449 |
int *colnames = matrix->colnames; |
| 450 |
double *obj = matrix->obj; |
| 451 |
int *matbeg = matrix->matbeg; |
| 452 |
row_ind_type *matind = matrix->matind; |
| 453 |
int rownum_minus_1 = rownum - 1; |
| 454 |
|
| 455 |
int j; /* runs thru columns */ |
| 456 |
int i; /* runs thru rows intersecting a column */ |
| 457 |
int col_count, nz_count; /* count columns and nonzeros */ |
| 458 |
int col_nzcnt; /* number of nonzeros in a column */ |
| 459 |
int value; /* a temporary variable */ |
| 460 |
int deleted_cols; /* number of columns deleted */ |
| 461 |
|
| 462 |
|
| 463 |
for ( j = 0, nz_count = 0, col_count = 0, deleted_cols = 0, |
| 464 |
matbeg[0] = 0; j < colnum; j++ ) { |
| 465 |
|
| 466 |
/* read in the name of the column, obj fn coeff and |
| 467 |
num of rows intersecting this column */ |
| 468 |
if (fscanf(f, "%i%lf%i", |
| 469 |
&colnames[col_count], &obj[col_count], &col_nzcnt) != 3) |
| 470 |
IO_ERROR; |
| 471 |
|
| 472 |
/* read in row indices */ |
| 473 |
for ( i = 0; i < col_nzcnt; i++){ |
| 474 |
if (fscanf(f, "%i", &value) != 1) |
| 475 |
IO_ERROR; |
| 476 |
if ( value < 0 || value > rownum_minus_1) |
| 477 |
IO_ERROR; |
| 478 |
matind[nz_count++] = value; |
| 479 |
} |
| 480 |
matbeg[col_count+1] = matbeg[col_count] + col_nzcnt; |
| 481 |
|
| 482 |
/* if this this and the previous column are of the same length */ |
| 483 |
if ( col_count > 0 && |
| 484 |
matbeg[col_count] - matbeg[col_count-1] == col_nzcnt ) { |
| 485 |
/* if the two columns ARE the same */ |
| 486 |
if (!memcmp( matind+matbeg[col_count-1], matind+matbeg[col_count], |
| 487 |
col_nzcnt * sizeof(row_ind_type)) ) { |
| 488 |
/* if new column has a better obj fn coeff then keep this; |
| 489 |
no change in matind or matbeg since cols are same */ |
| 490 |
if ( obj[col_count] < obj[col_count-1] ) { |
| 491 |
obj[col_count-1] = obj[col_count]; |
| 492 |
colnames[col_count-1] = colnames[col_count]; |
| 493 |
} |
| 494 |
deleted_cols++; |
| 495 |
nz_count -= col_nzcnt; |
| 496 |
} else { |
| 497 |
col_count++; |
| 498 |
} |
| 499 |
} else { |
| 500 |
col_count++; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
if ( deleted_cols ) { |
| 505 |
matrix->colnum = col_count; |
| 506 |
matrix->rownum = rownum; |
| 507 |
matrix->nzcnt = nz_count; |
| 508 |
matrix->colnames = (int *) realloc((char *)matrix->colnames, |
| 509 |
col_count * ISIZE); |
| 510 |
matrix->obj = (double *) realloc((char *)matrix->obj, |
| 511 |
col_count * DSIZE ); |
| 512 |
matrix->matbeg = (int *) realloc((char *)matrix->matbeg, |
| 513 |
(col_count+1) * ISIZE ); |
| 514 |
matrix->matind = (row_ind_type *) |
| 515 |
realloc((char *)matrix->matind, nz_count * sizeof(row_ind_type) ); |
| 516 |
} |
| 517 |
return(deleted_cols); |
| 518 |
} |
| 519 |
|
| 520 |
/*****************************************************************************/ |
| 521 |
|
| 522 |
void spp_fix_lex(spp_problem *spp) |
| 523 |
{ |
| 524 |
int *order; /* order is allocated in lexsort() */ |
| 525 |
col_ordered *matrix = spp->cmatrix; |
| 526 |
int colnum = matrix->colnum; |
| 527 |
int *colnames = matrix->colnames; |
| 528 |
double *obj = matrix->obj; |
| 529 |
int *matbeg = matrix->matbeg; |
| 530 |
row_ind_type *matind = matrix->matind; |
| 531 |
|
| 532 |
int *new_colnames = (int *) malloc(colnum * ISIZE); |
| 533 |
double *new_obj = (double *) malloc( colnum * DSIZE); |
| 534 |
int *new_matbeg = (int *) malloc((colnum+1) * ISIZE); |
| 535 |
row_ind_type *new_matind = (row_ind_type *) |
| 536 |
malloc( matrix->nzcnt * sizeof(row_ind_type)); |
| 537 |
|
| 538 |
int i, col, col_length; |
| 539 |
|
| 540 |
/* order is a permutation of column indices that shows their lex order */ |
| 541 |
order = spp_lexsort(matrix); |
| 542 |
|
| 543 |
/* copy the columns into the new data structure */ |
| 544 |
for ( i = 0, new_matbeg[0] = 0; i < colnum; i++) { |
| 545 |
col = order[i]; |
| 546 |
new_colnames[i] = colnames[col]; |
| 547 |
new_obj[i] = obj[col]; |
| 548 |
col_length = matbeg[col+1] - matbeg[col]; |
| 549 |
memcpy((char *)(new_matind + new_matbeg[i]), |
| 550 |
(char *)(matind + matbeg[col]), |
| 551 |
col_length * sizeof(row_ind_type) ); |
| 552 |
new_matbeg[i+1] = new_matbeg[i] + col_length; |
| 553 |
} |
| 554 |
|
| 555 |
FREE(colnames); |
| 556 |
FREE(obj); |
| 557 |
FREE(matbeg); |
| 558 |
FREE(matind); |
| 559 |
|
| 560 |
matrix->colnames = new_colnames; |
| 561 |
matrix->obj = new_obj; |
| 562 |
matrix->matbeg = new_matbeg; |
| 563 |
matrix->matind = new_matind; |
| 564 |
|
| 565 |
FREE(order); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
/*****************************************************************************/ |
| 570 |
|
| 571 |
/*===========================================================================* |
| 572 |
* Write the column ordered matrix into a file (or stdout), in our format. |
| 573 |
* counter is increased here. |
| 574 |
*===========================================================================*/ |
| 575 |
|
| 576 |
void spp_matrix_to_our(col_ordered *matrix, char *filename, int *counter) |
| 577 |
{ |
| 578 |
char fname[MAX_FILE_NAME_LENGTH]; |
| 579 |
FILE *f; |
| 580 |
|
| 581 |
int i; /* runs thru columns */ |
| 582 |
int colnum = matrix->colnum; |
| 583 |
int *colnames = matrix->colnames; |
| 584 |
double *obj = matrix->obj; |
| 585 |
int *matbeg = matrix->matbeg; |
| 586 |
row_ind_type *matind = matrix->matind; |
| 587 |
int col_nzcnt; /* number of rows intersecting a column */ |
| 588 |
int j; /* runs thru rows intersecting a column */ |
| 589 |
row_ind_type *col_beg; /* pointer to the beginning of a column in matind */ |
| 590 |
|
| 591 |
f = get_filehandler(*counter, filename, fname); |
| 592 |
(*counter)++; |
| 593 |
|
| 594 |
/* first row: type, rownum, colnum, nzcnt */ |
| 595 |
fprintf(f, "%8i %8i %15i %15i\n", OUR_FORMAT_0, matrix->rownum, |
| 596 |
colnum, matrix->nzcnt); |
| 597 |
|
| 598 |
/* for each column: colname, obj, col_nzcnt, row indices */ |
| 599 |
for ( i = 0; i < colnum; i++ ) { |
| 600 |
col_nzcnt = matbeg[i+1]-matbeg[i]; |
| 601 |
col_beg = matind + matbeg[i]; |
| 602 |
fprintf(f, "%8i%9.2f%3i ", colnames[i], obj[i], col_nzcnt); |
| 603 |
for ( j = 0; j < col_nzcnt; j++ ) |
| 604 |
fprintf(f, " %4i", col_beg[j]); |
| 605 |
fprintf(f, "\n"); |
| 606 |
} |
| 607 |
|
| 608 |
if ( f != stdout ) |
| 609 |
fclose(f); |
| 610 |
|
| 611 |
printf("########################################################\n"); |
| 612 |
printf("Saved problem matrix into file %s\n\n\n", fname); |
| 613 |
} |
| 614 |
|
| 615 |
/*****************************************************************************/ |
| 616 |
|
| 617 |
/*===========================================================================* |
| 618 |
* Write the column ordered matrix into a file (or stdout), in flat (matlab |
| 619 |
* readable) format. (note that vectors indices start from 1 in matlab) |
| 620 |
* counter is increased here. |
| 621 |
*===========================================================================*/ |
| 622 |
|
| 623 |
void spp_matrix_to_matlab(col_ordered *matrix, char *filename, int *counter) |
| 624 |
{ |
| 625 |
char fname[MAX_FILE_NAME_LENGTH]; |
| 626 |
FILE *f; |
| 627 |
|
| 628 |
int i; /* runs thru columns */ |
| 629 |
int colnum = matrix->colnum; |
| 630 |
int col_nzcnt; /* number of rows intersecting a column */ |
| 631 |
int *matbeg = matrix->matbeg; |
| 632 |
row_ind_type *matind = matrix->matind; |
| 633 |
int j; /* runs thru rows intersecting a column */ |
| 634 |
row_ind_type *col_beg; /* pointer to the beginning of a column in matind */ |
| 635 |
|
| 636 |
f = get_filehandler(*counter, filename, fname); |
| 637 |
(*counter)++; |
| 638 |
|
| 639 |
/* first print out "rownum colnum 0" */ |
| 640 |
fprintf(f, "%10i %10i %10i\n", matrix->rownum, colnum, 0); |
| 641 |
|
| 642 |
/* for each column, and for each row intersecting this column |
| 643 |
print "row_index column_index 1" */ |
| 644 |
for ( i = 0; i < colnum; i++ ) { |
| 645 |
col_nzcnt = matbeg[i+1] - matbeg[i]; |
| 646 |
col_beg = matind + matbeg[i]; |
| 647 |
for ( j = 0; j < col_nzcnt; j++ ) { |
| 648 |
fprintf(f, "%10i %10i %10i\n", col_beg[j]+1, i+1, 1); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
if ( f != stdout ) |
| 653 |
fclose(f); |
| 654 |
|
| 655 |
printf("########################################################\n"); |
| 656 |
printf("Saved problem matrix into file %s\n\n", fname); |
| 657 |
} |