File Coverage

blib/lib/Algorithm/Simplex/Role/Solve.pm
Criterion Covered Total %
statement 3 14 21.4
branch 0 2 0.0
condition n/a
subroutine 1 2 50.0
pod 1 1 100.0
total 5 19 26.3


line stmt bran cond sub pod time code
1             package Algorithm::Simplex::Role::Solve;
2 1     1   14138 use Moo::Role;
  1         15  
  1         7  
3              
4             =head1 Name
5              
6             Algorithm::Simplex::Role::Solve - solve() method implemented as Moose role.
7              
8             =cut
9              
10             =head1 Synposis
11              
12             use Algorithm::Simplex::Rational;
13             use Data::Dumper;
14             my $matrix = [
15             [ 5, 2, 30],
16             [ 3, 4, 20],
17             [10, 8, 0],
18             ];
19             my $tableau = Algorithm::Simplex::Rational->new( tableau => $matrix );
20             $tableau->solve;
21             print Dumper $tableau_object->display_tableau;
22            
23             =cut
24              
25             requires(
26             'tableau', 'determine_bland_pivot_row_and_column_numbers',
27             'pivot', 'exchange_pivot_variables'
28             );
29              
30             =head1 Methods
31              
32             =head2 solve
33              
34             Walk the simplex of feasible solutions by moving to an adjacent vertex
35             one step at a time. Each vertex of the feasible region corresponds to
36             a tableau.
37              
38             This solve() method assumes we are starting with a feasible solution.
39             This is referred to a phase 2 of the Simplex algorithm, where phase 1
40             is obtaining a feasible solution so phase 2 can be applied.
41              
42             Returns 1 if an optimal solution is found, 0 otherwise.
43              
44             =cut
45              
46             sub solve {
47 0     0 1   my $tableau_object = shift;
48              
49 0           my $counter = 1;
50 0           until ($tableau_object->is_optimal) {
51 0           my ($pivot_row_number, $pivot_column_number) =
52             $tableau_object->determine_bland_pivot_row_and_column_numbers;
53 0           $tableau_object->pivot($pivot_row_number, $pivot_column_number);
54 0           $tableau_object->exchange_pivot_variables($pivot_row_number,
55             $pivot_column_number);
56 0           $counter++;
57              
58             # Too many pivots?
59 0 0         if ($counter > $tableau_object->MAXIMUM_PIVOTS) {
60 0           warn "HALT: Exceeded the maximum number of pivots allowed: "
61             . $tableau_object->MAXIMUM_PIVOTS . "\n";
62 0           return 0;
63             }
64             }
65              
66 0           return 1;
67             }
68              
69             1;