Debian Codegraph @

CoinMP/src/CoinProblem.c

raw
1
2 /* CoinProblem.c */
3
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <float.h> /* DBL_MAX */
8
9 #include "CoinProblem.h"
10
11
12 PPROBLEM coinCreateProblemObject()
13 {
14 PPROBLEM pProblem;
15
16 pProblem = (PPROBLEM)malloc(sizeof(PROBLEMINFO));
17 memset(pProblem, 0, sizeof(PROBLEMINFO));
18
19 pProblem->ColCount = 0;
20 pProblem->RowCount = 0;
21 pProblem->NZCount = 0;
22 pProblem->RangeCount = 0;
23 pProblem->ObjectSense = 0;
24 pProblem->ObjectConst = 0.0;
25
26 pProblem->lenColNamesBuf = 0;
27 pProblem->lenRowNamesBuf = 0;
28 pProblem->lenObjNameBuf = 0;
29
30 pProblem->ObjectCoeffs = NULL;
31 pProblem->RHSValues = NULL;
32 pProblem->RangeValues = NULL;
33 pProblem->RowType = NULL;
34 pProblem->MatrixBegin = NULL;
35 pProblem->MatrixCount = NULL;
36 pProblem->MatrixIndex = NULL;
37 pProblem->MatrixValues = NULL;
38 pProblem->LowerBounds = NULL;
39 pProblem->UpperBounds = NULL;
40 pProblem->ColNamesBuf = NULL;
41 pProblem->RowNamesBuf = NULL;
42 pProblem->ColNamesList = NULL;
43 pProblem->RowNamesList = NULL;
44 pProblem->ObjectName = NULL;
45
46 pProblem->InitValues = NULL;
47
48 pProblem->RowLower = NULL;
49 pProblem->RowUpper = NULL;
50
51 pProblem->ColType = NULL;
52
53 pProblem->SolveAsMIP = 0;
54 pProblem->IntCount = 0;
55 pProblem->BinCount = 0;
56 pProblem->numInts = 0;
57 pProblem->IsInt = NULL;
58
59 pProblem->PriorCount = 0;
60 pProblem->PriorIndex = NULL;
61 pProblem->PriorValues = NULL;
62 pProblem->PriorBranch = NULL;
63
64 pProblem->SosCount = 0;
65 pProblem->SosNZCount = 0;
66 pProblem->SosType = NULL;
67 pProblem->SosPrior = NULL;
68 pProblem->SosBegin = NULL;
69 pProblem->SosIndex = NULL;
70 pProblem->SosRef = NULL;
71
72 pProblem->SemiCount = 0;
73 pProblem->SemiIndex = NULL;
74 pProblem->SemiIndex = NULL;
75
76 return pProblem;
77 }
78
79
80
81 void coinClearProblemObject(PPROBLEM pProblem)
82 {
83 if (!pProblem) {
84 return;
85 }
86 if (pProblem->ObjectCoeffs) free(pProblem->ObjectCoeffs);
87 if (pProblem->RHSValues) free(pProblem->RHSValues);
88 if (pProblem->RangeValues) free(pProblem->RangeValues);
89 if (pProblem->RowType) free(pProblem->RowType);
90 if (pProblem->MatrixBegin) free(pProblem->MatrixBegin);
91 if (pProblem->MatrixCount) free(pProblem->MatrixCount);
92 if (pProblem->MatrixIndex) free(pProblem->MatrixIndex);
93 if (pProblem->MatrixValues) free(pProblem->MatrixValues);
94 if (pProblem->LowerBounds) free(pProblem->LowerBounds);
95 if (pProblem->UpperBounds) free(pProblem->UpperBounds);
96
97 if (pProblem->ColNamesBuf) free(pProblem->ColNamesBuf);
98 if (pProblem->RowNamesBuf) free(pProblem->RowNamesBuf);
99 if (pProblem->ColNamesList) free(pProblem->ColNamesList);
100 if (pProblem->RowNamesList) free(pProblem->RowNamesList);
101 if (pProblem->ObjectName) free(pProblem->ObjectName);
102
103 if (pProblem->InitValues) free(pProblem->InitValues);
104
105 if (pProblem->RowLower) free(pProblem->RowLower);
106 if (pProblem->RowUpper) free(pProblem->RowUpper);
107
108 if (pProblem->ColType) free(pProblem->ColType);
109
110 if (pProblem->PriorIndex) free(pProblem->PriorIndex);
111 if (pProblem->PriorValues) free(pProblem->PriorValues);
112 if (pProblem->PriorBranch) free(pProblem->PriorBranch);
113
114 if (pProblem->SemiIndex) free(pProblem->SemiIndex);
115
116 if (pProblem->SosType) free(pProblem->SosType);
117 if (pProblem->SosPrior) free(pProblem->SosPrior);
118 if (pProblem->SosBegin) free(pProblem->SosBegin);
119 if (pProblem->SosIndex) free(pProblem->SosIndex);
120 if (pProblem->SosRef) free(pProblem->SosRef);
121
122 free(pProblem);
123 }
124
125
126
127 void coinSetProblemName(PPROBLEM pProblem, const char *ProblemName)
128 {
129 size_t len;
130
131 len = strlen(ProblemName);
132 if (len >= sizeof(pProblem->ProblemName)) {
133 len = sizeof(pProblem->ProblemName) - 1;
134 }
135 strncpy(pProblem->ProblemName, ProblemName, len);
136 pProblem->ProblemName[len] = '\0';
137 }
138
139
140 int coinStoreMatrix(PPROBLEM pProblem, int ColCount, int RowCount, int NZCount, int RangeCount,
141 int ObjectSense, double ObjectConst, double* ObjectCoeffs, double* LowerBounds,
142 double* UpperBounds, const char* RowType, double* RHSValues, double* RangeValues,
143 int* MatrixBegin, int* MatrixCount, int* MatrixIndex, double* MatrixValues)
144 {
145 if (ColCount == 0) {
146 return 0;
147 }
148 pProblem->ColCount = ColCount;
149 pProblem->RowCount = RowCount;
150 pProblem->NZCount = NZCount;
151 pProblem->RangeCount = RangeCount;
152 pProblem->ObjectSense = ObjectSense;
153 pProblem->ObjectConst = ObjectConst;
154
155 if (ObjectCoeffs) pProblem->ObjectCoeffs = (double*) malloc(pProblem->ColCount * sizeof(double));
156 if (LowerBounds) pProblem->LowerBounds = (double*) malloc(pProblem->ColCount * sizeof(double));
157 if (UpperBounds) pProblem->UpperBounds = (double*) malloc(pProblem->ColCount * sizeof(double));
158 if (RowType) pProblem->RowType = (char*) malloc(pProblem->RowCount * sizeof(char));
159 if (RHSValues) pProblem->RHSValues = (double*) malloc(pProblem->RowCount * sizeof(double));
160 if (RangeValues) pProblem->RangeValues = (double*) malloc(pProblem->RowCount * sizeof(double));
161 if (MatrixBegin) pProblem->MatrixBegin = (int*) malloc((pProblem->ColCount+1) * sizeof(int));
162 if (MatrixCount) pProblem->MatrixCount = (int*) malloc(pProblem->ColCount * sizeof(int));
163 if (MatrixIndex) pProblem->MatrixIndex = (int*) malloc(pProblem->NZCount * sizeof(int));
164 if (MatrixValues) pProblem->MatrixValues = (double*) malloc(pProblem->NZCount * sizeof(double));
165
166 if ((ObjectCoeffs && !pProblem->ObjectCoeffs) ||
167 (LowerBounds && !pProblem->LowerBounds) ||
168 (UpperBounds && !pProblem->UpperBounds) ||
169 (RowType && !pProblem->RowType) ||
170 (RHSValues && !pProblem->RHSValues) ||
171 (RangeValues && !pProblem->RangeValues) ||
172 (MatrixBegin && !pProblem->MatrixBegin) ||
173 (MatrixCount && !pProblem->MatrixCount) ||
174 (MatrixIndex && !pProblem->MatrixIndex) ||
175 (MatrixValues && !pProblem->MatrixValues)) {
176 return 0;
177 }
178 if (ObjectCoeffs) memcpy(pProblem->ObjectCoeffs, ObjectCoeffs, pProblem->ColCount * sizeof(double));
179 if (LowerBounds) memcpy(pProblem->LowerBounds, LowerBounds, pProblem->ColCount * sizeof(double));
180 if (UpperBounds) memcpy(pProblem->UpperBounds, UpperBounds, pProblem->ColCount * sizeof(double));
181 if (RowType) memcpy(pProblem->RowType, RowType, pProblem->RowCount * sizeof(char));
182 if (RHSValues) memcpy(pProblem->RHSValues, RHSValues, pProblem->RowCount * sizeof(double));
183 if (RangeValues) memcpy(pProblem->RangeValues, RangeValues, pProblem->RowCount * sizeof(double));
184 if (MatrixBegin) memcpy(pProblem->MatrixBegin, MatrixBegin, (pProblem->ColCount+1) * sizeof(int));
185 if (MatrixCount) memcpy(pProblem->MatrixCount, MatrixCount, pProblem->ColCount * sizeof(int));
186 if (MatrixIndex) memcpy(pProblem->MatrixIndex, MatrixIndex, pProblem->NZCount * sizeof(int));
187 if (MatrixValues) memcpy(pProblem->MatrixValues, MatrixValues, pProblem->NZCount * sizeof(double));
188
189 if (!coinComputeRowLowerUpper(pProblem, DBL_MAX)) {
190 return 0;
191 }
192 return 1;
193 }
194
195
196 int coinStoreNamesList(PPROBLEM pProblem, char** ColNamesList, char** RowNamesList, const char* ObjectName)
197 {
198 if (ObjectName) {
199 pProblem->lenObjNameBuf = (int)strlen(ObjectName) + 1;
200 pProblem->ObjectName = (char*) malloc(pProblem->lenObjNameBuf * sizeof(char));
201 if (!pProblem->ObjectName) {
202 return 0;
203 }
204 memcpy(pProblem->ObjectName, ObjectName, pProblem->lenObjNameBuf * sizeof(char));
205 }
206 if (ColNamesList) {
207 pProblem->lenColNamesBuf = coinGetLenNameListBuf(ColNamesList, pProblem->ColCount);
208 pProblem->ColNamesList = (char**) malloc(pProblem->ColCount * sizeof(char* ));
209 pProblem->ColNamesBuf = (char*) malloc(pProblem->lenColNamesBuf * sizeof(char));
210 if (!pProblem->ColNamesList && !pProblem->ColNamesBuf) {
211 return 0;
212 }
213 coinCopyNamesList(pProblem->ColNamesList, pProblem->ColNamesBuf, ColNamesList, pProblem->ColCount);
214 }
215 if (RowNamesList) {
216 pProblem->lenRowNamesBuf = coinGetLenNameListBuf(RowNamesList, pProblem->RowCount);
217 pProblem->RowNamesList = (char**) malloc(pProblem->RowCount * sizeof(char* ));
218 pProblem->RowNamesBuf = (char*) malloc(pProblem->lenRowNamesBuf * sizeof(char));
219 if (!pProblem->RowNamesList && !pProblem->RowNamesBuf) {
220 return 0;
221 }
222 coinCopyNamesList(pProblem->RowNamesList, pProblem->RowNamesBuf, RowNamesList, pProblem->RowCount);
223 }
224 return 1;
225 }
226
227
228 int coinStoreNamesBuf(PPROBLEM pProblem, const char* ColNamesBuf, const char* RowNamesBuf, const char* ObjectName)
229 {
230 char** ColNamesList;
231 char** RowNamesList;
232 int result;
233
234 ColNamesList = (char**)malloc(pProblem->ColCount * sizeof(char*));
235 RowNamesList = (char**)malloc(pProblem->RowCount * sizeof(char*));
236 if (!ColNamesList && !RowNamesList) {
237 return 0;
238 }
239 coinSetupNamesList(ColNamesList, ColNamesBuf, pProblem->ColCount);
240 coinSetupNamesList(RowNamesList, RowNamesBuf, pProblem->RowCount);
241 result = coinStoreNamesList(pProblem, ColNamesList, RowNamesList, ObjectName);
242 if (ColNamesList) free(ColNamesList);
243 if (RowNamesList) free(RowNamesList);
244 return result;
245
246 }
247
248 int coinStoreInitValues(PPROBLEM pProblem, double* InitValues)
249 {
250 if (!InitValues) {
251 return 0;
252 }
253 pProblem->InitValues = (double* ) malloc(pProblem->ColCount * sizeof(double));
254 if (!pProblem->InitValues) {
255 return 0;
256 }
257 memcpy(pProblem->InitValues, InitValues, pProblem->ColCount * sizeof(double));
258 return 1;
259 }
260
261
262 int coinStoreInteger(PPROBLEM pProblem, const char* ColType)
263 {
264 int numInts;
265
266 if (pProblem->ColCount == 0) {
267 return 0;
268 }
269 if (!ColType) {
270 return 0;
271 }
272 pProblem->ColType = (char* )malloc(pProblem->ColCount * sizeof(char));
273 if (!pProblem->ColType) {
274 return 0;
275 }
276 memcpy(pProblem->ColType, ColType, pProblem->ColCount * sizeof(char));
277 numInts = coinComputeIntVariables(pProblem);
278 pProblem->SolveAsMIP = (numInts > 0);
279 return 1;
280 }
281
282
283 int coinStorePriority(PPROBLEM pProblem, int PriorCount, int* PriorIndex,
284 int* PriorValues, int* PriorBranch)
285 {
286 if (PriorCount == 0) {
287 return 0;
288 }
289 pProblem->PriorCount = PriorCount;
290 if (PriorIndex) pProblem->PriorIndex = (int* )malloc(PriorCount * sizeof(int));
291 if (PriorValues) pProblem->PriorValues = (int* )malloc(PriorCount * sizeof(int));
292 if (PriorBranch) pProblem->PriorBranch = (int* )malloc(PriorCount * sizeof(int));
293
294 if ((PriorIndex && !pProblem->PriorIndex) ||
295 (PriorValues && !pProblem->PriorValues) ||
296 (PriorBranch && !pProblem->PriorBranch)) {
297 return 0;
298 }
299 if (PriorIndex) memcpy(pProblem->PriorIndex, PriorIndex, PriorCount * sizeof(int));
300 if (PriorValues) memcpy(pProblem->PriorValues, PriorValues, PriorCount * sizeof(int));
301 if (PriorBranch) memcpy(pProblem->PriorBranch, PriorBranch, PriorCount * sizeof(int));
302 return 0;
303 }
304
305
306 int coinStoreSos(PPROBLEM pProblem, int SosCount, int SosNZCount,
307 int* SosType, int* SosPrior, int* SosBegin,
308 int* SosIndex, double* SosRef)
309 {
310 if ((SosCount == 0) || (SosNZCount == 0)) {
311 return 0;
312 }
313 pProblem->SosCount = SosCount;
314 pProblem->SosNZCount = SosNZCount;
315
316 if (SosType) pProblem->SosType = (int* )malloc(SosCount * sizeof(int));
317 if (SosPrior) pProblem->SosPrior = (int* )malloc(SosCount * sizeof(int));
318 if (SosBegin) pProblem->SosBegin = (int* )malloc((SosCount+1) * sizeof(int));
319 if (SosIndex) pProblem->SosIndex = (int* )malloc(SosNZCount * sizeof(int));
320 if (SosRef) pProblem->SosRef = (double* )malloc(SosNZCount* sizeof(double));
321
322 if ((SosType && !pProblem->SosType) ||
323 (SosPrior && !pProblem->SosPrior) ||
324 (SosBegin && !pProblem->SosBegin) ||
325 (SosIndex && !pProblem->SosIndex) ||
326 (SosRef && !pProblem->SosRef)) {
327 return 0;
328 }
329 if (SosType) memcpy(pProblem->SosType, SosType, SosCount * sizeof(int));
330 if (SosPrior) memcpy(pProblem->SosPrior, SosPrior, SosCount * sizeof(int));
331 if (SosBegin) memcpy(pProblem->SosBegin, SosBegin, (SosCount+1) * sizeof(int));
332 if (SosIndex) memcpy(pProblem->SosIndex, SosIndex, SosNZCount * sizeof(int));
333 if (SosRef) memcpy(pProblem->SosRef, SosRef, SosNZCount * sizeof(double));
334
335 pProblem->SolveAsMIP = 1;
336 return 1;
337 }
338
339
340 int coinStoreSemiCont(PPROBLEM pProblem, int SemiCount, int* SemiIndex)
341 {
342 if (SemiCount == 0) {
343 return 0;
344 }
345 if (!SemiIndex) {
346 return 0;
347 }
348 pProblem->SemiCount = SemiCount;
349 pProblem->SemiIndex = (int* )malloc(pProblem->SemiCount * sizeof(int));
350 if (!pProblem->SemiIndex) {
351 return 0;
352 }
353 memcpy(pProblem->SemiIndex, SemiIndex, pProblem->SemiCount * sizeof(int));
354
355 pProblem->SolveAsMIP = 1;
356 return 1;
357 }
358
359
360 int coinComputeRowLowerUpper(PPROBLEM pProblem, double CoinDblMax)
361 {
362 int i;
363 double RangeABS, RangeValue;
364
365 if (pProblem->RowCount == 0) {
366 return 0;
367 }
368 pProblem->RowLower = (double* )malloc(pProblem->RowCount*sizeof(double));
369 pProblem->RowUpper = (double* )malloc(pProblem->RowCount*sizeof(double));
370 if (!pProblem->RowLower || !pProblem->RowUpper) {
371 return 0;
372 }
373 if (!pProblem->RowType) {
374 /* if NO RowType, we treat RHSValues as RowLower and RangeValues as RowUpper */
375 for (i = 0; i < pProblem->RowCount; i++) {
376 pProblem->RowLower[i] = pProblem->RHSValues ? pProblem->RHSValues[i] : -CoinDblMax;
377 pProblem->RowUpper[i] = pProblem->RangeValues ? pProblem->RangeValues[i] : CoinDblMax;
378 }
379 return 1;
380 }
381 for (i = 0; i < pProblem->RowCount; i++ ){
382 switch (pProblem->RowType[i]) {
383 case 'L':
384 pProblem->RowLower[i] = -CoinDblMax;
385 pProblem->RowUpper[i] = pProblem->RHSValues ? pProblem->RHSValues[i] : CoinDblMax;
386 break;
387
388 case 'G':
389 pProblem->RowLower[i] = pProblem->RHSValues ? pProblem->RHSValues[i] : -CoinDblMax;
390 pProblem->RowUpper[i] = CoinDblMax;
391 break;
392
393 case 'E':
394 pProblem->RowLower[i] = pProblem->RHSValues ? pProblem->RHSValues[i] : 0.0;
395 pProblem->RowUpper[i] = pProblem->RHSValues ? pProblem->RHSValues[i] : 0.0;
396 break;
397
398 case 'R':
399 RangeValue = pProblem->RangeValues ? pProblem->RangeValues[i] : 0.0;
400 RangeABS = (RangeValue >= 0.0) ? RangeValue : -RangeValue;
401 pProblem->RowLower[i] = (pProblem->RHSValues ? pProblem->RHSValues[i] : -CoinDblMax) - RangeABS;
402 pProblem->RowUpper[i] = pProblem->RHSValues ? pProblem->RHSValues[i] : CoinDblMax;
403 break;
404
405 case 'N':
406 pProblem->RowLower[i] = -CoinDblMax;
407 pProblem->RowUpper[i] = CoinDblMax;
408 break;
409
410 default:
411 return 0;
412 }
413 }
414 return 1;
415 }
416
417
418 int coinComputeIntVariables(PPROBLEM pProblem)
419 {
420 int i;
421
422 if (pProblem->ColCount == 0) {
423 return 0;
424 }
425 pProblem->IsInt = (char* )malloc(pProblem->ColCount * sizeof(char));
426 if (!pProblem->IsInt) {
427 return 0;
428 }
429 for (i = 0; i < pProblem->ColCount; i++ ) {
430 switch (pProblem->ColType[i]) {
431 case 'B':
432 pProblem->BinCount++;
433 pProblem->IsInt[i] = 1;
434 break;
435
436 case 'I':
437 pProblem->IntCount++;
438 pProblem->IsInt[i] = 1;
439 break;
440
441 default:
442 pProblem->IsInt[i] = 0;
443 break;
444 }
445 }
446 pProblem->numInts = pProblem->IntCount + pProblem->BinCount;
447 return pProblem->numInts;
448 }
449
450
451 int coinGetLenNameBuf(const char* NameBuf, int Count)
452 {
453 int i, len;
454 int lenBuf;
455 const char* pName;
456
457 lenBuf = 0;
458 pName = &NameBuf[0];
459 for (i = 0; i < Count; i++) {
460 len = (int)strlen(pName) + 1;
461 lenBuf += len;
462 pName = pName + len;
463 }
464 return lenBuf;
465 }
466
467
468 int coinGetLenNameListBuf(char** NameList, int Count)
469 {
470 int i, len;
471 int lenBuf;
472
473 lenBuf = 0;
474 for (i = 0; i < Count; i++) {
475 len = (int)strlen(NameList[i]) + 1;
476 lenBuf += len;
477 }
478 return lenBuf;
479 }
480
481
482 int coinCopyNamesList(char** NamesList, char* NamesBuf, char** argNamesList, int Count)
483 {
484 int i,k,len;
485
486 if (!NamesList || !argNamesList || !NamesBuf || (Count == 0)) {
487 return 0;
488 }
489 k = 0;
490 for (i = 0; i < Count; i++) {
491 NamesList[i] = &NamesBuf[k];
492 strcpy(NamesList[i], argNamesList[i]);
493 len = (int)strlen(NamesList[i]) + 1;
494 k += len;
495 }
496 return 1;
497 }
498
499
500 int coinSetupNamesList(char** NamesList, const char* NamesBuf, int Count)
501 {
502 int i,k,len;
503
504 if (!NamesList || !NamesBuf || (Count == 0)) {
505 return 0;
506 }
507 k = 0;
508 for (i = 0; i < Count; i++) {
509 NamesList[i] = (char*)&NamesBuf[k]; /* (char*) cast removes warning, NamesList will not be changed */
510 len = (int)strlen(NamesList[i]) + 1;
511 k += len;
512 }
513 return 1;
514 }
515