Example #1
0
import pygame
from Context import Context

pygame.display.set_caption('Reinforcement Learning 2D Driving Car')

context = Context()

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type >= pygame.USEREVENT:
            context.handleEvent(event.type)

    context.update()
    context.draw()
Example #2
0
from ProgramNode import ProgramNode
from Context import Context

if __name__ == "__main__":
    try:
        with open("program.txt") as f:
            for text in f.readlines():
                text = "".join(text.splitlines())
                print("text = \"{}\"".format(text))
                node = ProgramNode()
                node.parse(Context(text))
                print("node = " + str(node))
    except Exception as e:
        import logging
        logging.exception(e)
Example #3
0
def test_CodeGen():
    context = Context('data')
    codegen = CodeGen.CodeGen('./code_base/basis_code.jcb',
                              './code_base/basis_code.evd', Example)

    problem = context.get_problem_startswith(
        '917bccba.json')  # Hard enough to be unsolved

    codegen.demo_q = []
    codegen.demo_a = []
    codegen.question = []

    for example in problem:
        q, a, is_test = example.data
        if is_test:
            codegen.question.append(q)
        else:
            codegen.demo_q.append(q)
            codegen.demo_a.append(a)

    root = MctsNode()

    new_moves = codegen.new_moves(root)

    visits = 0
    rewards = 0
    for code_item, prior, reward, eval in new_moves:
        assert isinstance(code_item, tuple)

        assert prior > 0 and prior <= 2
        assert reward > 0 and reward <= 1

        pr = codegen.predict_rewards(np.stack([eval], axis=0))

        assert pr[0] == reward

        child = MctsNode(code_item, prior, reward, root)

        assert isinstance(child, MctsNode)

        visits += 1
        rewards += reward

    root.visits += visits
    root.reward += REWARD_DISCOUNT * rewards

    node = root

    while not node.is_leaf():
        node = node.select_child()

    new_moves = codegen.new_moves(node)

    visits = 0
    rewards = 0
    for code_item, prior, reward, eval in new_moves:
        assert isinstance(code_item, tuple)

        assert prior > 0 and prior <= 2
        assert reward > 0 and reward <= 1

        pr = codegen.predict_rewards(np.stack([eval], axis=0))

        assert pr[0] == reward

        child = MctsNode(code_item, prior, reward, root)

        assert isinstance(child, MctsNode)

        visits += 1
        rewards += reward
 def call(self, address):
     self.check_address(address)
     self._contexts.append(Context(self._cur_line_id))
     self._cur_line_id = address
Example #5
0
 def exec_(self, startip):
     self.ip = startip
     if self.metadata != None:
         #уподобляем определению main()
         self.ctx = Context(None, 0, self.metadata[0])
     self.cpu()
Example #6
0
def SimFn(pipe):
    #Lengths are in ft and masses are in lbs
    def getColors():
        """return scale and switch colors in that order"""
        switch = ["red", "blue"]
        if random.random() >= 0.5:
            switch[0], switch[1] = switch[1], switch[0]
        scale = ["red", "blue"]
        if random.random() >= 0.5:
            scale[0], scale[1] = scale[1], scale[0]
        return scale, switch

    def AddObjects(list):
        for elem in list:
            context.objects[elem.shape._get_shapeid()] = elem

    def scaleScore(scaleZones):  #call every second pls
        change = SCALE_POINTS * int(
            not scaleZones[0].numRet == scaleZones[1].numRet)
        if scaleZones[int(
                scaleZones[0].numRet < scaleZones[1].numRet)].color == "red":
            context.redScore.val += change
        else:
            context.blueScore.val += change
        return change

    def switchScore(switchZones):  #call every second pls
        change1 = SCALE_POINTS * int(
            not switchZones[0].numRet == switchZones[1].numRet)
        if switchZones[int(
                switchZones[0].numRet < switchZones[1].numRet)].color == "red":
            context.redScore.val += change1
        else:
            context.blueScore.val += change1

        change2 = SCALE_POINTS * int(
            not switchZones[2].numRet == switchZones[3].numRet)
        if switchZones[int(
                switchZones[2].numRet < switchZones[3].numRet)].color == "red":
            context.redScore.val += change2
        else:
            context.blueScore.val += change2
        return change1, change2

    def vaultScore(vaultZones, prevRed, prevBlue):  # per cube
        redIndex = int(vaultZones[1].color == "red")
        context.redScore.val += VAULT_POINTS * (vaultZones[redIndex].numRet -
                                                prevRed)
        context.blueScore.val += VAULT_POINTS * (
            vaultZones[1 - redIndex].numRet - prevBlue)
        return vaultZones[redIndex].numRet, vaultZones[1 - redIndex].numRet

    if currentProcess == SIM_PROC_NAME:
        context = Context()
        manager = mp.Manager()
        bots = []
        vaults = []
        scales = []
        switches = []
        scaleBarriers = []
        scalePenalties = []
        switchBarriers = []
        cubes = []
        cubePickup = []
        platforms = []
        context.scaleColor, context.switchColor = getColors()
        if MODE == "DRAW":
            infoPipe = mp.Pipe()
            info = mp.Process(target=InfoWindow.run,
                              args=[infoPipe[1]],
                              daemon=True)
            info.start()

        def MakeBots():
            for i in range(NUM_BOTS):
                if i < NUM_BOTS / 2:
                    bots.append(
                        Bot(BOT_NAME,
                            context,
                            pos=BOT_START_POS[i],
                            color="red",
                            manager=manager))
                else:
                    bots.append(
                        Bot(BOT_NAME,
                            context,
                            pos=BOT_START_POS[i],
                            color="blue",
                            manager=manager))
                bots[-1].canPickup = True
                bots[-1].ReceiveRet(CUBE_NAME)
                bots[-1].canPickup = False

        def MakeVaults():
            vaults.append(
                ScoreZone(VAULT_NAME,
                          context,
                          Vec2d((2.5, 17.5)),
                          True,
                          False,
                          False,
                          "red",
                          3,
                          4,
                          retKey=VAULT_RETKEY))
            vaults.append(
                ScoreZone(VAULT_NAME,
                          context,
                          Vec2d((FIELD_LENGTH - 0.5, FIELD_WIDTH - 16.5)),
                          True,
                          False,
                          False,
                          "blue",
                          3,
                          4,
                          retKey=VAULT_RETKEY))

        def MakeSwitches():
            switches.append(
                ScoreZone(SWITCH_NAME,
                          context,
                          Vec2d((15, 21)),
                          True,
                          False,
                          False,
                          context.switchColor[0],
                          radius=3,
                          retKey=SWITCH_RETKEY))
            switches.append(
                ScoreZone(SWITCH_NAME,
                          context,
                          Vec2d((15, 8)),
                          True,
                          False,
                          False,
                          context.switchColor[1],
                          radius=3,
                          retKey=SWITCH_RETKEY))
            switches.append(
                ScoreZone(SWITCH_NAME,
                          context,
                          Vec2d((41, 21)),
                          True,
                          False,
                          False,
                          context.switchColor[0],
                          radius=3,
                          retKey=SWITCH_RETKEY))
            switches.append(
                ScoreZone(SWITCH_NAME,
                          context,
                          Vec2d((41, 8)),
                          True,
                          False,
                          False,
                          context.switchColor[1],
                          radius=3,
                          retKey=SWITCH_RETKEY))

        def MakeScales():
            scales.append(
                ScoreZone(SCALE_NAME,
                          context,
                          Vec2d((28, 20)),
                          True,
                          False,
                          False,
                          context.scaleColor[0],
                          radius=3.5,
                          retKey=SCALE_RETKEY))
            scales.append(
                ScoreZone(SCALE_NAME,
                          context,
                          Vec2d((28, 9)),
                          True,
                          False,
                          False,
                          context.scaleColor[1],
                          radius=3.5,
                          retKey=SCALE_RETKEY))

        def MakeScaleBarriers():
            """physical scale obstacle"""
            scaleBarriers.append(
                Obstacle(OBSTACLE_NAME, context, Vec2d((28, 14.5)), 1.42,
                         10.79))

        def MakeScalePenalties():
            """space under scales (penalized bc unpredictable)"""
            scalePenalties.append(
                ScoreZone(SCALE_PENALTY_NAME, context, Vec2d((28, 20)), False,
                          True, False, context.scaleColor[0], 4, 3))
            scalePenalties.append(
                ScoreZone(SCALE_PENALTY_NAME, context, Vec2d((28, 9)), False,
                          True, False, context.scaleColor[1], 4, 3))

        def MakeSwitchBarriers():
            """physical switch obstacle"""
            switchBarriers.append(
                Obstacle(OBSTACLE_NAME, context, Vec2d((15, 14.5)), 3, 12.75))
            switchBarriers.append(
                Obstacle(OBSTACLE_NAME, context, Vec2d((41, 14.5)), 3, 12.75))

        def MakeCubes():
            # add retrievable cubes
            for i in range(10):
                cubes.append(
                    Retrievable(
                        CUBE_NAME, context,
                        Vec2d((random.uniform(-1, 1) + 12.125,
                               14.5 + random.uniform(-1, 1))), 3, 1.083,
                        1.083))  # form group to simulate pyramid
                cubes.append(
                    Retrievable(
                        CUBE_NAME, context,
                        Vec2d((random.uniform(-1, 1) + FIELD_LENGTH - 11.125,
                               14.5 + random.uniform(-1, 1))), 3, 1.083,
                        1.083))

            for i in range(6):
                cubes.append(
                    Retrievable(CUBE_NAME, context,
                                Vec2d((16.875, 20.354 - i * 2.342)), 3, 1.083,
                                1.083))
                cubes.append(
                    Retrievable(
                        CUBE_NAME, context,
                        Vec2d((FIELD_LENGTH - 14.875, 20.354 - i * 2.342)), 3,
                        1.083, 1.083))

        def MakeCubePickupZones():
            """areas where you pick up cubes"""
            cubePickup.append(
                ScoreZone(PICKUP_NAME,
                          context,
                          Vec2d((2, FIELD_LENGTH / 2)),
                          False,
                          False,
                          True,
                          "blue",
                          radius=1.5,
                          retKey=PICKUP_RETKEY))
            cubePickup.append(
                ScoreZone(PICKUP_NAME,
                          context,
                          Vec2d((2, 2)),
                          False,
                          False,
                          True,
                          "blue",
                          radius=1.5,
                          retKey=PICKUP_RETKEY))
            cubePickup.append(
                ScoreZone(PICKUP_NAME,
                          context,
                          Vec2d((FIELD_LENGTH, FIELD_LENGTH / 2)),
                          False,
                          False,
                          True,
                          "red",
                          radius=1.5,
                          retKey=PICKUP_RETKEY))
            cubePickup.append(
                ScoreZone(PICKUP_NAME,
                          context,
                          Vec2d((FIELD_LENGTH, 2)),
                          False,
                          False,
                          True,
                          "red",
                          radius=1.5,
                          retKey=PICKUP_RETKEY))

        def MakePlatforms():
            top = [8.66517, 4.4893, 3.4375]
            botm = [10.76892, 3.4375, 4.4893]
            length = [1.06175, 1.06175, 1.06175]
            vertices = [(Vec2d(-length[0],
                               -botm[0]), Vec2d(-length[0], botm[0]),
                         Vec2d(length[0], top[0]), Vec2d(length[0], -top[0])),
                        (Vec2d(botm[1] - top[1],
                               -length[1]), Vec2d(-top[1], length[1]),
                         Vec2d(top[1], length[1]), Vec2d(top[1], -length[1])),
                        (Vec2d(-botm[2],
                               -length[2]), Vec2d(top[2] - botm[2], length[2]),
                         Vec2d(botm[2], length[2]), Vec2d(botm[2],
                                                          -length[2]))]
            otherverts = copy.deepcopy(vertices)
            # mirror platform to other side of scale
            for list in otherverts:
                for vertex in list:
                    vertex[0] = -vertex[0]
                vertices.append(list)
            # correct vertices
            for list in vertices:
                for vertex in list:
                    vertex /= 2
            platforms.append(
                Slope(PLATFORM_NAME, context, vertices[0], 0.271593,
                      (23.3111, 14.5), Vec2d(-1, 0)))
            platforms.append(
                Slope(PLATFORM_NAME, context, vertices[1], 0.271593,
                      (25.042, 19.375), Vec2d(0, 1)))
            platforms.append(
                Slope(PLATFORM_NAME, context, vertices[2], 0.271593,
                      (25.042, 9.6273), Vec2d(0, -1)))
            platforms.append(
                Slope(PLATFORM_NAME, context, vertices[3], 0.271593,
                      (FIELD_LENGTH - 21.3111, 14.5), Vec2d(1, 0)))
            platforms.append(
                Slope(PLATFORM_NAME, context, vertices[4], 0.271593,
                      (FIELD_LENGTH - 23.042, 19.375), Vec2d(0, 1)))
            platforms.append(
                Slope(PLATFORM_NAME, context, vertices[5], 0.271593,
                      (FIELD_LENGTH - 23.042, 9.6273), Vec2d(0, -1)))

        def Reset():
            context.redScore.val = 0
            context.blueScore.val = 0
            context.gameTime = 0
            context.numBlueRets = 14
            context.numRedRets = 14

            for shape in context.space.shapes:
                object = context.objects[shape._get_shapeid()]
                if object != None and (object.name in TO_RESET):
                    object.CleanUp()
                    del object
            bots.clear()
            cubes.clear()
            switches.clear()
            scales.clear()
            context.numRets = 0
            MakeBots()
            MakeCubes()
            context.scaleColor, context.switchColor = getColors()
            MakeSwitches()
            MakeScales()
            for vault in vaults:
                vault.numRet = 0
                vault.CleanOut()
            for bot in bots:
                bot.AddToSpace()
            for cube in cubes:
                cube.AddToSpace()
            for scale in scales:
                scale.AddToSpace()
            for switch in switches:
                switch.AddToSpace()

        ### Physics stuff
        context.space.gravity = (0.0, 0.0)
        context.space.damping = 0.05
        penaltyCounter = 0
        canForce = False
        canLevitate = False
        canBoost = False
        static_body = context.space.static_body

        #add field walls
        field = Field(context, static_body)

        MakeBots()
        MakeVaults()
        MakeScales()
        MakeScaleBarriers()
        MakeScalePenalties()
        MakeSwitches()
        MakeSwitchBarriers()
        MakeCubes()
        MakeCubePickupZones()
        MakePlatforms()

        #add everything to space
        for key, object in context.objects.items():
            object.AddToSpace()

        #set collision handlers, pass objects registry as data so they can trace shapes back to objects
        botSwitch = context.space.add_collision_handler(
            collision_types[BOT_NAME], collision_types[SWITCH_NAME])
        botSwitch.data[0] = context
        botSwitch.pre_solve = beginBotScore
        botSwitch.separate = endBotScore

        botScale = context.space.add_collision_handler(
            collision_types[BOT_NAME], collision_types[SCALE_NAME])
        botScale.data[0] = context
        botScale.pre_solve = beginBotScore
        botScale.separate = endBotScore

        botVault = context.space.add_collision_handler(
            collision_types[BOT_NAME], collision_types[VAULT_NAME])
        botVault.data[0] = context
        botVault.pre_solve = beginBotVault
        botVault.separate = endBotVault

        botBot = context.space.add_collision_handler(collision_types[BOT_NAME],
                                                     collision_types[BOT_NAME])
        botBot.data[0] = context
        botBot.pre_solve = duringBotBot

        botPickup = context.space.add_collision_handler(
            collision_types[BOT_NAME], collision_types[PICKUP_NAME])
        botPickup.data[0] = context
        botPickup.pre_solve = beginBotPickup
        botPickup.separate = endBotPickup

        botPlatform = context.space.add_collision_handler(
            collision_types[BOT_NAME], collision_types[PLATFORM_NAME])
        botPlatform.data[0] = context
        botPlatform.pre_solve = duringBotPlatform

        botScalePenalty = context.space.add_collision_handler(
            collision_types[BOT_NAME], collision_types[SCALE_PENALTY_NAME])
        botScalePenalty.data[0] = context
        botScalePenalty.begin = beginBotScalePenalty
        FieldRets = []
        for retName in RET_NAMES:
            FieldRets.append(
                context.space.add_collision_handler(
                    collision_types[RETFIELD_NAME], collision_types[retName]))
            FieldRets[-1].data[0] = context
            FieldRets[-1].pre_solve = beginFieldRet
            FieldRets[-1].separate = endFieldRet
        forwardFlag = False
        backFlag = False
        rightFlag = False
        leftFlag = False
        dropFlag = False
        pickupFlag = False
        pygame.init()
        screen = pygame.display.set_mode((1200, 600))
        draw_options = pymunk.pygame_util.DrawOptions(screen)
        clock = pygame.time.Clock()
        running = True
        inputs = []
        #6 processes to simulate decisions of 6 robots
        pipes = []  # 2nd index 0 = parentconn, 1 = childconn
        nnProcs = []
        #for i in range(NUM_BOTS):
        #pipes.append(mp.Pipe())
        #nnProcs.append(mp.Process(target = getAction,name = NN_PROC_NAME, args = [network, pipes[i][1]], daemon = True))
        #nnProcs[i].start()
        print("done with setup", "w")
        inFile = open("inputs.txt", "w")
        outFile = open("outputs.txt", "w")
        rewardFile = open("rewards.txt", "w")
        #main loop
        while (True):
            cue = pipe.recv()
            if context.count > 0:
                nw.saver.restore(nw.sess, nw.SAVE_PATH)
            context.count += 1  #number of simulations done
            prevRed = 0
            prevBlue = 0
            print("starting simulation")
            print(context.gameTime)
            while running and context.gameTime < GAME_DURATION:
                dropFlag = False
                pickupFlag = False
                for event in pygame.event.get():
                    # debug input
                    if event.type == QUIT:
                        running = False
                    elif event.type == KEYDOWN and event.key == K_ESCAPE:
                        running = False
                    elif event.type == KEYDOWN and event.key == K_e:
                        dropFlag = True
                        bots[1].DropOffNearest(CUBE_NAME)
                        #if len(bots[1].rets[CUBE_NAME]) > 0:
                        #    zones = bots[1].ScoreZoneCheck()
                        #    if len(zones) > 0:
                        #        bots[1].DropOff(bots[1].rets[CUBE_NAME][0], zones[0])
                        #    else:
                        #       bots[1].DropOff(bots[1].rets[CUBE_NAME][0])
                    elif event.type == KEYDOWN and event.key == K_w:
                        forwardFlag = True
                    elif event.type == KEYUP and event.key == K_w:
                        forwardFlag = False
                    elif event.type == KEYDOWN and event.key == K_s:
                        backFlag = True
                    elif event.type == KEYUP and event.key == K_s:
                        backFlag = False
                    elif event.type == KEYDOWN and event.key == K_d:
                        rightFlag = True
                    elif event.type == KEYUP and event.key == K_d:
                        rightFlag = False
                    elif event.type == KEYDOWN and event.key == K_a:
                        leftFlag = True
                    elif event.type == KEYUP and event.key == K_a:
                        leftFlag = False
                    elif event.type == KEYDOWN and event.key == K_q:
                        pickupFlag = True
                        bots[1].PickUpNearest(CUBE_NAME)
                        #zones = bots[1].PickUpZoneCheck()
                        #if len(zones)>0:
                        #    zones[0].GiveRet(bots[1], CUBE_NAME)
                        #else:
                        #    bots[1].PickUp(bots[1].GetClosestRet())
                    elif event.type == KEYDOWN and event.key == K_f:
                        vaultScore(vaults, prevRed, prevBlue)
                    elif event.type == KEYDOWN and event.key == K_g:
                        bots[1].Brake()
                if forwardFlag:
                    bots[1].Forward(SCALE)
                if rightFlag:
                    bots[1].TurnRight(SCALE)
                if leftFlag:
                    bots[1].TurnLeft(SCALE)
                if backFlag:
                    bots[1].Backward(SCALE)
                if MODE == "DRAW":
                    ### Clear screen
                    screen.fill(THECOLORS["white"])
                    ### Draw stuff
                    context.space.debug_draw(draw_options)
                ## Update physics, move forward 1/NUM_STEPS second
                dt = 1.0 / NUM_STEPS
                for x in range(1):
                    for bot in bots:
                        bot.ControlVelUpdate(dt)
                    context.space.step(dt)
                    context.gameTime += dt
                    for bot in bots:
                        bot.immobileTime -= dt

                latestIdx = -1
                latestValue = -1
                #for i in range(NUM_BOTS):
                #pipes[i][0].send(bots[i].inputs[-15:])
                #feedForward.eval(feed_dict = {placeholder: input}, session = sess)[0]
                if (int(context.gameTime / dt) %
                        ACTION_TIMING == 0):  #get bot's action

                    infoAction = []
                    #save input for training neural net later
                    origSize = []
                    for bot in bots:
                        origSize.append(bot.SaveInput())
                    for i in range(NUM_BOTS):
                        infoAction.append("")
                        numTypes = len(bots[i].rets.items())
                        numPick = len(bots[i].objectList) + len(RET_NAMES)
                        zones = bots[i].ScoreZoneCheck()
                        #batchOutput = pipes[i][0].recv()
                        batchInput = bots[i].inputs[-SEQ_LEN:]
                        while len(batchInput) < SEQ_LEN:
                            batchInput.append([0] * INPUT_SIZE)
                        batchOutput = nw.probs.eval(
                            feed_dict={nw.next_element["input"]: [batchInput]},
                            session=nw.sess)[0]
                        bots[i].RNNInputs.append(batchInput)
                        output = batchOutput
                        latestIdx = np.argmax(output)
                        latestVal = output
                        bots[i].SaveLogits(output.tolist())
                        #idx = np.argmax(output)
                        if np.random.uniform(
                        ) >= 0:  #1-((START+context.count)/NUM_GAMES):
                            idx = latestIdx
                            #idx = np.random.choice(range(0,OUTPUT_SIZE),p = output)
                        else:
                            # default probability distribution, such that 1/3 of the time it moves, 1/3 it picks stuff up, 1/3 it drops off
                            # makes sure bot has variety of experiences
                            prob = []
                            for j in range(MVMT_TYPE_SIZE):
                                prob.append(1 / (MVMT_TYPE_SIZE * 3))
                            for j in range(numTypes):
                                prob.append(1 / (numTypes * 3))
                            for j in range(numPick):
                                prob.append(1 / (numPick * 3))
                        # idx = np.random.choice(MVMT_TYPE_SIZE + numTypes + numPick, p = prob)
                            idx = np.random.choice(range(0, OUTPUT_SIZE))
                        action = idx
                        if forwardFlag:
                            idx = 0
                            print(idx)
                        elif backFlag:
                            idx = 1
                            print(idx)
                        elif rightFlag:
                            idx = 2
                            print(idx)
                        elif leftFlag:
                            idx = 3
                            print(idx)
                        elif dropFlag:
                            idx = MVMT_TYPE_SIZE
                            print(idx)
                        elif pickupFlag:
                            if len(zones) > 0:
                                idx = MVMT_TYPE_SIZE + numTypes
                            else:
                                idx = MVMT_TYPE_SIZE + numTypes
                            print(idx)
                        if idx < MVMT_TYPE_SIZE:
                            mvmt = bots[i].mvmtKey[
                                idx]  # get appropriate movement based on nn output
                            infoAction[-1] += (mvmt.__name__)
                            mvmt(bots[i],
                                 SCALE * ACTION_TIMING)  # execute movement

                        elif idx < MVMT_TYPE_SIZE + numTypes:
                            idx -= MVMT_TYPE_SIZE
                            #logic for dropping off rets
                            bots[i].DropOffNearest(RET_NAMES[idx])
                            infoAction[-1] += " DROPOFF" + str(RET_NAMES[idx])
                            idx += MVMT_TYPE_SIZE
                        else:
                            idx -= (MVMT_TYPE_SIZE + numTypes)
                            #logic for picking stuff up
                            bots[i].PickUpNearest(RET_NAMES[idx])
                            idx += MVMT_TYPE_SIZE + numTypes
                        #action.append(idx)
                        bots[i].SaveAction(action)
                        for name in RET_NAMES:
                            infoAction[-1] += " " + name + ": " + str(
                                len(bots[i].rets[name]))

                #stuff done every second
                if not int(context.gameTime - dt) == int(context.gameTime):
                    #update scale and switch scores
                    scaleChange = scaleScore(scales)
                    switchChanges = switchScore(switches)
                    prevRed, prevBlue = vaultScore(vaults, prevRed, prevBlue)
                    if MODE == "DRAW":
                        infoAction[-1] += " scale: " + str(
                            scaleChange) + " switches: " + str(switchChanges)
                        # update infowindow
                        infoPipe[0].send(infoAction)
                        pass
                    print(
                        str(context.gameTime) + " " +
                        str(context.redScore.val) + " " +
                        str(context.blueScore.val) + " " + str(context.count) +
                        " " + str(latestIdx) + " " + str(latestVal))
                    #save scores
                    for bot in bots:
                        bot.SaveReward()
                if MODE == "DRAW":
                    ### Flip screen
                    pygame.display.flip()
                    clock.tick()
                    pygame.display.set_caption("fps: " + str(clock.get_fps()) +
                                               " red: " +
                                               str(context.redScore.val) +
                                               " blue: " +
                                               str(context.blueScore.val) +
                                               " " + str(vaults[0].numRet) +
                                               " " + str(vaults[1].numRet))

            inputs = []
            actions = []
            rewards = []
            logits = []
            print("sending data")
            for bot in bots:
                bot.AssignReward()
                inputs.extend(bot.RNNInputs)
                actions.extend(bot.actions)
                rewards.extend(bot.rewards)
                logits.extend(bot.logits)
            pipe.send([inputs, actions, rewards, logits])
            print("sim done")
            Reset()
