File Coverage

blib/lib/AI/Pathfinding/OptimizeMultiple/IterState.pm
Criterion Covered Total %
statement 61 61 100.0
branch n/a
condition n/a
subroutine 18 18 100.0
pod 7 7 100.0
total 86 86 100.0


line stmt bran cond sub pod time code
1             package AI::Pathfinding::OptimizeMultiple::IterState;
2             $AI::Pathfinding::OptimizeMultiple::IterState::VERSION = '0.0.15';
3 2     2   13 use strict;
  2         4  
  2         43  
4 2     2   8 use warnings;
  2         6  
  2         36  
5              
6 2     2   32 use 5.012;
  2         6  
7              
8 2     2   312 use MooX qw/late/;
  2         7468  
  2         12  
9              
10 2     2   27160 use PDL ();
  2         16  
  2         46  
11              
12 2     2   10 use vars (qw(@fields));
  2         4  
  2         194  
13              
14             has _main => (is => 'rw');
15             has _num_solved => (isa => 'Int', is => 'ro', init_arg => 'num_solved', required => 1);
16             has _quota => (isa => 'Int', is => 'ro', init_arg => 'quota', required => 1);
17             has _scan_idx => (isa => 'Int', is => 'ro', init_arg => 'scan_idx', required => 1);
18              
19             use Exception::Class
20             (
21 2         13 'AI::Pathfinding::OptimizeMultiple::Error::OutOfQuotas'
22 2     2   786 );
  2         12498  
23              
24             sub attach_to
25             {
26 8     8 1 17 my $self = shift;
27 8         14 my $main_obj = shift;
28              
29 8         23 $self->_main($main_obj);
30              
31 8         20 return;
32             }
33              
34              
35             sub get_chosen_struct
36             {
37 8     8 1 58 my $self = shift;
38 8         39 return $self->_main->_calc_chosen_scan($self->_scan_idx, $self->_quota);
39             }
40              
41              
42             sub detach
43             {
44 8     8 1 16 my $self = shift;
45 8         148 $self->_main(undef);
46             }
47              
48              
49             sub idx_slice
50             {
51 16     16 1 27 my $self = shift;
52              
53 16         237 my $scans_data = $self->_main()->_scans_data();
54              
55 16         127 my @dims = $scans_data->dims();
56              
57 16         412 return $scans_data->slice(
58             join(",",
59             ":", $self->_scan_idx(), (("(0)") x (@dims-2))
60             )
61             );
62             }
63              
64              
65             sub update_total_iters
66             {
67 8     8 1 15 my $state = shift;
68              
69             # $r is the result of this scan.
70 8         21 my $r = $state->idx_slice();
71              
72             # Add the total iterations for all the states that were solved by
73             # this scan.
74 8         396 $state->_main()->_add_to_total_iters(
75             PDL::sum((($r <= $state->_quota()) & ($r > 0)) * $r)
76             );
77              
78             # Find all the states that weren't solved.
79 8         268 my $indexes = PDL::which(($r > $state->_quota()) | ($r < 0));
80              
81             # Add the iterations for all the states that have not been solved
82             # yet.
83 8         439 $state->_main()->_add_to_total_iters($indexes->nelem() * $state->_quota());
84              
85             # Keep only the states that have not been solved yet.
86 8         122 $state->_main()->_scans_data(
87             $state->_main()->_scans_data()->dice($indexes, "X")->copy()
88             );
89             }
90              
91              
92              
93             sub update_idx_slice
94             {
95 4     4 1 9 my $state = shift;
96 4         10 my $r = $state->idx_slice()->copy();
97             # $r cannot be 0, because the ones that were 0, were already solved
98             # in $state->update_total_iters().
99 4         159 my $idx_slice = $state->idx_slice();
100 4         203 $idx_slice .=
101             (($r > 0) * ($r - $state->_quota())) +
102             (($r < 0) * ($r ));
103             }
104              
105             sub _mark_as_used
106             {
107 8     8   11 my $state = shift;
108              
109 8         47 $state->_main()->_selected_scans()->[$state->_scan_idx()]->mark_as_used();
110              
111 8         187 return;
112             }
113              
114             sub _add_chosen
115             {
116 8     8   16 my $state = shift;
117              
118 8         12 push @{$state->_main()->chosen_scans()}, $state->get_chosen_struct();
  8         154  
119              
120 8         1953 return;
121             }
122              
123             sub _update_total_boards_solved
124             {
125 8     8   14 my $state = shift;
126              
127 8         36 $state->_main()->_add_to_total_boards_solved($state->_num_solved());
128              
129 8         16 return;
130             }
131              
132             sub _trace_wrapper
133             {
134 8     8   13 my $state = shift;
135              
136 8         121 $state->_main()->_trace(
137             {
138             'iters_quota' => $state->_quota(),
139             'selected_scan_idx' => $state->_scan_idx(),
140             'total_boards_solved' => $state->_main()->_total_boards_solved(),
141             }
142             );
143              
144 8         19 return;
145             }
146              
147              
148             sub register_params
149             {
150 8     8 1 14 my $state = shift;
151              
152 8         24 $state->_add_chosen();
153 8         25 $state->_mark_as_used();
154 8         25 $state->_update_total_boards_solved();
155 8         24 $state->_trace_wrapper();
156              
157 8         16 return;
158             }
159              
160              
161             1;
162              
163             __END__