Exemplo n.º 1
0
 def test_shift_rows2(self):
     a = [[0x01, 0x02, 0x03, 0x04], [0x05, 0x06, 0x07, 0x08],
          [0x09, 0x0a, 0x0b, 0x0c], [0x0d, 0x0e, 0x0f, 0x10]]
     b = [[0x01, 0x02, 0x03, 0x04], [0x06, 0x07, 0x08, 0x05],
          [0x0b, 0x0c, 0x09, 0x0a], [0x10, 0x0d, 0x0e, 0x0f]]
     s = State(a)
     s = s.shift_rows()
     self.assertEqual(s, State(b))
Exemplo n.º 2
0
 def test_eq(self):
     a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
     b = [
         '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
         'e', 'f', '10'
     ]
     c = [int(s, 16) for s in b]
     self.assertEqual(State(a), State(c))
     self.assertEqual(State(a), State(b))
Exemplo n.º 3
0
    def test_expanded_keys(self):
        a = '00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f'.split(' ')
        b = 'd6 aa 74 fd d2 af 72 fa da a6 78 f1 d6 ab 76 fe'.split(' ')
        c = '54 99 32 d1 f0 85 57 68 10 93 ed 9c be 2c 97 4e'.split(' ')
        self.assertEqual(Key(a).round(1), State(b))
        self.assertEqual(Key(a).round(9), State(c))

        a = '3C A1 0B 21 57 F0 19 16 90 2E 13 80 AC C1 07 BD'.split(' ')
        b = '45 64 71 B0 12 94 68 A6 82 BA 7B 26 2E 7B 7C 9B'.split(' ')
        self.assertEqual(Key(a).round(1), State(b))
Exemplo n.º 4
0
 def test_shift_rows2(self):
     a = [
         '0xb1', '0x33', '0xb1', '0x33', '0x5b', '0x85', '0x5b', '0x85',
         '0x5b', '0x85', '0x5b', '0x85', '0x5b', '0x85', '0x5b', '0x85'
     ]
     b = [[0x01, 0x02, 0x03, 0x04], [0x06, 0x07, 0x08, 0x05],
          [0x0b, 0x0c, 0x09, 0x0a], [0x10, 0x0d, 0x0e, 0x0f]]
     s = State(a)
     s = s.shift_rows()
     self.assertEqual(s, State(b))
Exemplo n.º 5
0
 def default_states(self):
     ground = State([DQExc([0, 1], [2, 3])], [0.11176849919227788], 4, 2)
     first_exc_1 = State([SQExc(0, 3)], [1.570796325683595], 4, 2)
     first_exc_2 = State([SQExc(1, 2)], [1.570796325683595], 4, 2)
     first_exc_3 = State(
         [DQExc([0, 1], [2, 3]), SQExc(1, 3)],
         [-numpy.pi / 4, numpy.pi / 2], 4, 2)
     self.H_lower_state_terms = [[factor, state] for factor, state in zip(
         [2.2, 1.55, 1.55, 1.55],
         [ground, first_exc_1, first_exc_2, first_exc_3])]
     return self.H_lower_state_terms
Exemplo n.º 6
0
 def test_shift_rows(self):
     a = [[0x0f, 0x15, 0x71, 0xc9], [0x47, 0xd9, 0xe8, 0x59],
          [0x0c, 0xb7, 0xad, 0xd6], [0xaf, 0x7f, 0x67, 0x98]]
     b = [[0x0f, 0x15, 0x71, 0xc9], [0xd9, 0xe8, 0x59, 0x47],
          [0xad, 0xd6, 0x0c, 0xb7], [0x98, 0xaf, 0x7f, 0x67]]
     s = State(a)
     s = s.shift_rows()
     self.assertNotEqual(s, State(a))
     self.assertEqual(s, State(b))
     s = s.shift_rows().shift_rows().shift_rows()
     self.assertEqual(s, State(a))
Exemplo n.º 7
0
    def setUp(self):
        self.transition = [[0, 2, 3], [3, 0, 4], [3, 4, 0]]
        self.books = [
            Book(0, 1, 5),
            Book(1, 0, 3),
            Book(2, 1, 10),
            Book(3, 1, 2),
            Book(4, 2, 8),
        ]
        self.readers = [
            Reader({
                0: 3,
                1: 2
            }, 0, 10),
            Reader({
                1: 1,
                2: 10,
                3: 3
            }, 0, 10),
            Reader({
                0: 1,
                1: 1,
                2: 1,
                3: 1,
                4: 1
            }, 1, 10),
            Reader({
                2: 5,
                4: 5
            }, 2, 10),
        ]

        self.state = State(self.transition, self.books, self.readers)
