Thursday, February 17, 2011

Einstein's Riddle as CSP

This python script solves a riddle known on the web as "Einstein's riddle", although there seems to be little if any support for Einstein actually creating it. Nevertheless, it has become my "hello world" for machine learning/reasoning technologies.
The riddle as described here is as follows:
Einsteins' Riddle
  1. In a street there are five houses, painted five different colours.
  2. In each house lives a person of different nationality
  3. These five homeowners each drink a different kind of beverage, smoke a different brand of cigar and keep a different pet.
THE QUESTION: WHO OWNS THE FISH?

HINTS
  1. The Brit lives in a red house.
  2. The Swede keeps dogs as pets.
  3. The Dane drinks tea.
  4. The Green house is next to, and on the left of the White house.
  5. The owner of the Green house drinks coffee.
  6. The person who smokes Pall Mall rears birds.
  7. The owner of the Yellow house smokes Dunhill.
  8. The man living in the centre house drinks milk.
  9. The Norwegian lives in the first house.
  10. The man who smokes Blends lives next to the one who keeps cats.
  11. The man who keeps horses lives next to the man who smokes Dunhill.
  12. The man who smokes Blue Master drinks beer.
  13. The German smokes Prince.
  14. The Norwegian lives next to the blue house.
  15. The man who smokes Blends has a neighbour who drinks water.
Here is a simple python script using python-constraint:

from constraint import *
problem
= Problem()
def left_of(a,b):
return a < b
def right_of(a,b):
return not left_of(a,b)
def next_to(a,b):
return abs(a - b) == 1
colors
= ["yellow","red","green","blue","white"]
nats
= ["norwegian","dane","brit","german","swede"]
bevs
= ["water","tea","milk","coffee","beer"]
smokes
= ["dunhill","blends","pall_mall", "prince", "bluemaster"]
pets
= ["cat","horse","bird","fish","dog"]

house_numbers
= [1,2,3,4,5]

problem
.addVariables(colors, house_numbers)
problem
.addVariables(nats, house_numbers)
problem
.addVariables(bevs, house_numbers)
problem
.addVariables(smokes, house_numbers)
problem
.addVariables(pets, house_numbers)

problem
.addConstraint(AllDifferentConstraint(),colors)
problem
.addConstraint(AllDifferentConstraint(),nats)
problem
.addConstraint(AllDifferentConstraint(),bevs)
problem
.addConstraint(AllDifferentConstraint(),smokes)
problem
.addConstraint(AllDifferentConstraint(),pets)

#specific constraints from the riddle
problem
.addConstraint(lambda brit, red: brit == red, ("brit", "red"))
problem
.addConstraint(lambda swede, dog: swede == dog, ("swede", "dog"))
problem
.addConstraint(lambda dane, tea: dane == tea, ("dane", "tea"))
problem
.addConstraint(lambda green, white: next_to(green, white) and \
left_of(green, white),(
"green", "white"))
problem
.addConstraint(lambda green, coffee: green == coffee,
(
"green", "coffee"))
problem
.addConstraint(lambda pall_mall, bird: pall_mall == bird,
(
"pall_mall", "bird"))
problem
.addConstraint(lambda yellow, dunhill: yellow == dunhill,
(
"yellow", "dunhill"))
problem
.addConstraint(lambda milk: milk == 3, ["milk"])
problem
.addConstraint(lambda norwegian: norwegian == 1, ["norwegian"])
problem
.addConstraint(lambda blends, cat: next_to(blends, cat),
(
"blends", "cat"))
problem
.addConstraint(lambda horse, dunhill: next_to(horse, dunhill),
(
"horse", "dunhill"))
problem
.addConstraint(lambda bluemaster, beer: bluemaster == beer,
(
"bluemaster", "beer"))
problem
.addConstraint(lambda german, prince: german == prince,
(
"german","prince"))
problem
.addConstraint(lambda norwegian, blue: next_to(norwegian, blue),
(
"norwegian", "blue"))
problem
.addConstraint(lambda blends, water: next_to(blends, water),
(
"blends", "water"))

# now get a solution
solution
= problem.getSolution()

# get in the form we want
answer
= {}
for i in range(1,6):
answer[i]
= {}

for k,v in solution.items():
if k in colors:
answer[v][
'color'] = k
elif k in nats:
answer[v][
'nationality'] = k
elif k in bevs:
answer[v][
'beverage'] = k
elif k in smokes:
answer[v][
'smokes'] = k
elif k in pets:
answer[v][
'pet'] = k

print "**** Solution ****"
for i in range(1,6):
print i, answer[i]['color'],answer[i]['nationality'],\
answer[i][
'beverage'],answer[i]['smokes'], answer[i]['pet']

for i in range(1,6):
if answer[i]['pet'] == 'fish':
owner_of_fish
= answer[i]['nationality']
break
print
print "Answer to riddle question: who has the fish for a pet? - the", \
owner_of_fish
 
# end of script -------------------------------------------------------

Thursday, January 28, 2010

x

all learning is a quest
for more awareness

learning must then
eventually fold in on itself

the program that reads its
own code

the blue faced devil

i have sat with the
blue faced devil
laughing at tragedy
at the ugly
and lame
and simple minded
and passionate
and weak
and hopeful
and trusting
and playful
and i have seen
past him
and before him
as history
as past
as dreams
as nothing

Monday, January 18, 2010

The network of "me"

How Many?

How many voices
inside your voice
inside your head?

How many heads
inside your voice?

How many
ways-it-can-be?
ways-it-was?

Whatever you do,
don't let it be one.

One is never enough.

Sunday, November 8, 2009

Evolution is good - correction: Evolution is God

First of all - I do not believe in any religion, on the contrary, if there were a "god", prime mover, etc., it would most certainly be NP-Complete, and hence not codifiable. The world's religions were developed before we had any insight into the universal nature of computability.

God cannot describe him/her/it self!

Why? - Godel's Incompleteness.
If there is a god of some sort, e.g. supernatural agent that determines all things, then there must be some unknowable part of it.

Is there such a thing? Yes. But it is not a primitive explanation from our evolutionary past.
It is reason. Everything reason says is true. Reason says what is fair. Reason says what is just, not the atavistic "HEART".
The heart is a magnificent organ. The emotions are a set of sensations that can or can not be associated with a set of circumstances.
When today's religions sprang up, they represented the best reasoning of the day. The explanations of where things came from was the cosmology of the day. The rules were the social science and economics of the day. The meaning of life fit with all the rest of their "science" and represented the best philosophy of the day.
If the "bibles" had just been amended as reasoning evolved we would have science for cosmology and social science for morals and ethics and epsitemology and ontology and logic and so on.
We would have learned Marx, and Sartre, and Bordieau in sunday school. We would learn how game theory applies to society and how mirror neurons are part of the "mystery" ingredient of "completeness" even when it is logically impossible. We would have learned how life "actually" works (given the current science) - and our children would get an even better picture.
We could have avoided ourselves the embarassment of Dawkin's clumsy, overly emotive, defense of something he barely has a grasp on.
Reason explains everything....eventually.
I may not always obey reason, but if I want to "do right" I follow it, no matter what it says.
If reasoning says the heavy airplane will fly, I will not doubt it can fly.
If reasoning says the fair way to deal with my neighbor can not be oblivious to Pareto optimality, then I will not consider myself fair until I know about it.
If reasoning says that cooperative Nash Equilibrium is equivalent to looking out for the group first, then yourself, I will see that as a mandate for two identities, the self, and the group: first human, then animals, then all life, then all things.
I pray by reasoning, I live by reasoning, I am defined by reasoning - and am becoming more and more "like" reasoning, every day.
Reasoning is my "God" - my bible is everything knowable - it is not static - but evolving.
Thinking this way, it is easy to say that God is also "Evolution" (not just biological)
Yet, here we are, in the 21st century, pomo, post relativity, post quantum, ... and are debating evolution??? Because someone forgot to update their blog????

Thursday, April 9, 2009

zenophobia

---------------------------------------------------------------------
Backward in time

the plant explains
the seed explains
the earth explains
the water explains

Here and now
explains the map
Past and future
stand in reflection
---------------------------------------------------------------------
It is so easy to die
let your life to this moment go
and admit about this corpse
what you could never admit about
"yourself "
It is so easy to die
when you do it this way
--------------------------------------------------
I am Philosopher


I am philosopher because I love wisdom
and I love the love of wisdom
and because i have taken the time to learn to
say and think loveOf* wisdomOf*


i am philosopher because i make no apologies
for seeking the truth above all things


i am philospher because everything i do
and learn and experience adds to one thing:
"what is this and how does it work?"





i am philosopher because i say so
because i look in every field, every art,
every image or movement for one thing


i am philosopher because i can tell the
difference between the sign and the signified


i am philosopher
i philosophize always


i am philosopher
because "people" hate me
i am a gadfly
i am hemlock
i am refusenik
and commie


i don't stop asking
expanding
including
reasoning

ever


i am


i am not disturbed by Godel
or Turing machines

computation is not at odds with philosophy


i am not at odds with Bayes


not only is "god" dead
god was never alive
nor any true believer in god


i am philosopher
full time


when i eat (what is eating, what is food)
when i shower (what is clean, what is running water, what is sensation)
when i dream - what i dream


i am philosopher in spite
of the puppet show called
culture

with planned stages like chickens hatched
and farmed each with chicken myths repeated
endlessly


in spite of colonies
and atavistic loyalties
in spite of stoogism

and impenetrable hypocrisy


i philosophize in the thick
of being right

i philosophize as the world prays
to walls and rocks and books and bosses and fathers and feelings
and mom

i philosophize against
against
against


fight-flight, punishment-reward, pain-pleasure, plus-minus....

these are the politic of Braitenberg vehicles
no room for philosophers in there




what kind of philosopher MUST know these things - must know de-sade and sartre
and soren and witt (in words and in pictures) and buddha?


what kind of philosopher MUST know about everything
that changes everything?!


"my kind' ...





What a racket there is each time an ape presents.
Each symbolic ape pulling the lever that labels
the specimen correctly - each receiving their treat


What monotony! Lifting the cup to their lips with
the same expression each time
laughing at the same place each time
voiding in the same place
and calling it "being at home".


"the first technology fed on superstition and fear
- it was called religion
the second was based on religion, and power
-it was called empire
the third was based on stupidity and cowardice and empire
-it was called self-preservation


the fourth was based on empire and self-interest and deceit
-it was called the middle class
the fifth technology was based on everything else"













Monday, March 16, 2009

how many ways?

Wordle: bias


Off In the Distance

these big dreams...
that take so long to get to

promised to the young

given to the old

and in between

the "straight" line

straight past

this or that

that doesn't fit

straight off into the distance

---------------------------------

The First Tremolo

drawn by the sound
of beating wings
of
sound

stirred by the thing that makes
us children

they come running to the music
room
like tiny silver bells


Wordle: nyquist frequency of reality


You Will Not Be Played

not again, anyway