Created
January 6, 2015 07:17
-
-
Save aldous-rey/9768b81c48a5404c0683 to your computer and use it in GitHub Desktop.
generate and interpret demonstration - python script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
f = open('generate-and-interpret-output.csv','w') | |
iterations = 10000 | |
n_min = 5.0 #min constraint on n | |
n_max = 95.0 #max constraint on n | |
operator = 0 #operator for generation | |
#0 = add | |
#1 = sub | |
#2 = multiply | |
#3 = divide | |
#Generate an artefact | |
def generate(): | |
global n_min,n_max,operator | |
n1 = randint(n_min,n_max) | |
n2 = randint(n_min,n_max) | |
artefact = 0.0 #initialise artefact of generation as a float | |
if operator == 0: | |
artefact = n1+n2 | |
elif operator == 1: | |
artefact = n1-n2 | |
elif operator == 2: | |
artefact = n1*n2 | |
elif operator == 3: | |
artefact = n1/n2 | |
f.write("generated," + str(artefact)+"\n") | |
return artefact | |
#interpret artefacts | |
def interpret(artefact,a1,a2): | |
global n_min,n_max,operator | |
#Check for arithmetic progression | |
ap1 = artefact - a1 | |
ap2 = a1 - a2 | |
if ap1 == ap2 and ap1 > 0: | |
operator = 0 #switch to addition, e.g. after 3,5,7 | |
f.write("changed operator, addition\n") | |
check_constraints(ap1) | |
if ap1 == ap2 and ap1 < 0: | |
operator = 1 #switch to subtraction, e.g. after 8,5,2 | |
f.write("changed operator, subtraction\n") | |
check_constraints(ap1) | |
gp1 = 0.0 #init geometric progression test val as float | |
gp2 = 0.0 #init geometric progression test val as float | |
if a1 == 0: | |
gp1 = 0.0 | |
else: | |
gp1 = float(artefact) / float(a1) | |
if a2 == 0: | |
gp2 = 0.0 | |
else: | |
gp2 = float(a1) / float(a2) | |
if gp1 == gp2 and gp1 > 1: | |
operator = 2 #switch to multiply. e.g. after 80,40,20 | |
f.write("changed operator, multiply" + str(artefact) + " " + str(a1) + " " +str(a2) + "\n") | |
check_constraints(gp1) | |
if gp1 == gp2 and gp1 < 1 and gp1>0: | |
operator = 3 #switch to divide. e.g. after 90,30,10 | |
f.write("changed operator, divide\n") | |
check_constraints(gp1) | |
def check_constraints(test_value): | |
global n_min,n_max | |
#Extend constraints if they've been pushed | |
if test_value < n_min: | |
n_min = test_value | |
f.write("changed constraints, downwards to"+str(test_value)+"\n") | |
if test_value > n_max: | |
n_max = test_value | |
f.write("changed constraints, upwards to"+str(test_value)+"\n") | |
#initialise the system | |
a2 = generate() | |
a1 = generate() | |
counter = 0 | |
#loop through generate-and-interpret as much as is needed | |
while counter < iterations: | |
artefact = generate() | |
interpret(artefact,a1,a2) | |
a2 = a1 | |
a1 = artefact | |
counter += 1 | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment