Tour/RandomWalk.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 172 CC 14 SCAR 34.8
CC
14
Cov
0%
next
56.0
_collect_dirs
56.0
direction
6.0
CRAP 32.6
1  
2 =head1 NAME
3  
4 Array::Tour::RandomWalk - Return coordinates to take a random path.
5  
6 =head1 SYNOPSIS
7  
8 use Array::Tour::RandomWalk qw(:directions);
9
10 my $rndwalk = Array::Tour::RandomWalk->new(
11 dimensions => [13, 7, 1],
12 start => [0, 0, 0]
13 backtrack => "queue",
14 );
15  
16 The object is created with the following attributes:
17  
18 =over 4
19  
20 =item dimensions
21  
22 Set the size of the grid:
23  
24 my $spath1 = Array::Tour->new(dimensions => [16, 16]);
25  
26 If the grid is going to be square, a single integer is sufficient:
27  
28 my $spath1 = Array::Tour->new(dimensions => 16);
29  
30 =item start
31  
32 I<Default value: [0, 0, 0].> Starting point of the random walk.
33  
34 =item backtrack
35  
36 I<Default value: 'queue'.> Method of looking up cells to backtrack to. As the
37 random walk is made, cells where there were more than one direction to go are
38 stored in a list. When the random walk hits a dead end, it goes back to a cell
39 in the list. By default, the list is treated as a queue: the first cell on the
40 list is the first cell used.
41  
42 If backtrack is set to 'stack', the list is treated as a stack: the last cell on
43 the list is the first cell used.
44  
45 The final choice, 'random', will choose a cell at random from the list.
46  
47 =back
48  
49 The new() method is defined in the Array::Tour class. Attributes unique to this
50 class are dealt with in its own _set() method.
51  
52 =head1 PREREQUISITES
53  
54 Perl 5.8 or later. This is the version of perl under which this module
55 was developed.
56  
57 =head1 DESCRIPTION
58  
59 A simple iterator that will return the coordinates of the next cell if
60 one were to randomly tour a matrix.
61  
62 =cut
63 package Array::Tour::RandomWalk;
64 use 5.008;
65 use strict;
66 use warnings;
67 use integer;
68 use base q(Array::Tour);
69 use Array::Tour qw(:directions :status);
70  
71 our $VERSION = '0.06';
72  
73 =head3 direction()
74  
75 Returns the current direction as found in the :directions EXPORT tag.
76  
77 Overrides Array::Tour's direction() method.
78  
79 =cut
80  
81 sub direction()
82 {
83 my $self = shift;
84 return ($self->{tourstatus} == STOP)? NoDirection: $self->{direction};
85 }
86  
87 =head2 Tour Object Methods
88  
89 =head3 next()
90  
91 Returns an array reference to the next coordinates to use. Returns undef if
92 the object is finished.
93  
94 Overrides Array::Tour's next() method.
95  
96 =cut
97  
98 sub next
99 {
100 my $self = shift;
101  
102 return undef unless ($self->has_next());
103  
104 my @dir = $self->_collect_dirs();
105  
106 # print "Position => [", join(", ", @{$self->get_position()}), "]\n",
107 # "Can go (", join(", ", (map{$self->direction_name($_)} @dir)), ")\n";
108 #
109 # There is a cell to break into.
110 #
111 if (@dir > 0)
112 {
113 my $p = $self->{position};
114  
115 #
116 # If there were multiple choices, save it
117 # for future reference.
118 #
119 push @{ $self->{plist} }, $p if (@dir > 1);
120  
121 #
122 # Choose a wall at random and break into the next cell.
123 #
124 $self->{direction} = $self->{wander}->(\@dir, $p);
125 $self->_break_through();
126 ++$self->{odometer};
127 }
128 else # No place to go, back up.
129 {
130 if (@{ $self->{plist} } == 0) # No place to back up, quit.
131 {
132 $self->{tourstatus} = STOP;
133 return undef;
134 }
135  
136 $self->{direction} = SetPosition;
137 if ($self->{backtrack} eq 'stack')
138 {
139 $self->{position} = pop @{ $self->{plist} };
140 }
141 elsif ($self->{backtrack} eq 'random')
142 {
143 $self->{position} = splice @{ $self->{plist} }, rand @{ $self->{plist} }, 1;
144 }
145 else
146 {
147 $self->{position} = shift @{ $self->{plist} };
148 }
149 }
150  
151 # print "\n********\n", $self->dump_array(), "\n********\n";
152  
153 return $self->adjusted_position();
154 }
155  
156 =head2 Internal Tour Object Methods
157  
158 =head3 _set()
159  
160 $self->_set(%attributes);
161
162 Overrides Array::Tour's _set() method.
163  
164 =cut
165 sub _set
166 {
167 my $self = shift;
168 my(%params) = @_;
169  
170 #
171 # Parameter checks.
172 #
173 warn "Unknown paramter $_" foreach (grep{$_ !~ /backtrack|wander|start/} (keys %params));
174 $params{start} ||= [0, 0, 0];
175 $params{backtrack} ||= "queue";
176  
177 #
178 # We've got the dimensions, now set up an array.
179 #
180 $self->_make_array();
181  
182 $self->{position} = $self->{start} = $params{start};
183 $self->{wander} = $params{wander} || \&_random_dir;
184 $self->{backtrack} = $params{backtrack};
185  
186 $self->{plist} = ();
187  
188 return $self;
189 }
190  
191 =head3 _random_dir()
192  
193 The default function used to perform the random walk.
194  
195 The function may be overridden if a function is referenced in {wander}. This
196 function will take two arguments, a reference the list of possible directions
197 to move to, and a reference to the position (an array of [column, row, level]).
198  
199 =cut
200  
201 sub _random_dir
202 {
203 return ${$_[0]}[int(rand(@{$_[0]}))];
204 }
205  
206 =head3 _collect_dirs()
207  
208 @directions = $obj->_collect_dirs($c, $r, $l);
209  
210 Find all of our possible directions to wander through the array.
211 You are only allowed to go into not-yet-broken cells. The directions
212 are deliberately accumulated in a counter-clockwise fashion.
213  
214 =cut
215  
216 sub _collect_dirs
217 {
218 my $self = shift;
219 my($c, $r, $l) = @{ $self->{position} };
220 my $m = $self->get_array();
221 my($c_siz, $r_siz, $l_siz) = map {$_ - 1} $self->get_dimensions();
222 my @dir;
223  
224 #
225 # Search for enclosed cells in a partially sorted order,
226 # starting from North and going counter-clockwise (Ceiling
227 # and Floor will always be pushed last).
228 #
229 push(@dir, North) if ($r > 0 and $$m[$l][$r - 1][$c] == 0);
230 push(@dir, West) if ($c > 0 and $$m[$l][$r][$c - 1] == 0);
231 push(@dir, South) if ($r < $r_siz and $$m[$l][$r + 1][$c] == 0);
232 push(@dir, East) if ($c < $c_siz and $$m[$l][$r][$c + 1] == 0);
233 push(@dir, Ceiling) if ($l > 0 and $$m[$l - 1][$r][$c] == 0);
234 push(@dir, Floor) if ($l < $l_siz and $$m[$l + 1][$r][$c] == 0);
235 return @dir;
236 }
237  
238 =head3 _break_through()
239  
240 =cut
241  
242 sub _break_through
243 {
244 my $self = shift;
245 my $dir = $self->{direction};
246 my($c, $r, $l) = @{$self->{position}};
247 my $m = $self->get_array();
248  
249 $$m[$l][$r][$c] |= $dir;
250 ($c, $r, $l) = @{$self->_move_to($dir)};
251 $$m[$l][$r][$c] |= $self->opposite_direction($dir);
252 $self->{position} = [$c, $r, $l];
253 }
254  
255 1;
256 __END__