Algorithm-Search
view release on metacpan or search on metacpan
example/15.pl view on Meta::CPAN
#!/usr/bin/perl
#Copyright 2008 Arthur S Goldstein
#solves the classic fifteen puzzle using bfs
package fifteen;
sub new {return bless {}}
sub set_position {my $self = shift;
my $string = shift;
my @lines = split /\n/, $string;
my $row = 0;
foreach my $line (@lines) {
my @numbers = split /\s+/, $line;
$self->{board}->[$row] = \@numbers;
foreach my $count (0..$#numbers) {
if ($numbers[$count] == 0) {
$self->{zero_at} = [$row, $count];
example/distance_with_dfs.pl view on Meta::CPAN
'St. Paul' => ['Minneapolis', 'Madison'],
'Madison' => ['Rockford', 'St. Paul', 'Chicago'],
'Rockford' => ['Bloomington', 'Madison'],
'Bloomington' => ['Champaign'],
'Champaign' => ['Urbana', 'Chicago'],
'Chicago' => ['Minneapolis', 'Urbana'],
'Urbana' => [],
'Duluth' => [],
);
sub new {return bless {}}
sub next_moves {my $self = shift;
return @{$roads{$self->{position}}}}
sub move {my $self = shift; $self->{position} = shift; return 0;}
sub value {my $self = shift; return $self->{position}}
sub copy {my $self = shift; my $copy = $self->new;
$copy->move($self->{position}); return $copy;};
sub is_solution {my $self = shift;
return $self->{position} eq $destination;}
sub set_destination {my $self = shift; $destination = shift;}
example/distance_with_rdfs.pl view on Meta::CPAN
'St. Paul' => 505,
'Madison' => 252,
'Rockford' => 185,
'Bloomington' => 56,
'Champaign' => 2,
'Chicago' => 140,
'Urbana' => 0,
'Duluth' => 575,
);
sub new {return bless {}}
sub move_after_given {
my $self = shift;
my $previous = shift;
my $move_count = 0;
if ($previous) {
$move_count = $previous->[2] + 1;
}
my $city = $self->{position};
if (scalar(@{$roads{$city}}) > $move_count) {
return [$self->{position}, $roads{$city}->[$move_count], $move_count]
example/mdcost.pl view on Meta::CPAN
'Champaign' => 2,
'Chicago' => 140,
'Urbana' => 0,
'Duluth' => 575,
);
sub distance_to_urbana {
my $self = shift;
return $distance_to_urbana{$self->{position}};
}
sub new {return bless {}}
sub next_moves {my $self = shift;
return @{$roads{$self->{position}}}}
sub move {my $self = shift; $self->{position} = shift;
return $distance_to_urbana{$self->{position}};}
sub value {my $self = shift; return $self->{position}}
sub copy {my $self = shift; my $copy = $self->new;
$copy->move($self->{position}); return $copy;};
sub is_solution {my $self = shift;
return $self->{position} eq 'Urbana';}
lib/Algorithm/Search.pm view on Meta::CPAN
}
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
lib/Algorithm/Search.pm view on Meta::CPAN
'St. Paul' => ['Minneapolis', 'Madison'],
'Madison' => ['Rockford', 'St. Paul', 'Chicago'],
'Rockford' => ['Bloomington', 'Madison'],
'Bloomington' => ['Champaign'],
'Champaign' => ['Urbana', 'Chicago'],
'Chicago' => ['Minneapolis', 'Urbana'],
'Urbana' => [],
'Duluth' => [],
);
sub new {return bless {}}
sub next_moves {my $self = shift;
return @{$roads{$self->{position}}}}
sub move {my $self = shift; $self->{position} = shift; return 0;}
sub value {my $self = shift; return $self->{position}}
sub copy {my $self = shift; my $copy = $self->new;
$copy->move($self->{position}); return $copy;};
sub is_solution {my $self = shift;
return $self->{position} eq $destination;}
sub set_destination {my $self = shift; $destination = shift;}
lib/Algorithm/Search.pm view on Meta::CPAN
'Champaign' => 2,
'Chicago' => 140,
'Urbana' => 0,
'Duluth' => 575,
);
sub distance_to_urbana {
my $self = shift;
return $distance_to_urbana{$self->{position}};
}
sub new {return bless {}}
sub next_moves {my $self = shift;
return @{$roads{$self->{position}}}}
sub move {my $self = shift; $self->{position} = shift;
return $distance_to_urbana{$self->{position}};}
sub value {my $self = shift; return $self->{position}}
sub copy {my $self = shift; my $copy = $self->new;
$copy->move($self->{position}); return $copy;};
sub is_solution {my $self = shift;
return $self->{position} eq 'Urbana';}
lib/Algorithm/Search.pm view on Meta::CPAN
'St. Paul' => ['Minneapolis', 'Madison'],
'Madison' => ['Rockford', 'St. Paul', 'Chicago'],
'Rockford' => ['Bloomington', 'Madison'],
'Bloomington' => ['Champaign'],
'Champaign' => ['Urbana', 'Chicago'],
'Chicago' => ['Minneapolis', 'Urbana'],
'Urbana' => [],
'Duluth' => [],
);
sub new {return bless {}}
sub move_after_given {
my $self = shift;
my $previous = shift;
my $move_count = 0;
if ($previous) {
$move_count = $previous->[2] + 1;
}
my $city = $self->{position};
if (scalar(@{$roads{$city}}) > $move_count) {
return [$self->{position}, $roads{$city}->[$move_count], $move_count]
#!/usr/bin/perl
#Copyright 2008 Arthur S Goldstein
use Test::More tests => 9;
my $loaded = 1;
package fifteen;
sub new {return bless {}}
sub set_position {my $self = shift;
my $string = shift;
my @lines = split /\n/, $string;
my $row = 0;
foreach my $line (@lines) {
my @numbers = split /\s+/, $line;
$self->{board}->[$row] = \@numbers;
foreach my $count (0..$#numbers) {
if ($numbers[$count] == 0) {
$self->{zero_at} = [$row, $count];
'St. Paul' => [['Minneapolis',10], ['Madison',100]],
'Madison' => [['Rockford',50], ['St. Paul',100], ['Chicago',100]],
'Rockford' => [['Bloomington',100], ['Madison',50]],
'Bloomington' => [['Champaign',50]],
'Champaign' => [['Urbana',5], ['Chicago',100]],
'Chicago' => [['Minneapolis',300], ['Urbana',95]],
'Urbana' => [],
'Duluth' => [['Chicago',400]],
);
sub new {return bless {}}
sub next_moves {my $self = shift;
#print STDERR "Position is ".$self->{position}."\n";
return @{$roads{$self->{position}}}}
sub move {
my $self = shift;
my $road_taken = shift;
my $previous_cost = shift;
my $new_position = $road_taken->[0];
my $new_cost = $previous_cost + $road_taken->[1];
#print STDERR "current position ".$self->{position}." ";
#!/usr/bin/perl
#Copyright 2008 Arthur S Goldstein
use Test::More tests => 12;
my $loaded = 1;
package fifteen;
sub new {return bless {}}
sub set_position {my $self = shift;
my $string = shift;
my @lines = split /\n/, $string;
my $row = 0;
foreach my $line (@lines) {
my @numbers = split /\s+/, $line;
$self->{board}->[$row] = \@numbers;
foreach my $count (0..$#numbers) {
if ($numbers[$count] == 0) {
$self->{zero_at} = [$row, $count];
#!/usr/bin/perl
#Copyright 2008 Arthur S Goldstein
#Games Magazine, July 2008 page 28
use Test::More tests => 22;
package pearls_of_wisdom;
#our $xx = 0;
sub new {my $self = {moves => 1, row => 0, column => 0};
$self->{value}->[0]->[0] = 1; return bless $self}
sub set_rules {
my $self = shift;
my $parameters = shift;
$self->{max_row} = $parameters->{max_row};
$self->{max_column} = $parameters->{max_column};
$self->{in_moves} = $parameters->{in_moves};
$self->{fixed_value} = $parameters->{fixed_value};
}
sub move {
#!/usr/bin/perl
#Copyright 2008 Arthur S Goldstein
#Games Magazine, July 2008 page 54
use Test::More tests => 6;
package easy_as_one_two_three;
#our $xx = 0;
sub new {return bless {count => 0}}
sub set_rules {
my $self = shift;
my $parameters = shift;
$self->{max_row} = $parameters->{max_row};
$self->{max_column} = $parameters->{max_column};
$self->{start} = $parameters->{start};
$self->{position} = $parameters->{start};
$self->{final} = $parameters->{final};
$self->{not_allowed} = $parameters->{not_allowed};
$self->{not_allowed_count} = 0;
#!/usr/bin/perl
#Copyright 2008 Arthur S Goldstein
#Games Magazine, July 2008 page 78
use Test::More tests => 2;
package sum_product;
our $known_max = 0;
our $max_at;
sub new {return bless {product => 1, sum => 0}}
sub value {
my $self = shift;
return join("..",sort keys %{$self->{values}});
}
sub move {
my $self = shift;
my $number = shift;
'St. Paul' => ['Minneapolis', 'Madison'],
'Madison' => ['Rockford', 'St. Paul', 'Chicago'],
'Rockford' => ['Bloomington', 'Madison'],
'Bloomington' => ['Champaign'],
'Champaign' => ['Urbana', 'Chicago'],
'Chicago' => ['Minneapolis', 'Urbana'],
'Urbana' => [],
'Duluth' => [],
);
sub new {return bless {}}
sub move_after_given {
my $self = shift;
my $previous = shift;
my $move_count = 0;
if ($previous) {
$move_count = $previous->[2] + 1;
}
my $city = $self->{position};
#print STDERR "mag city $city previous ".join("..",@$previous)."\n";
if (scalar(@{$roads{$city}}) > $move_count) {
'St. Paul' => 505,
'Madison' => 252,
'Rockford' => 185,
'Bloomington' => 56,
'Champaign' => 2,
'Chicago' => 140,
'Urbana' => 0,
'Duluth' => 575,
);
sub new {return bless {}}
sub move_after_given {
my $self = shift;
my $previous = shift;
my $move_count = 0;
if ($previous) {
$move_count = $previous->[2] + 1;
#print STDERR "previous ".join("..",@$previous)."\n";
}
my $city = $self->{position};
#print STDERR "mag city $city\n";
'St. Paul' => ['Minneapolis', 'Madison'],
'Madison' => ['Rockford', 'St. Paul', 'Chicago'],
'Rockford' => ['Bloomington', 'Madison'],
'Bloomington' => ['Champaign'],
'Champaign' => ['Urbana', 'Chicago'],
'Chicago' => ['Minneapolis', 'Urbana'],
'Urbana' => [],
'Duluth' => [],
);
sub new {return bless {}}
sub next_moves {my $self = shift;
return @{$roads{$self->{position}}}}
sub move {my $self = shift; $self->{position} = shift; return 0;}
sub value {my $self = shift; return $self->{position}}
sub copy {my $self = shift; my $copy = $self->new;
$copy->move($self->{position}); return $copy;};
sub is_solution {my $self = shift;
return $self->{position} eq $destination;}
sub set_destination {my $self = shift; $destination = shift;}
'Champaign' => 2,
'Chicago' => 140,
'Urbana' => 0,
'Duluth' => 575,
);
sub distance_to_urbana {
my $self = shift;
return $distance_to_urbana{$self->{position}};
}
sub new {return bless {}}
sub next_moves {my $self = shift;
return @{$roads{$self->{position}}}}
sub move {my $self = shift; $self->{position} = shift;
return $distance_to_urbana{$self->{position}};}
sub value {my $self = shift; return $self->{position}}
sub copy {my $self = shift; my $copy = $self->new;
$copy->move($self->{position}); return $copy;};
sub is_solution {my $self = shift;
return $self->{position} eq 'Urbana';}
}
close $fh;
#use Data::Dumper;
#print STDERR Dumper($tree)."\n";
#print STDERR "is AD a word? ";
#print STDERR $word_list{'AD'};
#print STDERR "\n";
#exit;
# print STDERR "Word count ".scalar(keys %word_list);
sub new {return bless {}}
sub set_position {
my $self = shift;
my ($row, $column, $board) = @_;
#print STDERR "board is $board\n";
$self->{row} = $row;
$self->{column} = $column;
$self->{tree} = $tree;
my @rows = split (" ", $board);
my $row_count = 0;
( run in 0.463 second using v1.01-cache-2.11-cpan-de7293f3b23 )