Skip to content

Instantly share code, notes, and snippets.

@evanthebouncy
Created August 15, 2022 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanthebouncy/ffa855eac2caa38716b3bc8d8b62645a to your computer and use it in GitHub Desktop.
Save evanthebouncy/ffa855eac2caa38716b3bc8d8b62645a to your computer and use it in GitHub Desktop.
rectangle synthesis
from rectangle import is_inside, is_correct, inside, outside, W
import random
def random_writer(spec):
# ignores the spec
T, D, L, R = random.randint(0,W), random.randint(0,W), random.randint(0,W), random.randint(0,W)
return [T, D, L, R]
def better_writer(spec):
# get the coordinates of spec that are inside
inside_coords = [coord for coord,bool in spec if bool]
if inside_coords == []:
# if there are no inside coordinates, default to a random
return random_writer(spec)
# otherwise, use the inside coords to suggest parameters of the rectangle
row_coords = [coord[0] for coord in inside_coords]
col_coords = [coord[1] for coord in inside_coords]
T, D = random.choice(row_coords), random.choice(row_coords)
L, R = random.choice(col_coords), random.choice(col_coords)
return [T, D, L, R]
def program_cheker(prog, spec):
return is_correct(prog, spec)
# a synthesizer that returns both a working program
# and the number of samples it took to find it
def get_synthesizer(writer, checker, budget):
def synthesizer(spec):
prog = writer(spec)
for i in range(budget):
prog = writer(spec)
if checker(prog, spec):
return (i, prog)
return None
return synthesizer
if __name__ == '__main__':
synthesizer = get_synthesizer(random_writer, program_cheker, 1000)
spec1 = [( (0,4), outside), ( (4,1), outside), ( (1,1), inside), ( (3,3), inside)]
n_tries, prog = synthesizer(spec1)
print (n_tries, prog)
synthesizer2 = get_synthesizer(better_writer, program_cheker, 1000)
n_tries, prog = synthesizer2(spec1)
print (n_tries, prog)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment