Beispiel #1
0
    def _get_probs(self, step: int, score: int,
                   history: pd.DataFrame) -> np.ndarray:
        # Saving the current state
        init_state = random.getstate()
        next_move = -1
        # If there still are multiple candidates
        if len(history) > 0 and len(self.seeds) > 1:
            # Saving previous moves
            self.previous_moves.append(
                int(history.loc[step - 1, "opponent_action"]))
            # Checking each possible seed
            for i in range(len(self.seeds) - 1, -1, -1):
                # Running for previous moves
                random.seed(self.seeds[i])
                for s in range(step):
                    move = random.randint(0, 2)
                    # Testing their move order
                    if move != self.previous_moves[s]:
                        self.seeds.pop(i)
                        break
        # Seed found: Get the next move
        elif len(self.seeds) == 1:
            random.seed(self.seeds[0])
            for _ in range(step):
                move = random.randint(0, 2)
            next_move = random.randint(0, 2)

        # Resetting the state to not interfere with the opponent
        random.setstate(init_state)
        if next_move > -1:
            return one_hot((next_move + 1) % 3)
        return EQUAL_PROBS
Beispiel #2
0
    def _get_probs(self, step: int, score: int,
                   history: pd.DataFrame) -> np.ndarray:

        if len(history) == 0:
            return EQUAL_PROBS
        else:
            self.action = int(history.loc[step - 1, "action"])
            self.my_opp_hist.append(
                (int(history.loc[step - 1, "opponent_action"]), self.action))
            self.opp_hist.append(self.action)

            if self.last_feat is not None:
                this_offset = (self.basis[(self.opp_hist[-1] + 1) %
                                          3]) * self.last_feat.conjugate()
                self.offset = (1 - 0.01) * self.offset + 0.01 * this_offset

            hist_match = self.find_all_longest(self.my_opp_hist, 20)
            if not hist_match:
                pred = 0
            else:
                feat = self.basis[self.opp_hist[hist_match[0].idx]]
                self.last_feat = self.complex_to_probs(
                    feat / abs(feat)) @ self.basis
                pred = self.last_feat * self.offset * cmath.exp(
                    2j * cmath.pi * 1 / 9)

            probs = self.complex_to_probs(pred)
            if probs[np.argmax(probs)] > 0.334:
                return one_hot((int(np.argmax(probs)) + 1) % 3)
            else:
                return probs
Beispiel #3
0
 def _get_probs(self, step: int, score: int,
                history: pd.DataFrame) -> np.ndarray:
     rps_to_text = ("rock", "paper", "scissors")
     act = player(
         [rps_to_text[int(action)] for action in history.loc[:, "action"]],
         [
             rps_to_text[int(action)]
             for action in history.loc[:, "opponent_action"]
         ],
     )
     return one_hot(act)
Beispiel #4
0
    def _get_probs(self, step: int, score: int, history: pd.DataFrame) -> np.ndarray:
        last_action = int(history.loc[step - 1, "action"]) if len(history) > 0 else -1
        # add my_agent's current step to current_memory
        if len(history) > 0:
            last_action = self.previous_action["action"]
            self.update_current_memory(last_action)
            if self.previous_action["action"] != last_action:
                self.previous_action["action"] = last_action
                self.previous_action["action_from_pattern"] = False
                self.previous_action["pattern_group_index"] = None
                self.previous_action["pattern_index"] = None
        """ your ad here """
        # action of my_agent
        my_action = None
        # Removed the random action
        # if it's not first step
        if len(history) > 0:
            # add opponent's last step to current_memory
            self.current_memory.append(int(history.loc[step - 1, "opponent_action"]))
            # previous step won or lost
            previous_step_result = self.get_step_result_for_my_agent(
                last_action, int(history.loc[step - 1, "opponent_action"])
            )
            self.reward += previous_step_result
            # if previous action of my_agent was taken from pattern
            if self.previous_action["action_from_pattern"]:
                self.evaluate_pattern_efficiency(previous_step_result)

        for i in range(len(self.groups_of_memory_patterns)):
            # if possible, update or add some memory pattern in this group
            self.update_memory_pattern(self.groups_of_memory_patterns[i], history, step)
            # if action was not yet found
            if my_action is None:
                my_action, pattern_index = self.find_action(
                    self.groups_of_memory_patterns[i], i
                )
                if my_action is not None:
                    # save action's data
                    self.previous_action["action"] = my_action
                    self.previous_action["action_from_pattern"] = True
                    self.previous_action["pattern_group_index"] = i
                    self.previous_action["pattern_index"] = pattern_index

        # if no action was found
        if my_action is None:
            # choose action randomly
            my_action = random.randint(0, 2)
            # save action's data
            self.previous_action["action"] = my_action
            self.previous_action["action_from_pattern"] = False
            self.previous_action["pattern_group_index"] = None
            self.previous_action["pattern_index"] = None
        return one_hot(my_action)
Beispiel #5
0
    def _get_probs(self, step: int, score: int,
                   history: pd.DataFrame) -> np.ndarray:
        """The main iocaine "move" function."""
        if len(history) == 0:
            them = -1
        else:
            them = int(history.loc[step - 1, "opponent_action"])
            self.histories[0].append(int(history.loc[step - 1, "action"]))

        # histories[0] stores our moves (last one already previously decided);
        # histories[1] stores their moves (last one just now being supplied to us);
        # histories[2] stores pairs of our and their last moves.
        # stats[0] and stats[1] are running counters our recent moves and theirs.
        if them != -1:
            self.histories[1].append(them)
            self.histories[2].append((self.histories[0][-1], them))
            for watch in range(2):
                self.stats[watch].add(self.histories[watch][-1], 1)

        # Execute the basic RNG strategy and the fixed-move strategy.
        rand = random.randrange(3)
        self.predict_random.addguess(them, rand)
        self.predict_fixed.addguess(them, 0)

        # Execute the history and frequency stratgies.
        for a, age in enumerate(ages):
            # For each time window, there are three ways to recall a similar time:
            # (0) by history of my moves; (1) their moves; or (2) pairs of moves.
            # Set "best" to these three timeframes (zero if no matching time).
            best = [recall(age, hist) for hist in self.histories]
            for mimic in range(2):
                # For each similar historical moment, there are two ways to anticipate
                # the future: by mimicing what their move was; or mimicing what my
                # move was.    If there were no similar moments, just move randomly.
                for watch, when in enumerate(best):
                    if not when:
                        move = rand
                    else:
                        move = self.histories[mimic][when]
                    self.predict_history[a][mimic][watch].addguess(them, move)
                # Also we can anticipate the future by expecting it to be the same
                # as the most frequent past (either counting their moves or my moves).
                mostfreq, score = self.stats[mimic].max(age, rand, -1)
                self.predict_frequency[a][mimic].addguess(them, mostfreq)

        # All the predictors have been updated, but we have not yet scored them
        # and chosen a winner for this round.    There are several timeframes
        # on which we can score second-guessing, and we don't know timeframe will
        # do best.    So score all 50 predictors on all 6 timeframes, and record
        # the best 6 predictions in meta predictors, one for each timeframe.
        for meta, age in enumerate(ages):
            best = (-1, -1)
            for predictor in self.predictors:
                best = predictor.bestguess(age, best)
            self.predict_meta[meta].addguess(them, best[0])

        # Finally choose the best meta prediction from the final six, scoring
        # these against each other on the whole-game timeframe.
        best = (-1, -1)
        for meta in range(len(ages)):
            best = self.predict_meta[meta].bestguess(len(self.histories[0]),
                                                     best)

        # And return it.
        return one_hot(best[0])