def scheduler(world_object, country_name, num_output_schedules, depth_bound, frontier_max_size, transforms, transfers): frontier = DEPQ(maxlen=frontier_max_size) # Successors schedules = DEPQ(maxlen=num_output_schedules) # Output schedules initial_state = world_object current_path = initial_state.get_path() initial_state.reset_path() # Our queue is now keeps track of the first step and the final step (because we want to search for best # move several layers deep but only want the first move to be made) frontier.insert((initial_state, initial_state), 0) # While there are states to explore and we still want more schedules while (frontier.is_empty() is not True) and (schedules.size() < num_output_schedules): current_state, current_first_step = frontier.popfirst()[ 0] # just the state not the tuple (state, util) # If we still want to explore further (if not add to list of finished schedules if current_state.get_depth() < depth_bound: successor_states = get_successors(current_state, country_name, transforms, transfers) # Get successors # insert successors by their expected utility if len(successor_states) != 0 and successor_states[0].get_depth( ) == 1: for successor in successor_states: frontier.insert( (successor, successor), successor.expected_utility(country_name, initial_state)) else: for successor in successor_states: frontier.insert( (successor, current_first_step), successor.expected_utility(country_name, initial_state)) else: schedules.insert( (current_state, current_first_step), current_state.expected_utility(country_name, initial_state)) # return schedules_to_string(schedules) this is what we used for our previous runs # There is a problem here where some countries seem to run out of schedules # I am also resetting the path at the start of this method so countries act as if this was their first turn final_state = initial_state if schedules.size() > 0: schedule_tuple = schedules.popfirst()[0] final_state = schedule_tuple[1] else: initial_state.country_passes(country_name) final_state = copy.deepcopy(initial_state) # This adds back the old path + the move that was just made new_path = final_state.get_path() current_path.append(new_path) final_state.set_path(current_path) return final_state
def list_optimize(A_list, beta, iterations, scoreFunc, flipFunc, listSize, minimize=True, A_target=None, numParams=False): factor = -1 deque = DEPQ(iterable=None, maxlen=listSize) if not minimize: factor = 1 for i in range(listSize): deque.insert(deepcopy(A_list[i]), -105) for i in range(iterations): for j in range(listSize): element = deque[j] curr = compose(flipFunc, seeded_rng.randint(len(A_list[0])))(element[0]) count = 0 while not isConnected(curr): curr = compose(flipFunc, seeded_rng.randint(len(A_list[0])))(element[0]) count += 1 if count > 30: curr = element[0] if numParams: paramCount, parameterized = scoreFunc(curr) score = paramCount else: score = scoreFunc(curr, beta, A_target) currScore = factor * score if currScore >= deque.low(): deque.insert(curr, currScore) print( str(i) + "\t" + str(deque.high()) + "\t" + str([x[1] for x in deque])) return deque.high(), deque.first()
def __init__(self, N): self.pq = DEPQ(iterable=None, maxlen=N) self.N = N
def test_init_set_iterable(self): depq_non_default = DEPQ([['last', 1], ['first', 3]]) self.assertEqual(len(depq_non_default), 2) self.assertEqual(is_ordered(depq_non_default), True)
def setUp(self): self.depq = DEPQ() self.random = SystemRandom()
def test_addlast_exceed_maxlen(self): depq_non_default = DEPQ(((None, i) for i in range(5)), 5) depq_non_default.addlast(None, -1) self.assertEqual(depq_non_default.low(), 0)
def test_insert_exceed_maxlen(self): depq_non_default = DEPQ(((None, i) for i in range(5)), 4) self.assertEqual(depq_non_default.low(), 1)