| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::SimulateReads::Role::WeightedRaffle; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Extends class with weighted raffle. | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 6 |  |  | 6 |  | 4220 | use App::SimulateReads::Base 'role'; | 
|  | 6 |  |  |  |  | 22 |  | 
|  | 6 |  |  |  |  | 63 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | requires '_build_weights'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.05'; # VERSION | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 11 |  |  |  |  |  |  | #  Moose attributes | 
| 12 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 13 |  |  |  |  |  |  | has 'weights'     => ( | 
| 14 |  |  |  |  |  |  | is         => 'ro', | 
| 15 |  |  |  |  |  |  | isa        => 'My:Weights', | 
| 16 |  |  |  |  |  |  | builder    => '_build_weights', | 
| 17 |  |  |  |  |  |  | lazy_build => 1 | 
| 18 |  |  |  |  |  |  | ); | 
| 19 |  |  |  |  |  |  | has 'num_weights' => ( | 
| 20 |  |  |  |  |  |  | is         => 'ro', | 
| 21 |  |  |  |  |  |  | isa        => 'My:IntGt0', | 
| 22 |  |  |  |  |  |  | builder    => '_build_num_weights', | 
| 23 |  |  |  |  |  |  | lazy_build => 1 | 
| 24 |  |  |  |  |  |  | ); | 
| 25 |  |  |  |  |  |  | has 'max_weight'  => ( | 
| 26 |  |  |  |  |  |  | is         => 'ro', | 
| 27 |  |  |  |  |  |  | isa        => 'My:IntGt0', | 
| 28 |  |  |  |  |  |  | builder    => '_build_max_weight', | 
| 29 |  |  |  |  |  |  | lazy_build => 1 | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | #===  CLASS METHOD  ============================================================ | 
| 33 |  |  |  |  |  |  | #        CLASS: My::Role::WeightedRaffle (Role) | 
| 34 |  |  |  |  |  |  | #       METHOD: _build_num_weights (BUILDER) | 
| 35 |  |  |  |  |  |  | #   PARAMETERS: Void | 
| 36 |  |  |  |  |  |  | #      RETURNS: Int > 0 | 
| 37 |  |  |  |  |  |  | #  DESCRIPTION: Builds num_weights | 
| 38 |  |  |  |  |  |  | #       THROWS: If weights is not builded, throws an error | 
| 39 |  |  |  |  |  |  | #     COMMENTS: none | 
| 40 |  |  |  |  |  |  | #     SEE ALSO: n/a | 
| 41 |  |  |  |  |  |  | #=============================================================================== | 
| 42 |  |  |  |  |  |  | sub _build_num_weights { | 
| 43 | 4 |  |  | 4 |  | 19 | my $self = shift; | 
| 44 | 4 |  |  |  |  | 149 | my $weights = $self->weights; | 
| 45 | 4 | 50 |  |  |  | 20 | croak "Not found a weights object\n" unless defined $weights; | 
| 46 | 4 |  |  |  |  | 161 | return scalar @$weights; | 
| 47 |  |  |  |  |  |  | } ## --- end sub _build_num_weights | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | #===  CLASS METHOD  ============================================================ | 
| 50 |  |  |  |  |  |  | #        CLASS: My::Role::WeightedRaffle (Role) | 
| 51 |  |  |  |  |  |  | #       METHOD: _build_max_weight (BUILDER) | 
| 52 |  |  |  |  |  |  | #   PARAMETERS: Void | 
| 53 |  |  |  |  |  |  | #      RETURNS: Int > 0 | 
| 54 |  |  |  |  |  |  | #  DESCRIPTION: Builds max_weight | 
| 55 |  |  |  |  |  |  | #       THROWS: If weights is not builded, throws an error | 
| 56 |  |  |  |  |  |  | #     COMMENTS: none | 
| 57 |  |  |  |  |  |  | #     SEE ALSO: n/a | 
| 58 |  |  |  |  |  |  | #=============================================================================== | 
| 59 |  |  |  |  |  |  | sub _build_max_weight { | 
| 60 | 4 |  |  | 4 |  | 13 | my $self = shift; | 
| 61 | 4 |  |  |  |  | 206 | my $weights = $self->weights; | 
| 62 | 4 | 50 |  |  |  | 38 | croak "Not found a weights object\n" unless defined $weights; | 
| 63 | 4 |  |  |  |  | 158 | return $weights->[-1]{up}; | 
| 64 |  |  |  |  |  |  | } ## --- end sub _build_max_weight | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | #===  CLASS METHOD  ============================================================ | 
| 67 |  |  |  |  |  |  | #        CLASS: My::Role::WeightedRaffle (Role) | 
| 68 |  |  |  |  |  |  | #       METHOD: calculate_weights | 
| 69 |  |  |  |  |  |  | #   PARAMETERS: $line HashRef[Int] | 
| 70 |  |  |  |  |  |  | #      RETURNS: My:Weights | 
| 71 |  |  |  |  |  |  | #  DESCRIPTION: Calculates weight based in a hash -> key => weight, giving: | 
| 72 |  |  |  |  |  |  | #              	[ { down, up, feature }, { down, up, feature } .. ] | 
| 73 |  |  |  |  |  |  | #       THROWS: no exceptions | 
| 74 |  |  |  |  |  |  | #     COMMENTS: none | 
| 75 |  |  |  |  |  |  | #     SEE ALSO: n/a | 
| 76 |  |  |  |  |  |  | #=============================================================================== | 
| 77 |  |  |  |  |  |  | sub calculate_weights { | 
| 78 | 20 |  |  | 20 | 0 | 55 | my ($self, $line) = @_; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 20 |  |  |  |  | 25 | my @weights; | 
| 81 | 20 |  |  |  |  | 35 | my $left = 0; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 20 |  |  |  |  | 60 | for my $feature (keys %$line) { | 
| 84 |  |  |  |  |  |  | my %weight = ( | 
| 85 |  |  |  |  |  |  | down    => $left, | 
| 86 | 100 |  |  |  |  | 245 | up      => $left + $line->{$feature} - 1, | 
| 87 |  |  |  |  |  |  | feature => $feature | 
| 88 |  |  |  |  |  |  | ); | 
| 89 | 100 |  |  |  |  | 120 | $left += $line->{$feature}; | 
| 90 | 100 |  |  |  |  | 180 | push @weights => \%weight; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 20 |  |  |  |  | 595 | return \@weights; | 
| 94 |  |  |  |  |  |  | } ## --- end sub calculate_weights | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | #===  CLASS METHOD  ============================================================ | 
| 97 |  |  |  |  |  |  | #        CLASS: My::Role::WeightedRaffle (Role) | 
| 98 |  |  |  |  |  |  | #       METHOD: weighted_raffle | 
| 99 |  |  |  |  |  |  | #   PARAMETERS: Void | 
| 100 |  |  |  |  |  |  | #      RETURNS: $self->_search() | 
| 101 |  |  |  |  |  |  | #  DESCRIPTION: Makes a binary search on the intervals between the weights. The | 
| 102 |  |  |  |  |  |  | #               bigger the interval bigger the weight. It begins by making a | 
| 103 |  |  |  |  |  |  | #               raffle on the sum of weights, then calls _search that searches | 
| 104 |  |  |  |  |  |  | #               for the feature whose value hit the interval | 
| 105 |  |  |  |  |  |  | #       THROWS: no exceptions | 
| 106 |  |  |  |  |  |  | #     COMMENTS: none | 
| 107 |  |  |  |  |  |  | #     SEE ALSO: _search | 
| 108 |  |  |  |  |  |  | #=============================================================================== | 
| 109 |  |  |  |  |  |  | sub weighted_raffle { | 
| 110 | 1710 |  |  | 1710 | 0 | 3083 | my $self = shift; | 
| 111 | 1710 |  |  |  |  | 64644 | my $range = int(rand($self->max_weight + 1)); | 
| 112 | 1710 |  |  |  |  | 57512 | return $self->_search(0, $self->num_weights - 1, $range); | 
| 113 |  |  |  |  |  |  | } ## --- end sub weighted_raffle | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | #===  CLASS METHOD  ============================================================ | 
| 116 |  |  |  |  |  |  | #        CLASS: My::Role::WeightedRaffle (Role) | 
| 117 |  |  |  |  |  |  | #       METHOD: _search (PRIVATE) | 
| 118 |  |  |  |  |  |  | #   PARAMETERS: $min_index Int >= 0, $max_index Int > 0, $range Int > 0 | 
| 119 |  |  |  |  |  |  | #      RETURNS: $weight->{feature} when found | 
| 120 |  |  |  |  |  |  | #  DESCRIPTION: Binary search | 
| 121 |  |  |  |  |  |  | #       THROWS: If $min_index greater the $max_index, which may not occur, throws | 
| 122 |  |  |  |  |  |  | #               an exception | 
| 123 |  |  |  |  |  |  | #     COMMENTS: none | 
| 124 |  |  |  |  |  |  | #     SEE ALSO: weighted_raffle | 
| 125 |  |  |  |  |  |  | #=============================================================================== | 
| 126 |  |  |  |  |  |  | sub _search { | 
| 127 | 4077 |  |  | 4077 |  | 8652 | my ($self, $min_index, $max_index, $range) = @_; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 4077 | 50 |  |  |  | 7986 | if ($min_index > $max_index) { | 
| 130 | 0 |  |  |  |  | 0 | croak "Random feature not found"; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 4077 |  |  |  |  | 8582 | my $selected_index = int(($min_index + $max_index) / 2); | 
| 134 | 4077 |  |  |  |  | 129660 | my $weight = $self->weights->[$selected_index]; | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 4077 | 100 | 100 |  |  | 16813 | if ($range >= $weight->{down} && $range <= $weight->{up}) { | 
|  |  | 100 |  |  |  |  |  | 
| 137 | 1710 |  |  |  |  | 7396 | return $weight->{feature}; | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  | elsif ($range > $weight->{down}) { | 
| 140 | 1685 |  |  |  |  | 4387 | return $self->_search($selected_index + 1, | 
| 141 |  |  |  |  |  |  | $max_index, $range); | 
| 142 |  |  |  |  |  |  | } else { | 
| 143 | 682 |  |  |  |  | 2366 | return $self->_search($min_index, | 
| 144 |  |  |  |  |  |  | $selected_index - 1, $range); | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | } ## --- end sub _search | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | __END__ | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =pod | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =encoding UTF-8 | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =head1 NAME | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | App::SimulateReads::Role::WeightedRaffle - Extends class with weighted raffle. | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =head1 VERSION | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | version 0.05 | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | =head1 AUTHOR | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | Thiago L. A. Miller <tmiller@mochsl.org.br> | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | This software is Copyright (c) 2017 by Teaching and Research Institute from SÃrio-Libanês Hospital. | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | This is free software, licensed under: | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | The GNU General Public License, Version 3, June 2007 | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | =cut |