[6ba162] | 1 | // testdata.cc |
---|
| 2 | |
---|
| 3 | // Implements the data generation routines declared in testdata.h. |
---|
| 4 | |
---|
| 5 | #ifndef TESTDATA_CC |
---|
| 6 | #define TESTDATA_CC |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | #include "testdata.h" |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | int random_matrix(const short& rows, const short& columns, |
---|
| 16 | const Integer& lower_bound, const Integer& upper_bound, |
---|
| 17 | ofstream& MATRIX) |
---|
| 18 | { |
---|
| 19 | // check arguments |
---|
| 20 | |
---|
| 21 | if(rows<=0) |
---|
| 22 | { |
---|
| 23 | cerr<<"ERROR: int random_matrix(const short&, const short&, \n" |
---|
| 24 | " const Integer&, const Integer&, ofstream&):\n" |
---|
| 25 | "first argument out of range: number of matrix rows must be positive" |
---|
| 26 | <<endl; |
---|
| 27 | return 0; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | if(columns<=0) |
---|
| 31 | { |
---|
| 32 | cerr<<"ERROR: int random_matrix(const short&, const short&, \n" |
---|
| 33 | " const Integer&, const Integer&, ofstream&):\n" |
---|
| 34 | "second argument out of range: number of matrix columns must be positive" |
---|
| 35 | <<endl; |
---|
| 36 | return 0; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | if(upper_bound<0) |
---|
| 40 | { |
---|
| 41 | cerr<<"ERROR: int random_matrix(const short&, const short&, \n" |
---|
| 42 | " const Integer&, const Integer&, ofstream&):\n" |
---|
| 43 | "fourth argument (upper bound for random cost vector entries) must be\n" |
---|
| 44 | "nonnegative\n"<<endl; |
---|
| 45 | return 0; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | if(upper_bound<lower_bound) |
---|
| 49 | { |
---|
| 50 | cerr<<"ERROR: int random_matrix(const short&, const short&, \n" |
---|
| 51 | " const Integer&, const Integer&, ofstream&):\n" |
---|
| 52 | "third argument (lower bound for random entries) must be less\n" |
---|
| 53 | "or equal the fourth argument (upper bound)"<<endl; |
---|
| 54 | return 0; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | // create test file |
---|
| 58 | |
---|
| 59 | MATRIX<<"MATRIX"<<endl<<endl; |
---|
| 60 | |
---|
| 61 | MATRIX<<"columns:"<<endl; |
---|
| 62 | MATRIX<<columns<<endl<<endl; |
---|
| 63 | |
---|
| 64 | // random cost vector |
---|
| 65 | MATRIX<<"cost vector:"<<endl; |
---|
| 66 | for(short j=0;j<columns;j++) |
---|
| 67 | MATRIX<<setw(4)<<rand()%(upper_bound+1); |
---|
| 68 | // random entries between 0 and upper_bound |
---|
| 69 | MATRIX<<endl; |
---|
| 70 | |
---|
| 71 | MATRIX<<"rows:"<<endl; |
---|
| 72 | MATRIX<<rows<<endl<<endl; |
---|
| 73 | |
---|
| 74 | // random matrix |
---|
| 75 | MATRIX<<"matrix:"<<endl; |
---|
| 76 | for(short i=0;i<rows;i++) |
---|
| 77 | { |
---|
| 78 | for(short j=0;j<columns;j++) |
---|
| 79 | MATRIX<<setw(4)<<rand()%(upper_bound-lower_bound+1)+lower_bound; |
---|
| 80 | // random entries between lower_bound and upper_bound |
---|
| 81 | MATRIX<<endl; |
---|
| 82 | } |
---|
| 83 | MATRIX<<endl; |
---|
| 84 | |
---|
| 85 | MATRIX<<"positive row space vector:"<<endl; |
---|
| 86 | |
---|
| 87 | return 1; |
---|
| 88 | |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | int transportation_problem(const short& sources, const short& targets, |
---|
| 95 | const Integer& upper_bound, ofstream& MATRIX) |
---|
| 96 | { |
---|
| 97 | // check arguments |
---|
| 98 | |
---|
| 99 | if(sources<=0) |
---|
| 100 | { |
---|
| 101 | cerr<<"ERROR: int transportation_problem(const short&, const short&, \n" |
---|
| 102 | " const Integer&, ofstream&):\n" |
---|
| 103 | "first argument out of range: number of sources must be positive" |
---|
| 104 | <<endl; |
---|
| 105 | return 0; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | if(targets<=0) |
---|
| 109 | { |
---|
| 110 | cerr<<"ERROR: int transportation_problem(const short&, const short&, \n" |
---|
| 111 | " const Integer&, ofstream&):\n" |
---|
| 112 | "second argument out of range: number of targets must be positive" |
---|
| 113 | <<endl; |
---|
| 114 | return 0; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if(upper_bound<0) |
---|
| 118 | { |
---|
| 119 | cerr<<"ERROR: int transportation_problem(const short&, const short&, \n" |
---|
| 120 | " const Integer&, const Integer&, ofstream&):\n" |
---|
| 121 | "third argument (upper bound for random cost vector entries) must be\n" |
---|
| 122 | "nonnegative\n"<<endl; |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | // create test file |
---|
| 127 | |
---|
| 128 | MATRIX<<"MATRIX"<<endl<<endl; |
---|
| 129 | |
---|
| 130 | MATRIX<<"columns:"<<endl; |
---|
| 131 | MATRIX<<sources*targets<<endl<<endl; |
---|
| 132 | |
---|
| 133 | // random cost vector |
---|
| 134 | MATRIX<<"cost vector:"<<endl; |
---|
| 135 | for(short j=0;j<sources*targets;j++) |
---|
| 136 | MATRIX<<setw(4)<<rand()%(upper_bound+1); |
---|
| 137 | // random entries between 0 and upper_bound |
---|
| 138 | MATRIX<<endl; |
---|
| 139 | |
---|
| 140 | MATRIX<<"rows:"<<endl; |
---|
| 141 | MATRIX<<sources+targets<<endl<<endl; |
---|
| 142 | |
---|
| 143 | // constraint matrix in the usual formulation of the transportation problem |
---|
| 144 | // as an IP problem |
---|
| 145 | MATRIX<<"matrix:"<<endl; |
---|
| 146 | |
---|
| 147 | for(int i=0;i<targets;i++) |
---|
| 148 | // generate matrix |
---|
| 149 | { |
---|
| 150 | for(int k=0;k<targets;k++) |
---|
| 151 | for(int j=0;j<sources;j++) |
---|
| 152 | MATRIX<<setw(2)<<(int)(i==k); |
---|
| 153 | MATRIX<<endl; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | for(int j=0;j<sources;j++) |
---|
| 157 | { |
---|
| 158 | for(int i=0;i<targets;i++) |
---|
| 159 | for(int k=0;k<sources;k++) |
---|
| 160 | MATRIX<<setw(2)<<(int)(j==k); |
---|
| 161 | MATRIX<<endl; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | MATRIX<<endl; |
---|
| 165 | |
---|
| 166 | MATRIX<<"positive row space vector:"<<endl; |
---|
| 167 | for(short j=0;j<sources*targets;j++) |
---|
| 168 | MATRIX<<setw(2)<<1<<endl; |
---|
| 169 | MATRIX<<endl; |
---|
| 170 | |
---|
| 171 | return 1; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | int random_problems(const short& vector_dimension, |
---|
| 177 | const long& number_of_instances, |
---|
| 178 | const Integer& lower_bound, const Integer& upper_bound, |
---|
| 179 | ofstream& PROBLEM) |
---|
| 180 | { |
---|
| 181 | // check arguments |
---|
| 182 | |
---|
| 183 | if(vector_dimension<=0) |
---|
| 184 | { |
---|
| 185 | cerr<<"ERROR: int random_problems(const short&, const long&, \n" |
---|
| 186 | " const Integer&, const Integer&, ofstream&):\n" |
---|
| 187 | "first argument out of range: vector dimension must be positive" |
---|
| 188 | <<endl; |
---|
| 189 | return 0; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | if(number_of_instances<0) |
---|
| 193 | { |
---|
| 194 | cerr<<"ERROR: int random_problems(const short&, const long&, \n" |
---|
| 195 | " const Integer&, const Integer&, ofstream&):\n" |
---|
| 196 | "second argument out of range: number of instances must be nonnegative" |
---|
| 197 | <<endl; |
---|
| 198 | return 0; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | if(upper_bound<lower_bound) |
---|
| 202 | { |
---|
| 203 | cerr<<"ERROR: int random_problems(const short&, const long&, \n" |
---|
| 204 | " const Integer&, const Integer&, ofstream&):\n" |
---|
| 205 | "third argument (lower bound for random entries) must be less\n" |
---|
| 206 | "or equal the fourth argument (upper bound)"<<endl; |
---|
| 207 | return 0; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | // create random problems |
---|
| 211 | |
---|
| 212 | PROBLEM<<"PROBLEM"<<endl<<endl; |
---|
| 213 | |
---|
| 214 | PROBLEM<<"vector size:"<<endl; |
---|
| 215 | PROBLEM<<vector_dimension<<endl<<endl; |
---|
| 216 | |
---|
| 217 | PROBLEM<<"number of instances:"<<endl; |
---|
| 218 | PROBLEM<<number_of_instances<<endl<<endl; |
---|
| 219 | |
---|
| 220 | PROBLEM<<"right hand or initial solution vectors:"<<endl; |
---|
| 221 | for(short i=0;i<number_of_instances;i++) |
---|
| 222 | { |
---|
| 223 | for(short j=0;j<vector_dimension;j++) |
---|
| 224 | PROBLEM<<setw(4)<<rand()%(upper_bound+1); |
---|
| 225 | // random entries between 0 and upper_bound |
---|
| 226 | PROBLEM<<endl; |
---|
| 227 | } |
---|
| 228 | PROBLEM<<endl; |
---|
| 229 | |
---|
| 230 | return 1; |
---|
| 231 | } |
---|
| 232 | #endif // TESTDATA_CC |
---|