co.py.cat
This repository hols an implementation of Douglas Hofstadter's copycat algorithm in Python.
It is an alogising algortithm, and explained on Wikipedia. It is the foundation of a group of similar algorithms metacat, musicat, seqsee, ....
This implementation is a copycat of Scott Boland's Java implementation, but re-written into Python3. It's not a direct translation, more like "based on a true story". I did not carry over the GUI, as GUIs restrict the platform too much, and thought that this code could more usefully be imported to other Python scripts.
In cases where I could not grok the Java implementation easily I took ideas from the LISP implementation, or directly from Melanie Mitchell's "Analogy-Making as Perception"
I keep trying to make the code "more pythonic".
Installation
There are no particular installation instructions, just clone and run, e.g.
$ git clone https://github.com/jalanb/co.py.cat.git
$ cd co.py.cat
$ python3 -m copycat abc abd ijkRunning
The program takes two arguments
- A pair of words with some change
- for example "ABC ABD".
- A word to be changed
- for example "PQR"
And one option
- How many solutions to evaluate
- for example
--solutions 10means "try 10 solutions"- if it's missing then the default is "try 1 solution"
- for example
So, to find a single x for the analogy ABC:ABD::PQR:x
$ python3 -m copycat ABC ABD PQR
PQS: 1 (average time 0.3 seconds, average temperature 14.43)Evaluating more solutions can produce output like
$ python3 -m copycat ABC ABD PQQRRR --solutions 10
PQQRRRR: 2 (average time 0.65 seconds, average temperature 17.1)
PQQRRS: 5 (average time 0.82 seconds, average temperature 17.38)
PQQSSS: 3 (average time 1.29 seconds, average temperature 24.83)In that run the program considered three solutions:
PQQRRRR2 timesPQQRRS5 timesPQQSSS3 times
From that one might say "copycat liked PQQRRRR best" because:
- The average temperature is lowest (looks like the "most obvious" solution)
- The average time is also lowest (looks like it did the least work)
On the other hand PQQRRS might be considered best because:
- it was the accepted solution most often.
For me: PQQRRRR is the "best" solution, because increasing the number of Qs from 3 to 4 is the "best" analogy to increasing the 3rd letter (C) to the 4th letter (D). However the other 2 answers also seem "quite good" because PQQRRS is the most direct (just increase the last letter), and PQQSSS is also fairly direct (increase letter in the last group).
This example output emphasizes that Copycat is not intended to produce a single definitive solution to an analogy problem, but to explore solutions, and try different strategies to find them. It also shows the different "levels" that Copycat considered in this problem: changing letters, or groups of letters, or both.
Note that there is no "correct" solution to an analogy problem - there are arguments to be made for each of PQQRRRR, PQQRRS and PQQSSS above, and only one's own preferences can decide that one of them is "best".
Importing
The program can also be imported and run from within Python, e.g.
>>> from copycat import copycat
>>> answers = copycat.run("abc", "abd", "pqqrrr", 10)
>>> print(answers)
{'pqqrrrr': {'avgtemp': 18.320790853668182, 'avgtime': 759.0, 'count': 1},
'pqqrrs': {'avgtemp': 38.58653638621074, 'avgtime': 1294.1666666666667, 'count': 6},
'pqqsss': {'avgtemp': 37.86964564086443, 'avgtime': 1642.6666666666667, 'count': 3}}Thanks
A big "Thank You" for
Curation
Contributions
- @Quuxplusone for reducing spew
- @jtauber for cleaning up
Forks
- @Alex-Linhares
- @ayberkt
- @dproske
- @erkapilmehta
- @horacio
- @josephfosco
- @jtauber
- @OrmesKD
- @Quuxplusone
- @qython
- @sjolicoeur
- @skaslev
You geeks make it all so worthwhile.
Links
Readers who got this far will definitely enjoy analogising this project with @Alex-Linhares's collection of FARGonautica, a collection of computational architectures that have been developed frm Douglas Hofstadter's group's research in Fluid Concepts & Creative Analogies.
They've got Lisp (lotsa (Lisp)), Python, C++, Java, even Perl. If you know one of those languages, then you too can be a FARGonaut.
See Also
- "The Copycat Project: An Experiment in Nondeterminism and Creative Analogies" by Hofstadter, Douglas
- "Analogy-Making as Perception" by Mitchell, Melanie
- Arthur O'Dwyer (Quuxplusone on GitHub) has further cleaned and extended this code (including a GUI) in a fork available here.