Esempio n. 1
0
 def satisfy_apart_constraint(self, person_ids: set):
     sorted_person_ids = sorted(
         random_shuffle(person_ids),
         key=lambda person_id: self.pairing_counts_map[person_id]
     )
     # IDs of groups that are not full
     candidate_group_ids = {
         group_id for group_id, person_ids in self.entry.items()
         if len(person_ids) < self.group_sizes[group_id]
     }
     for person_id in sorted_person_ids:
         group_id = self.get_group_id(person_id)
         # if the person is not already inserted
         if not group_id:
             best_group_ids = self.get_best_group_ids_by_group_occurrences(
                 person_ids={person_id}, candidate_group_ids=candidate_group_ids
             )
             group_id = random_choice(best_group_ids)
             self.entry[group_id].add(person_id)
             self.remaining_person_ids.remove(person_id)
         candidate_group_ids.remove(group_id)
         # if there are no more groups to insert the person,
         # we cannot satisfy the constraint further
         if not candidate_group_ids:
             return
Esempio n. 2
0
    def insert_remaining_persons(self):
        # sort the person IDs according to their number of past pairings
        sorted_remaining_person_ids = sorted(
            random_shuffle(self.remaining_person_ids),
            key=lambda person_id: self.pairing_counts_map[person_id]
        )

        # put the remaining persons in non-full groups
        # in a way to minimize the average number of occurrences with other persons/groups
        while sorted_remaining_person_ids:
            # get the person ID with the lowest number of past pairings
            person_id = sorted_remaining_person_ids.pop(0)
            # get the set of candidate group IDs
            best_group_ids = self.get_best_group_ids_by_person_occurrences(person_ids={person_id})
            # if no group was found
            if not best_group_ids:
                best_group_ids = self.get_best_group_ids_by_group_occurrences(person_ids={person_id})
            # if there is a tie
            elif len(best_group_ids) > 1:
                best_group_ids = self.get_best_group_ids_by_group_occurrences(
                    person_ids={person_id}, candidate_group_ids=best_group_ids
                )
            # in case there is still a tie, choose randomly
            group_id = random_choice(best_group_ids)
            self.entry[group_id].add(person_id)
Esempio n. 3
0
 def epsilon_greedy(self, board, player, valid_action, ts):
     """
     使用epsilon-greedy策略选择下一步动作
     以epsilon以内的概率随机选择动作
     以1-epsilon的概率选择最大Q值的动作
     :return: 下一个动作: (from,to)
     """
     if np.random.random() > self.epsilon:
         # 选取Q值最大的
         action, q = ts.predict(board, player)
         return action, q
     else:
         # 随机选择
         return util.random_choice(valid_action), None
Esempio n. 4
0
    def return_times(self, start_node, K):
        k = 0
        t = 0
        u = start_node  # Current node
        self.accumulator = self.accum_func(u)

        while k < K:
            # Compute un-normalised transition probabilities
            w = map(lambda v: self.ew_func(u, v), self.G.neighbors_iter(u))
            next_node = random_choice(self.G.neighbors(u), w)

            if next_node == start_node:
                yield (k + 1, t + 1, self.accumulator)
                k += 1

            self.accumulator += self.accum_func(next_node)

            t += 1
            u = next_node
Esempio n. 5
0
    def insert_couples_with_least_occurrences(self):
        # sort the person-person pairings using 2 keys:
        # - primary: number of occurrences where the 2 persons have been together
        # - secondary: minimum number of pairings for the 2 persons
        sorted_person_occurrences = sorted(
            random_shuffle(self.person_occurrences_map.values()),
            key=lambda item: (
                item['count'],
                min(
                    self.pairing_counts_map[item['person1Id']],
                    self.pairing_counts_map[item['person2Id']]
                )
            )
        )

        empty_group_ids = {
            group_id for group_id in self.group_sizes.keys()
            if not self.entry[group_id]
        }
        # insert in empty groups couples of persons who have been the least frequently together
        while len(self.remaining_person_ids) >= 2:
            person_occurrence = sorted_person_occurrences.pop(0)
            person1_id = person_occurrence['person1Id']
            person2_id = person_occurrence['person2Id']
            # if the two persons have not been already inserted
            if person1_id in self.remaining_person_ids and person2_id in self.remaining_person_ids:
                person_ids = {person1_id, person2_id}
                best_group_ids = self.get_best_group_ids_by_group_occurrences(
                    person_ids=person_ids, candidate_group_ids=empty_group_ids
                )
                if not best_group_ids:
                    return
                group_id = random_choice(best_group_ids)
                self.entry[group_id].update(person_ids)
                self.remaining_person_ids.remove(person1_id)
                self.remaining_person_ids.remove(person2_id)
                empty_group_ids.remove(group_id)
