def load(self, model): self.idx = model['idx'] self.solved = model['solved'] self.likelihood = model['likelihood'] self.total_examples = model['total_examples'] self.arity = model['arity'] self.program = None if model['program'] is None else ProgramWrapper( Program.parse(model['program']))
def manualLogoTask(name, expression, proto=False, needToTrain=False, supervise=False, lambdaCalculus=False): p = Program.parse(expression) if lambdaCalculus else parseLogo(expression) from dreamcoder.domains.logo.logoPrimitives import primitives from dreamcoder.grammar import Grammar g = Grammar.uniform(primitives, continuationType=turtle) gp = Grammar.uniform(primitives) try: l = g.logLikelihood(arrow(turtle, turtle), p) lp = gp.logLikelihood(arrow(turtle, turtle), p) assert l >= lp eprint(name, -l, "nats") except: eprint("WARNING: could not calculate likelihood of manual logo", p) attempts = 0 while True: [output, highresolution] = drawLogo(p, p, resolution=[28, 128], cost=True) if output == "timeout" or highresolution == "timeout": attempts += 1 else: break if attempts > 0: eprint( f"WARNING: Took {attempts} attempts to render task {name} within timeout" ) cost = output[1] output = output[0] assert highresolution[1] == cost highresolution = highresolution[0] shape = list(map(int, output)) highresolution = list(map(float, highresolution)) t = Task(name, arrow(turtle, turtle), [(([0]), shape)]) t.mustTrain = needToTrain t.proto = proto t.specialTask = ("LOGO", {"proto": proto}) t.specialTask[1]["cost"] = cost * 1.05 t.highresolution = highresolution if supervise: t.supervisedSolution = p return t
def get(): results = [p.get() for p in promises] frontiers = [] with timing("(Helmholtz enumeration) Decoded json into frontiers"): for request, result in zip(requests, results): response = json.loads(result.decode("utf-8")) for b, entry in enumerate(response): frontiers.append( Frontier([ FrontierEntry(program=Program.parse(p), logPrior=entry["ll"], logLikelihood=0.) for p in entry["programs"] ], task=Task(str(b), request, []))) eprint("Total number of Helmholtz frontiers:", len(frontiers)) return frontiers
def expression(e, environment): for n, v in enumerate(environment): if e == v: return Index(n) if isinstance(e, int): return Program.parse(str(e)) mapping = { "1a": _ua, "1d": _ul, "1l": _ul, "0a": _za, "0d": _zl, "0l": _zl, "/a": _da, "/l": _dl, "/d": _dl, "*a": _ma, "*l": _ml, "*d": _ml, "+a": _aa, "+d": _al, "+l": _al, "-a": _sa, "-d": _sl, "-l": _sl, "+": _addition, "infinity": _infinity, "epsilonAngle": _ea, "epsilonDistance": _el, "epsilonLength": _el } if e == float('inf'): return _infinity for name, value in mapping.items(): if e == Symbol(name): return value assert isinstance(e, list), "not a list %s" % e for name, value in mapping.items(): if e[0] == Symbol(name): f = value for argument in e[1:]: f = Application(f, expression(argument, environment)) return f assert False
def parseLogo(s): _ua = Program.parse("logo_UA") _ul = Program.parse("logo_UL") _za = Program.parse("logo_ZA") _zl = Program.parse("logo_ZL") _da = Program.parse("logo_DIVA") _ma = Program.parse("logo_MULA") _dl = Program.parse("logo_DIVL") _ml = Program.parse("logo_MULL") _aa = Program.parse("logo_ADDA") _sa = Program.parse("logo_SUBA") _al = None #Program.parse("logo_ADDL") _sl = None #Program.parse("logo_SUBL") _pu = None #Program.parse("logo_PU") _pd = None #Program.parse("logo_PD") _p = Program.parse("logo_PT") _move = Program.parse("logo_FWRT") _embed = Program.parse("logo_GETSET") _addition = Program.parse("+") _infinity = Program.parse("logo_IFTY") _ea = Program.parse("logo_epsA") _el = Program.parse("logo_epsL") _loop = Program.parse("logo_forLoop") from sexpdata import loads, Symbol s = loads(s) def command(k, environment, continuation): assert isinstance(k, list) if k[0] == Symbol("move"): return Application( Application(Application(_move, expression(k[1], environment)), expression(k[2], environment)), continuation) if k[0] == Symbol("for") or k[0] == Symbol("loop"): v = k[1] b = expression(k[2], environment) newEnvironment = [None, v] + environment body = block(k[3:], newEnvironment, Index(0)) return Application( Application(Application(_loop, b), Abstraction(Abstraction(body))), continuation) if k[0] == Symbol("embed"): body = block(k[1:], [None] + environment, Index(0)) return Application(Application(_embed, Abstraction(body)), continuation) if k[0] == Symbol("p"): body = block(k[1:], [None] + environment, Index(0)) return Application(Application(_p, Abstraction(body)), continuation) assert False def expression(e, environment): for n, v in enumerate(environment): if e == v: return Index(n) if isinstance(e, int): return Program.parse(str(e)) mapping = { "1a": _ua, "1d": _ul, "1l": _ul, "0a": _za, "0d": _zl, "0l": _zl, "/a": _da, "/l": _dl, "/d": _dl, "*a": _ma, "*l": _ml, "*d": _ml, "+a": _aa, "+d": _al, "+l": _al, "-a": _sa, "-d": _sl, "-l": _sl, "+": _addition, "infinity": _infinity, "epsilonAngle": _ea, "epsilonDistance": _el, "epsilonLength": _el } if e == float('inf'): return _infinity for name, value in mapping.items(): if e == Symbol(name): return value assert isinstance(e, list), "not a list %s" % e for name, value in mapping.items(): if e[0] == Symbol(name): f = value for argument in e[1:]: f = Application(f, expression(argument, environment)) return f assert False def block(b, environment, continuation): if len(b) == 0: return continuation return command(b[0], environment, block(b[1:], environment, continuation)) try: return Abstraction(command(s, [], Index(0))) except: return Abstraction(block(s, [], Index(0)))
'(map _ $x)', '(mapi _ $x)', '(flatten $x)', '(map _ (zip (droplast 1 $x) (drop 1 $x)))', '(map _ (drop 1 $x))' ], lambda e: e == '$l') ]) ## features computed in 'predict()' in bin/list_routines_misc.py Primitive.GLOBALS.clear() Grammar.uniform(list_routines_misc.primitives()) pre_features = { 'program_length': lambda _, __, ___, p: Program.parse(p).size(), 'depth': lambda _, __, ___, p: Program.parse(p).depth(), 'apps': lambda _, __, ___, p: list_routines_misc.count_applications(Program.parse(p)) } ## miscellaneous features misc_features = {} concept_examples = {'model': {}, 'dataset': {}} for purpose, num in [('model', 100), ('dataset', 150)]: concepts = os.listdir('analysis/concept_examples/{}'.format(purpose)) for concept in map(lambda n: 'c{:03}'.format(n), range(1, num+1)): with open('analysis/concept_examples/{}/{}'.format( purpose,
Primitive("gt?", arrow(tint, tint, tbool), _gt), Primitive("if", arrow(tbool, t0, t0, t0), _if), Primitive("eq?", arrow(tint, tint, tbool), _eq), Primitive("+", arrow(tint, tint, tint), _addition), Primitive("-", arrow(tint, tint, tint), _subtraction), ] + [Primitive(str(j), tint, j) for j in range(2)] if __name__ == "__main__": bootstrapTarget() g = Grammar.uniform(McCarthyPrimitives()) # with open("/home/ellisk/om/ec/experimentOutputs/list_aic=1.0_arity=3_ET=1800_expandFrontier=2.0_it=4_likelihoodModel=all-or-nothing_MF=5_baseline=False_pc=10.0_L=1.0_K=5_rec=False.pickle", "rb") as handle: # b = pickle.load(handle).grammars[-1] # print b p = Program.parse( "(lambda (lambda (lambda (if (empty? $0) empty (cons (+ (car $1) (car $0)) ($2 (cdr $1) (cdr $0)))))))") t = arrow(tlist(tint), tlist(tint), tlist(tint)) # ,tlist(tbool)) print(g.logLikelihood(arrow(t, t), p)) assert False print(b.logLikelihood(arrow(t, t), p)) # p = Program.parse("""(lambda (lambda # (unfold 0 # (lambda (+ (index $0 $2) (index $0 $1))) # (lambda (1+ $0)) # (lambda (eq? $0 (length $1)))))) # """) p = Program.parse("""(lambda (lambda (map (lambda (+ (index $0 $2) (index $0 $1))) (range (length $0)) )))""") # .replace("unfold", "#(lambda (lambda (lambda (lambda (fix1 $0 (lambda (lambda (#(lambda (lambda (lambda (if $0 empty (cons $1 $2))))) ($1 ($3 $0)) ($4 $0) ($5 $0)))))))))").\ # replace("length", "#(lambda (fix1 $0 (lambda (lambda (if (empty? $0) 0 (+ ($1 (cdr $0)) 1))))))").\
for char, name in disallowed ] + [ Primitive("r_dot", tpregex, emp_dot_no_letter(corpus)), Primitive("r_d", tpregex, emp_d(corpus)), Primitive("r_s", tpregex, pregex.s), Primitive("r_kleene", arrow(tpregex, tpregex), _kleene), #Primitive("r_plus", arrow(tpregex, tpregex), _plus), #Primitive("r_maybe", arrow(tpregex, tpregex), _maybe), Primitive("r_alt", arrow(tpregex, tpregex, tpregex), _alt), Primitive("r_concat", arrow(tpregex, tpregex, tpregex), _concat), ] if __name__ == '__main__': concatPrimitives() from dreamcoder.program import Program p = Program.parse( "(lambda (r_kleene (lambda (r_maybe (lambda (string_x $0)) $0)) $0))") print(p) print(p.runWithArguments([pregex.String("")])) prims = concatPrimitives() g = Grammar.uniform(prims) for i in range(100): prog = g.sample(arrow(tpregex, tpregex)) preg = prog.runWithArguments([pregex.String("")]) print("preg:", preg.__repr__()) print("sample:", preg.sample())
def main(args): """ Takes the return value of the `commandlineArguments()` function as input and trains/tests the model on LOGO tasks. """ # The below legacy global statement is required since prefix_dreams is used by LogoFeatureCNN. # TODO(lcary): use argument passing instead of global variables. global prefix_dreams # The below global statement is required since primitives is modified within main(). # TODO(lcary): use a function call to retrieve and declare primitives instead. global primitives visualizeCheckpoint = args.pop("visualize") if visualizeCheckpoint is not None: with open(visualizeCheckpoint, 'rb') as handle: primitives = pickle.load(handle).grammars[-1].primitives visualizePrimitives(primitives) sys.exit(0) dreamCheckpoint = args.pop("dreamCheckpoint") dreamDirectory = args.pop("dreamDirectory") proto = args.pop("proto") if dreamCheckpoint is not None: #outputDreams(dreamCheckpoint, dreamDirectory) enumerateDreams(dreamCheckpoint, dreamDirectory) sys.exit(0) animateCheckpoint = args.pop("animate") if animateCheckpoint is not None: animateSolutions(loadPickle(animateCheckpoint).allFrontiers) sys.exit(0) target = args.pop("target") red = args.pop("reduce") save = args.pop("save") prefix = args.pop("prefix") prefix_dreams = prefix + "/dreams/" + ('_'.join(target)) + "/" prefix_pickles = prefix + "/logo." + ('.'.join(target)) if not os.path.exists(prefix_dreams): os.makedirs(prefix_dreams) tasks = makeTasks(target, proto) eprint("Generated", len(tasks), "tasks") costMatters = args.pop("cost") for t in tasks: t.specialTask[1]["costMatters"] = costMatters # disgusting hack - include whether cost matters in the dummy input if costMatters: t.examples = [(([1]), t.examples[0][1])] os.chdir("prototypical-networks") subprocess.Popen(["python", "./protonet_server.py"]) time.sleep(3) os.chdir("..") test, train = testTrainSplit(tasks, args.pop("split")) eprint("Split tasks into %d/%d test/train" % (len(test), len(train))) try: if test: montageTasks(test, "test_") montageTasks(train, "train_") except: eprint( "WARNING: couldn't generate montage. Do you have an old version of scipy?" ) if red is not []: for reducing in red: try: with open(reducing, 'r') as f: prods = json.load(f) for e in prods: e = Program.parse(e) if e.isInvented: primitives.append(e) except EOFError: eprint("Couldn't grab frontier from " + reducing) except IOError: eprint("Couldn't grab frontier from " + reducing) except json.decoder.JSONDecodeError: eprint("Couldn't grab frontier from " + reducing) primitives = list(OrderedDict((x, True) for x in primitives).keys()) baseGrammar = Grammar.uniform(primitives, continuationType=turtle) eprint(baseGrammar) timestamp = datetime.datetime.now().isoformat() outputDirectory = "experimentOutputs/logo/%s" % timestamp os.system("mkdir -p %s" % outputDirectory) generator = ecIterator(baseGrammar, train, testingTasks=test, outputPrefix="%s/logo" % outputDirectory, evaluationTimeout=0.01, **args) r = None for result in generator: iteration = len(result.learningCurve) dreamDirectory = "%s/dreams_%d" % (outputDirectory, iteration) os.system("mkdir -p %s" % dreamDirectory) eprint("Dreaming into directory", dreamDirectory) dreamFromGrammar(result.grammars[-1], dreamDirectory) r = result needsExport = [ str(z) for _, _, z in r.grammars[-1].productions if z.isInvented ] if save is not None: with open(save, 'w') as f: json.dump(needsExport, f)
def visualizePrimitives(primitives, export='/tmp/logo_primitives.png'): from itertools import product from dreamcoder.program import Index, Abstraction, Application from dreamcoder.utilities import montageMatrix, makeNiceArray from dreamcoder.type import tint import scipy.misc from dreamcoder.domains.logo.makeLogoTasks import parseLogo angles = [ Program.parse(a) for a in [ "logo_ZA", "logo_epsA", "(logo_MULA logo_epsA 2)", "(logo_DIVA logo_UA 4)", "(logo_DIVA logo_UA 5)", "(logo_DIVA logo_UA 7)", "(logo_DIVA logo_UA 9)", ] ] specialAngles = { "#(lambda (lambda (logo_forLoop logo_IFTY (lambda (lambda (logo_FWRT (logo_MULL logo_UL 3) (logo_MULA $2 4) $0))) $1)))": [Program.parse("(logo_MULA logo_epsA 4)")] + [Program.parse("(logo_DIVA logo_UA %d)" % n) for n in [7, 9]] } numbers = [Program.parse(n) for n in ["1", "2", "5", "7", "logo_IFTY"]] specialNumbers = { "#(lambda (#(lambda (lambda (lambda (lambda (logo_forLoop $2 (lambda (lambda (logo_FWRT $5 (logo_DIVA logo_UA $3) $0))) $0))))) (logo_MULL logo_UL $0) 4 4))": [Program.parse(str(n)) for n in [1, 2, 3]] } distances = [ Program.parse(l) for l in [ "logo_ZL", "logo_epsL", "(logo_MULL logo_epsL 2)", "(logo_DIVL logo_UL 2)", "logo_UL" ] ] subprograms = [ parseLogo(sp) for sp in [ "(move 1d 0a)", "(loop i infinity (move (*l epsilonLength 4) (*a epsilonAngle 2)))", "(loop i infinity (move (*l epsilonLength 5) (/a epsilonAngle 2)))", "(loop i 4 (move 1d (/a 1a 4)))" ] ] entireArguments = { "#(lambda (lambda (#(#(lambda (lambda (lambda (logo_forLoop $2 (lambda (lambda (logo_FWRT $2 $3 $0))))))) logo_IFTY) (logo_MULA (#(logo_DIVA logo_UA) $1) $0) (#(logo_MULL logo_UL) 3))))": [[Program.parse(str(x)) for x in xs] for xs in [("3", "1", "$0"), ("4", "1", "$0"), ("5", "1", "$0"), ("5", "3", "$0"), ("7", "3", "$0")] ] } specialDistances = { "#(lambda (lambda (logo_forLoop 7 (lambda (lambda (#(lambda (lambda (lambda (#(lambda (lambda (lambda (logo_forLoop $2 (lambda (lambda (logo_FWRT $2 $3 $0))))))) 7 $1 $2 $0)))) $3 logo_epsA $0))) $0)))": [Program.parse("(logo_MULL logo_epsL %d)" % n) for n in range(5)] } matrix = [] for p in primitives: if not p.isInvented: continue t = p.tp eprint(p, ":", p.tp) if t.returns() != turtle: eprint("\t(does not return a turtle)") continue def argumentChoices(t): if t == turtle: return [Index(0)] elif t == arrow(turtle, turtle): return subprograms elif t == tint: return specialNumbers.get(str(p), numbers) elif t == tangle: return specialAngles.get(str(p), angles) elif t == tlength: return specialDistances.get(str(p), distances) else: return [] ts = [] for arguments in entireArguments.get( str(p), product(*[argumentChoices(t) for t in t.functionArguments()])): eprint(arguments) pp = p for a in arguments: pp = Application(pp, a) pp = Abstraction(pp) i = np.reshape(np.array(drawLogo(pp, resolution=128)), (128, 128)) if i is not None: ts.append(i) if ts == []: continue matrix.append(ts) if len(ts) < 6: ts = [ts] else: ts = makeNiceArray(ts) r = montageMatrix(ts) fn = "/tmp/logo_primitive_%d.png" % len(matrix) eprint("\tExported to", fn) scipy.misc.imsave(fn, r) matrix = montageMatrix(matrix) scipy.misc.imsave(export, matrix)
"non", "l", "erase", "m", "comes", "up", "comparison", "during", "'s value is the largest inclusive, which is strictly less than maximum element in numbers from 1 to the element in `a` which'", "'s value is the biggest (inclusive), which is strictly less than maximum element of range from 1 to the element in `a` which'", "'s value is the highest, which is strictly less than maximum element among sequence of digits of the element in `a` which'"] if __name__ == "__main__": #g = Grammar.uniform(deepcoderPrimitives()) g = Grammar.fromProductions(algolispProductions(), logVariable=.9) #p=Program.parse("(lambda (fn_call filter (list_add_symbol (lambda1_call == (list_add_symbol 1 (list_init_symbol (fn_call mod ( list_add_symbol 2 (list_init_symbol arg1)) ))) ) (list_init_symbol $0)) )") p=Program.parse("(lambda (fn_call filter (list_add_symbol (lambda1_call eq (list_add_symbol (symbol_constant 1) (list_init_symbol (fn_call mod ( list_add_symbol (symbol_constant 2) (list_init_symbol (symbol_constant arg1))) ))) ) (list_init_symbol (symbol_constant $0)))))") print(p) #tree = p.evaluate(["a"]) tree = p.evaluate([]) print(tree("a")) #
import sys sys.setrecursionlimit(int(1e4)) sys.path.insert(0, "./semantics/dreamcoder") from dreamcoder.domains.hint.hintPrimitives import McCarthyPrimitives _ = McCarthyPrimitives() from dreamcoder.program import Program from semantics.semantics import ProgramWrapper # Plus pg = Program.parse( "(lambda (lambda (fix2 $1 $0 (lambda (lambda (lambda (if0 $0 $1 ($2 (incr $1) (decr0 $0)))))))))" ) pg = ProgramWrapper(pg) print(pg(1, 2)) # pg = Program.parse("(lambda (lambda (fix2 $1 $0 (lambda (lambda (lambda (if0 $0 $1 ($2 $0 (decr0 $1)))))))))") pg = Program.parse( "(lambda (lambda (fix2 $1 $0 (lambda (lambda (lambda (if0 $0 $1 (incr ($2 $1 (decr0 $0))))))))))" ) pg = ProgramWrapper(pg) print(pg(1, 1)) # Minus # pg = Program.parse("(lambda (lambda (fix2 $1 $0 (lambda (lambda (lambda (if0 $0 $1 ($2 (decr0 $1) (decr0 $0)))))))))") pg = Program.parse( "(lambda (lambda (fix2 $1 $0 (lambda (lambda (lambda (if0 $0 $1 ($2 $0 (decr0 $1)))))))))" ) pg = ProgramWrapper(pg) print(pg(0, 2))
makeTask("c^2 = a^2 + b^2", arrow(tpositive, tpositive, tpositive), lambda a, b: a * a + b * b) ] bootstrapTarget() equationPrimitives = [ # real, f0, f1, fpi, real_power, real_subtraction, real_addition, real_division, real_multiplication ] + [ Program.parse(n) for n in ["map", "fold", "empty", "cons", "car", "cdr", "zip"] ] baseGrammar = Grammar.uniform(equationPrimitives) eprint("Got %d equation discovery tasks..." % len(tasks)) explorationCompression(baseGrammar, tasks, outputPrefix="experimentOutputs/scientificLaws", evaluationTimeout=0.1, testingTasks=[], **commandlineArguments( compressor="ocaml", featureExtractor=DummyFeatureExtractor, iterations=10,
def rustInduce(g0, frontiers, _=None, topK=1, pseudoCounts=1.0, aic=1.0, structurePenalty=0.001, a=0, CPUs=1, iteration=-1, topk_use_only_likelihood=False, vs=False): def finite_logp(l): return l if l != float("-inf") else -1000 message = { "strategy": { "version-spaces": { "top_i": 50 } } if vs else { "fragment-grammars": {} }, "params": { "structure_penalty": structurePenalty, "pseudocounts": int(pseudoCounts + 0.5), "topk": topK, "topk_use_only_likelihood": topk_use_only_likelihood, "aic": aic if aic != float("inf") else None, "arity": a, }, "primitives": [{ "name": p.name, "tp": str(t), "logp": finite_logp(l) } for l, t, p in g0.productions if p.isPrimitive], "inventions": [ { "expression": str(p.body), "logp": finite_logp(l) } # -inf=-100 for l, t, p in g0.productions if p.isInvented ], "variable_logprob": finite_logp(g0.logVariable), "frontiers": [{ "task_tp": str(f.task.request), "solutions": [{ "expression": str(e.program), "logprior": finite_logp(e.logPrior), "loglikelihood": e.logLikelihood, } for e in f], } for f in frontiers], } eprint("running rust compressor") messageJson = json.dumps(message) with open("jsonDebug", "w") as f: f.write(messageJson) # check which version of python we are using # if >=3.6 do: if sys.version_info[1] >= 6: p = subprocess.Popen(['./rust_compressor/rust_compressor'], encoding='utf-8', stdin=subprocess.PIPE, stdout=subprocess.PIPE) elif sys.version_info[1] == 5: p = subprocess.Popen(['./rust_compressor/rust_compressor'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) messageJson = bytearray(messageJson, encoding='utf-8') # convert messageJson string to bytes else: eprint("must be python 3.5 or 3.6") assert False p.stdin.write(messageJson) p.stdin.flush() p.stdin.close() if p.returncode is not None: raise ValueError("rust compressor failed") if sys.version_info[1] >= 6: resp = json.load(p.stdout) elif sys.version_info[1] == 5: import codecs resp = json.load(codecs.getreader('utf-8')(p.stdout)) productions = [(x["logp"], p) for p, x in zip((p for (_, _, p) in g0.productions if p.isPrimitive), resp["primitives"])] + \ [(i["logp"], Invented(Program.parse(i["expression"]))) for i in resp["inventions"]] productions = [(l if l is not None else float("-inf"), p) for l, p in productions] g = Grammar.fromProductions(productions, resp["variable_logprob"], continuationType=g0.continuationType) newFrontiers = [ Frontier([ FrontierEntry(Program.parse(s["expression"]), logPrior=s["logprior"], logLikelihood=s["loglikelihood"]) for s in r["solutions"] ], f.task) for f, r in zip(frontiers, resp["frontiers"]) ] return g, newFrontiers
def parse_program(expression): return Program.parse(expression)
def ocamlInduce(g, frontiers, _=None, topK=1, pseudoCounts=1.0, aic=1.0, structurePenalty=0.001, a=0, CPUs=1, bs=1000000, topI=300): # This is a dirty hack! # Memory consumption increases with the number of CPUs # And early on we have a lot of stuff to compress # If this is the first iteration, only use a fraction of the available CPUs topK = 5 topI = 600 if all(not p.isInvented for p in g.primitives): if a > 3: CPUs = max(1, int(CPUs / 6)) else: CPUs = max(1, int(CPUs / 3)) else: CPUs = max(1, int(CPUs / 2)) CPUs = 2 # X X X FIXME X X X # for unknown reasons doing compression all in one go works correctly and doing it with Python and the outer loop causes problems iterations = 99 # maximum number of components to add at once while True: g0 = g originalFrontiers = frontiers t2f = {f.task: f for f in frontiers} frontiers = [f for f in frontiers if not f.empty] message = { "arity": a, "topK": topK, "pseudoCounts": float(pseudoCounts), "aic": aic, "bs": bs, "topI": topI, "structurePenalty": float(structurePenalty), "CPUs": CPUs, "DSL": g.json(), "iterations": iterations, "frontiers": [f.json() for f in frontiers] } message = json.dumps(message) if True: timestamp = datetime.datetime.now().isoformat() os.system("mkdir -p compressionMessages") fn = "compressionMessages/%s" % timestamp with open(fn, "w") as f: f.write(message) eprint("Compression message saved to:", fn) try: # Get relative path compressor_file = os.path.join(get_root_dir(), 'compression') process = subprocess.Popen(compressor_file, stdin=subprocess.PIPE, stdout=subprocess.PIPE) response, error = process.communicate( bytes(message, encoding="utf-8")) response = json.loads(response.decode("utf-8")) except OSError as exc: raise exc g = response["DSL"] g = Grammar(g["logVariable"], [(l, p.infer(), p) for production in g["productions"] for l in [production["logProbability"]] for p in [Program.parse(production["expression"])]], continuationType=g0.continuationType) frontiers = { original.task: Frontier([ FrontierEntry(p, logLikelihood=e["logLikelihood"], logPrior=g.logLikelihood(original.task.request, p)) for e in new["programs"] for p in [Program.parse(e["program"])] ], task=original.task) for original, new in zip(frontiers, response["frontiers"]) } frontiers = [ frontiers.get(f.task, t2f[f.task]) for f in originalFrontiers ] if iterations == 1 and len(g) > len(g0): eprint("Grammar changed - running another round of consolidation.") continue else: eprint("Finished consolidation.") return g, frontiers
Primitive("logo_DIVA", arrow(tangle, tint, tangle), ""), Primitive("logo_MULA", arrow(tangle, tint, tangle), ""), Primitive("logo_DIVL", arrow(tlength, tint, tlength), ""), Primitive("logo_MULL", arrow(tlength, tint, tlength), ""), Primitive("logo_ADDA", arrow(tangle, tangle, tangle), ""), Primitive("logo_SUBA", arrow(tangle, tangle, tangle), ""), # Primitive("logo_ADDL", arrow(tlength,tlength,tlength), ""), # Primitive("logo_SUBL", arrow(tlength,tlength,tlength), ""), # Primitive("logo_PU", arrow(turtle,turtle), ""), # Primitive("logo_PD", arrow(turtle,turtle), ""), Primitive("logo_PT", arrow(arrow(turtle, turtle), arrow(turtle, turtle)), None), Primitive("logo_FWRT", arrow(tlength, tangle, turtle, turtle), ""), Primitive("logo_GETSET", arrow(arrow(turtle, turtle), turtle, turtle), "") ] + [ Primitive("logo_IFTY", tint, ""), Primitive("logo_epsA", tangle, ""), Primitive("logo_epsL", tlength, ""), Primitive( "logo_forLoop", arrow(tint, arrow(tint, turtle, turtle), turtle, turtle), "ERROR: python has no way of expressing this hence you shouldn't eval on this" ), ] + [Primitive(str(j), tint, j) for j in range(10)] if __name__ == "__main__": expr_s = "(lambda (logo_forLoop 3 (lambda (lambda (logo_GET (lambda (logo_FWRT (logo_S2L (logo_I2S 1)) (logo_S2A (logo_I2S 0)) (logo_SET $0 (logo_FWRT (logo_S2L eps) (logo_DIVA (logo_S2A (logo_I2S 2)) (logo_I2S 3)) ($1)))))))) ($0)))" x = Program.parse(expr_s) print(x)