Showing posts with label The Art of Prolog. Show all posts
Showing posts with label The Art of Prolog. Show all posts

Friday, September 04, 2015

Clare vs. 'the Cat'

This afternoon I programmed my very first simulation of the family pet, Shadow. In this simplistic iteration, the cat plays a game with the user. The idea is to stack 20 cans of cat food. The user and the cat take turns to stack 1, 2 or three cans at a time: the first to 20 wins.

Here is a transcript of the start of play. The cat's moves are between asterisks ***; the user's moves are signposted with -->.

Miaow, my name is Shadow. The game is to stack 20 cans of cat food. You can choose 1 or 2 or 3 cans at each turn. Me too! First to 20 wins.

Do you want to play first? type y for yes, n for no followed by return.

So, do you want to play first (y/n and press return)? n

*** My move is 2 cans. Total number of cans = 2 ***

enter your number 1, 2 or 3 |: 2

--> You entered 2 cans. Total number of cans = 4

*** My move is 1 cans. Total number of cans = 5 ***

enter your number - 1, 2 or 3: |:  ...

And here is a video of my guinea pig, Clare, playing the cat. What was it that Kipling said about winning and losing? (You may have to double-click on video to get full screen).



The game was an exercise from 'The Art of Prolog' and it took me a while to develop the game-play to program. Here is the folder containing the Prolog program by the way: you may use it to infer the winning strategy.

Thursday, September 03, 2015

AI Progress Report



So I got 'Eliza' working today, the simplified version from 'The Art of Prolog'. Time to take stock and figure out where to go next.

First, Prolog. Its supporters always touted it as a higher-level language than Lisp - though I always found Lisp more congenial, I like to set up data structures and manipulate them explicitly. With Prolog you define relationships between things and the miraculous powers of unification and depth-first search with backtracking pull magical rabbits out of hats. The Eliza program in Prolog can be read in its entirety on one screen, ditto for the blocks world planning system.

This procedural power is the result of enormously complex recursive structures built at execution time by the Prolog system. It frequently defies one's powers of abstraction, short-term memory and inference to visualise what's actually going on. I know you're meant to read and understand the programs declaratively, but in reality you don't get too far without a consideration of what actually happens at run-time.

Still, the power to write ridiculously-powerful programs in just a few lines of code is addictive. It reminds me of the first time I fired the General-Purpose Machine Gun (GPMG). I was good with the Lee-Enfield rifle and prided myself on my accuracy; the GPMG just bounced around and hosed the target. So much power and so little control!

---

Eliza and the Blocks World Planner were little milestones I had set myself, like climbing Pen y Fan. Items on my bucket list if you like. So what next?

Once you know how to set up knowledge bases and inferential systems you have the tools for developing intelligent agents. But, as I have cited before on these pages, 'in the knowledge lies the power'. If your agent lives in a closed-world with a fixed and limited database and rule set, it's going to run out of new things to do pretty fast. The interest comes from its interactions with the wider world.

Yet as Doug Lenat noted in the context of his 'Cyc' project:
"Any time you look at any kind of real life piece of text or utterance that one human wrote or said to another human, it's filled with analogies, modal logic, belief, expectation, fear, nested modals, lots of variables and quantifiers," Lenat said. "Everyone else is looking for a free-lunch way to finesse that. Shallow chatbots show a veneer of intelligence or statistical learning from large amounts of data. Amazon and Netflix recommend books and movies very well without understanding in any way what they're doing or why someone might like something.

"It's the difference between someone who understands what they're doing and someone going through the motions of performing something."

Cycorp's product, Cyc, isn't "programmed" in the conventional sense. It's much more accurate to say it's being "taught." Lenat told us that most people think of computer programs as "procedural, [like] a flowchart," but building Cyc is "much more like educating a child."

"We're using a consistent language to build a model of the world," he said.

This means Cyc can see "the white space rather than the black space in what everyone reads and writes to each other." An author might explicitly choose certain words and sentences as he's writing, but in between the sentences are all sorts of things you expect the reader to infer; Cyc aims to make these inferences.

Consider the sentence, "John Smith robbed First National Bank and was sentenced to 30 years in prison." It leaves out the details surrounding his being caught, arrested, put on trial, and found guilty. A human would never actually go through all that detail because it's alternately boring, confusing, or insulting. You can safely assume other people know what you're talking about. It's like pronoun use - he, she, it - one assumes people can figure out the referent. This stuff is very hard for computers to understand and get right, but Cyc does both.

"If computers were human," Lenat told us, "they'd present themselves as autistic, schizophrenic, or otherwise brittle. It would be unwise or dangerous for that person to take care of children and cook meals, but it's on the horizon for home robots. That's like saying, 'We have an important job to do, but we're going to hire dogs and cats to do it.'"
Cyc has been in development since 1984 and its knowledge base currently contains over one million human-defined assertions, rules or common sense ideas. Yet it's still barely found practical use. I'm certainly not planning on reproducing that level of effort.

I think we're back to virtualizing the cat ...

Tuesday, August 18, 2015

Logic Puzzle

From page 262 of 'The Art of Prolog' by Leon Sterling and Ehud Shapiro.

"The challenge is to write a Prolog program to solve the following logic puzzle.
(a) The Englishman lives in the red house.
(b) The Spaniard owns the dog.
(c) Coffee is drunk in the green house.
(d) The Ukrainian drinks tea.
(e) The green house is immediately to the right (your right) of the ivory house.
(f) The Winston smoker owns snails.
(g) Kools are smoked in the yellow house.
(h) Milk is drunk in the middle house.
(i) The Norwegian lives in the first house on the left.
(j) The man who smokes Chesterfields lives in the house next to the man with the fox.
(k) Kools are smoked in the house next to the house where the horse is kept.
(1) The Lucky Strike smoker drinks orange juice.
(m) The Japanese smokes Parliaments.
(n) The Norwegian lives next to the blue house.

Who owns the Zebra? Who drinks water?"
They provide the solution to a simpler problem.

Clare had a go and figured out that the water-drinker might be Norwegian, then things bogged down a little. My own programming efforts, meanwhile, were mired in bugs.

It is often the little things in life which absurdly please. Imagine my delight (spoilers follow) when my Prolog program produced the following output:
Logic Puzzle v. 0082
Persons = [person(yellow,norwegian,fox,water,kools,1),
person(blue,ukranian,horse,tea,chesterfield,2),
person(red,english,snails,milk,winston,3),
person(ivory,spanish,dog,orange,luckystrike,4),
person(green,japanese,zebra,coffee,parliaments,5)]

S = [['The zebra is owned by the ', japanese], ['the ', norwegian, ' drinks water']].
For my next trick it's implementing one of those block-stacking planning programs, to be followed by Eliza. And after that, let's put them all together and have a go at a more interesting conversationalist.