Пример #1
0
def statespace(t, parameterDP):
    '''the set of states'''

    inventory = []
    limit = parameterDP.MaxInventory
    partial = np.zeros(parameterDP.N)
    utils.enumerate(inventory, limit, partial, 0)

    result = []
    for elem in inventory:
        result.append(utils.State(t, elem))
    return result
Пример #2
0
    def __init__(self, model_name, env):
        super(DQNAgent, self).__init__(model_name, env)
        self.episode = self.configs.episode
        self.batch_size = self.configs.batch_size
        self.gamma = self.configs.gamma
        self.eps_start = self.configs.eps_start
        self.eps_end = self.configs.eps_end
        self.eps_decay = self.configs.eps_decay
        self.target_update_episode = self.configs.target_update_episode

        self.model_path = self.configs.save_path
        self.save_episode = self.configs.save_episode
        self.plot_episode = self.configs.plot_episode

        self.policy_net = models.DQN(self.configs, env).to(self.device)
        self.target_net = models.DQN(self.configs, env).to(self.device)
        self.load_model(self.model_path)
        self.optimizer = optim.Adam(
            self.policy_net.parameters(),
            lr=self.configs.optimizer_lr,
            betas=(self.configs.optimizer_beta1, self.configs.optimizer_beta2),
            eps=self.configs.optimizer_eps,
            weight_decay=self.configs.optimizer_weight_decay)
        self.memory = utils.ReplayMemory(10000)
        self.num_random_choose = 0

        self.num_choice_per_dim = self.configs.num_choice_per_dim
        self.action_dim = env.action_spec().shape
        self.action_min = env.action_spec().minimum
        self.action_max = env.action_spec().maximum

        self.action_space = utils.enumerate(self.num_choice_per_dim,
                                            self.action_min, self.action_max)
Пример #3
0
def find_closest(location, centroids):
    """Return the centroid in centroids that is closest to location.
    If multiple centroids are equally close, return the first one.

    >>> find_closest([3.0, 4.0], [[0.0, 0.0], [2.0, 3.0], [4.0, 3.0], [5.0, 5.0]])
    [2.0, 3.0]
    """
    # BEGIN Question 3
    d = {i : distance(location, c) for (i, c) in enumerate(centroids)}
    return centroids[min(d, key=lambda k: d[k])]
Пример #4
0
def find_closest(location, centroids):
    """Return the centroid in centroids that is closest to location.
    If multiple centroids are equally close, return the first one.

    >>> find_closest([3.0, 4.0], [[0.0, 0.0], [2.0, 3.0], [4.0, 3.0], [5.0, 5.0]])
    [2.0, 3.0]
    """
    # BEGIN Question 3
    min_key = key_of_min_value(
        dict(enumerate([distance(location, c) for c in centroids])))
    return centroids[min_key]
Пример #5
0
def find_closest(location, centroids):
    """Return the centroid in centroids that is closest to location.
    If multiple centroids are equally close, return the first one.

    >>> find_closest([3.0, 4.0], [[0.0, 0.0], [2.0, 3.0], [4.0, 3.0], [5.0, 5.0]])
    [2.0, 3.0]
    """
    # BEGIN Question 3
    "*** YOUR CODE HERE ***"
    return centroids[min(enumerate(
        [distance(location, centroid) for centroid in centroids]),
                         key=lambda x: x[1])[0]]
Пример #6
0
def best_predictor(user, restaurants, feature_fns):
    """Find the feature within feature_fns that gives the highest R^2 value
    for predicting ratings by the user; return a predictor using that feature.

    Arguments:
    user -- A user
    restaurants -- A list of restaurants
    feature_fns -- A sequence of functions that each takes a restaurant
    """
    reviewed = user_reviewed_restaurants(user, restaurants)
    # BEGIN Question 8
    return max(enumerate([find_predictor(user, reviewed, fn) for fn in feature_fns]), key=lambda e: e[1][1])[1][0]
