Skip to content

Commit 00665f6

Browse files
author
Thomas Irgang
committed
comments, check unit tests prepared
1 parent da82d5d commit 00665f6

13 files changed

+2989
-216
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ obj/*
1010
*.out.*
1111
*.out
1212

13+
doc/*
1314

Doxyfile

+2,413
Large diffs are not rendered by default.

check.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdlib.h>
2+
#include <check.h>
3+
4+
#include "check_simplex.h"
5+
#include "check_rational.h"
6+
7+
int main(void)
8+
{
9+
int number_failed;
10+
SRunner *sr;
11+
Suite *s_rational = rational_suite();
12+
Suite *s_simplex = simplex_suite();
13+
14+
15+
sr = srunner_create(s_simplex);
16+
srunner_add_suite(sr, s_rational);
17+
18+
srunner_run_all(sr, CK_NORMAL);
19+
20+
number_failed = srunner_ntests_failed(sr);
21+
22+
srunner_free(sr);
23+
24+
return (number_failed == 0)? EXIT_SUCCESS : EXIT_FAILURE;
25+
}

check_rational.c

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
/**
2+
* @brief Check unit tests for rational numbers.
3+
*
4+
* This file contains the unit tests for the rational numbers.
5+
*
6+
* @file check_rational.c
7+
* @author Thomas Irgang
8+
* @date 17 Feb 2015
9+
*/
10+
111
#include <stdlib.h>
212
#include <check.h>
313

414
#include "rational.h"
515

6-
START_TEST(test_getRational)
16+
START_TEST(test_rational_get)
717
{
818
struct Rational *r;
919

10-
r = getRational(2,3);
20+
r = rational_get(2,3);
1121

1222
ck_assert_int_eq(r->n, 2);
1323
ck_assert_int_eq(r->d, 3);
@@ -25,26 +35,9 @@ Suite *rational_suite(void)
2535

2636
tc_core = tcase_create("Core");
2737

28-
tcase_add_test(tc_core, test_getRational);
38+
tcase_add_test(tc_core, test_rational_get);
2939
suite_add_tcase(s, tc_core);
3040

3141
return s;
3242
}
3343

34-
int main(void)
35-
{
36-
int number_failed;
37-
Suite *s;
38-
SRunner *sr;
39-
40-
s = rational_suite();
41-
sr = srunner_create(s);
42-
43-
srunner_run_all(sr, CK_NORMAL);
44-
number_failed = srunner_ntests_failed(sr);
45-
46-
srunner_free(sr);
47-
48-
return (number_failed == 0)? EXIT_SUCCESS : EXIT_FAILURE;
49-
}
50-

check_rational.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @brief Check unit tests for rational numbers.
3+
*
4+
*
5+
* @file check_rational.h
6+
* @author Thomas Irgang
7+
* @date 17 Feb 2015
8+
*/
9+
10+
Suite *rational_suite(void);

check_simplex.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @brief Check unit tests for simplex algorithm.
3+
*
4+
* This file contains the unit tests for the simples algorithm.
5+
*
6+
* @file check_simplex.c
7+
* @author Thomas Irgang
8+
* @date 17 Feb 2015
9+
*/
10+
11+
#include <stdlib.h>
12+
#include <check.h>
13+
14+
#include "simplex.h"
15+
16+
Suite *simplex_suite(void)
17+
{
18+
Suite *s;
19+
TCase *tc_core;
20+
21+
s = suite_create("Simplex");
22+
23+
tc_core = tcase_create("Core");
24+
25+
/*tcase_add_test(tc_core, test_);*/
26+
suite_add_tcase(s, tc_core);
27+
28+
return s;
29+
}
30+
31+
32+
33+

check_simplex.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @brief Check unit tests for simplex algorithm.
3+
*
4+
* @file check_simplex.h
5+
* @author Thomas Irgang
6+
* @date 17 Feb 2015
7+
*/
8+
9+
Suite *simplex_suite(void);

main.c