Example #7
0
	def prepare_test(self):
		test_context_path = self.testdir + self.testname + '.context'
		pdb.set_trace()
		test_context = Context(self.testname, self.testcallgraph, self.depth, self.type)
		pdb.set_trace()
		pickle.dump(test_context, open(test_context_path, 'wb'))
Example #8
0
def test_instance_everything():
	a = Answer([[1, 0], [0, 1]])
	assert isinstance(a, Answer)
	assert Answer.__base__ == Block
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Bebop()
	assert isinstance(a, Bebop)
	assert Bebop.__base__ == object
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Block('hello', Block.type_error)
	assert isinstance(a, Block)
	assert Block.__base__ == object
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Bond(None, None)
	assert isinstance(a, Bond)
	assert Bond.__base__ == Block
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = BopBack()
	assert isinstance(a, BopBack)
	assert BopBack.__base__ == object
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = BopForward()
	assert isinstance(a, BopForward)
	assert BopForward.__base__ == object
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Code((Function(len, [Block.type_picture], Block.type_picture), Function(len, [Block.type_picture], Block.type_picture)))
	assert isinstance(a, Code)
	assert Code.__base__ == Block
	with pytest.raises(AttributeError):
		_ = a.__base__

	code = a

	a = CodeBase()
	assert isinstance(a, CodeBase)
	assert CodeBase.__base__ == Container
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = CodeEval(Example)
	assert isinstance(a, CodeEval)
	assert CodeEval.__base__ == Field
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = CodeGen(None, None, Example)
	assert isinstance(a, CodeGen)
	assert CodeGen.__base__ == CodeTree
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = CodeTree(None, Example)
	assert isinstance(a, CodeTree)
	assert CodeTree.__base__ == CodeEval
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Core(code, [Block.new_picture(list_of_list=[[3, 2], [2, 3]])])
	assert isinstance(a, Core)
	assert Core.__base__ == Block
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Container()
	assert isinstance(a, Container)
	assert Container.__base__ == object
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Context()
	assert isinstance(a, Context)
	assert Context.__base__ == Container
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Example([[3, 2], [2, 3]], [[1, 0], [0, 1]], False)
	assert isinstance(a, Example)
	assert Example.__base__ == Bond
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Field(Example)
	assert isinstance(a, Field)
	assert Field.__base__ == Bebop
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Function(len, Block.type_nothing, Block.type_nothing)
	assert isinstance(a, Function)
	assert Function.__base__ == Block
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = MCTS(None, None, Example)
	assert isinstance(a, MCTS)
	assert MCTS.__base__ == CodeGen
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = MctsNode()
	assert isinstance(a, MctsNode)
	assert MctsNode.__base__ == object
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Problem()
	assert isinstance(a, Problem)
	assert Problem.__base__ == Container
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Question([[3, 2], [2, 3]])
	assert isinstance(a, Question)
	assert Question.__base__ == Block
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Search()
	assert isinstance(a, Search)
	assert Search.__base__ == object
	with pytest.raises(AttributeError):
		_ = a.__base__

	a = Source(('nop'))
	assert isinstance(a, Source)
	assert Source.__base__ == Block
	with pytest.raises(AttributeError):
		_ = a.__base__
Example #9
0
__author__ = 'blu2'

import zerorpc
import sys
from myRDD import *
import StringIO
import cloudpickle
from datetime import datetime
import params
from Context import Context


if __name__ == '__main__':
    C = Context()
    R = C.init()
    rdd = R.TextFile("inputfile4.txt").flatMap(lambda x: x.split(" ")).map(lambda x: (x, 1)).reduceByKey_Hash(lambda a, b: a + b)
    print rdd.collect()



Example #10
0
def main():
    first_list = LinkedList()
    second_list = LinkedList()
    list_dict = {1: first_list,
                 2: second_list}
    first_strat = StrategyOne()
    second_strat = StrategyTwo()
    strategy_to_use = Context()
    option = int(menu_ui())
    while option != 8:
        # print("Option is", option)
        if option == 1:
            strategy_to_use.set_strategy(first_strat)
            print("Using first strategy")

        elif option == 2:
            strategy_to_use.set_strategy(second_strat)
            print("Using second strategy")

        elif option == 3:
            # print("\noption 3\n")
            list_choice = list_choice_func()
            position = input('Enter position to insert new values: ')
            data = input('Enter the value of the parameter (amount to generate or name of file): ')
            new_list = strategy_to_use.generate(list_dict[list_choice], position, data)
            if new_list is not False:
                print('New list:')
                new_list.print()
                list_dict[list_choice] = new_list
                # print('New list:')
                # list_dict[list_choice].print()

        elif option == 4:
            list_choice = list_choice_func()
            position = input('Enter position of an element you want to delete: ')
            if list_dict[list_choice].delete_by_position(position):
                print('List with element removed at position', position, "is", list_dict[list_choice])

        elif option == 5:
            list_choice = list_choice_func()
            print("Enter the positions for numbers to be deleted between:")
            start = input("Enter starting position: ")
            end = input("Enter ending position: ")
            list_dict[list_choice].delete_between(start, end)

        elif option == 6:
            print("starting function")
            if list_dict[1].size() == list_dict[2].size():
                N = list_dict[1].size()
                k = input('Enter K:')
                if represents_int(k):
                    k_in_x = list_dict[1].search(k)  # щоб не було матрьошки функцій
                    if list_dict[1].biggest() == int(k):  # працює
                        if len(k_in_x) == 1:  # працює
                            if k_in_x[0] < N // 2:  # працює
                                if list_dict[2].no_positive():
                                    max_in_y = list_dict[2].biggest()
                                    index_of_max_in_y = list_dict[2].search(max_in_y)
                                    if len(index_of_max_in_y) == 1:
                                        list_dict[2].cubing(index_of_max_in_y[0])
                                    else:
                                        print('More than 1 biggest number in list y')
                                else:
                                    print('There are positive number(s) in list y')
                            else:
                                print('k is in the second part of list x', k_in_x[0])
                        else:
                            print('There are more than one k in list x or no k in x')
                    else:
                        print('Biggest element in list x != k, biggest is', list_dict[1].biggest())
                    print('The result list is:')
                    list_dict[2].print()
            else:
                print("lists must be same length")

        elif option == 7:
            if list_dict[1].size() != 0:
                print("First list:")
                list_dict[1].print()
            else:
                print("First list is empty")
            if list_dict[2].size() != 0:
                print("Second list:")
                list_dict[2].print()
            else:
                print("Second list is empty")

        elif option == 8:
            break

        option = int(menu_ui())
Example #11
0
    def run_experiment(experiment_path, mcts=None):
        """
		Runs the MCTS.run_search() over all the problems in the experiment:
		  - saves the best codes
		  - verifies the solution and saves the result

		Both result files are updated at each search iteration. You can tail -f the summary to see the progress

		Full details are stored in experiment_path + '/result_details.json' as a dictionary where the key is the problem name and the
		value is another dictionary with:

			(Computed by MCTS)
			'source'	  : A list of (maximum) NUM_TOP_SOLUTIONS as source code to help serialization
 			'evaluation'  : A list of (maximum) NUM_TOP_SOLUTIONS CodeEval np.array.tolist() evaluations for each snippet
			'elapsed'	  : A list of (maximum) NUM_TOP_SOLUTIONS the second each solution was found
			'num_walks'	  : A list of (maximum) NUM_TOP_SOLUTIONS the walk number at which the solution was found
 			'prediction'  : A LoL for each question in the problem and (maximum) NUM_TOP_SOLUTIONS the guess as an np.array.tolist()
			'tot_elapsed' : The total number of seconds the search used
			'tot_walks'	  : The total number of walks the search did
			'stopped_on'  : Either 'time' or 'found' the condition that stopped the search

			(Computed by run_experiment)
			'correct'	  : A list for each question of (int) number of correct guesses for the question

		Summary details are stored in experiment_path + '/result_summary.txt' as a text file (and printed to stdout) as:

			'Total number of problems          : %4i of %4i'
			'Total number of questions         : %4i'
			'Total number of correct solutions : %4i (%3.1f%%)'
			'Total running time                : %4i minutes'
			''
			'Correct solutions in code base    : %4i (%3.1f%%)'
			'New correct solutions             : %4i (%3.1f%%)'
			'Failed in code base               : %s'
			'Found and wrong                   : %s'
			'New correct found                 : %s'
		"""
        mcts = mcts if mcts is not None else MCTS(
            './code_base/basis_code.jcb', './code_base/basis_code.evd',
            Example)

        context = Context('data')

        with open(experiment_path + '/config.json', 'r') as f:
            conf = json.load(f)

        filenames = conf['solved'] + conf['not_solved']

        num_prob_todo = len(filenames)
        num_prob_done = 0
        tot_num_questions = 0
        tot_correct_sol = 0
        tot_running_sec = 0
        correct_in_cdb = 0
        correct_new = 0
        failed_in_codebase = []
        found_and_wrong = []
        found_new = []

        details = {}

        for filename in filenames:
            problem = context.get_problem_startswith(filename)

            answers = []
            for example in problem:
                _, answer, is_test = example.data
                if is_test:
                    answers.append(answer)

            results = mcts.run_search(problem, conf['stop_rlz'])

            results['correct'] = []

            correct = 0
            found = False
            for eval, preds in zip(results['evaluation'],
                                   results['prediction']):
                if eval[IDX_PIC_REACH_MIN] == EVAL_FULL_MATCH:
                    found = True

                for pred, answer in zip(preds, answers):
                    if np.array_equal(answer.data,
                                      np.array(pred, dtype=np.int32)):
                        correct += 1

                results['correct'].append(correct)

            if correct > 0:
                tot_correct_sol += 1
                if filename in conf['solved']:
                    correct_in_cdb += 1
                else:
                    correct_new += 1
                    found_new.append(filename)
            else:
                if filename in conf['solved']:
                    failed_in_codebase.append(filename)

                if found:
                    found_and_wrong.append(filename)

            tot_num_questions += len(answers)

            details[filename] = results

            num_prob_done += 1
            tot_running_sec += results['tot_elapsed']

            summary = []

            summary.append(
                'Total number of problems          : %4i (%3.1f%%)' %
                (num_prob_done, 100 * num_prob_done / num_prob_todo))
            summary.append('Total number of questions         : %4i' %
                           tot_num_questions)
            summary.append(
                'Total number of correct solutions : %4i (%3.1f%%)' %
                (tot_correct_sol, 100 * tot_correct_sol / tot_num_questions))
            summary.append('Total running time                : %4i minutes' %
                           int(tot_running_sec / 60))
            summary.append('')
            summary.append(
                'Correct solutions in code base    : %4i (%3.1f%%)' %
                (correct_in_cdb, 100 * correct_in_cdb / tot_num_questions))
            summary.append(
                'New correct solutions             : %4i (%3.1f%%)' %
                (correct_new, 100 * correct_new / tot_num_questions))
            summary.append('Failed in code base               : %s' %
                           ', '.join(failed_in_codebase))
            summary.append('Found and wrong                   : %s' %
                           ', '.join(found_and_wrong))
            summary.append('New correct found                 : %s' %
                           ', '.join(found_new))

            with open(experiment_path + '/result_details.json', 'w') as f:
                json.dump(details, f)

            with open(experiment_path + '/result_summary.txt', 'w') as f:
                f.writelines(["%s\n" % line for line in summary])

        return summary
Example #12
0
def test_Multicore():
    # '1b2d62fb.json' : Source(('get_question',
    #   						'pic_fork_on_v_axis_as_pics',
    # 							'pics_as_2pic',
    # 							'2pic_maximum',
    # 							'(0, 8)',
    # 							'swap_top2',
    # 							'pic_intp_swap_colors',
    # 							'(9, 0)',
    # 							'swap_top2',
    # 							'pic_intp_swap_colors')),
    ctx = Context('data')

    prob = ctx.get_problem_startswith('1b2d62fb')

    eval = CodeEval(Example)

    eval.demo_q = []
    eval.demo_a = []
    eval.question = []

    for example in prob:
        q, a, is_test = example.data
        if is_test:
            eval.question.append(q)
        else:
            eval.demo_q.append(q)
            eval.demo_a.append(a)

    c11 = eval.compile(
        Source(('get_question', 'pic_fork_on_v_axis_as_pics', 'pics_as_2pic',
                '2pic_maximum')))
    c12 = eval.compile(
        Source(('get_question', 'pic_fork_on_h_axis_as_pics', 'pics_as_2pic',
                '2pic_maximum')))
    c13 = eval.compile(
        Source(('get_question', 'pic_fork_on_v_axis_as_pics', 'pics_as_2pic',
                '2pic_xor_masks_to_1')))

    c21 = eval.compile(Source(('(0, 8)', 'swap_top2', 'pic_intp_swap_colors')))
    c22 = eval.compile(Source(('(5, 4)', 'swap_top2', 'pic_intp_swap_colors')))
    c23 = eval.compile(
        Source(('(5, 4)', 'swap_top2', 'pic_intp_select_columns')))

    c31 = eval.compile(Source(('(9, 0)', 'swap_top2', 'pic_intp_swap_colors')))
    c32 = eval.compile(Source(('(5, 4)', 'swap_top2', 'pic_intp_recolor')))
    c33 = eval.compile(Source(('pic_transpose', )))

    assert c11.type == Block.type_no_error and c12.type == Block.type_no_error and c13.type == Block.type_no_error
    assert c21.type == Block.type_no_error and c22.type == Block.type_no_error and c23.type == Block.type_no_error
    assert c31.type == Block.type_no_error and c32.type == Block.type_no_error and c33.type == Block.type_no_error

    eval.multicore_clear()

    ret = eval.multicore_run_all(c11)

    assert ret.type == Block.type_no_error

    for pl in eval.multicore_state['pic_lists']:
        assert len(pl) == 1

    ret = eval.multicore_run_all(c21)

    assert ret.type == Block.type_no_error

    for pl in eval.multicore_state['pic_lists']:
        assert len(pl) == 2

    ret = eval.multicore_run_all(c31)

    assert ret.type == Block.type_no_error

    for pic_list, example in zip(eval.multicore_state['pic_lists'], prob):
        assert len(pic_list) == 3

        _, answer, _ = example.data

        assert np.array_equal(pic_list[2].data, answer.data)

    eval.multicore_clear()

    root = eval.multicore_copy_state(eval.multicore_state)

    ret = eval.multicore_run_all(c13)
    assert ret.type == Block.type_no_error
    ret = eval.multicore_run_all(c21)
    assert ret.type == Block.type_no_error

    for pl in eval.multicore_state['pic_lists']:
        assert len(pl) == 2

    eval.multicore_set_state(root)

    for pl in eval.multicore_state['pic_lists']:
        assert len(pl) == 0

    ret = eval.multicore_run_all(c12)
    assert ret.type == Block.type_no_error
    ret = eval.multicore_run_all(c23)
    assert ret.type == Block.type_no_error

    for pl in eval.multicore_state['pic_lists']:
        assert len(pl) == 2

    eval.multicore_set_state(root)

    ret = eval.multicore_run_all(c11)

    for pl in eval.multicore_state['pic_lists']:
        assert len(pl) == 1

    lev_1 = eval.multicore_copy_state(eval.multicore_state)

    ret = eval.multicore_run_all(c23)
    assert ret.type == Block.type_no_error
    ret = eval.multicore_run_all(c33)
    assert ret.type == Block.type_no_error

    eval.multicore_set_state(lev_1)

    ret = eval.multicore_run_all(c22)
    assert ret.type == Block.type_no_error
    ret = eval.multicore_run_all(c31)
    assert ret.type == Block.type_no_error

    eval.multicore_set_state(lev_1)

    ret = eval.multicore_run_all(c21)

    lev_2 = eval.multicore_copy_state(eval.multicore_state)

    ret = eval.multicore_run_all(c33)
    assert ret.type == Block.type_no_error

    eval.multicore_set_state(lev_2)

    ret = eval.multicore_run_all(c32)
    assert ret.type == Block.type_no_error

    eval.multicore_set_state(lev_2)

    ret = eval.multicore_run_all(c31)

    assert ret.type == Block.type_no_error

    pic_lists = eval.multicore_state['pic_lists']

    for pic_list, example in zip(pic_lists, prob):
        assert len(pic_list) == 3

        _, answer, _ = example.data

        assert np.array_equal(pic_list[2].data, answer.data)
Example #13
0
def test_CodeEval():
    # '1b2d62fb.json' : Source(('get_question',
    #   						'pic_fork_on_v_axis_as_pics',
    # 							'pics_as_2pic',
    # 							'2pic_maximum',
    # 							'(0, 8)',
    # 							'swap_top2',
    # 							'pic_intp_swap_colors',
    # 							'(9, 0)',
    # 							'swap_top2',
    # 							'pic_intp_swap_colors')),
    ctx = Context('data')

    prob = ctx.get_problem_startswith('1b2d62fb')

    eval = CodeEval(Example)

    with pytest.raises(TypeError):  # self.multicore_state is None
        eval.eval_code()

    eval.demo_q = []
    eval.demo_a = []
    eval.question = []

    eval.multicore_clear()  # Builds a state without cores

    with pytest.raises(LookupError):  # self.multicore_state is None
        eval.eval_code()

    for example in prob:
        q, a, is_test = example.data
        if is_test:
            eval.question.append(q)
        else:
            eval.demo_q.append(q)
            eval.demo_a.append(a)

    eval.multicore_clear()

    with pytest.raises(LookupError):  # Runs an eval without a picture
        eval.eval_code()

    c1 = eval.compile(
        Source(('get_question', 'pic_fork_on_v_axis_as_pics', 'pics_as_2pic',
                '2pic_maximum')))
    c2 = eval.compile(Source(('(0, 8)', 'swap_top2', 'pic_intp_swap_colors')))
    c3 = eval.compile(Source(('(9, 0)', 'swap_top2', 'pic_intp_swap_colors')))

    ret = eval.multicore_run_all(c1)
    assert ret.type == Block.type_no_error

    vec = eval.eval_code()

    assert vec[IDX_PIC_REACH_MIN] == 0 and vec[IDX_PIC_REACH_MAX] == 0
    assert vec[IDX_PAT_REACH_MIN] == 0 and vec[IDX_PAT_REACH_MAX] == 0
    assert vec[IDX_PIC_BETTER_MEAN] == EVAL_WRONG_SHAPE and vec[
        IDX_PIC_WORSE_MEAN] == EVAL_WRONG_SHAPE

    ret = eval.multicore_run_all(c2)
    assert ret.type == Block.type_no_error

    vec = eval.eval_code()

    assert 0 < vec[IDX_PIC_REACH_MIN] and vec[IDX_PIC_REACH_MIN] < vec[
        IDX_PIC_REACH_MEAN]
    assert vec[IDX_PIC_REACH_MEAN] < vec[IDX_PIC_REACH_MAX] and vec[
        IDX_PIC_REACH_MAX] < 1

    assert vec[IDX_PIC_REACH_MIN] == vec[IDX_PAT_REACH_MIN] and vec[
        IDX_PIC_REACH_MEAN] == vec[IDX_PAT_REACH_MEAN]

    assert vec[IDX_PIC_WORSE_MAX] == 0 and vec[IDX_PAT_WORSE_MAX] == 0

    assert 0 < vec[IDX_PIC_BETTER_MIN] and vec[IDX_PIC_BETTER_MIN] < vec[
        IDX_PIC_BETTER_MEAN]
    assert vec[IDX_PIC_BETTER_MEAN] < vec[IDX_PIC_BETTER_MAX] and vec[
        IDX_PIC_BETTER_MAX] < 1

    assert vec[IDX_PIC_BETTER_MIN] == vec[IDX_PAT_BETTER_MIN] and vec[
        IDX_PIC_BETTER_MEAN] == vec[IDX_PAT_BETTER_MEAN]

    ret = eval.multicore_run_all(c3)
    assert ret.type == Block.type_no_error

    vec = eval.eval_code()

    assert vec[IDX_PIC_REACH_MIN] == EVAL_FULL_MATCH
    assert vec[IDX_PAT_REACH_MIN] == EVAL_FULL_MATCH

    assert vec[IDX_PIC_WORSE_MIN] == 0 and vec[IDX_PAT_WORSE_MIN] == 0
    assert vec[IDX_PIC_WORSE_MEAN] == 0 and vec[IDX_PAT_WORSE_MEAN] == 0
    assert vec[IDX_PIC_WORSE_MAX] == 0 and vec[IDX_PAT_WORSE_MAX] == 0

    assert 0 < vec[IDX_PIC_BETTER_MIN] and vec[IDX_PIC_BETTER_MIN] < vec[
        IDX_PIC_BETTER_MEAN]
    assert vec[IDX_PIC_BETTER_MEAN] < vec[IDX_PIC_BETTER_MAX] and vec[
        IDX_PIC_BETTER_MAX] < 1

    eval.multicore_clear()

    c1 = eval.compile(Source(('get_question', 'pic_transpose')))

    ret = eval.multicore_run_all(c1)
    assert ret.type == Block.type_no_error

    vec = eval.eval_code()

    for vv in vec.tolist():
        assert vv == EVAL_WRONG_SHAPE
Example #14
0
if __name__ == "__main__":

    #read in and format file
    print("Reading in in.txt")

    lines = []
    with open("in.txt") as myfile:
        lines = myfile.readlines()

    lines = [line.strip().split(' ') for line in lines]

    #create context object that will hold strategy
    print("Tests Started")

    my_context = Context()

    #change strategy based on lines and test
    expected = [6, -13, -91, -44, 1, 0, 8]

    for i in range(len(lines)):
        type = lines[i][0]
        num1 = int(lines[i][1])
        num2 = int(lines[i][2])

        if (type == "add"):
            my_context.change_strategy(AddStrategy())
        elif (type == "subtract"):
            my_context.change_strategy(SubtractStrategy())
        else:
            raise ValueError('Invalid type in in.txt')
Example #15
0
 def __init__(self):
     self.__context = Context()
     self.__primitive_actions = {}
Example #16
0
class GFAction(Application.Application):
    map = {}
    refList = []  # (Template,expectedXY)
    COMPENSATE_TIME = 3
    run_times = None
    now_times = None
    TRACE_PATH = os.path.abspath("trace.txt")
    context = Context()

    @abc.abstractmethod
    def filePath(self):
        pass

    def __init__(self):
        super().__init__()
        # log_path = os.path.join(os.path.dirname(os.path.abspath(self.filePath())), "log")
        # if os.path.exists(log_path):
        #     shutil.rmtree(os.path.join(os.path.dirname(os.path.abspath(self.filePath())), "log"))

    def addCompensateTuple(self, template, expectedXY):
        self.refList.append((template, expectedXY))

    @abc.abstractmethod
    def compensateConfig(self):
        pass

    def compensate(self):
        self.refList.clear()
        self.compensateConfig()
        for i in range(0, self.COMPENSATE_TIME):
            for entry in self.refList:
                template = entry[0]
                expectedXY = entry[1]
                nowXY = self.existsFast(template)
                if nowXY:
                    delta = (nowXY[0] - expectedXY[0],
                             nowXY[1] - expectedXY[1])
                    for key in self.map.keys():
                        old = self.map[key]
                        self.map[key] = (old[0] + delta[0], old[1] + delta[1])
                    return
                else:
                    continue
        raise BaseException(str.format("找不到参照点,重复%d次" % self.COMPENSATE_TIME))

    def getMap(self):
        return self.map

    def touchBlank(self, sleep_time=1):
        self.touch(self.config.BLANK)
        self.sleep(sleep_time)

    def touch(self, obj, sleep_time=1):
        if type(obj) == Template:
            pass
        elif type(obj) == tuple and obj.__len__() == 2:
            pass
        elif type(obj) == str:
            obj = self.map[obj]
        else:
            raise Exception("类型不正确" + obj.__str__())
        return super().touch(obj, sleep_time)

    def supply(self, key):
        self.touch(key)
        self.touch(key)
        self.touch(self.config.ECHELON_SUPPLY)
        self.touchBlank()
        self.sleep(1)

    def switch(self, key1, key2):
        self.touch(key1)
        self.touch(key2)
        self.touch(Util.getSwitchPos(self.map[key2]))
        self.touchBlank()

    def addEchelon(self, v, sleep_time=1):
        self.touch(v)
        self.touch(self.config.ECHELON_ADD_CERTAIN)
        self.sleep(sleep_time)

    def beginOrEnd(self):
        self.touch(self.config.END_BEGIN)

    def begin(self):
        self.beginOrEnd()
        self.sleep(3)

    def endTurn(self):
        self.beginOrEnd()

    def withdrawEchelon(self, key, sleep_time=1):
        self.touch(key)
        self.touch(key)
        self.touch(self.config.ECHELON_WITHDRAW)
        self.touch(self.config.DIALOG_CERTAIN)
        self.sleep(1)
        self.sleep(sleep_time)

    def schedule(self, key1, key2):
        self.touch(self.config.SCHEDULE)
        self.touch(key1)
        self.touch(key2)

    def sleepUntilAssert(self,
                         sleep_time,
                         picture,
                         interval=10,
                         time_range=20):
        realtime = sleep_time - time_range
        if realtime < 0:
            realtime = 0
        self.sleep(realtime)
        times = int((2 * time_range) / interval)
        for i in range(0, times):
            if not self.existsFast(picture):
                self.sleep(10)
            else:
                self.sleep(3)
                return
        raise Exception("找不到图片" + picture.__str__())

    def renew(self, sleep_time=4):
        self.touch(self.config.TERMINATE)
        self.touch(self.config.TERMINATE_RENEW)
        self.sleep(sleep_time)

    @staticmethod
    def existsFast(*args):
        for i in range(0, args.__len__()):
            try:
                if type(args[i]) != Template:
                    raise Exception("传入参数不是Template")
                return loop_find(args[i], timeout=1, interval=0.1)
            except TargetNotFoundError:
                logging.warning("找不到图片%s" % (args[i].filename))
        return False

    def findAll(self, *args):
        res = []
        for i in range(0, args.__len__()):
            if type(args[i]) != Template:
                raise Exception("传入参数不是Template")
            list = find_all(args[i])
            if list:
                for j in range(0, list.__len__()):
                    res.append(list[j]['result'])
        if res.__len__() == 0:
            return False
        return res

    def run(self, times=None):
        if times:
            self.run_times = times
        if not self.run_times:
            self.run_times = int(input("input times\n"))
        self.now_times = 0
        try:
            for i in range(0, self.run_times):
                self.step()
                self.now_times = i + 1
                print(
                    str.format(
                        "第%d次,剩余%d次" %
                        (self.now_times, self.run_times - self.now_times)))
                print(
                    str.format(
                        "第%d次,剩余%d次" %
                        (self.now_times, self.run_times - self.now_times)))
        except Exception as e:
            f = open(self.TRACE_PATH, 'w', encoding="UTF-8")
            traceback.print_exc(None, f, True)
            localtime = time.asctime(time.localtime(time.time()))
            f.write(localtime.__str__() + "\n")
            f.flush()
            f.close()
            if self.context.debug:
                os.system("start " + self.TRACE_PATH)
            # print(traceback.format_exc())
            traceback.print_exc()
        except KeyboardInterrupt as interrupt:
            if self.context.debug:
                os.system("start " + self.TRACE_PATH)
        finally:
            print(
                str.format("第%d次未完整完成,剩余%d次" %
                           (self.now_times, self.run_times - self.now_times)))
            simple_report(self.filePath(), logpath=True)

    def getNowTimes(self):
        return self.now_times

    @abc.abstractmethod
    def step(self):
        pass
Example #17
0
    else:
        rules.append(
            (sig + r"\(" + ",".join([types.get("string")
                                     for _ in params]) + r"\)", SIGNATURE))
        dic[sig] = ([
            e.replace("int", "INT").replace("string", "VARCHAR")
            for _, e in params
        ], [])

rules.extend(Lexer.get_rules())
tokens = Lexer.lex(util.parse_formula(formula_loc), rules)
node = MyParser.parse(tokens)
print(node.to_str())

conn = sqlite3.connect(":memory:")
context = Context(conn, ["A", "B"])

with open(log_loc, 'r') as f:
    for line in f:
        parse_ts = re.compile(r"@[0-9]+").findall(line)
        if len(parse_ts) == 1:
            ts = int(parse_ts[0][1:])
        else:
            raise RuntimeError("No timestamp found")
        context.set_ts(ts)
        data = {}
        for sig in dic.keys():
            data[sig] = (dic[sig][0].copy(), [])
        for sig in data.keys():
            regex = re.compile(
                sig + r"\(" +
Example #18
0
 def get_Technic(self, id):
     if id < 0:
         return Context(self._menu_list[-id - 1]())
     else:
         return self._Technic[id]
Example #19
0
 def test_addition(self):
     addition = StrategyAddition(2, 1)
     context = Context(addition)
     result = context.execute()
     self.assertEqual(3, result)
Example #20
0
    def cpu(self):
        opcode = -1
        #sp+=1 steck[sp]=val
        #Выделяем некоторое место на стеке,а затем записываем туда значение.
        #Аналог push()
        #---
        #C++:steck[sp++](постинкремент)
        #---
        #val=steck[sp]=val sp-=1->val=TOP() sp-=1
        #копируем верхушку стека в val и передвигаем указатель на 1
        #к меньшему адресу,чтобы TOP() могли ползоваться
        #другие команды.
        #Аналог pop()
        #---
        #C++:steck[--sp](преинкремент)
        #---
        while (self.ip < len(self.code) and opcode != HALT):
            opcode = self.code[self.ip]  #fetch
            #некоторая трассировка-вывод только опкодов, без
            #аргументов
            if self.trace:
                print('%d:%s\n' % (self.ip, opcodes[opcode]))
            if (opcode == ICONST):  #switch
                self.ip += 1  #exec
                self.sp += 1
                self.steck[self.sp] = self.code[self.ip]
            elif (opcode == PRINT):
                v = self.steck[self.sp]
                self.sp -= 1
                print(v)
            elif opcode == GSTORE:  #сохраняем значение с
                #верхушки стека,в глобальную память по индексу(адресу) взятого
                #как параметр инструкции
                v = self.steck[self.sp]
                #print('v',v)
                self.sp -= 1
                self.ip += 1
                addr = self.code[self.ip]
                self.globals_[addr] = v
#загружаем на верхушку стека значение из глобальной памяти
#взятого по индексу(адресу) как параметр инструкции
            elif opcode == GLOAD:
                self.ip += 1
                addr = self.code[self.ip]
                v = self.globals_[addr]
                self.sp += 1
                self.steck[self.sp] = v
            #выход из функции cpu()
            elif opcode == HALT:
                return
            elif opcode == DUMP:
                print('steck:', self.steck)
            elif opcode == DUMP_SP:
                print('steck sp:', self.sp)
            elif opcode == BR:
                self.ip += 1
                self.ip = self.code[self.ip]
                continue
            elif opcode == BRT:
                self.ip += 1
                addr = self.code[self.ip]
                if self.steck[self.sp] == TRUE:
                    self.ip = addr
                    self.sp -= 1
                    continue
            elif opcode == BRF:
                self.ip += 1
                addr = self.code[self.ip]
                if self.steck[self.sp] == FALSE:
                    self.ip = addr
                    self.sp -= 1
                    continue
            elif opcode == IADD:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = a + b
            elif opcode == ISUB:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = a - b
            elif opcode == IMUL:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = a * b
            elif opcode == IDIV:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = a / b
            elif opcode == LES:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                if a < b:
                    self.sp += 1
                    self.steck[self.sp] = TRUE  #True
                else:
                    self.sp += 1
                    self.steck[self.sp] = FALSE  #False
            elif opcode == PRINTST:
                self.ip += 1
                v = self.code[self.ip]
                print(v)
            elif opcode == DUMP_IP:
                print('ip:', self.ip)
            elif opcode == POP:
                self.sp -= 1
#Загружает на верхушку стека,значение из 'локальной' карты,
#взятое по индексу-аргументу LOAD
            elif opcode == LOAD:
                self.ip += 1
                regnum = self.code[self.ip]
                self.sp += 1
                self.steck[self.sp] = self.ctx.locals_[regnum]
#'консервируем' в 'локальную' карту верхушку
#стека на индекс(адрес) как аргумент STORE
            elif opcode == STORE:
                self.ip += 1
                regnum = self.code[self.ip]
                self.ctx.locals_[regnum] = self.steck[self.sp]
                self.sp -= 1
            elif opcode == CALL:
                #ожидается все аргументы на стеке
                self.ip += 1
                #индекс целевой функции
                findex = self.code[self.ip]
                #сколько аргументов она положила на стек
                nargs = self.metadata[findex].nargs
                #ip+1 как аргумент-это returnip,адрес возврата для данной функции,
                #используется в RET
                self.ctx = Context(self.ctx, self.ip + 1,
                                   self.metadata[findex])
                #первый аргумент
                firstarg = self.sp - nargs + 1
                #копируем аргументы со стека
                #в новый контекст
                for i in range(0, nargs):
                    self.ctx.locals_[i] = self.steck[firstarg + i]
                self.sp -= nargs
                self.ip = self.metadata[findex].address
                continue
            elif opcode == RET:
                self.ip = self.ctx.returnip
                self.ctx = self.ctx.invokingContext
                continue
            elif opcode == INC:
                v = self.steck[self.sp]
                v += 1
                self.steck[self.sp] = v
            elif opcode == DEC:
                v = self.steck[self.sp]
                v -= 1
                self.steck[self.sp] = v
            elif opcode == MOD:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = a % b
            elif opcode == ABI:
                v = self.steck[self.sp]
                self.steck[self.sp] = abs(v)
            elif opcode == NEQ:  #a != b ?
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                if a != b:
                    self.sp += 1
                    self.steck[self.sp] = TRUE  #True
                else:
                    self.sp += 1
                    self.steck[self.sp] = FALSE  #False
            elif opcode == LEQ:  #a <= b ?
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                if a <= b:
                    self.sp += 1
                    self.steck[self.sp] = TRUE  #True
                else:
                    self.sp += 1
                    self.steck[self.sp] = FALSE  #False
            elif opcode == EQU:  #a == b ?
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                if a == b:
                    self.sp += 1
                    self.steck[self.sp] = TRUE  #True
                else:
                    self.sp += 1
                    self.steck[self.sp] = FALSE  #False
            elif opcode == GEQ:  #a == b ?
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                if a >= b:
                    self.sp += 1
                    self.steck[self.sp] = TRUE  #True
                else:
                    self.sp += 1
                    self.steck[self.sp] = FALSE  #False
            elif opcode == ODD:
                v = self.steck[self.sp]
                if v % 2 != 0:
                    self.steck[self.sp] = TRUE
                else:
                    self.steck[self.sp] = FALSE
            elif opcode == AND:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = a and b
            elif opcode == OR:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = a or b
            elif opcode == XOR:
                b = self.steck[self.sp]
                self.sp -= 1
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = a ^ b
            elif opcode == NOT:
                a = self.steck[self.sp]
                self.sp -= 1
                self.sp += 1
                self.steck[self.sp] = not_tabl[a]
            else:
                raise Exception("invalid opcode:", opcodes[opcode], " at ip=",
                                (self.ip))

            self.ip += 1
Example #21
0
    def test_subtraction(self):
        substraction = StrategySubtraction(3, 2)
        context = Context(substraction)
        result = context.execute()

        self.assertEqual(1, result)
Example #22
0
            if hasattr(self, "timer"):
                self.timer.cancel()
            self.ruleEngine.stop()
        except Exception, e:
            self.logger.error("Error on RuleUpdater.stop(): %s" % (e))

    def run(self):
        try:
            self.logger.info("RuleUpdater.run(): Periodic rule engine update.")
            self.ruleEngine.update()
            self.start()
        except Exception, e:
            self.logger.error("Error on RuleUpdater.run(): %s" % (e))


if __name__ == "__main__":

    logger = logging.getLogger('RestoreStatusRule')
    hdlr = logging.FileHandler('log/rule.log')
    formatter = logging.Formatter(
        "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)

    context = Context(1)
    r1 = AbstractRule(context, logger)
    re = RuleEngine(context, logger)
    ru = RuleUpdater(re, logger)
    re.update()
    ru.start()
Example #23
0
 def get_worker(self, id):
     if id < 0:
         return Context(self._menu_list[-id-1]())
     else:
         return self._workers[id]
Example #24
0
 def get_context(self):
     return Context(self)
Example #25
0
 def on_creat(self, param):
     route = Route()
     context = Context()
     route.attach_context(context)
     context.set_route(route)
     self.context = context
Example #26
0
from Context import Context
from S1 import S1
if __name__ == '__main__':
    ctx = Context()
    ctx.Condition = 2
    ctx.SetState(S1())
    ctx.DoSomething()

    ctx.Condition = 7
    ctx.DoSomething()

    ctx.Condition = 8
    ctx.DoSomething()

    ctx.Condition = 6
    ctx.DoSomething()

    ctx.Condition = 5
    ctx.DoSomething()

    ctx.Condition = 8
    ctx.DoSomething()
Example #27
0
 def reset(self):
     self.context = Context()
Example #28
0
        key = ConfigurationConstants.getPresenceTimer()
        config.add_comment(
            section,
            "Insert timer for presence periods in minutes (-1 infinite timer) eg. "
            + key, "10")
        config.set(section, key, "10")

        key = ConfigurationConstants.getIsRuleEnabled()
        config.add_comment(section,
                           "Is the rule enabled? (True or False) eg. " + key,
                           "True")
        config.set(section, key, "True")

        config.add_section(ConfigurationConstants.getRuleSettings())

        config.write(f)


if __name__ == "__main__":
    context = Context(3)
    logger = logging.getLogger('RestoreStatusRule')
    hdlr = logging.FileHandler('log/rule.log')
    formatter = logging.Formatter(
        "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)

    rule = DefaultTimerRule(context, logger, "conf/agents/rule.conf")
    rule.process()
Example #29
0
def test_MCTS():
    context = Context('data')
    mcts = MCTS('./code_base/basis_code.jcb', './code_base/basis_code.evd',
                Example)

    stop_rlz = {
        'broken_threshold': 0.1,
        'max_broken_walks': 50,
        'max_elapsed_sec': 2,
        'min_num_walks': 5,
        'stop_num_full_matches': 1
    }

    prob1a = context.get_problem_startswith('ed36ccf7')  # One item
    prob1b = context.get_problem_startswith('007bbfb7')  # One item
    prob2 = context.get_problem_startswith('0692e18c')  # Two items
    probX = context.get_problem_startswith('c6e1b8da')  # Nightmare task

    answer = None
    for example in prob1a:
        _, a, is_test = example.data
        if is_test:
            answer = a

    ret = mcts.run_search(prob1a, stop_rlz)

    assert ret['stopped_on'] == 'found' and ret[
        'tot_walks'] == 5 and ret['tot_elapsed'] < 10

    N = 0
    for src, evl, ela, n_w, prd in zip(ret['source'], ret['evaluation'],
                                       ret['elapsed'], ret['num_walks'],
                                       ret['prediction']):
        code = mcts.compile(Source(src))

        assert code.type == Block.type_no_error

        assert evl[IDX_PIC_REACH_MIN] == EVAL_FULL_MATCH

        assert ela < 10

        assert n_w <= 5

        for pic in prd:
            assert np.array_equal(np.array(pic, dtype=np.int32), answer.data)

        N += 1

    assert N == 3

    ret = mcts.run_search(prob1b, stop_rlz)

    assert ret['stopped_on'] == 'found' and ret[
        'tot_walks'] == 5 and ret['tot_elapsed'] < 10

    answer = None
    for example in prob2:
        _, a, is_test = example.data
        if is_test:
            answer = a

    root = MctsNode()

    ret = mcts.run_search(prob2, stop_rlz, root=root)

    assert ret['tot_walks'] < 100 and ret['tot_elapsed'] < 10

    N = 0
    for src, evl, ela, n_w, prd in zip(ret['source'], ret['evaluation'],
                                       ret['elapsed'], ret['num_walks'],
                                       ret['prediction']):
        code = mcts.compile(Source(src))

        assert code.type == Block.type_no_error
        assert ela < 10
        assert n_w <= 100

        N += 1 * (evl[IDX_PIC_REACH_MIN] == EVAL_FULL_MATCH)

        assert ret['stopped_on'] == 'time' or N > 0

    root.print(field=mcts)
    last = root.children[-1]
    last.print()

    ret = mcts.run_search(probX, stop_rlz)

    assert ret['stopped_on'] == 'time' and ret['tot_walks'] < 100 and ret[
        'tot_elapsed'] < 10

    stop_rlz['broken_threshold'] = 1
    stop_rlz['max_broken_walks'] = 4

    ret = mcts.run_search(probX, stop_rlz)

    assert ret['stopped_on'] == 'lost' and ret['tot_walks'] < 100 and ret[
        'tot_elapsed'] < 10