Algorithm-Search
view release on metacpan or search on metacpan
lib/Algorithm/Search.pm view on Meta::CPAN
$self->{first_search_step} = \&first_search_step;
$self->{search_step} = \&search_step;
}
elsif ($parameters->{search_type} eq 'rdfs') {
$self->{first_search_step} = \&rdfs_first_search_step;
$self->{search_step} = \&rdfs_search_step;
}
else {
die "Unknown search type ".$parameters->{search_type};
}
}
else {
$self->{first_search_step} = $self->{default_first_search_step};
$self->{search_step} = $self->{default_search_step};
$self->{search_type} = $self->{default_search_type};
}
$self->{handled} = {};
$self->{move_list} = [undef];
$self->{moving_forward} = 1;
$self->{continue_search} = 1;
$self->{search_completed} = 0;
$self->{solutions_found} = 0;
$self->{solutions} = [];
$self->{paths} = [];
$self->{trace} = [];
&{$self->{first_search_step}}($self);
$self->{steps} = 1;
$self->continue_search;
}
sub new {
my $type = shift;
my $class = ref($type) || $type;
my $parameters = shift;
my $self = {};
$self->{default_first_search_step} = \&first_search_step;
$self->{default_search_type} = 'dfs';
$self->{default_search_step} = \&search_step;
bless $self, $class;
return $self;
}
1;
__END__
=head1 NAME
Algorithm::Search - Module for traversing an object.
=head1 SYNOPSIS
use Algorithm::Search;
my $as = new Algorithm::Search();
$as->search({ # Example parameters here are the default parameters
search_this => $object_to_search, #no default
search_type => 'dfs', # dfs, bfs, cost, or rdfs
max_steps => 20000, # number of moves to look at
maximum_depth => 0, # longest allowable path length if > 0
solutions_to_find => 0, # search stops when number reached, 0 finds all
do_not_repeat_values => 0, # only traverse position with value once
cost_cannot_increase => 0, # whether or not moves can increase cost
initial_cost => undef, # for cost based search
return_search_trace => 0, # does $as->search_trace return array ref of moves
});
if (!$as->completed) {
$as->continue_search({additional_steps => 300});
}
if ($as->solution_found) {
@solutions = $as->solutions;
@paths_to_solution = $as->paths;
}
$steps_taken = $search->steps_taken;
=head1 DESCRIPTION AND DEFINITIONS
A user provided traversable object starts in an initial position.
The traversable
object must have certain methods, such as 'move', described below.
This is passed into the search method via the 'search_this' parameter.
At any position, the object has a list of moves to new positions,
the list may be empty.
A position is a solution if the "is_solution" function returns true.
A traversal does not require that a solution be found or even looked for.
A search is a traversal that looks for a solution.
A path corresponds to a list of valid moves from the initial position.
The path values correspond to the list of values by the positions
the object moves along the path.
A move is valid if the move function returns a value.
The number of steps, is the number of calls to the move function.
A queue based traversal stores positions and paths on a queue, copying
the object before performing each move.
In depth first search (dfs), the next position to search is the
most recently placed on the queue.
In breadth first search (bfs), the next item to search is the least
recently placed on the queue.
In cost based search (cost) , the next item to search is the lowest
cost item on the queued, ties go to the most recently placed on the queue.
Cost based search with all the same costs behaves as depth first search.
A reversible depth first search (rdfs) traversal uses
reverse move to traverse paths with the search object.
A reversible traversal does not use a queue resulting in less memory
usage. A reversible traversal is more complicated to set up and only
allows depth first search.
=head1 Methods Required in object being searched.
( run in 2.033 seconds using v1.01-cache-2.11-cpan-d7f47b0818f )