コード例 #1
0
ファイル: main.py プロジェクト: liqing-ustc/dreamcoder
def animateSolutions(allFrontiers):
    programs = []
    filenames = []
    for n,(t,f) in enumerate(allFrontiers.items()):
        if f.empty: continue

        programs.append(f.bestPosterior.program)
        filenames.append(f"/tmp/logo_animation_{n}")
        
    drawLogo(*programs, pretty=True, smoothPretty=True, resolution=128, animate=True,
             filenames=filenames)
コード例 #2
0
 def tasksOfPrograms(self, ps, types):
     images = drawLogo(*ps, resolution=128)
     if len(ps) == 1: images = [images]
     tasks = []
     for i in images:
         if isinstance(i, str): tasks.append(None)
         else:
             t = Task("Helm", arrow(turtle, turtle), [])
             t.highresolution = i
             tasks.append(t)
     return tasks
コード例 #3
0
def dreamFromGrammar(g, directory, N=100):
    if isinstance(g, Grammar):
        programs = [
            p for _ in range(N)
            for p in [g.sample(arrow(turtle, turtle), maximumDepth=20)]
            if p is not None
        ]
    else:
        programs = g
    drawLogo(*programs,
             pretty=False,
             smoothPretty=False,
             resolution=512,
             filenames=[f"{directory}/{n}.png" for n in range(len(programs))],
             timeout=1)
    drawLogo(*programs,
             pretty=True,
             smoothPretty=False,
             resolution=512,
             filenames=[
                 f"{directory}/{n}_pretty.png" for n in range(len(programs))
             ],
             timeout=1)
    drawLogo(*programs,
             pretty=False,
             smoothPretty=True,
             resolution=512,
             filenames=[
                 f"{directory}/{n}_smooth_pretty.png"
                 for n in range(len(programs))
             ],
             timeout=1)
    for n, p in enumerate(programs):
        with open(f"{directory}/{n}.dream", "w") as handle:
            handle.write(str(p))
コード例 #4
0
ファイル: rbBaseline.py プロジェクト: zlapp/ec
def test_task(m, task, timeout):
    start = time.time()
    failed_cands = set()

    print(task.examples)

    frontier = []

    sampleFrequency = {}

    while time.time() - start < timeout:
        query = makeExamples(task)
        #import pdb; pdb.set_trace()
        candidates = m.sample([query] * BATCHSIZE)  #i think this works
        #print('len failed', len(failed_cands))
        for cand in candidates:
            try:
                p = Program.parse(" ".join(cand))
            except ParseFailure:
                continue
            except IndexError:
                continue
            except AssertionError:
                continue
            if p not in failed_cands:
                if "STRING" in str(p):
                    assert arguments.domain == 'text'
                    if len(task.stringConstants) == 0: ll = float('-inf')
                    else:
                        ci = Text.ConstantInstantiateVisitor(
                            [[cc for cc in sc] for sc in task.stringConstants],
                            sample=False)
                        ll = min(
                            task.logLikelihood(pp,
                                               timeout=0.1 if arguments.
                                               domain != 'rational' else None)
                            for pp in p.visit(ci))
                if arguments.domain == 'regex':
                    # regex is handled specially
                    # we just collect all of the candidates and then marginalize over them
                    # but we have to make sure that each candidate is well typed and well formed
                    ll = float('-inf')
                    if not p.canHaveType(task.request): p = None
                    else:
                        from examineFrontier import ConstantVisitor
                        p = p.visit(ConstantVisitor(task.str_const))
                        try:
                            regex = p.evaluate([])(pre.String(""))
                            if arguments.sampleLikelihood:
                                sampleFrequency[
                                    regex] = 1 + sampleFrequency.get(regex)
                                p = None
                            else:
                                dataLikelihood = sum(
                                    regex.match("".join(y))
                                    for _, y in task.examples)
                                logPrior = g.logLikelihood(task.request, p)
                                frontier.append(
                                    FrontierEntry(
                                        p,
                                        logPrior=logPrior,
                                        logLikelihood=dataLikelihood))
                            #print("sampled program",p,
                            #      "which translates into regex",regex,
                            #      "and which assigns the following likelihood to the test data",
                            #      dataLikelihood,
                            #      "and which has prior probability",logPrior)
                        except:
                            p = None

                elif arguments.domain != 'logo':
                    ll = task.logLikelihood(
                        p,
                        timeout=0.1
                        if arguments.domain != 'rational' else None)
                else:
                    try:
                        yh = drawLogo(p, timeout=1., resolution=28)
                        if isinstance(yh, list) and list(map(
                                int, yh)) == task.examples[0][1]:
                            ll = 0.
                        else:
                            ll = float('-inf')
                        #print("no warning, we are cool.jpeg")
                    except JSONDecodeError:
                        eprint(
                            "WARNING: Could not decode json. If this occurs occasionally it might be because the neural network is producing invalid code. Otherwise, if this occurs frequently, then this is a bug."
                        )
                        ll = float('-inf')

                #print(ll)
                if ll > float('-inf'):
                    #print(p)
                    #print(task.name)
                    return True

                elif p is not None:
                    failed_cands.add(p)

    if arguments.domain != 'regex':
        return False

    from examineFrontier import testingRegexLikelihood
    if arguments.sampleLikelihood:
        return lse([
            math.log(frequency) + testingRegexLikelihood(task, regex)
            for regex, frequency in sampleFrequency.items()
        ])
    # calculate that thing that we have to for regex
    frontier = Frontier(frontier, task)
    from graphs import addStupidRegex
    frontier = addStupidRegex(frontier, g)
    print("for this task I think that the following is the map estimate:\n",
          frontier.topK(1))
    if arguments.taskLikelihood:
        return lse([e.logPrior + e.logLikelihood for e in frontier])
    return lse([
        e.logPosterior + testingRegexLikelihood(task, e.program)
        for e in frontier
    ])
コード例 #5
0
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)