| 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 |
// symphony headers |
| 24 |
#include "symphony.h" |
| 25 |
#include "sym_master.h" |
| 26 |
// application header |
| 27 |
#include "user.h" |
| 28 |
// C library headers |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
int user_read_data(user_problem * prob, char * infile); |
| 32 |
int user_load_problem(sym_environment *env, user_problem *prob); |
| 33 |
|
| 34 |
int main(int argc, char **argv) { |
| 35 |
int termcode; |
| 36 |
sym_environment *env = sym_open_environment(); |
| 37 |
if (!env){ |
| 38 |
printf("Error initializing environement\n"); |
| 39 |
exit(0); |
| 40 |
} |
| 41 |
sym_version(); |
| 42 |
user_problem *prob = (user_problem *) calloc(1, sizeof(user_problem)); |
| 43 |
CALL_FUNCTION( sym_set_user_data(env, (void *)prob) ); |
| 44 |
CALL_FUNCTION( sym_parse_command_line(env, argc, argv) ); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
CALL_FUNCTION( user_read_data(prob, prob->par.infile) ); |
| 50 |
CALL_FUNCTION( user_load_problem(env, prob) ); |
| 51 |
/* CALL_FUNCTION( sym_find_initial_bounds(env) ); */ |
| 52 |
CALL_FUNCTION( sym_solve(env) ); |
| 53 |
CALL_FUNCTION( sym_close_environment(env) ); |
| 54 |
return(0); |
| 55 |
} |
| 56 |
|
| 57 |
// here you are supposed to fill user_prob instance. |
| 58 |
int user_read_data(user_problem * prob, char * infile) { |
| 59 |
// fill just the conic related part of the problem, this is what we will |
| 60 |
// store in (user_prob *) prob |
| 61 |
int i; |
| 62 |
int j; |
| 63 |
int errors; |
| 64 |
CoinMpsIO mps; |
| 65 |
// read cones from the input file |
| 66 |
int nOfCones = 0; |
| 67 |
int * coneStart = NULL; |
| 68 |
int * coneIdx = NULL; |
| 69 |
int * coneType = NULL; |
| 70 |
// store infile in prob->par->infile |
| 71 |
strncpy(prob->par.infile, infile, MAX_FILE_NAME_LENGTH); |
| 72 |
prob->par.infile[MAX_FILE_NAME_LENGTH-1] = 0; |
| 73 |
mps.messageHandler()->setLogLevel(0); |
| 74 |
mps.setInfinity(mps.getInfinity()); |
| 75 |
errors = mps.readMps(infile,""); |
| 76 |
if (!(errors==0 || errors==-3)) { |
| 77 |
return(errors); |
| 78 |
} |
| 79 |
errors = mps.readConicMps(NULL, coneStart, coneIdx, coneType, nOfCones); |
| 80 |
// when there is no conic section errors is -3. |
| 81 |
if (errors==-3) { |
| 82 |
fprintf(stdout, "OsiConic: No conic section is mps file.\n"); |
| 83 |
} |
| 84 |
else { |
| 85 |
fprintf(stderr, "OsiConic: readConicMps returned code %d.\n", errors); |
| 86 |
assert (!errors); |
| 87 |
} |
| 88 |
// set number of cones |
| 89 |
prob->num_cones = nOfCones; |
| 90 |
// allocate memory for type, size, members |
| 91 |
prob->cone_type = (int *) malloc(nOfCones*sizeof(int)); |
| 92 |
prob->cone_size = (int *) malloc(nOfCones*sizeof(int)); |
| 93 |
prob->cone_members = (int **) malloc(nOfCones*sizeof(int*)); |
| 94 |
for (i=0; i<nOfCones; ++i) { |
| 95 |
if (coneType[i]!=1 and coneType[i]!=2) { |
| 96 |
fprintf(stderr, "Invalid cone type!\n"); |
| 97 |
assert(0); |
| 98 |
} |
| 99 |
int num_members = coneStart[i+1]-coneStart[i]; |
| 100 |
if (coneType[i]==2 && num_members<3) { |
| 101 |
fprintf(stderr, "Rotated cones should have at least 3 members!\n"); |
| 102 |
assert(0); |
| 103 |
} |
| 104 |
// set size of cone i |
| 105 |
prob->cone_size[i] = num_members; |
| 106 |
// set type for cone i |
| 107 |
if (coneType[i]==1) |
| 108 |
prob->cone_type[i] = 0; |
| 109 |
else |
| 110 |
prob->cone_type[i] = 1; |
| 111 |
// set members of cone i |
| 112 |
prob->cone_members[i] = (int *) malloc(num_members*sizeof(int)); |
| 113 |
int k=0; |
| 114 |
for (j=coneStart[i]; j<coneStart[i+1]; ++j) { |
| 115 |
prob->cone_members[i][k] = coneIdx[j]; |
| 116 |
k++; |
| 117 |
} |
| 118 |
} |
| 119 |
// check log level and print accordingly |
| 120 |
if (nOfCones) { |
| 121 |
printf("Conic section has %d cones\n",nOfCones); |
| 122 |
for (int iCone=0;iCone<nOfCones;iCone++) { |
| 123 |
printf("Cone %d has %d entries (type %d) ",iCone,coneStart[iCone+1]-coneStart[iCone], |
| 124 |
coneType[iCone]); |
| 125 |
for (int j=coneStart[iCone];j<coneStart[iCone+1];j++) |
| 126 |
printf("%d ",coneIdx[j]); |
| 127 |
printf("\n"); |
| 128 |
} |
| 129 |
} |
| 130 |
free(coneStart); |
| 131 |
free(coneIdx); |
| 132 |
free(coneType); |
| 133 |
if (errors==0 || errors==-3) |
| 134 |
return (FUNCTION_TERMINATED_NORMALLY); |
| 135 |
else |
| 136 |
return (ERROR__USER); |
| 137 |
} |
| 138 |
|
| 139 |
// read problem using prob->infile and load it to env |
| 140 |
int user_load_problem(sym_environment *env, user_problem *prob) { |
| 141 |
int j; |
| 142 |
int errors; |
| 143 |
int n; |
| 144 |
int m; |
| 145 |
int nz; |
| 146 |
double * obj = 0; |
| 147 |
double * lb = 0; |
| 148 |
double * ub = 0; |
| 149 |
char * is_int = 0; |
| 150 |
int * matbeg = 0; |
| 151 |
char ** colname = 0; |
| 152 |
double * rhs = 0; |
| 153 |
char * sense = 0; |
| 154 |
double * rngval = 0; |
| 155 |
double * matval = 0; |
| 156 |
int * matind = 0; |
| 157 |
double obj_offset = 0.0; |
| 158 |
CoinMpsIO mps; |
| 159 |
mps.messageHandler()->setLogLevel(0); |
| 160 |
mps.setInfinity(mps.getInfinity()); |
| 161 |
errors = mps.readMps(prob->par.infile,""); |
| 162 |
// when there is no conic section errors is -3. |
| 163 |
if (errors==-3) { |
| 164 |
fprintf(stdout, "OsiConic: No conic section is mps file.\n"); |
| 165 |
} |
| 166 |
else { |
| 167 |
fprintf(stderr, "OsiConic: readConicMps returned code %d.\n", errors); |
| 168 |
assert (!errors); |
| 169 |
} |
| 170 |
//strncpy(probname, mps.getProblemName(), 80); |
| 171 |
m = mps.getNumRows(); |
| 172 |
n = mps.getNumCols(); |
| 173 |
nz = mps.getNumElements(); |
| 174 |
prob->rownum = m; |
| 175 |
prob->colnum = n; |
| 176 |
const CoinPackedMatrix * matrixByCol = mps.getMatrixByCol(); |
| 177 |
if (n) { |
| 178 |
obj = (double *) malloc(DSIZE * n); |
| 179 |
ub = (double *) malloc(DSIZE * n); |
| 180 |
lb = (double *) malloc(DSIZE * n); |
| 181 |
is_int = (char *) calloc(CSIZE, n); |
| 182 |
memcpy(obj, mps.getObjCoefficients(), DSIZE * n); |
| 183 |
memcpy(ub, mps.getColUpper(), DSIZE * n); |
| 184 |
memcpy(lb, mps.getColLower(), DSIZE * n); |
| 185 |
matbeg = (int *) malloc(ISIZE * (n + 1)); |
| 186 |
memcpy(matbeg, matrixByCol->getVectorStarts(), ISIZE * (n + 1)); |
| 187 |
colname = (char **) malloc(sizeof(char *) * n); |
| 188 |
} |
| 189 |
if (m) { |
| 190 |
rhs = (double *) malloc(DSIZE * m); |
| 191 |
sense = (char *) malloc(CSIZE * m); |
| 192 |
rngval = (double *) malloc(DSIZE * m); |
| 193 |
memcpy(rhs, mps.getRightHandSide(), DSIZE * m); |
| 194 |
memcpy(sense, mps.getRowSense(), CSIZE * m); |
| 195 |
memcpy(rngval, mps.getRowRange(), DSIZE * m); |
| 196 |
} |
| 197 |
//user defined matind, matval, matbeg--fill as column ordered |
| 198 |
if (nz) { |
| 199 |
matval = (double *) malloc(DSIZE*matbeg[n]); |
| 200 |
matind = (int *) malloc(ISIZE*matbeg[n]); |
| 201 |
memcpy(matval, matrixByCol->getElements(), DSIZE * matbeg[n]); |
| 202 |
memcpy(matind, matrixByCol->getIndices(), ISIZE * matbeg[n]); |
| 203 |
} |
| 204 |
for (j=0; j<n; j++){ |
| 205 |
is_int[j] = mps.isInteger(j); |
| 206 |
colname[j] = (char *) malloc(CSIZE * MAX_NAME_SIZE); |
| 207 |
strncpy(colname[j], mps.columnName(j), MAX_NAME_SIZE); |
| 208 |
colname[j][MAX_NAME_SIZE-1] = 0; |
| 209 |
} |
| 210 |
prob->is_int = is_int; |
| 211 |
// the following is not relevant here, since all mps problems are |
| 212 |
// in minimization |
| 213 |
/* if (obj_sense == SYM_MAXIMIZE){ */ |
| 214 |
/* for (j = 0; j < mip->n; j++){ */ |
| 215 |
/* mip->obj[j] *= -1.0; */ |
| 216 |
/* } */ |
| 217 |
/* } */ |
| 218 |
obj_offset = -mps.objectiveOffset(); |
| 219 |
/* Load the problem to SYMPHONY */ |
| 220 |
sym_explicit_load_problem(env, n, m, matbeg, matind, |
| 221 |
matval, lb, ub, is_int, obj, 0, sense, |
| 222 |
rhs, rngval, true); |
| 223 |
return (FUNCTION_TERMINATED_NORMALLY); |
| 224 |
} |