Using MOSES to evolve programs¶
This Jupyter notebook shows how MOSES can be used to evolve programs and mevis can be used to visualize the results. The .ipynb file can be found here.
Sources:
OpenCog Wiki
Python console: https://wiki.opencog.org/w/Python
GitHub
[1]:
from opencog.asmoses.pyasmoses import moses
import mevis as mv
[2]:
moses = moses()
Define training data¶
A list of input-output pairs
Input: first two columns
Output: last column
[3]:
input_data = [
[0, 0, 0],
[1, 1, 0],
[1, 0, 1],
[0, 1, 1],
]
print('Training data: {}'.format(input_data))
Training data: [[0, 0, 0], [1, 1, 0], [1, 0, 1], [0, 1, 1]]
Evolve Python programs¶
[4]:
output = moses.run(input=input_data, python=True)
[5]:
best_result = output[0]
program_text = best_result.program.decode()
print('Score:', best_result.score)
print('\nPython program:\n', program_text)
Score: 0.0
Python program:
#!/usr/bin/env python
#score: 0
import operator as op
from functools import reduce
from math import log, exp, sin
def l0(i): return 0 < i
def adds(*args): return sum(args)
def muls(*args): return reduce(op.mul, args)
def pdiv(a, b): return a / (b + 0.000001)
def impulse(a): return 1.0 if a else 0.0
def moses_eval(i):
return ((not(i[0]) or not(i[1])) and (i[0] or i[1]))
[6]:
model = output[0].eval
print('Testing model on data:')
print('[0, 0]: {}'.format(model([0, 0])))
print('[0, 1]: {}'.format(model([0, 1])))
print('[1, 0]: {}'.format(model([1, 0])))
print('[1, 1]: {}'.format(model([1, 1])))
Testing model on data:
[0, 0]: 0
[0, 1]: 1
[1, 0]: 1
[1, 1]: False
Evolve Scheme programs¶
[7]:
output = moses.run(input=input_data, scheme=True)
[8]:
best_result = output[0]
program_text = best_result.program.decode()
print('Score:', best_result.score)
print('\nScheme program:\n', program_text)
Score: 0
Scheme program:
(AndLink (OrLink (NotLink (PredicateNode "$1")) (NotLink (PredicateNode "$2"))) (OrLink (PredicateNode "$1") (PredicateNode "$2")))
Evolve a complex Scheme program and put it into an AtomSpace¶
[9]:
from opencog.atomspace import AtomSpace
from opencog.scheme import scheme_eval
from opencog.utilities import set_default_atomspace
[10]:
input_data = [
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0],
[0, 0, 1, 1, 0, 1],
[0, 0, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 1, 1],
[0, 1, 1, 0, 0, 1],
[0, 1, 1, 0, 1, 1],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 0],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 0, 0],
[1, 0, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 0],
[1, 0, 1, 0, 1, 1],
[1, 0, 1, 1, 0, 0],
[1, 0, 1, 1, 1, 0],
[1, 1, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 1],
[1, 1, 0, 1, 0, 0],
[1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 0],
]
output = moses.run(input=input_data, scheme=True)
best_result = output[0]
score = best_result.score
program_text = best_result.program.decode()
print('Score:', best_result.score)
print('\nPython program:\n', program_text)
Score: -8
Python program:
(OrLink (AndLink (OrLink (AndLink (NotLink (PredicateNode "$3")) (PredicateNode "$4")) (NotLink (PredicateNode "$2"))) (PredicateNode "$5")) (AndLink (OrLink (PredicateNode "$1") (NotLink (PredicateNode "$5"))) (PredicateNode "$2") (PredicateNode "$3")) (AndLink (PredicateNode "$2") (NotLink (PredicateNode "$4")) (NotLink (PredicateNode "$5"))))
[11]:
atomspace = AtomSpace()
set_default_atomspace(atomspace)
string = scheme_eval(atomspace, program_text)
print(string.decode())
(OrLink
(AndLink
(PredicateNode "$3")
(PredicateNode "$2")
(OrLink
(PredicateNode "$1")
(NotLink
(PredicateNode "$5"))))
(AndLink
(PredicateNode "$2")
(NotLink
(PredicateNode "$4"))
(NotLink
(PredicateNode "$5")))
(AndLink
(PredicateNode "$5")
(OrLink
(AndLink
(PredicateNode "$4")
(NotLink
(PredicateNode "$3")))
(NotLink
(PredicateNode "$2")))))
[12]:
mv.plot(atomspace, 'vis', 'dot')
[12]:
Details for selected element
[13]:
mv.store(atomspace, 'moses_result.scm', overwrite=True)
graph = mv.convert(atomspace)
mv.export(graph, 'moses_result.gml', overwrite=True)