+76-46
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,104 @@
1+
/**
2+
* @brief Little program using simplex algorithm.
3+
*
4+
* This file implements a little example using the simplex algorithm.
5+
*
6+
* @file rational_test.c
7+
* @author Thomas Irgang
8+
* @date 17 Feb 2015
9+
*/
10+
111
#include <stdio.h>
212
#include <stdlib.h>
313
#include <string.h>
414
#include <time.h>
515

616
#include "simplex.h"
7-
#include "rational.h"
8-
9-
struct Tableau *createTestdata(void);
10-
struct Tableau *createTestdataPhase2(void);
1117

18+
/**
19+
* @brief Create tableau with example problem.
20+
*
21+
* This function creates a new (invalid) simplex tableau filled with a
22+
* small test optimization problem.
23+
*
24+
* Maximize 300x + 500y
25+
* s.t.: 1) x + 2y <= 170
26+
* 2) x + y <= 150
27+
* 3) 3y <= 180
28+
* 4) y >= 1
29+
*
30+
* @return tableau for problem
31+
*/
32+
struct Tableau *create_testproblem_tableau(void);
33+
34+
/**
35+
* @brief Solve example problem.
36+
*
37+
* This function solves the little example problem using
38+
* phase 1 and 2 of simplex algorithm.
39+
*/
1240
int main(void)
1341
{
14-
struct Tableau *tableau = NULL, *phase1 = NULL;
15-
clock_t start, s_p1, e_p1, s_prep, e_prep, s_p2, e_p2, end, calc;
42+
struct Tableau *tableau = NULL, *phase1 = NULL; /* variables for tableaus */
43+
clock_t start, s_p1, e_p1, s_prep = 0, e_prep = 0, s_p2 = 0, e_p2 = 0, end, calc; /* variables for time */
1644

1745
start = clock();
1846

19-
tableau = createTestdata();
20-
printf("aktualisiere Pivot-Element ...\n");
21-
updatePivot(tableau);
22-
printf("schreibe Tableau ...\n");
23-
printTableau(tableau);
47+
tableau = create_testproblem_tableau(); /* Create test problem. */
48+
printf("Print tableau ...\n");
49+
simplex_print_tableau(tableau); /* Print test problem to stdout. */
2450

2551

26-
printf("Simplex Phase I ...\n");
52+
printf("Run simplex phase 1 ...\n");
2753
s_p1 = clock();
28-
phase1 = simplexPhaseI(tableau);
54+
phase1 = simplex_find_start_corner(tableau); /* Calculate start corner for phase 2 of simplex algorithm. */
2955
e_p1 = clock();
30-
printTableau(phase1);
31-
printf("Lösung von Phase I:\n");
32-
printSolution(phase1);
56+
simplex_print_tableau(phase1); /* Print solved, extended tableau. */
57+
printf("Solution of phase 1:\n");
58+
simplex_print_solution(phase1); /* Print solution of phase 1. */
3359

3460
if((phase1->z)->n == 0)
3561
{
36-
printf("Phase I hat erfolgreiche eine Startlösung gefunden.\n");
37-
printf("Phase II vorbereiten ...\n");
62+
printf("Phase 1 found a start corner for phase 2.\n");
63+
printf("Prepare tableau for phase 2 ...\n");
3864
s_prep = clock();
39-
prepareTableauForPhaseII(phase1, tableau);
65+
prepare_with_start_corner(phase1, tableau); /* Update tableau with found solution of phase 1. */
4066
e_prep = clock();
41-
printf("Tableau für Phase II:\n");
42-
printTableau(tableau);
67+
printf("Tableau updated for phase 2:\n");
68+
simplex_print_tableau(tableau); /* Print tableau used for phase 2 of simplex algorithm. */
4369

44-
printf("Simplex Phase II ...\n");
70+
printf("Run simplex phase 2 ...\n");
4571
s_p2 = clock();
46-
simplexPhaseII(tableau);
72+
simplex_find_best_solution(tableau); /* Phase 2: Find best solution for problem. */
4773
e_p2 = clock();
48-
printTableau(tableau);
49-
printf("Lösung von Phase II: ");
50-
printSolution(tableau);
74+
simplex_print_tableau(tableau); /* Print final tableau of phase 2. */
75+
printf("Best solution of problem: ");
76+
simplex_print_solution(tableau); /* Print best solution of problem. */
5177
}
5278

53-
printf("gebe Tableau Speicher frei ...\n");
54-
freeTableau(phase1);
55-
freeTableau(tableau);
56-
5779
end = clock();
5880

59-
calc = (double)(end - start);
81+
calc = (double)(end - start); /* Calculate and print times. */
6082
printf("Total time: %f ms\n", (double)(calc*1000)/CLOCKS_PER_SEC);
6183
calc = (double)(e_p1 - s_p1);
62-
printf("Phase I: %f ms\n", (double)(calc*1000)/CLOCKS_PER_SEC);
63-
calc = (double)(e_prep - s_prep);
64-
printf("Prepare: %f ms\n", (double)(calc*1000)/CLOCKS_PER_SEC);
65-
calc = (double)(e_p2 - s_p2);
66-
printf("Phase II: %f ms\n", (double)(calc*1000)/CLOCKS_PER_SEC);
84+
85+
if((phase1->z)->n == 0)
86+
{
87+
printf("Simplex phase 1: %f ms\n", (double)(calc*1000)/CLOCKS_PER_SEC);
88+
calc = (double)(e_prep - s_prep);
89+
printf("Prepare phase 2: %f ms\n", (double)(calc*1000)/CLOCKS_PER_SEC);
90+
calc = (double)(e_p2 - s_p2);
91+
printf("Simplex phase 2: %f ms\n", (double)(calc*1000)/CLOCKS_PER_SEC);
92+
}
93+
94+
printf("Free tableau memory ...\n");
95+
simplex_free_tableau(phase1); /* Free memory of tableaus. */
96+
simplex_free_tableau(tableau);
6797

6898
return EXIT_SUCCESS;
6999
}
70100

