-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_qs.c
733 lines (612 loc) · 18.2 KB
/
test_qs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
* This file is part of QSopt_ex.
*
* (c) Copyright 2006 by David Applegate, William Cook, Sanjeeb Dash,
* and Daniel Espinoza
* (c) Copyright 2015 Jon Lund Steffensen <[email protected]>
*
* Sanjeeb Dash ownership of copyright in QSopt_ex is derived from his
* copyright in QSopt.
*
* This code may be used under the terms of the GNU General Public License
* (Version 2.1 or later) as published by the Free Software Foundation.
*
* Alternatively, use is granted for research purposes only.
*
* It is your choice of which of these two licenses you are operating
* under.
*
* We make no guarantees about the correctness or usefulness of this code.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "QSopt_ex.h"
typedef void test_func(int test_id);
/* */
/* Using QSload_prob to load the following LP problem */
/* Maximize 3.0x + 2.0y + 4.0z */
/* Subject to */
/* 3.0x + 2.0y + 1.0z <= 12.0 */
/* 5.0x + 1.0y = 10.0 */
/* x >= 2.0 */
/* y free */
/* 1.0 <= z <= 10.0 */
/* */
static int load_test_problem(mpq_QSprob *p)
{
int i, rval = 0;
int cmatcnt[3] = { 2, 2, 1 };
int cmatbeg[3] = { 0, 2, 4 };
int cmatind[5] = { 0, 1, 0, 1, 0 };
char sense[2] = { 'L', 'E' };
const char *colnames[3] = { "x", "y", "z" };
const char *rownames[2] = { "c1", "c2"};
mpq_t cmatval[5];
mpq_t obj[3];
mpq_t rhs[2];
mpq_t lower[3];
mpq_t upper[3];
for (i = 0; i < 5; i++) mpq_init (cmatval[i]);
mpq_set_d (cmatval[0], 3.0);
mpq_set_d (cmatval[1], 5.0);
mpq_set_d (cmatval[2], 2.0);
mpq_set_d (cmatval[3], 1.0);
mpq_set_d (cmatval[4], 1.0);
for (i = 0; i < 3; i++) mpq_init (obj[i]);
mpq_set_d (obj[0], 3.0);
mpq_set_d (obj[1], 2.0);
mpq_set_d (obj[2], 4.0);
for (i = 0; i < 2; i++) mpq_init (rhs[i]);
mpq_set_d (rhs[0], 12.0);
mpq_set_d (rhs[1], 10.0);
for (i = 0; i < 3; i++) mpq_init (lower[i]);
mpq_set_d (lower[0], 2.0);
mpq_set (lower[1], mpq_ILL_MINDOUBLE);
mpq_set_d (lower[2], 1.0);
for (i = 0; i < 3; i++) mpq_init (upper[i]);
mpq_set (upper[0], mpq_ILL_MAXDOUBLE);
mpq_set (upper[1], mpq_ILL_MAXDOUBLE);
mpq_set_d (upper[2], 10.0);
/* CPXcopylpwnames */
*p = mpq_QSload_prob ("small", 3, 2, cmatcnt, cmatbeg, cmatind, cmatval,
QS_MAX, obj, rhs, sense, lower, upper, colnames,
rownames);
if (*p == NULL) {
fprintf (stderr, "Unable to load the LP problem\n");
rval = 1; goto CLEANUP;
}
CLEANUP:
for (i = 0; i < 5; i++) mpq_clear (cmatval[i]);
for (i = 0; i < 3; i++) mpq_clear (obj[i]);
for (i = 0; i < 2; i++) mpq_clear (rhs[i]);
for (i = 0; i < 3; i++) mpq_clear (lower[i]);
for (i = 0; i < 3; i++) mpq_clear (upper[i]);
return rval;
}
/* Load and solve test problem. */
static int solve_test_problem(mpq_QSprob *p, int *status)
{
int rval = 0;
rval = load_test_problem(p);
if (rval) return rval;
rval = QSexact_solver(*p, NULL, NULL, NULL, DUAL_SIMPLEX, status);
if (rval) return rval;
return rval;
}
static void test_new_column(int test_id)
{
mpq_t objective, lower, upper;
mpq_init(objective);
mpq_init(lower);
mpq_init(upper);
mpq_set_d(objective, 4.0);
mpq_set_d(lower, -5.0);
mpq_set_d(upper, 75.5);
const char *name = "test_var";
mpq_QSprob p = mpq_QScreate_prob("test", QS_MAX);
if (p == NULL) {
printf("not ok %i - Unable to create LP problem\n", test_id);
goto CLEANUP;
}
/* Test create new column */
int rval = mpq_QSnew_col(p, objective, lower, upper, name);
if (rval) {
printf("not ok %i - Failed to create variable\n", test_id);
} else {
printf("ok %i - New variable created\n", test_id);
}
CLEANUP:
mpq_clear(objective);
mpq_clear(lower);
mpq_clear(upper);
if (p) mpq_QSfree_prob(p);
}
static void test_new_column_without_name(int test_id)
{
mpq_t objective, lower, upper;
mpq_init(objective);
mpq_init(lower);
mpq_init(upper);
mpq_set_d(objective, 4.0);
mpq_set_d(lower, -5.0);
mpq_set_d(upper, 75.5);
mpq_QSprob p = mpq_QScreate_prob("test", QS_MAX);
if (p == NULL) {
printf("not ok %i - Unable to create LP problem\n", test_id);
goto CLEANUP;
}
/* Test create new column */
int rval = mpq_QSnew_col(p, objective, lower, upper, NULL);
if (rval) {
printf("not ok %i - Failed to create variable\n", test_id);
} else {
printf("ok %i - New variable created without name\n", test_id);
}
CLEANUP:
mpq_clear(objective);
mpq_clear(lower);
mpq_clear(upper);
if (p) mpq_QSfree_prob(p);
}
static void test_new_column_with_duplicate_name(int test_id)
{
mpq_QSprob p = NULL;
mpq_t objective, lower, upper;
mpq_init(objective);
mpq_init(lower);
mpq_init(upper);
mpq_set_d(objective, 4.0);
mpq_set_d(lower, -5.0);
mpq_set_d(upper, 75.5);
int rval = load_test_problem(&p);
if (rval) {
printf("not ok %i - Unable to load the LP\n", test_id);
goto CLEANUP;
}
/* Test create new column. Column "x" already exists in the test problem. */
rval = mpq_QSnew_col(p, objective, lower, upper, "x");
if (rval) {
printf("ok %i - Failed to create variable with duplicate name\n",
test_id);
} else {
printf("not ok %i - New variable created with duplicate name\n",
test_id);
}
CLEANUP:
mpq_clear(objective);
mpq_clear(lower);
mpq_clear(upper);
if (p) mpq_QSfree_prob(p);
}
static void test_delete_column(int test_id)
{
mpq_QSprob p = NULL;
int rval = load_test_problem(&p);
if (rval) {
printf("not ok %i - Unable to load the LP\n", test_id);
goto CLEANUP;
}
/* Test delete column */
rval = mpq_QSdelete_col(p, 0);
if (rval) {
printf("not ok %i - Failed to delete variable\n", test_id);
} else {
printf("ok %i - Deleted varable\n", test_id);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_delete_invalid_column(int test_id)
{
mpq_QSprob p = NULL;
int rval = load_test_problem(&p);
if (rval) {
printf("not ok %i - Unable to load the LP\n", test_id);
goto CLEANUP;
}
/* Test delete column */
rval = mpq_QSdelete_col(p, 10);
if (rval) {
printf("ok %i - Error reported when deleting invalid variable\n",
test_id);
} else {
printf("not ok %i - No error reported\n", test_id);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_delete_column_in_empty_problem(int test_id)
{
mpq_QSprob p = mpq_QScreate_prob("test", QS_MAX);
if (p == NULL) {
printf("not ok %i - Unable to create LP problem\n", test_id);
goto CLEANUP;
}
/* Test delete column */
int rval = mpq_QSdelete_col(p, 0);
if (rval) {
printf("ok %i - Error reported when deleting invalid variable\n",
test_id);
} else {
printf("not ok %i - No error reported\n", test_id);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_delete_row(int test_id)
{
mpq_QSprob p = NULL;
int rval = load_test_problem(&p);
if (rval) {
printf("not ok %i - Unable to load the LP\n", test_id);
goto CLEANUP;
}
/* Test delete row */
rval = mpq_QSdelete_row(p, 1);
if (rval) {
printf("not ok %i - Failed to delete constraint\n", test_id);
} else {
printf("ok %i - Deleted constraint\n", test_id);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_delete_invalid_row(int test_id)
{
mpq_QSprob p = NULL;
int rval = load_test_problem(&p);
if (rval) {
printf("not ok %i - Unable to load the LP\n", test_id);
goto CLEANUP;
}
/* Test delete row */
rval = mpq_QSdelete_row(p, 5);
if (rval) {
printf("ok %i - Error reported when deleting invalid constraint\n",
test_id);
} else {
printf("not ok %i - No error reported\n", test_id);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_delete_row_in_empty_problem(int test_id)
{
mpq_QSprob p = mpq_QScreate_prob("test", QS_MAX);
if (p == NULL) {
printf("not ok %i - Unable to create LP problem\n", test_id);
goto CLEANUP;
}
/* Test delete row */
int rval = mpq_QSdelete_row(p, 0);
if (rval) {
printf("ok %i - Error reported when deleting invalid constraint\n",
test_id);
} else {
printf("not ok %i - No error reported\n", test_id);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_row_count(int test_id)
{
mpq_QSprob p = NULL;
int rval = load_test_problem(&p);
if (rval) {
printf("not ok %i - Unable to load the LP\n", test_id);
goto CLEANUP;
}
/* Test get row count */
int nrows = mpq_QSget_rowcount (p);
if (nrows != 2) {
printf("not ok %i - Invalid number of constraints: %d\n",
test_id, nrows);
} else {
printf("ok %i - Got number of constraints\n", test_id);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_col_count(int test_id)
{
mpq_QSprob p = NULL;
int rval = load_test_problem(&p);
if (rval) {
printf("not ok %i - Unable to load the LP\n", test_id);
goto CLEANUP;
}
/* Test get col count */
int ncols = mpq_QSget_colcount(p);
if (ncols != 3) {
printf("not ok %i - Invalid number of variables: %d\n",
test_id, ncols);
} else {
printf("ok %i - Got number of variables\n", test_id);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_solve_no_constraints(int test_id)
{
mpq_t objective, lower, upper;
mpq_init(objective);
mpq_init(lower);
mpq_init(upper);
mpq_set_d(objective, 4.0);
mpq_set_d(lower, -5.0);
mpq_set_d(upper, 75.5);
mpq_QSprob p = mpq_QScreate_prob("test", QS_MAX);
if (p == NULL) {
printf("not ok %i - Unable to create LP problem\n", test_id);
goto CLEANUP;
}
/* Test create new column. */
int rval = mpq_QSnew_col(p, objective, lower, upper, "x");
if (rval) {
printf("not ok %i - Unable to create variable\n", test_id);
goto CLEANUP;
}
/* Test solving with no constraints */
int status = 0;
rval = QSexact_solver(p, NULL, NULL, NULL, DUAL_SIMPLEX, &status);
if (rval) {
printf("not ok %i - Failed solving problem\n", test_id);
goto CLEANUP;
}
if (status != QS_LP_OPTIMAL) {
printf("not ok %i - Did not find an optimal solution.\n", test_id);
goto CLEANUP;
}
printf("ok %i - Solution was found\n", test_id);
CLEANUP:
mpq_clear(objective);
mpq_clear(lower);
mpq_clear(upper);
if (p) mpq_QSfree_prob(p);
}
static void test_solution_is_optimal(int test_id)
{
mpq_QSprob p = NULL;
int status = 0;
int rval = solve_test_problem(&p, &status);
if (rval) {
printf("not ok %i - Unable to solve the LP\n", test_id);
goto CLEANUP;
}
/* Test status */
if (status != QS_LP_OPTIMAL) {
printf("not ok %i - Did not find an optimal solution.\n", test_id);
goto CLEANUP;
}
printf("ok %i - Solution was found\n", test_id);
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_solution_objective(int test_id)
{
mpq_QSprob p = NULL;
int status = 0;
int rval = 0;
mpq_t value;
mpq_init(value);
rval = solve_test_problem(&p, &status);
if (rval) {
printf("not ok %i - Unable to solve the LP\n", test_id);
goto CLEANUP;
}
if (status != QS_LP_OPTIMAL) {
printf("not ok %i - Did not find an optimal solution.\n", test_id);
goto CLEANUP;
}
/* Test objective */
rval = mpq_QSget_objval(p, &value);
if (rval) {
printf("not ok %i - Could not get obj value, error code %d\n",
test_id, rval);
} else {
if (mpq_cmp_ui(value, 42, 1) != 0) {
printf("not ok %i - Unexpected obj value: %.6f\n", test_id,
mpq_get_d(value));
} else {
printf("ok %i - The correct objective value was obtained\n",
test_id);
}
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
mpq_clear(value);
}
static void test_solution_get_variables(int test_id)
{
mpq_QSprob p = NULL;
mpq_t *x = NULL;
int status = 0;
int rval = 0;
int i;
int ncols = 3;
rval = solve_test_problem(&p, &status);
if (rval) {
printf("not ok %i - Unable to solve the LP\n", test_id);
goto CLEANUP;
}
if (status != QS_LP_OPTIMAL) {
printf("not ok %i - Did not find an optimal solution.\n", test_id);
goto CLEANUP;
}
/* Test get variables */
x = malloc(ncols * sizeof(mpq_t));
for (i = 0; i < ncols; i++) mpq_init(x[i]);
rval = mpq_QSget_x_array(p, x);
if (rval) {
printf("not ok %i - Could not get x-vector, error code %d\n",
test_id, rval);
} else {
char **colnames = malloc(ncols * sizeof(char *));
mpq_QSget_colnames(p, colnames);
printf("ok %i - Obtained variable values and names\n", test_id);
free(colnames);
}
CLEANUP:
if (x) {
for (i = 0; i < ncols; i++) mpq_clear(x[i]);
free(x);
}
if (p) mpq_QSfree_prob(p);
}
static void test_solution_get_dual_values(int test_id)
{
mpq_QSprob p = NULL;
mpq_t *y = NULL;
int status = 0;
int rval = 0;
int i;
int nrows = 2;
rval = solve_test_problem(&p, &status);
if (rval) {
printf("not ok %i - Unable to solve the LP\n", test_id);
goto CLEANUP;
}
if (status != QS_LP_OPTIMAL) {
printf("not ok %i - Did not find an optimal solution.\n", test_id);
goto CLEANUP;
}
/* Test get dual values */
y = malloc(nrows * sizeof (mpq_t));
for (i = 0; i < nrows; i++) mpq_init(y[i]);
rval = mpq_QSget_pi_array(p, y);
if (rval) {
printf("not ok %i - Could not get dual values, error code %d\n",
test_id, rval);
} else {
char **rownames = malloc(nrows * sizeof(char *));
mpq_QSget_rownames(p, rownames);
printf("ok %i - Obtained dual values and contraint names\n", test_id);
free(rownames);
}
CLEANUP:
if (y) {
for (i = 0; i < nrows; i++) mpq_clear(y[i]);
free(y);
}
if (p) mpq_QSfree_prob(p);
}
static void test_write_problem_to_file(int test_id, const char *filetype)
{
mpq_QSprob p = NULL;
int rval = load_test_problem(&p);
if (rval) {
printf("not ok %i - Unable to load the problem\n", test_id);
goto CLEANUP;
}
/* Test write to file */
rval = mpq_QSwrite_prob (p, "/dev/null", filetype);
if (rval) {
printf("not ok %i - Could not write the %s, error code %d\n",
test_id, filetype, rval);
} else {
printf("ok %i - %s written to /dev/null\n", test_id, filetype);
}
CLEANUP:
if (p) mpq_QSfree_prob(p);
}
static void test_write_problem_to_lp_file(int test_id)
{
test_write_problem_to_file(test_id, "LP");
}
static void test_write_problem_to_mps_file(int test_id)
{
test_write_problem_to_file(test_id, "MPS");
}
static void test_write_problem_no_constraints(
int test_id, const char *filetype)
{
mpq_t objective, lower, upper;
mpq_init(objective);
mpq_init(lower);
mpq_init(upper);
mpq_set_d(objective, 4.0);
mpq_set_d(lower, -5.0);
mpq_set_d(upper, 75.5);
mpq_QSprob p = mpq_QScreate_prob("test", QS_MAX);
if (p == NULL) {
printf("not ok %i - Unable to create LP problem\n", test_id);
goto CLEANUP;
}
/* Test create new column. */
int rval = mpq_QSnew_col(p, objective, lower, upper, "x");
if (rval) {
printf("not ok %i - Unable to create variable\n", test_id);
goto CLEANUP;
}
/* Test write to file */
rval = mpq_QSwrite_prob (p, "/dev/null", filetype);
if (rval) {
printf("not ok %i - Could not write the %s, error code %d\n",
test_id, filetype, rval);
} else {
printf("ok %i - %s written to /dev/null\n", test_id, filetype);
}
CLEANUP:
mpq_clear(objective);
mpq_clear(lower);
mpq_clear(upper);
if (p) mpq_QSfree_prob(p);
}
static void test_write_lp_problem_no_constraints(int test_id)
{
test_write_problem_no_constraints(test_id, "LP");
}
static void test_write_mps_problem_no_constraints(int test_id)
{
test_write_problem_no_constraints(test_id, "MPS");
}
int main(int ac, char **av)
{
int rval = 0;
QSexactStart();
if (ac > 1) {
printf ("Usage: %s\n", *av);
rval = EXIT_FAILURE; goto CLEANUP;
}
/* Must call QSexact_set_precision before any other QS function */
/* Cannot use mpq_ILL_MAXDOUBLE or mpq_ILL_MINDOUBLE before this call */
QSexact_set_precision(128);
static test_func* test_functions[] = {
test_new_column,
test_new_column_without_name,
test_new_column_with_duplicate_name,
test_delete_row,
test_delete_invalid_row,
test_delete_row_in_empty_problem,
test_delete_column,
test_delete_invalid_column,
test_delete_column_in_empty_problem,
test_row_count,
test_col_count,
test_solve_no_constraints,
test_solution_is_optimal,
test_solution_objective,
test_solution_get_variables,
test_solution_get_dual_values,
test_write_problem_to_lp_file,
test_write_problem_to_mps_file,
test_write_lp_problem_no_constraints,
test_write_mps_problem_no_constraints
};
/* Print number of tests */
int count = sizeof(test_functions) / sizeof(test_functions[0]);
printf("1..%i\n", count);
/* Run tests */
int i;
for (i = 0; i < count; i++) {
int test_id = i + 1;
test_functions[i](test_id);
}
CLEANUP:
QSexactClear();
return rval;
}