Commit 3a9de335 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

first pass at identifying useful instructions

parent 2633c47b
......@@ -10,7 +10,7 @@ import random
import time
EXPLORE = False
EXPLORE = True
class Skython(object):
MAGIC_NUMBER = None #-12345
......@@ -35,6 +35,14 @@ class Skython(object):
'SKIP', 'LAND', 'LOR', 'IFDO',
#'IFF',
]
INSTRUCTIONS3 = [
'LOCO_0', 'INS', 'LOCO_N', 'SWAB',
'SWAC', 'MULT', 'PUSH', 'INC',
'ADD', 'JNE', 'NAND', 'DIV',
'NEG', 'XOR', 'LOCO_2', 'DEC', 'SWBC',
'PEEK', 'LAND', 'LOR', 'IFDO',
'IFF', 'NOT', 'EQ', 'LOCO_1', 'MOD',
]
ENABLED_FIB = [ 'SWAB', 'PUSH', 'INC', 'ADD', 'JNE', ]
ENABLED = [
'LOCO_0', 'INS', 'SWAB',
......@@ -180,8 +188,8 @@ class Skython(object):
elif op == 20: # PEEK
if sp > 0 and output[sp - 1] != self.MAGIC_NUMBER:
b = output[sp - 1]
else:
pc = self.MAX_PROG_SIZE
#else:
# pc = self.MAX_PROG_SIZE
elif op == 21: # SKIP
pc *= 2
......@@ -249,6 +257,7 @@ class Skython(object):
def explore(self, iterations=1000000, bound=MAX_PROG_SIZE // 2, max_steps=50):
timeout = total = redundant = degen = 0
results = {}
progs = {}
for _ in xrange(iterations):
prog = self.make_program(bound)
......@@ -274,9 +283,12 @@ class Skython(object):
s = ','.join([str(x) for x in output])
if s not in results:
results[s] = 0
progs[s] = [0] * 30
results[s] += 1
for op in prog:
progs[s][op] += 1
return results
return results, progs
def run(self, model_out, model_in=None, bound=50):
timeout = total = redundant = degen = 0
......@@ -371,11 +383,12 @@ class Skython(object):
if __name__ == '__main__':
skyt = Skython()
if EXPLORE:
iterations = 10**9
skyt.ENABLED_CODES = [skyt.OP_CODE[x] for x in skyt.INSTRUCTIONS2] # random.sample(range(30), random.randint(12, 26))
skyt.TRIM = 25
results = skyt.explore(iterations=iterations, max_steps=300, bound=50)
iterations = 10**6
skyt.ENABLED_CODES = [skyt.OP_CODE[x] for x in skyt.INSTRUCTIONS3] # random.sample(range(30), random.randint(12, 26))
skyt.TRIM = 16
results, progs = skyt.explore(iterations=iterations, max_steps=256, bound=32)
inverted = {}
q = []
for k, v in results.items():
if v not in inverted:
inverted[v] = []
......@@ -384,6 +397,19 @@ if __name__ == '__main__':
for k in keys:
print "{:.5%}\n\t{}\n\n".format(1.0 * k / iterations, '\n\t'.join(inverted[k]))
print len(keys), len(results), [skyt.INSTRUCTIONS[x] for x in sorted(skyt.ENABLED_CODES)]
for i in range(30):
n = 0
sum_percent = 0.0
for k in keys:
for pat in inverted[k]:
n += progs[pat][i] #progs[inverted[k]][i]
#sum_percent += progs[inverted[k]][i] * (1.0 * k / iterations)
sum_percent += progs[pat][i] * (1.0 * k / iterations)
if n:
q += [(sum_percent / n, "{:02} {:<6}: {:6.4%} (based on {})".format(
i, skyt.INSTRUCTIONS[i], sum_percent / n, n))]
for p, r in sorted(q):
print r
else:
print [skyt.INSTRUCTIONS[x] for x in sorted(skyt.ENABLED_CODES)]
#skyt.run(model_out=[1,2,3,3,4,5], model_in=[4,3,1,3,5,2], bound=30)
......
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