There's more than one way to do things | |
PerlMonks |
AI in Perl - proof of conceptby Ovid (Cardinal) |
on Jul 12, 2002 at 18:40 UTC ( [id://181368]=perlmeditation: print w/replies, xml ) | Need Help?? |
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):
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:
Incidentally, for a hint on how Abigail-II' regex works, print the regex and look at the lines which resemble the following:
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, Update: Fixed typo that Courage pointed out. Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
Back to
Meditations
|
|