-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_random_LPs.pl
More file actions
123 lines (95 loc) · 2.85 KB
/
benchmark_random_LPs.pl
File metadata and controls
123 lines (95 loc) · 2.85 KB
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
use strict;
use Benchmark;
use PDL;
use Tableau_bones;
=head1 Model Comparison
Benchmark the three models against a number of randomly generated
Linear Programs.
=head1 USAGE
perl benchmark_random_LPs.pl --rows 50 --columns 50 -n 50
=cut
#use Model::Generic;
use Getopt::Long;
use Model::Float;
use Model::PDL;
use Model::Rational;
use Data::Dumper;
my $rows = 20;
my $columns = 20;
my $number_of_LPs = 20;
GetOptions(
'rows|r=i' => \$rows,
'columns|c=i' => \$columns,
'number_of_LPs|n=i' => \$number_of_LPs,
);
srand;
my $matrix = random_float_matrix( $rows, $columns, 1 );
timethese(
$number_of_LPs,
{
float => 'solve_LP("float")',
piddle => 'solve_LP("piddle")',
rational => 'solve_LP("rational")',
}
);
####---- subs below
sub solve_LP {
my $model = shift;
my $tableau = matrix_copy($matrix);
# extra step for piddles.
$tableau = pdl $tableau if ( $model eq 'piddle' );
my $tableau_object =
$model eq 'float' ? Model::Float->new($tableau)
: $model eq 'piddle' ? Model::PDL->new($tableau)
: $model eq 'rational' ? Model::Rational->new($tableau)
: die "The model type: $model could not be found.";
$tableau_object->set_number_of_rows_and_columns;
$tableau_object->set_generic_variable_names_from_dimensions;
# extra step for rationals (fracts)
$tableau_object
->convert_natural_number_tableau_to_fractional_object_tableau
if ( $model eq 'rational' );
my $counter = 1;
until ( $tableau_object->tableau_is_optimal ) {
my ( $pivot_row_number, $pivot_column_number ) =
$tableau_object->determine_bland_pivot_row_and_column_numbers;
$tableau_object->pivot( $pivot_row_number, $pivot_column_number );
$tableau_object->exchange_pivot_variables( $pivot_row_number,
$pivot_column_number );
$counter++;
die "Too many loops" if ( $counter > 200 );
}
}
sub random_float_matrix {
# code to produce a matrix of random floats (or naturals)
my $rows = shift;
my $columns = shift;
my $natural_numbers;
$natural_numbers = 0 unless $natural_numbers = shift;
my $matrix;
for my $i ( 0 .. $rows - 1 ) {
for my $j ( 0 .. $columns - 1 ) {
$matrix->[$i]->[$j] =
$natural_numbers == 0 ? rand : int( 10 * rand );
}
}
return $matrix;
}
sub random_pdl_matrix {
# code to produce a random pdl matrix
my $rows = shift;
my $columns = shift;
my $matrix = random( double, $rows, $columns );
return $matrix;
}
sub matrix_copy {
# code to copy matrix
my $matrix = shift;
my $matrix_copy;
for my $i ( 0 .. $rows - 1 ) {
for my $j ( 0 .. $columns - 1 ) {
$matrix_copy->[$i]->[$j] = $matrix->[$i]->[$j];
}
}
return $matrix_copy;
}