Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

AI::Perlog - more thieves

by Ovid (Cardinal)
on Aug 19, 2002 at 01:51 UTC ( #191060=perlmeditation: print w/replies, xml ) Need Help??

belkajm sent me a great bit of code to handle the unification problem in AI::Perlog, of which you can download the latest copy at this link. Unfortunately, belkajm didn't make his post a root node, which I think it should have been because it was a very well-explained solution to the problem. Go vote for it :)

To show how interesting this is (to me, at least), consider the problem listed in Who's a thief?. The code I have below solves the listed problem. You can compare this to code in AI in Perl - proof of concept. That code is much clumsier, less scalable, and not as easy to modify.

Also, I want to take a closer look at the code that arhuman put together in AI::Perlog(2) unification proposition and an explanatory follow-up. It has some interesting features that I hadn't considered, but also haven't had the time to delve into. All in all, I have to say "thanks" to both arhuman and belkajm for their help and am looking forward to seeing more.


#!/usr/bin/perl -w use strict; use AI::Perlog; my $pg = AI::Perlog->new; load_data(); my $acts_of_horror = who_steals_from_whom(); unless ( @$acts_of_horror ) { print "Looks like everybody's safe\n"; } else { foreach my $crime ( @$acts_of_horror ) { my ( $thief, $victim ) = @$crime; print "$thief will steal from $victim.\n"; } } exit; sub who_steals_from_whom { my $thieves = $pg->thief( '$person' ); my $victims = $pg->owns( qw/ $person _ / ); my @crimes; foreach my $victim ( @$victims ) { my $curr_victim = $victim->{person}; foreach my $thief ( @$thieves ) { my $curr_thief = $thief->{person}; if (rich($curr_victim) and ! $pg->knows($curr_thief,$curr_ +victim)){ push @crimes => [$curr_thief, $curr_victim]; } } } return \@crimes; } sub rich { my $person = shift; my $items = $pg->owns( $person, '$item' ); local $_; foreach ( @$items ) { return 1 if $pg->valuable( $_->{ item } ); } return; } sub load_data { while ( <DATA> ) { chomp; next if ( ! $_ or substr( $_, 0, 1 ) eq '#' ); my @fact = split /\t/; $pg->add_fact( @fact ); } } __DATA__ owns merlyn gold owns Ovid pocket lint owns Ovid cheap whiskey owns kudra domination fund valuable gold valuable domination fund thief badguy thief badgirl # :) knows badgirl merlyn


Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: AI::Perlog - more thieves
by belkajm (Beadle) on Aug 19, 2002 at 07:19 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://191060]
Approved by VSarkiss
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2021-06-24 15:49 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (129 votes). Check out past polls.