Spaces:
Sleeping
Sleeping
File size: 3,218 Bytes
ecbc9c7 3f02d46 ecbc9c7 3f02d46 a417ea3 389a372 ecbc9c7 3f02d46 a417ea3 3f02d46 a417ea3 3f02d46 389a372 3f02d46 a417ea3 3f02d46 a417ea3 3f02d46 a417ea3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
'''
BOTTOM UP ENUMERATIVE SYNTHESIS
Ayush Noori
CS252R, Fall 2020
Example of usage:
python synthesizer.py --domain arithmetic --examples addition
'''
# load libraries
import numpy as np
import argparse
# import examples
from arithmetic import *
from examples import example_set, check_examples
import config
# PARSE ARGUMENTS
def parse_args():
'''
Parse command line arguments.
'''
parser = argparse.ArgumentParser(description="Bottom-up enumerative synthesis in Python.")
# define valid choices for the 'domain' argument
valid_domain_choices = ["arithmetic", "string"]
# add examples
parser.add_argument('--domain', type=str, required=True, # default="arithmetic",
choices=valid_domain_choices,
help='Domain of synthesis (either "arithmetic" or "string").')
parser.add_argument('--examples', dest='examples_key', type=str, required=True, # default="addition",
choices=example_set.keys(),
help='Examples to synthesize program from. Must be a valid key in the "example_set" dictionary.')
parser.add_argument('--max_weight', type=int, required=False, default=3,
help='Maximum weight of programs to consider before terminating search.')
args = parser.parse_args()
return args
# EXTRACT CONSTANTS AND VARIABLES
def extract_constants(examples):
'''
Extracts the constants from the input-output examples. Also constructs variables as needed
based on the input-output examples, and adds them to the list of constants.
'''
# check validity of provided examples
# if valid, extract arity and argument types
arity, arg_types = check_examples(examples)
# initialize list of constants
constants = []
# get unique set of inputs
inputs = [input for example in examples for input in example[0]]
inputs = set(inputs)
# add 1 to the set of inputs
inputs.add(1)
# extract constants in input
for input in inputs:
if type(input) == int:
constants.append(IntegerConstant(input))
elif type(input) == str:
# constants.append(StringConstant(input))
pass
else:
raise Exception("Input of unknown type.")
# initialize list of variables
variables = []
# extract variables in input
for position, arg in enumerate(arg_types):
if arg == int:
variables.append(IntegerVariable(position))
elif arg == str:
# variables.append(StringVariable(position))
pass
else:
raise Exception("Input of unknown type.")
return constants + variables
# RUN SYNTHESIZER
def run_synthesizer(args):
'''
Run bottom-up enumerative synthesis.
'''
# retrieve selected input-output examples
examples = example_set[args.examples_key]
# extract constants from examples
program_bank = extract_constants(examples)
print(examples)
pass
if __name__ == '__main__':
# parse command line arguments
args = parse_args()
# print(args)
# run bottom-up enumerative synthesis
run_synthesizer(args)
|