Exemplo n.º 8
0
def move():
    if game_over(state_board):
        print("GAME IS OVER")
        response = app.response_class(
            response="game over",
            status=200
        )
        return response

    source = int(request.args.get('from', default=''))
    destination = int(request.args.get('to', default=''))

    # human move
    human_move(source, destination)

    # computer_move
    state = State(state_board, Turn.BLACK, [])
    move = move_function(state)
    computer_move(move)

    response = app.response_class(
        response=BOARD.fen(),
        status=200
    )

    return response
Exemplo n.º 9
0
def init_cmd(force: bool) -> None:
    if state_path.exists() and not force:
        click.echo("state.json already exists, blog already initialized")
        sys.exit(1)

    state = State([Post(0, "test_post", datetime.now(), ["secret_language"])])
    state_path.write_text(json.dumps(state.to_json()))
Exemplo n.º 10
0
    def __init__(
        self,
        output=None,
        eufy=True,
        location=None,
        filename="/home/pi/rpi-terminal-hub/data/eufy.json",
        increment=15,
        autorun=True,
    ):
        schedule = getData("schedule.json")
        locations = getData("locations.json")

        if eufy:
            from src.eufy import Eufy

            self.startup()
            self.eufy = Eufy(filename=filename)
        self.webTime = WebTime()
        self.weather = Weather(locations, location)
        self.alerts = Alerts(location or list(locations.keys())[0])

        self.retry = False
        self.increment = increment
        self.schedule = {
            k: datetime.strptime(schedule[k], "%I:%M%p")
            for k in schedule.keys()
        }
        self.output = output
        self.start = True

        self.data = State()

        if autorun:
            self.loop()
Exemplo n.º 11
0
    def backup(self, cur_visit, visits, reward):

        # get previous state
        prev_visit = visits[len(visits) - 1]
        state_action = list(map(int, prev_visit.split(':')))
        state = State()
        state.dealer = state_action[0]
        state.player = state_action[1]
        action = state_action[2]

        # update eligibility trace (accumulating)

        Fv = self.get_coarse_feature(state, action)
        self.E[Fv == 1] += 1.0
        delta = reward - np.sum(Fv * self.theta)

        state_action = list(map(int, cur_visit.split(':')))
        state.dealer = state_action[0]
        state.player = state_action[1]
        action = state_action[2]

        Fv = self.get_coarse_feature(state, action)
        delta += np.sum(Fv * self.theta)
        self.E *= CONST.LAMBDAS[self.l]

        # update theta
        self.theta += CONST.ALPHA * delta * self.E
Exemplo n.º 12
0
def game_over(board):
    """
    1: num_pieces >=4
    2: num_pieces = 1 -> 1 wins
    """

    b_counter = 0
    w_counter = 0
    for i in range(64):
        if board[i] == "b" or board[i] == "B":
            b_counter += 1
        elif board[i] == "w" or [i] == "W":
            w_counter += 1

    state = State(board, Turn.WHITE, [])

    if w_counter == 0 or (b_counter > 3 and w_counter < 2) or len(state.get_states()) == 0:
        print("GAME OVER! You lost!")
        return True

    if b_counter == 0 or (w_counter > 3 and b_counter < 2):
        print("CONGRATS! You won!")
        return True

    return False
Exemplo n.º 13
0
def test_state_moves(board, roll, expected):
    s = State(board=board, roll=roll)
    actual = {tuple(x.board.board) for x in s.transitions}
    # print(expected - actual)
    # print('123')
    # print(actual - expected)
    assert len(actual) == len(expected)
    assert actual == expected
Exemplo n.º 14
0
 def test_store_multiple(self):
     s = State()
     s.append(PredicateInstance('happy', Instance('a')))
     s.append(PredicateInstance('unhappy', Instance('a')))
     self.assertStateContains(PredicateInstance('happy', Instance('a')),
                              Evaluator(state=s))
     self.assertStateContains(PredicateInstance('unhappy', Instance('a')),
                              Evaluator(state=s))
