Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
machine_learning
skython
Commits
12ee5a72
Commit
12ee5a72
authored
Oct 25, 2016
by
Trevor Cappallo
Browse files
buncha changes
parent
79e777e4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
20 deletions
+70
-20
skython.py
skython.py
+70
-20
No files found.
skython.py
View file @
12ee5a72
...
...
@@ -11,12 +11,13 @@ import sys
import
random
import
time
import
pickle
from
argparse
import
ArgumentParser
import
re
class
Skython
(
object
):
MAGIC_NUMBER
=
None
#-12345
MAX_PROG_SIZE
=
100
STEP_LIMIT
=
25
0
STEP_LIMIT
=
100
0
SHRINK
=
True
TRIM
=
15
...
...
@@ -97,6 +98,7 @@ class Skython(object):
if
model_in
is
None
:
model_in
=
[]
sp
=
1
# output = [self.MAGIC_NUMBER] + model_in[:] + [0] * (self.MAX_PROG_SIZE - 1)
output
=
[
self
.
MAGIC_NUMBER
]
+
model_in
[:]
+
[
0
]
*
(
self
.
MAX_PROG_SIZE
-
1
)
else
:
sp
=
len
(
model_in
)
+
1
...
...
@@ -156,6 +158,13 @@ class Skython(object):
elif
op
==
7
:
# PUSH
if
sp
<
self
.
MAX_PROG_SIZE
-
1
:
# if sp >= len(output):
# output += [a]
# else:
# output[sp] = a
# sp += 1
# while sp >= len(output):
# output += [0]
output
[
sp
]
=
a
sp
+=
1
...
...
@@ -166,7 +175,8 @@ class Skython(object):
elif
sp
==
0
:
b
=
output
[
sp
]
if
b
==
self
.
MAGIC_NUMBER
:
pc
=
self
.
MAX_PROG_SIZE
# pc = self.MAX_PROG_SIZE
pc
=
len
(
prog
)
elif
op
==
9
:
# INC
a
+=
1
...
...
@@ -182,7 +192,7 @@ class Skython(object):
try
:
pc
=
output
[
sp
]
-
1
except
Exception
:
p
ass
p
c
=
-
1
elif
op
==
13
:
# NAND
a
=
0
if
a
&
b
else
1
...
...
@@ -253,13 +263,13 @@ class Skython(object):
if
sp
>=
b
:
sp
-=
b
if
sp
>=
len
(
output
):
output
.
append
(
0
)
#
output.append(0)
sp
=
len
(
output
)
elif
op
==
32
:
# RIGHT
sp
+=
b
sp
=
max
(
sp
,
0
)
if
sp
>=
len
(
output
):
output
.
append
(
0
)
#
output.append(0)
sp
=
len
(
output
)
elif
op
==
33
:
# JZ
if
b
==
0
:
...
...
@@ -292,7 +302,8 @@ class Skython(object):
if
step
>=
step_limit
:
#break
return
[],
666
sp
=
self
.
remove_magic_number
(
output
,
sp
)
return
output
,
666
# cool = False
# if sp >= 15 and output[0] != output[2] and output[3] != output[7] and output[8] != output[13]:
...
...
@@ -341,11 +352,11 @@ class Skython(object):
return
results
,
progs
def
run
(
self
,
model_out
,
model_in
=
None
,
bound
=
50
,
total_bound
=
3
*
10
**
6
):
def
run
(
self
,
model_out
,
model_in
=
None
,
bound
=
50
,
total_bound
=
3
*
10
**
6
,
jump
=
None
):
timeout
=
total
=
redundant
=
degen
=
0
exact
=
0
last_prog
=
None
lower_bound
,
upper_bound
=
9
,
2
0
lower_bound
,
upper_bound
=
5
,
2
5
if
model_in
is
None
:
model_in
=
[]
...
...
@@ -356,6 +367,10 @@ class Skython(object):
answers
=
pickle
.
load
(
f
)
with
open
(
'lengths.pickle'
,
'r'
)
as
f
:
shortest
=
pickle
.
load
(
f
)
with
open
(
'random_state.pickle'
,
'r'
)
as
f
:
random
.
setstate
(
pickle
.
load
(
f
))
if
jump
is
not
None
:
random
.
jumpahead
(
jump
)
while
True
:
prog
=
self
.
make_program
(
random
.
randint
(
lower_bound
,
upper_bound
))
...
...
@@ -366,6 +381,7 @@ class Skython(object):
total
+=
1
(
output
,
r
)
=
self
.
run_program
(
prog
,
model_in
,
model_out
)
if
r
<
0
:
redundant
+=
1
r
=
-
r
...
...
@@ -377,8 +393,28 @@ class Skython(object):
if
r
==
666
:
degen
+=
1
if
len
(
output
)
>=
10
and
output
[:
10
]
==
[
1
,
1
,
2
,
3
,
5
,
8
,
13
,
21
,
34
,
55
]:
with
open
(
'fibs.txt'
,
'a'
)
as
f
:
pretty_prog
=
"
\t
"
.
join
([
self
.
INSTRUCTIONS
[
op
]
for
op
in
prog
])
print
'fib
\t
'
+
pretty_prog
f
.
write
(
pretty_prog
+
'
\n
'
)
if
len
(
output
)
>=
5
and
output
[:
5
]
==
[
2
,
3
,
5
,
7
,
11
]:
with
open
(
'weak_primes.txt'
,
'a'
)
as
f
:
pretty_prog
=
"
\t
"
.
join
([
self
.
INSTRUCTIONS
[
op
]
for
op
in
prog
])
print
'weak
\t
'
+
pretty_prog
f
.
write
(
pretty_prog
+
'
\n
'
)
if
len
(
output
)
>=
10
and
output
[:
10
]
==
[
2
,
3
,
5
,
7
,
11
,
13
,
17
,
19
,
23
,
29
]:
with
open
(
'primes.txt'
,
'a'
)
as
f
:
pretty_prog
=
"
\t
"
.
join
([
self
.
INSTRUCTIONS
[
op
]
for
op
in
prog
])
print
'PRIMES
\t
'
+
pretty_prog
f
.
write
(
pretty_prog
+
'
\n
'
)
raise
RuntimeError
continue
if
exact
and
len
(
output
)
!=
1
:
continue
...
...
@@ -402,12 +438,12 @@ class Skython(object):
pickle
.
dump
(
answers
,
f
)
with
open
(
'lengths.pickle'
,
'w'
)
as
f
:
pickle
.
dump
(
shortest
,
f
)
with
open
(
'random_state.pickle'
,
'w'
)
as
f
:
pickle
.
dump
(
random
.
getstate
(),
f
)
def
main
(
self
):
def
main
(
self
,
jump
=
None
):
# EXPLORE = True
EXPLORE
=
False
global
thing
thing
+=
1
#thing = random.randint(1,100)
#print "thing =", thing
self
.
INSTRUCTIONS3
=
[
...
...
@@ -429,7 +465,7 @@ class Skython(object):
self
.
ENABLED_CODES
=
list
(
set
(
self
.
ENABLED_CODES
))
model_in
=
[]
model_out
=
[
thing
]
model_out
=
[]
#model_in = [1, 3, -5, 2, 20, 0, 6, 9, 7, -5]
#model_out = [4, -3, 20, 15, 2]
#model_out = [2, 15, 20, -3, 4]
...
...
@@ -507,13 +543,27 @@ class Skython(object):
#print [self.INSTRUCTIONS[x] for x in sorted(self.ENABLED_CODES)]
#self.run(model_out=[1,2,3,3,4,5], model_in=[4,3,1,3,5,2], bound=len(self.INSTRUCTIONS))
#self.run(model_out=[19,23,37,37,42], model_in=[23,37,42,37,19], bound=35)
self
.
run
(
model_out
=
model_out
,
model_in
=
model_in
,
bound
=
40
)
self
.
run
(
model_out
=
model_out
,
model_in
=
model_in
,
bound
=
40
,
jump
=
jump
)
def
str_to_num
(
self
,
prog
):
out
=
[]
for
instruction
in
re
.
split
(
'\s+'
,
prog
):
out
+=
[
sky
.
INSTRUCTIONS
.
index
(
instruction
)]
return
out
if
__name__
==
'__main__'
:
try
:
thing
=
int
(
sys
.
argv
[
1
])
-
1
except
Exception
:
pass
while
True
:
Skython
().
main
()
ap
=
ArgumentParser
()
ap
.
add_argument
(
'-p'
,
'--prog'
,
help
=
'trace the given program supplied as instructions'
)
options
=
ap
.
parse_args
()
sky
=
Skython
()
if
options
.
prog
:
sky
.
run_program
(
sky
.
str_to_num
(
options
.
prog
),
trace
=
True
)
else
:
# try:
# thing = int(sys.argv[1])
# except Exception:
thing
=
None
while
True
:
sky
.
main
(
jump
=
thing
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment