def cross_over(self, other, mutate_prob, cross_prob): self_alleles = self.copy_alleles() other_alleles = other.copy_alleles() cross = flip_coin(cross_prob) point = random.randint(0, len(self.alleles)) if cross else 0 #point = self.num_bits_x if cross else 0 son = self.copy() son.alleles = self_alleles[:point] + other_alleles[point:] son.father = self son.mother = other son.point = point if cross else None daughter = self.copy() daughter.alleles = other_alleles[:point] + self_alleles[point:] daughter.father = other daughter.mother = self daughter.point = point if cross else None if cross: son.mutate(mutate_prob) daughter.mutate(mutate_prob) return son, daughter
def get_action(self, state): """ Compute the action to take in the current state. With probability self.epsilon, we should take a random action and take the best policy action otherwise. Note that if there are no legal actions, which is the case at the terminal state, you should choose None as the action. HINT: You might want to use util.flip_coin(prob) HINT: To pick randomly from a list, use random.choice(list) """ # Pick Action legal_actions = self.get_legal_actions(state) action = None "*** YOUR CODE HERE ***" """ if there are no legal actions then return action, which is set to none Use the utlity function to flip a coin and choose to return a random action or use the current policy for the next action based on the return value from the flip_coin function """ if len(legal_actions) == 0: return action if util.flip_coin(self.epsilon): return random.choice(legal_actions) else: return self.get_policy(state)
def get_proxy(self, request_url): domain = parse_domain(request_url) queue = self.storage_mgr.redis_mgr.get_queue_by_domain(domain) rdq_active = RedisDetailQueue(queue_key=queue.queue_key, active=True) rdq_inactive = RedisDetailQueue(queue_key=queue.queue_key, active=False) logging.info("active queue count: %s" % rdq_active.length()) logging.info("inactive queue count: %s" % rdq_inactive.length()) use_active = True clone_seed = flip_coin(SEED_FREQUENCY) if rdq_active.length() < MIN_ACTIVE: self.load_from_seed_queue(queue) elif clone_seed: self.load_from_seed_queue(queue, num=1) if rdq_active.length() < MIN_ACTIVE: use_active = False if flip_coin(INACTIVE_PCT): use_active = False if use_active and rdq_active.length() > 0: logging.info("using active queue") draw_queue = rdq_active else: logging.info("using inactive queue") draw_queue = rdq_inactive detail = None detail = draw_queue.dequeue(requeue=False) proxy = ProxyObject(self.storage_mgr, detail) proxy.dispatch(rdq_active, rdq_inactive) return proxy
def epsilon_greedy(self, state, bestMove): if not bestMove: legalActions = self.board.get_possible_actions() move = None randomMoveProb = flip_coin(self.epsilon) if len(legalActions) > 0: if randomMoveProb > (1-randomMoveProb): move = self.find_move_from_qvalues(state) return move else: move = random.choice(legalActions) return move return None else: legalActions = self.board.get_possible_actions() move = None if len(legalActions) > 0: move = self.find_move_from_qvalues(state) return move return None
def mutate(self, mutate_prob): for idx, value in enumerate(self.alleles): if flip_coin(mutate_prob): self.alleles[idx] = '0' if value == '1' else '1'