Exemplo n.º 15
0
    def test_store_and_access(self):
        s = State()
        p = PredicateInstance('happy', Instance('a'))
        s.append(p)

        self.assertEqual([p], s.fetch('happy', [Instance('a')]))
        self.assertStateContains(PredicateInstance('happy', Instance('a')),
                                 Evaluator(state=s))
Exemplo n.º 16
0
def continue_conversation(message):
    history = request.get_json()
    state = State(DICTIONARY_PATH)

    if history:
        state.history = history

    state.eat(message.lower())
    return json.dumps(state.history)
Exemplo n.º 17
0
def run(model):

    # initialise state & environment
    state = State()

    visits = list()

    for episode in range(0, CONST.EPISODES):

        # Initialise state of game
        Environment.init_state(state)

        # clear all visits
        if model.full_backup:
            del visits[:]

        # for TD based tasks
        if not model.full_backup:
            action = model.action(state)

        # Play one game/episode until not Terminal
        while not Environment.terminal(state):

            # Capture State Visit per episode
            if not model.full_backup:
                visits.append("%d:%d:%d" %
                              (state.dealer, state.player, action))
                reward = Environment.step(state, action)

            # Get Action
            action = CONST.STICK if Environment.terminal(state) \
                else model.action(state)

            # Capture All State Visit per Episode
            if model.full_backup:
                visits.append("%d:%d:%d" %
                              (state.dealer, state.player, action))
                reward = Environment.step(state, action)

            # Backup Previous Visit
            if not model.full_backup:
                cur_visit = "%d:%d:%d" % (state.dealer, state.player, action)
                model.backup(cur_visit, visits, reward)
                del visits[:]

        print('\r' + str(model) + ' Episode...' + str(episode + 1), end='')

        if model.full_backup:
            # Record Visit/Reward
            for visit in visits:
                model.backup(visit, reward)

        if not model.full_backup:
            model.record_mse(episode)
Exemplo n.º 18
0
 def test_state_access(self):
     s = State([
         PredicateInstance('happy', Instance('a')),
         PredicateInstance('happy', Instance('a')),
         PredicateInstance('happy', Instance('a'))
     ])
     a = StateAccess(state=s)
     self.assertIsNotNone(a.fetch('happy', [Instance('a')]))
     self.assertIsNotNone(a.fetch('happy', [Instance('a')]))
     self.assertIsNotNone(a.fetch('happy', [Instance('a')]))
     self.assertIsNone(a.fetch('happy', [Instance('a')]))
     self.assertIsNone(a.fetch('happy', [Instance('a')]))
Exemplo n.º 19
0
def computer_move(board: list, turn: Turn):
    """
    Play the computer move
    """

    state = State(board, turn, [])
    print(f"\n{str(turn)} thinking...")
    move = move_function(state)
    jump_move = play_move(board, move, turn)
    print(move)
    print_board(board)
    while jump_move is not None:
        temp = board
        state = State(board, turn, [])
        move = move_function(state)
        jump_move = play_move(board, move, turn)
        if jump_move is None:
            board = temp
        else:
            print(move)
            print_board(board)
Exemplo n.º 20
0
def validate_move(coordinates_input: str, board: list):
    # noinspection PyBroadException
    try:
        tokens = coordinates_input.split()
        src, dest = int(tokens[0]), int(tokens[1])
    except Exception:
        return None

    move = [src, dest]
    state = State(board, Turn.WHITE, [])
    legal_moves = [el.move for el in state.get_states()]
    print(f'Available moves: {legal_moves}')
    if move in legal_moves:
        return move
    else:
        return None
Exemplo n.º 21
0
    def record_mse(self, episode):
        _mse = 0.0
        for dealer in range(1, 10):
            for player in range(1, CONST.EASY21):
                for action in [CONST.HIT, CONST.STICK]:
                    visit = "%d:%d:%d" % (dealer, player, action)
                    if _mse < 0.00000001 and not _mse == 0.0:
                        _mse = 0.0
                    if not _mse > 10000.0:
                        state = State()
                        state.dealer = dealer
                        state.player = player
                        F = self.get_coarse_feature(state, action)
                        Q = np.sum(F * self.theta)
                        _mse += (Q - self.MCQ[visit])**2

        self.mse[episode] = _mse / 10.0 / 21.0 / 2.0
Exemplo n.º 22
0
 def __init__(self,
              environment: Environment,
              epsilon,
              epsilon_decay_rate,
              solver='table',
              q=None):
     self.state = State(environment.observation_space)
     self.action = Action(environment.action_space, epsilon,
                          epsilon_decay_rate)
     if solver == 'table':
         self.q = QFunction(learning_rate, discount_rate)
     elif solver == 'NN_agent':
         self.q = QAgent(learning_rate, discount_rate)
     self.q.initialize(self.state, self.action, q)
     self.episode = QEpisode(environment, self.state, self.action, self.q)
     self.environment = environment
     self.performance = Performance()
Exemplo n.º 23
0
    def ansatz_from_data_frame(data_frame, q_system):
        ansatz_elements = []
        for i in range(len(data_frame)):
            element = data_frame.loc[i]['element']
            element_qubits = data_frame.loc[i]['element_qubits']
            if element[0] == 'e' and element[4] == 's':
                try:
                    ansatz_elements.append(EffSFExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
                except Exception:
                    ansatz_elements.append(EffSFExc(ast.literal_eval(element_qubits)[0][0], ast.literal_eval(element_qubits)[1][0],system_n_qubits=q_system.n_qubits))
            elif element[0] == 'e' and element[4] == 'd':
                ansatz_elements.append(EffDFExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
            elif element[0:3] == 's_f':
                ansatz_elements.append(SFExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
            elif element[0:3] == 'd_f':
                ansatz_elements.append(DFExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
            elif element[0] == 's' and element[2] == 'q':
                try:
                    ansatz_elements.append(SQExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
                except Exception:
                    ansatz_elements.append(SQExc(ast.literal_eval(element_qubits)[0][0], ast.literal_eval(element_qubits)[1][0], system_n_qubits=q_system.n_qubits))
            elif element[0] == 'd' and element[2] == 'q':
                ansatz_elements.append(DQExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
            elif element[:2] == '1j':
                ansatz_elements.append(PauliStringExc(QubitOperator(element), system_n_qubits=q_system.n_qubits))
            elif element[:8] == 'spin_s_f':
                try:
                    ansatz_elements.append(SpinCompEffSFExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
                except TypeError:
                    # new format
                    ansatz_elements.append(SpinCompEffSFExc(ast.literal_eval(element_qubits)[0][0],
                                                            ast.literal_eval(element_qubits)[1][0],
                                                            system_n_qubits=q_system.n_qubits))
            elif element[:8] == 'spin_d_f':
                ansatz_elements.append(SpinCompEffDFExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
            elif element[:8] == 'spin_s_q':
                ansatz_elements.append(SpinCompSQExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
            elif element[:8] == 'spin_d_q':
                ansatz_elements.append(SpinCompDQExc(*ast.literal_eval(element_qubits), system_n_qubits=q_system.n_qubits))
            else:
                print(element, element_qubits)
                raise Exception('Unrecognized ansatz element.')

        var_pars = list(data_frame['var_parameters'])

        return State(ansatz_elements, var_pars, q_system.n_qubits, q_system.n_electrons)
Exemplo n.º 24
0
def run(observe=False):
    if not os.path.exists('./logs'):
        os.makedirs('./logs')

    if not os.path.exists('./model'):
        os.makedirs('./model')

    interface = Interface()
    agent = Agent(interface)
    state = State(agent, interface)
    nn = NNModel()
    model = nn.build_model()

    try:
        nn.trainNetwork(model, state, observe=observe)
    except StopIteration:
        print("No model")
        interface.close()
Exemplo n.º 25
0
    def __init__(self):
        """Initialize game."""

        # Initialize pygame window
        pygame.init()
        pygame.display.set_caption("Snake Game")
        self.scene = pygame.display.set_mode(self.DIMENSIONS)

        # Initialize backend components
        self.background_color = 0, 0, 0
        self.board = Board((self.DIMENSIONS[1] // 10, self.DIMENSIONS[0] // 10))
        self.state = State()
        self.handler = RenderHandler([], self.scene)

        # Initialize game objects
        self.food = Food(self.board)
        self.player = Player(self.DIMENSIONS[0] // 20, self.DIMENSIONS[1] // 20,
                             self.board, self.food, self.state)
Exemplo n.º 26
0
def play():
    state = State()

    env = Environment()
    state.dealer = env.draw(initial=True)
    state.player = env.draw(initial=True)

    print(state)

    while state.player < CONST.STAY_VALUE \
            and not env.check_burst(state.player):

        env.step(state, CONST.HIT)
        print(state)

    if not env.check_burst(state.player):
        env.step(state, CONST.STICK)

    print(state, " Reward: ", env.check_reward(state))
Exemplo n.º 27
0
    def test_input_parser(self):
        ip = InputParser()
        for line in self.input_str.split("\n"):
            ip.parse_line(line)

        st = ip.get_state()
        trans = [[0, 2, 3], [3, 0, 4], [3, 4, 0]]
        books = [
            Book(0, 5, 0),
            Book(1, 3, 0),
            Book(2, 10, 1),
            Book(3, 2, 2),
            Book(4, 8, 2),
        ]
        readers = [
            Reader({
                0: 10,
                1: 2
            }, 0, 32),
            Reader({
                1: 1,
                2: 10,
                3: 3
            }, 0, 32),
            Reader({
                0: 1,
                1: 1,
                2: 1,
                3: 1,
                4: 1
            }, 1, 32),
            Reader({
                2: 5,
                4: 5
            }, 2, 32),
        ]
        expected = State(trans, books, readers)
        self.assertEqual(trans, st._transition)
        self.assertEqual(books, st._books)
        self.assertEqual(readers, st._readers)
        self.assertEqual(0, st._score)
        self.assertEqual(expected, st)
Exemplo n.º 28
0
    def get_state(self, player: Player, enemy_player: Player) -> State:
        """
        Obtiene el estado actual del programa

        Returns:
            State: Estado actual del programa
        """

        ids = []
        for _ in range(8):
            ids.append([None] * 8)

        my_knights = []
        for _ in range(8):
            my_knights.append([None] * 8)

        enemy_knights = []
        for _ in range(8):
            enemy_knights.append([None] * 8)

        my_knights_dict = {}
        enemy_knights_dict = {}

        # Cargando estado
        for id_knight, knight in player.knights.items():
            if not knight.alive:
                continue
            ids[knight.y][knight.x] = id_knight
            my_knights[knight.y][knight.x] = id_knight
            my_knights_dict[id_knight] = (knight.x, knight.y)

        for id_knight, knight in enemy_player.knights.items():
            if not knight.alive:
                continue
            ids[knight.y][knight.x] = id_knight
            enemy_knights[knight.y][knight.x] = id_knight
            enemy_knights_dict[id_knight] = (knight.x, knight.y)

        # Creando y retornando el estado
        return State(ids, my_knights, my_knights_dict, enemy_knights,
                     enemy_knights_dict)
Exemplo n.º 29
0
    def __init__(self, use_hat):
        self.use_hat = use_hat
        self.display_time = self.display_timeout
        self.stop_event = Event() #to be possible to stop threads
        self.request_queue = Queue() #queue of two tuples where the first element is method and the rest are the method's arguments
        self.screen = Screen()
        self.state = State()
        self.wifi = Wifi()
        #self.cd_info = CdInfo(self.stop_event, self._cd_track_count_changed, self._cd_track_names_changed)
        self.bluetooth = Bluetooth()
        self.tenth_scheduler_timer = Timer(self.tenth_scheduler_timeout, self._process_tenth_scheduler_timeout)
        self.player = Player(self.stop_event, self._playing_time_changed, self._playing_filished)
        #self.usb = Usb(self.stop_event, self._usb_path_changed)
        self.file_systems = (MusicFileSystem(), Usb(), CdInfo())
        if use_hat:
            from src.system.hat_io import HatIo
            self.io = HatIo(self.stop_event, self._key_pressed, self._close)
        else:
            from src.system.desktop_io import DesktopIo
            self.io = DesktopIo(self.stop_event, self._key_pressed, self._close, self.screen.image)

        self._fill_list(0)
Exemplo n.º 30
0
def from_json(filename):
    d = DFA()
    d.states = set()
    d.initial_state = None
    with open(filename, 'r') as r_file:
        dfa_data = json.load(r_file)
    d.set_alphabet(set(dfa_data["alphabet"]))
    states = dfa_data["states"]

    for label, state_data in states.items():
        s = State(label, state_data["accepting"], state_data["initial"])
        d.add_state(s)
        if s.initial:
            d.initial_state = s

    for label, state_data in states.items():
        from_state = get_state_by_label(label, d.states)
        # need to iter twice to create transitions - better way might exist
        for transition_label, to_state_label in state_data["outgoing"].items():
            to_state = get_state_by_label(to_state_label, d.states)
            d.add_edge(from_state, to_state, transition_label)

    return d