This repository has an implementation of Douglas Hofstadter's copycat algorithm in Python.
It is an analogising algorithm, 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 by other Python packages.
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".
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 pqr
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 10
means "try 10 solutions"- 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
$ 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:
PQQRRRR
2 times at 17˚ in 0.7 secondsPQQRRS
5 times at 17˚ in 0.8 secondsPQQSSS
3 times at 24˚ in 1.3 seconds
From that one might say "copycat liked PQQRRRR
best" because:
- The average temperature is lowest (
17˚
looks like the "most obvious" solution) - The average time is lowest (
0.7 seconds
looks like it did the least work)
On the other hand PQQRRS
might be considered "best" because it was the accepted solution most often: 5 times.
For me: PQQRRRR
is the "best" solution, because increasing the number of Q
s 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".
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}}
A big "Thank You" for
- @Quuxplusone for reducing spew
- @jtauber for cleaning up
- @Alex-Linhares
- @ayberkt
- @dproske
- @erkapilmehta
- @horacio
- @josephfosco
- @jtauber
- @OrmesKD
- @Quuxplusone
- @qython
- @sjolicoeur
- @skaslev
You geeks make it all so worthwhile.
Readers who got this far will definitely enjoy analogising this project with @Alex-Linhares's collection of FARGonautica, computational architectures that have been developed from Douglas Hofstadter's "FARGonautica" group's research in "[Fluid Concepts & Creative Analogies](https://archive.org/details/fluidconceptscre0000hofs_w7o9/page/n5/mode/2uphttps://archive.org/details/fluidconceptscre0000hofs_w7o9/page/n5/mode/2up".
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.
Programmers are machines for turning coffee into code
- "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.