71-
struct Tableau *createTestdata(void)
101+
struct Tableau *create_testproblem_tableau(void)
72102
{
73103
struct Tableau *tableau;
74104
int i, j;
@@ -83,32 +113,32 @@ struct Tableau *createTestdata(void)
83113
int basis[4] = {2,3,4,5};
84114
int nbvs[2] = {0,1};
85115

86-
printf("erzeuge Tableau ...\n");
87-
tableau = create_tableau(4,6);
88-
printf("fülle Tableau mit Testdaten...\n");
116+
printf("Create a new tableau ...\n");
117+
tableau = simplex_create_tableau(4,6); /* Create a new tableau for 4 conditions and 6 variables (x, y and slack variables for the 4 inequalities) */
118+
printf("Fill tableau with test problem data ...\n");
89119

90-
for(i=0; i<2; i++)
120+
for(i=0; i<2; i++) /* Target function. */
91121
{
92122
(tableau->c[i])->n = t[0][i];
93123
}
94124

95-
(tableau->z)->n = 0;
125+
(tableau->z)->n = 0; /* Target function value. */
96126

97-
for(i=0; i<4; i++)
127+
for(i=0; i<4; i++) /* Limits of inequalities. */
98128
{
99129
(tableau->b[i])->n = t[i+1][2];
100130
}
101131

102-
for(i=0; i<4; ++i)
132+
for(i=0; i<4; ++i) /* Tableau data (only none basis variable columns, (invalid) basis is s1, s2, s3, s4). */
103133
{
104134
for(j=0; j<2; ++j)
105135
{
106136
(tableau->A[i][j])->n = t[i+1][j];
107137
}
108138
}
109139

110-
memcpy(tableau->bvs, basis, 4*sizeof(int));
111-
memcpy(tableau->nbvs, nbvs, 2*sizeof(int));
140+
memcpy(tableau->bvs, basis, 4*sizeof(int)); /* Initialze basis variables. */
141+
memcpy(tableau->nbvs, nbvs, 2*sizeof(int)); /* Initialize none basis variables. */
112142

113143
return tableau;
114144
}

0 commit comments

Comments
 (0)