Пример #7
0
def setofaction(t, parameterDP, current_state):
    '''return the set of actions which can be adopted according to 
    capacity and the current state current_state'''

    try:
        if current_state.period != t - 1:
            raise Exception
    except:
        utils.printErrorAndExit('setofaction')

    temp = np.array(parameterDP.MaxInventory) - np.array(
        current_state.inventory)
    limit = utils.minElementwise(parameterDP.MaxOrder, temp.tolist())
    order = []
    partial = np.zeros(len(limit))
    utils.enumerate(order, limit, partial, 0)

    result = []
    for elem in order:
        result.append(utils.Action(t, elem))
    return result
Пример #8
0
def find_closest(location, centroids):
    """Return the centroid in centroids that is closest to location.
    If multiple centroids are equally close, return the first one.

    >>> find_closest([3.0, 4.0], [[0.0, 0.0], [2.0, 3.0], [4.0, 3.0], [5.0, 5.0]])
    [2.0, 3.0]
    """
    # BEGIN Question 3
    # so this beautiful list comprehension takes the distances between the given location and the centroids, enumerates them,
    # finds the lowest distance, accesses the index of the said distance, and uses that for accessing the closest centroid
    # yes, it's complicated, but it's too beautiful to delete it
    return centroids[min(enumerate(
        [distance(location, centroid) for centroid in centroids]),
                         key=lambda x: x[1])[0]]
def k_means(restaurants, k, max_updates=100):
    """Use k-means to group restaurants by location into k clusters."""
    assert len(restaurants) >= k, 'Not enough restaurants to cluster'
    old_centroids, n = [], 0

    # Select initial centroids randomly by choosing k different restaurants
    centroids = [restaurant_location(r) for r in sample(restaurants, k)]

    while old_centroids != centroids and n < max_updates:
        old_centroids = centroids
        # BEGIN Question 6
        clusterslist = group_by_centroid(restaurants, centroids)
        for i, cluster in enumerate(group_by_centroid(restaurants, centroids)):
            centroids[i] = find_centroid(cluster)
        # END Question 6
        n += 1
    return centroids
Пример #10
0
    def GetHierarchy(self, bases):       
        '''Parses the string "bases" from the xml into a list of tuples of Base
        instances. The first tuple is the most direct inheritance, and then it
        goes up in the hierarchy. 
        '''

        if bases is None:
            return []
        base_names = bases.split()
        this_level = []      
        next_levels = []
        for base in base_names:
            # get the visibility
            split = base.split(':')
            if len(split) == 2:
                visib = split[0]
                base = split[1]
            else:
                visib = Scope.public                            
            decl = self.GetDecl(base) 
            if not isinstance(decl, Class):
                # on windows, there are some classes which "bases" points to an
                # "Unimplemented" tag, but we are not interested in this classes
                # anyway
                continue
            base = Base(decl.FullName(), visib)
            this_level.append(base)
            # normalize with the other levels
            for index, level in enumerate(decl.hierarchy):
                if index < len(next_levels):
                    next_levels[index] = next_levels[index] + level
                else:
                    next_levels.append(level)
        hierarchy = []
        if this_level:
            hierarchy.append(tuple(this_level))
        if next_levels:
            hierarchy.extend(next_levels)
        return hierarchy
Пример #11
0
def group_by_centroid(restaurants, centroids):
    """Return a list of clusters, where each cluster contains all restaurants
    nearest to a corresponding centroid in centroids. Each item in
    restaurants should appear once in the result, along with the other
    restaurants closest to the same centroid.
    """
    # BEGIN Question 4

    #enumerate for later binding
    centList = enumerate(centroids)

    #find correct centroid for each restaurant, return ordered list of centroids
    centForRestaurant = [find_closest(restaurant_location(restaurants[i]), centroids)
    for i in range(0, len(restaurants))]

    #swap centroid for key of centroid in centList
    centroidIndex = [centroids.index(n) for n in centForRestaurant]

    #associate restaurant with key of closest centroid
    restMapping = zip(centroidIndex, restaurants)

    #return cluster using group_by_first
    return group_by_first(restMapping)