Commit 2a74a140 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

add universal complexity explorer

parent eb105ec7
......@@ -2,10 +2,11 @@ import random
import time
class Skython(object):
MAGIC_NUMBER = -12345
MAGIC_NUMBER = None #-12345
MAX_PROG_SIZE = 100
STEP_LIMIT = 250
SHRINK = True
TRIM = 15
INSTRUCTIONS = [
'LOCO_0', 'INS', 'LOCO_N', 'JGTE', 'SWAB',
......@@ -15,7 +16,7 @@ class Skython(object):
'PEEK', 'SKIP', 'LAND', 'LOR', 'IFDO',
'IFF', 'NOT', 'EQ', 'LOCO_1', 'MOD',
]
#ENABLED_FIB = [ 'SWAB', 'PUSH', 'INC', 'ADD', 'JNE', ]
ENABLED_FIB = [ 'SWAB', 'PUSH', 'INC', 'ADD', 'JNE', ]
ENABLED = [
'LOCO_0', 'INS', 'SWAB',
'SWAC', 'MULT', 'PUSH', 'POP', 'INC',
......@@ -24,7 +25,8 @@ class Skython(object):
'PEEK', 'SKIP', 'LAND', 'LOR', 'IFDO',
'IFF', 'NOT', 'EQ',
]
#ENABLED = ENABLED_FIB
ENABLED = ENABLED_FIB
#ENABLED = INSTRUCTIONS
OP_CODE = {}
ENABLED_CODES = []
......@@ -45,12 +47,16 @@ class Skython(object):
if output[i] == self.MAGIC_NUMBER:
for n in range(i, sp - 1):
output[n] = output[n + 1]
return output, sp - 1
return sp - 1
return sp
def run_program(self, prog, model_in, model_out, step_limit=STEP_LIMIT, trace=False):
def run_program(self, prog, model_in=None, model_out=None, step_limit=STEP_LIMIT, trace=False):
a = b = c = pc = step = la = lb = lc = lpc = lsp = 0
output = [self.MAGIC_NUMBER] + model_in[:] + [0] * (99 - len(model_in))
if model_in is None:
model_in = []
if model_out is None:
model_out = []
output = [self.MAGIC_NUMBER] + model_in[:] + [0] * (self.MAX_PROG_SIZE - 1 - len(model_in))
sp = len(model_in) + 1
if trace:
......@@ -75,7 +81,7 @@ class Skython(object):
for i in range(sp, 0, -1):
output[i] = output[i - 1]
output[0] = a
if sp < 99:
if sp < self.MAX_PROG_SIZE - 1:
sp += 1
elif op == 2: # LOCO_N
......@@ -95,10 +101,10 @@ class Skython(object):
elif op == 6: # MULT
a *= b
a %= 10000000
#a %= 10000000
elif op == 7: # PUSH
if sp < 99:
if sp < self.MAX_PROG_SIZE - 1:
output[sp] = a
sp += 1
......@@ -147,7 +153,7 @@ class Skython(object):
b, c = c, b
elif op == 20: # PEEK
if sp > 0:
if sp > 0 and output[sp - 1] != self.MAGIC_NUMBER:
b = output[sp - 1]
elif op == 21: # SKIP
......@@ -182,14 +188,14 @@ class Skython(object):
pc += 1
step += 1
if step >= 10 and la == a and lb == b and lc == c and lsp == sp and lpc == pc:
return [], 666
else:
la = a
lb = b
lc = c
lsp = sp
lpc = pc
# if step >= 10 and la == a and lb == b and lc == c and lsp == sp and lpc == pc:
# return [], 666
# else:
# la = a
# lb = b
# lc = c
# lsp = sp
# lpc = pc
if trace:
print "\t{}\t\t[{}\ta={}\tb={}\tc={}\tpc={}\tsp={}]\t\t(".format(self.INSTRUCTIONS[op], step, a, b, c, pc, sp),
......@@ -208,7 +214,42 @@ class Skython(object):
# cool = True
sp = self.remove_magic_number(output, sp)
return output, sp
if len(model_out) > 0:
return output, sp
else:
return output[:min(self.TRIM, len(output))], sp
def explore(self, iterations=1000000, bound=MAX_PROG_SIZE // 2, max_steps=50):
timeout = total = redundant = degen = 0
results = {}
for _ in xrange(iterations):
prog = self.make_program(bound)
total += 1
if total % (iterations/40) == 0:
print '.'
(output, r) = self.run_program(prog, step_limit=max_steps)
if r < 0:
redundant += 1
r = -r
continue
if r == 0:
timeout += 1
continue
if r == 666:
degen += 1
continue
s = ','.join([str(x) for x in output])
if s not in results:
results[s] = 0
results[s] += 1
return results
def run(self, bound=50):
timeout = total = redundant = degen = exact = 0
......@@ -296,4 +337,18 @@ class Skython(object):
break
if __name__ == '__main__':
Skython().run()
#Skython().run()
iterations = 10**6
skyt = Skython()
skyt.ENABLED_CODES = random.sample(range(30), random.randint(1, 30))
results = skyt.explore(iterations=iterations, max_steps=100, bound=20)
inverted = {}
for k, v in results.items():
if v not in inverted:
inverted[v] = []
inverted[v].append(k)
keys = sorted(inverted.keys(), reverse=False)
for k in keys:
print "{:.5%}\t{}\n\n".format(1.0 * k / iterations, '\t\t'.join(inverted[k]))
print len(keys), len(results), skyt.ENABLED_CODES
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment