So, after asking how one would program Who's a thief? in Perl, I decided to take a stab at it. Eventually, I plan to make this a more general module, but for now, this is a proof of concept. First, let's change the problem. I want to know who will steal from whom. Here's the Prolog (see the above link for an explanation, if it's not clear):
owns( merlyn, gold ). owns( ovid, pocketLint ). owns( ovid, cheapWhiskey ). owns( kudra, dominationFund ). valuable( gold ). valuable( dominationFund ). thief( badguy ). rich( X ) :- owns( X, Y ), valuable( Y ). steals_from( X, Y ) :- thief( X ), rich( Y ).
The last rule states that X will steal from Y if X is a thief and Y is rich.
To create this, I first created a %facts hash and added the 'owns', 'valuable', and 'thief' facts to it. Then, I created the rules for steals_from and rich. Asking the question, who will the badguy steal from becomes as simple as steals_from( $badguy, $victim ).
The code is pretty much a hack, but it's a start. This is not nearly as elegant as Abigail-II's regex solution for the n-queens problem, but I think it's a bit easier to understand.
Run the code below and it prints the following:
badguy will steal from merlyn badguy will not steal from ovid badguy will steal from kudra
#!/usr/bin/perl -w use strict; my %facts; # poor ( X ) :- # not rich( X ). my @owns = ( 2, merlyn => 'gold', ovid => 'pocket lint', ovid => 'cheap whiskey', kudra => 'domination fund' ); my @valuable = ( 1, 'gold', 'domination fund' ); my @thief = ( 1, 'badguy' ); $facts{ owns } = add_fact( \@owns ); $facts{ valuable } = add_fact( \@valuable ); $facts{ thief } = add_fact( \@thief ); foreach ( qw/ merlyn ovid kudra / ) { my $modifier = steals_from( 'badguy', $_ ) ? '' : ' not'; print "badguy will$modifier steal from $_\n"; } sub steals_from { # steals_from( X, Y ) :- # thief( X ), # rich( Y ). my ( $perp, $victim ) = @_; return thief( $perp ) && rich( $victim ); } sub thief { my $perp = shift; return grep { $_->[0] eq $perp } @{$facts{thief}}; } sub rich { # rich ( X ) :- # owns( X, Y ), # valuable( Y ). my $person = shift; my @results; my @items = map { $_->[1] } grep { $_->[0] eq $person } @{$facts{owns}}; foreach my $item ( @items ) { push @results => $item if grep { $_->[0] eq $item } @{$facts{valuable}}; } return @results ? 1 : 0; } sub add_fact { my $rule = shift; my $args = shift @$rule; my @rule; for ( my $i = 0; $i < @$rule; $i = $i + $args ) { my @args = @$rule[ $i .. $i + ( $args - 1 ) ]; push @rule => \@args; } return \@rule; }
Incidentally, for a hint on how Abigail-II' regex works, print the regex and look at the lines which resemble the following:
(?(?{attack $q [3], $q [4]})x|)
Basically, it says if one queen can attack another, match 'x' to the target string, otherwise, match the empty string. Since the target string is the empty string, this forces the backtracking. All in all, a very elegant bit of code. While my code does not even remotely resemble that beauty, I am considering adding regex backtracking support to mine in the future.
Cheers,
Ovid
Update: Fixed typo that Courage pointed out.
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: AI in Perl - proof of concept
by Courage (Parson) on Jul 12, 2002 at 18:57 UTC | |
by Ovid (Cardinal) on Jul 12, 2002 at 19:02 UTC | |
by vladb (Vicar) on Jul 13, 2002 at 15:45 UTC | |
Re: AI in Perl - proof of concept
by rbc (Curate) on Jul 12, 2002 at 20:27 UTC | |
Re: AI in Perl - proof of concept
by shotgunefx (Parson) on Jul 13, 2002 at 01:14 UTC | |
(shockme) Re: AI in Perl - proof of concept
by shockme (Chaplain) on Jul 13, 2002 at 00:07 UTC | |
Re: AI in Perl - proof of concept
by Abigail-II (Bishop) on Jul 15, 2002 at 13:33 UTC | |
Re: AI in Perl - proof of concept
by John M. Dlugosz (Monsignor) on Jul 15, 2002 at 20:34 UTC | |
by Ovid (Cardinal) on Jul 15, 2002 at 21:09 UTC |