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.16';
3 2     2   15 use strict;
  2         5  
  2         67  
4 2     2   10 use warnings;
  2         4  
  2         48  
5              
6 2     2   29 use 5.012;
  2         9  
7              
8 2     2   434 use MooX qw/late/;
  2         10881  
  2         14  
9              
10 2     2   164043 use PDL ();
  2         19  
  2         65  
11              
12 2     2   13 use vars (qw(@fields));
  2         5  
  2         193  
13              
14             has _main => ( is => 'rw' );
15             has _num_solved =>
16             ( isa => 'Int', is => 'ro', init_arg => 'num_solved', required => 1 );
17             has _quota => ( isa => 'Int', is => 'ro', init_arg => 'quota', required => 1 );
18             has _scan_idx =>
19             ( isa => 'Int', is => 'ro', init_arg => 'scan_idx', required => 1 );
20              
21 2     2   1442 use Exception::Class ('AI::Pathfinding::OptimizeMultiple::Error::OutOfQuotas');
  2         18066  
  2         16  
22              
23             sub attach_to
24             {
25 8     8 1 26 my $self = shift;
26 8         17 my $main_obj = shift;
27              
28 8         27 $self->_main($main_obj);
29              
30 8         18 return;
31             }
32              
33             sub get_chosen_struct
34             {
35 8     8 1 61 my $self = shift;
36 8         38 return $self->_main->_calc_chosen_scan( $self->_scan_idx, $self->_quota );
37             }
38              
39             sub detach
40             {
41 8     8 1 20 my $self = shift;
42 8         217 $self->_main(undef);
43             }
44              
45             sub idx_slice
46             {
47 16     16 1 28 my $self = shift;
48              
49 16         274 my $scans_data = $self->_main()->_scans_data();
50              
51 16         111 my @dims = $scans_data->dims();
52              
53 16         454 return $scans_data->slice(
54             join( ",", ":", $self->_scan_idx(), ( ("(0)") x ( @dims - 2 ) ) ) );
55             }
56              
57             sub update_total_iters
58             {
59 8     8 1 14 my $state = shift;
60              
61             # $r is the result of this scan.
62 8         20 my $r = $state->idx_slice();
63              
64             # Add the total iterations for all the states that were solved by
65             # this scan.
66 8         464 $state->_main()
67             ->_add_to_total_iters(
68             PDL::sum( ( ( $r <= $state->_quota() ) & ( $r > 0 ) ) * $r ) );
69              
70             # Find all the states that weren't solved.
71 8         322 my $indexes = PDL::which( ( $r > $state->_quota() ) | ( $r < 0 ) );
72              
73             # Add the iterations for all the states that have not been solved
74             # yet.
75 8         493 $state->_main()
76             ->_add_to_total_iters( $indexes->nelem() * $state->_quota() );
77              
78             # Keep only the states that have not been solved yet.
79 8         143 $state->_main()
80             ->_scans_data(
81             $state->_main()->_scans_data()->dice( $indexes, "X" )->copy() );
82             }
83              
84             sub update_idx_slice
85             {
86 4     4 1 8 my $state = shift;
87 4         13 my $r = $state->idx_slice()->copy();
88              
89             # $r cannot be 0, because the ones that were 0, were already solved
90             # in $state->update_total_iters().
91 4         195 my $idx_slice = $state->idx_slice();
92 4         251 $idx_slice .=
93             ( ( $r > 0 ) * ( $r - $state->_quota() ) ) + ( ( $r < 0 ) * ($r) );
94             }
95              
96             sub _mark_as_used
97             {
98 8     8   15 my $state = shift;
99              
100 8         49 $state->_main()->_selected_scans()->[ $state->_scan_idx() ]->mark_as_used();
101              
102 8         210 return;
103             }
104              
105             sub _add_chosen
106             {
107 8     8   13 my $state = shift;
108              
109 8         16 push @{ $state->_main()->chosen_scans() }, $state->get_chosen_struct();
  8         156  
110              
111 8         2619 return;
112             }
113              
114             sub _update_total_boards_solved
115             {
116 8     8   13 my $state = shift;
117              
118 8         35 $state->_main()->_add_to_total_boards_solved( $state->_num_solved() );
119              
120 8         14 return;
121             }
122              
123             sub _trace_wrapper
124             {
125 8     8   13 my $state = shift;
126              
127 8         139 $state->_main()->_trace(
128             {
129             'iters_quota' => $state->_quota(),
130             'selected_scan_idx' => $state->_scan_idx(),
131             'total_boards_solved' => $state->_main()->_total_boards_solved(),
132             }
133             );
134              
135 8         20 return;
136             }
137              
138             sub register_params
139             {
140 8     8 1 14 my $state = shift;
141              
142 8         25 $state->_add_chosen();
143 8         26 $state->_mark_as_used();
144 8         25 $state->_update_total_boards_solved();
145 8         24 $state->_trace_wrapper();
146              
147 8         17 return;
148             }
149              
150             1;
151              
152             __END__
153              
154             =pod
155              
156             =encoding UTF-8
157              
158             =head1 NAME
159              
160             AI::Pathfinding::OptimizeMultiple::IterState - iteration state object.
161              
162             =head1 VERSION
163              
164             version 0.0.16
165              
166             =head1 SUBROUTINES/METHODS
167              
168             =head2 $self->attach_to()
169              
170             Internal use.
171              
172             =head2 $self->get_chosen_struct()
173              
174             Internal use.
175              
176             =head2 $self->detach()
177              
178             Internal use.
179              
180             =head2 $self->idx_slice()
181              
182             Internal use.
183              
184             =head2 $self->update_total_iters()
185              
186             Internal use.
187              
188             =head2 $self->update_idx_slice()
189              
190             Internal use.
191              
192             =head2 $self->register_params()
193              
194             Internal use.
195              
196             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
197              
198             =head1 SUPPORT
199              
200             =head2 Websites
201              
202             The following websites have more information about this module, and may be of help to you. As always,
203             in addition to those websites please use your favorite search engine to discover more resources.
204              
205             =over 4
206              
207             =item *
208              
209             MetaCPAN
210              
211             A modern, open-source CPAN search engine, useful to view POD in HTML format.
212              
213             L<https://metacpan.org/release/AI-Pathfinding-OptimizeMultiple>
214              
215             =item *
216              
217             RT: CPAN's Bug Tracker
218              
219             The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
220              
221             L<https://rt.cpan.org/Public/Dist/Display.html?Name=AI-Pathfinding-OptimizeMultiple>
222              
223             =item *
224              
225             CPANTS
226              
227             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
228              
229             L<http://cpants.cpanauthors.org/dist/AI-Pathfinding-OptimizeMultiple>
230              
231             =item *
232              
233             CPAN Testers
234              
235             The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
236              
237             L<http://www.cpantesters.org/distro/A/AI-Pathfinding-OptimizeMultiple>
238              
239             =item *
240              
241             CPAN Testers Matrix
242              
243             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
244              
245             L<http://matrix.cpantesters.org/?dist=AI-Pathfinding-OptimizeMultiple>
246              
247             =item *
248              
249             CPAN Testers Dependencies
250              
251             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
252              
253             L<http://deps.cpantesters.org/?module=AI::Pathfinding::OptimizeMultiple>
254              
255             =back
256              
257             =head2 Bugs / Feature Requests
258              
259             Please report any bugs or feature requests by email to C<bug-ai-pathfinding-optimizemultiple at rt.cpan.org>, or through
260             the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=AI-Pathfinding-OptimizeMultiple>. You will be automatically notified of any
261             progress on the request by the system.
262              
263             =head2 Source Code
264              
265             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
266             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
267             from your repository :)
268              
269             L<http://github.com/shlomif/fc-solve>
270              
271             git clone ssh://git@github.com/shlomif/fc-solve.git
272              
273             =head1 AUTHOR
274              
275             Shlomi Fish <shlomif@cpan.org>
276              
277             =head1 BUGS
278              
279             Please report any bugs or feature requests on the bugtracker website
280             L<https://github.com/shlomif/ai-pathfinding-optimizemultiple/issues>
281              
282             When submitting a bug or request, please include a test-file or a
283             patch to an existing test-file that illustrates the bug or desired
284             feature.
285              
286             =head1 COPYRIGHT AND LICENSE
287              
288             This software is Copyright (c) 2012 by Shlomi Fish.
289              
290             This is free software, licensed under:
291              
292             The MIT (X11) License
293              
294             =cut