Esempio n. 6
0
K = 10
m = 1000
l = 100

s = 3

X, ground_truth = read_usps('data/zip.train')

acc = 0

for t in range(5):
    #anchors, Z = random_anchors.find(X, m, 4, s)
    #anchors, Z = kmeans_anchors.find(X, m, 4, s)
    #anchors, Z = fuzzy_cmeans_anchors.find(X, m, 1.10)
    anchors, Z = LAE.find(X, m, s)

    labeled = util.random_choice(l, K, ground_truth)
    Y = np.zeros((l, K))
    for id, real_id in enumerate(labeled):
        Y[id, ground_truth[real_id]] = 1

    pred = anchors_SSL.predict(0.040, Z, labeled, Y)

    current_acc = util.accuracy(ground_truth, pred)
    acc += current_acc

    print("[Try #" + str(t) + "] " + str(current_acc))

print("Average accuracy over 5 tries: " + str(acc / 5))
Esempio n. 7
0
 def satisfy_together_constraint(self, person_ids: set, mandatory_group_id: str=None, forbidden_group_ids: set=None):
     sorted_person_ids = sorted(
         random_shuffle(person_ids),
         key=lambda person_id: self.pairing_counts_map[person_id]
     )
     already_used_group_ids = set()
     not_inserted_person_ids = []
     for person_id in sorted_person_ids:
         group_id = self.get_group_id(person_id)
         if group_id:
             already_used_group_ids.add(group_id)
         else:
             not_inserted_person_ids.append(person_id)
     # if there are some groups where some of the persons of the constraint have already been inserted,
     # use them in priority
     if already_used_group_ids:
         for person_id in not_inserted_person_ids:
             best_group_ids = self.get_best_group_ids_by_person_occurrences(
                 person_ids={person_id}, candidate_group_ids=already_used_group_ids
             )
             # if there are still groups to insert the person
             if best_group_ids:
                 if len(best_group_ids) > 1:
                     best_group_ids = self.get_best_group_ids_by_group_occurrences(
                         person_ids={person_id}, candidate_group_ids=best_group_ids
                     )
                 group_id = random_choice(best_group_ids)
                 self.entry[group_id].add(person_id)
                 self.remaining_person_ids.remove(person_id)
     else:
         # if there is a mandatory group
         if mandatory_group_id:
             for person_id in sorted_person_ids:
                 # if the group is not full
                 if len(self.entry[mandatory_group_id]) < self.group_sizes[mandatory_group_id]:
                     self.entry[mandatory_group_id].add(person_id)
                     self.remaining_person_ids.remove(person_id)
         else:
             candidate_group_ids = set(self.group_sizes.keys())
             # if there are forbidden groups
             if forbidden_group_ids:
                 candidate_group_ids -= forbidden_group_ids
             best_group_ids = set()
             while not best_group_ids:
                 best_group_ids = self.get_best_group_ids_by_person_occurrences(
                     person_ids=set(sorted_person_ids), candidate_group_ids=candidate_group_ids
                 )
                 if not best_group_ids:
                     best_group_ids = self.get_best_group_ids_by_group_occurrences(
                         person_ids=set(sorted_person_ids), candidate_group_ids=candidate_group_ids
                     )
                     if not best_group_ids:
                         sorted_person_ids.pop()
             if len(best_group_ids) > 1:
                 best_group_ids = self.get_best_group_ids_by_group_occurrences(
                     person_ids=best_group_ids, candidate_group_ids=candidate_group_ids
                 )
             group_id = random_choice(best_group_ids)
             person_ids = set(sorted_person_ids)
             self.entry[group_id].update(person_ids)
             self.remaining_person_ids -= person_ids