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
+
1
11
#include <stdio.h>
2
12
#include <stdlib.h>
3
13
#include <string.h>
4
14
#include <time.h>
5
15
6
16
#include "simplex.h"
7
- #include "rational.h"
8
-
9
- struct Tableau * createTestdata (void );
10
- struct Tableau * createTestdataPhase2 (void );
11
17
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
+ */
12
40
int main (void )
13
41
{
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 */
16
44
17
45
start = clock ();
18
46
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. */
24
50
25
51
26
- printf ("Simplex Phase I ...\n" );
52
+ printf ("Run simplex phase 1 ...\n" );
27
53
s_p1 = clock ();
28
- phase1 = simplexPhaseI (tableau );
54
+ phase1 = simplex_find_start_corner (tableau ); /* Calculate start corner for phase 2 of simplex algorithm. */
29
55
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. */
33
59
34
60
if ((phase1 -> z )-> n == 0 )
35
61
{
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" );
38
64
s_prep = clock ();
39
- prepareTableauForPhaseII (phase1 , tableau );
65
+ prepare_with_start_corner (phase1 , tableau ); /* Update tableau with found solution of phase 1. */
40
66
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. */
43
69
44
- printf ("Simplex Phase II ...\n" );
70
+ printf ("Run simplex phase 2 ...\n" );
45
71
s_p2 = clock ();
46
- simplexPhaseII (tableau );
72
+ simplex_find_best_solution (tableau ); /* Phase 2: Find best solution for problem. */
47
73
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. */
51
77
}
52
78
53
- printf ("gebe Tableau Speicher frei ...\n" );
54
- freeTableau (phase1 );
55
- freeTableau (tableau );
56
-
57
79
end = clock ();
58
80
59
- calc = (double )(end - start );
81
+ calc = (double )(end - start ); /* Calculate and print times. */
60
82
printf ("Total time: %f ms\n" , (double )(calc * 1000 )/CLOCKS_PER_SEC );
61
83
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 );
67
97
68
98
return EXIT_SUCCESS ;
69
99
}
70
100
71
- struct Tableau * createTestdata (void )
101
+ struct Tableau * create_testproblem_tableau (void )
72
102
{
73
103
struct Tableau * tableau ;
74
104
int i , j ;
@@ -83,32 +113,32 @@ struct Tableau *createTestdata(void)
83
113
int basis [4 ] = {2 ,3 ,4 ,5 };
84
114
int nbvs [2 ] = {0 ,1 };
85
115
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" );
89
119
90
- for (i = 0 ; i < 2 ; i ++ )
120
+ for (i = 0 ; i < 2 ; i ++ ) /* Target function. */
91
121
{
92
122
(tableau -> c [i ])-> n = t [0 ][i ];
93
123
}
94
124
95
- (tableau -> z )-> n = 0 ;
125
+ (tableau -> z )-> n = 0 ; /* Target function value. */
96
126
97
- for (i = 0 ; i < 4 ; i ++ )
127
+ for (i = 0 ; i < 4 ; i ++ ) /* Limits of inequalities. */
98
128
{
99
129
(tableau -> b [i ])-> n = t [i + 1 ][2 ];
100
130
}
101
131
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). */
103
133
{
104
134
for (j = 0 ; j < 2 ; ++ j )
105
135
{
106
136
(tableau -> A [i ][j ])-> n = t [i + 1 ][j ];
107
137
}
108
138
}
109
139
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. */
112
142
113
143
return tableau ;
114
144
}
0 commit comments