def shapley_values_inefficient(map_player_to_cost: dict): """ Calculates the Shapley values for all players in an instance of the airport problem. NOTE: values are calculated inefficiently, by creating an instance of the generic Shapley value problem. This is done for demonstration purposes only. :param map_player_to_cost: a dict where each key is a char representing a single player, and its value is the cost of that player (alone). :return: a dict where each key is a single char representing a player, and each value is the player's Shapley value. >>> stringify(shapley_values_inefficient({"a": 3})) '{a:3.0}' >>> stringify(shapley_values_inefficient({"a": 3, "b": 23})) '{a:1.5, b:21.5}' >>> stringify(shapley_values_inefficient({"a": 3, "b": 23, "c": 123})) '{a:1.0, b:11.0, c:111.0}' """ all_players = map_player_to_cost.keys() map_subset_to_cost = { "".join(sorted(subset)): max([map_player_to_cost[player] for player in subset]) for subset in powerset.powerset(all_players) if len(subset) > 0 } map_subset_to_cost[""] = 0 return shapley.values(all_players, map_subset_to_cost)
def shapley_values_inefficient(road_graph: DiGraph, path: list): """ Calculates the Shapley values for all players in an instance of the ride-sharing problem. NOTE: values are calculated inefficiently, by constructing an instance of the generic Shapley value problem. This is done for demonstration purposes only. :param road_graph: a weighted directed graph, representing travel costs between destinations. :param path: the first element is the source; then comes the list of passangers, in the fixed order by which they should be dropped from the taxi. :return: a dict where each key is a single char representing a passanger, and each value is the player's Shapley value. >>> road_graph = DiGraph() >>> road_graph.add_edge("0", "a", weight=5) >>> road_graph.add_edge("0", "b", weight=9) >>> road_graph.add_edge("a", "b", weight=6) >>> road_graph.add_edge("b", "a", weight=6) >>> stringify(shapley_values_inefficient(road_graph, ["0", "a", "b"])) '{a:3.5, b:7.5}' >>> stringify(shapley_values_inefficient(road_graph, ["0", "b", "a"])) '{a:5.5, b:9.5}' """ source = path[0] players = path[1:] map_subset_to_cost = { "".join(sorted(subset)): path_cost(road_graph, [source] + sublist(players, subset)) for subset in powerset.powerset(players) if len(subset) > 0 } map_subset_to_cost[""] = 0 return shapley.values("".join(players), map_subset_to_cost)
def factors(n) : import powerset, factorial f = primeFactors(n) result = [1, n] + f if len(f) : for s in powerset.powerset(f) : if len(s) : p = factorial.product(s) result.append(n/p) return sorted(set(result))
def RemoveTriples(self, rcb, rcbNum, twins) : i = 0 sameBox = self.UsingSameBox(twins) # r is a powerset of integers (square numbers) for squares in a region that are not filled for r in powerset.powerset(twins) : if 1 < len(r) < len(twins) : others = self.AllRelated(rcb, rcbNum) cs = self.ComplementSet(others, i) rv = self.SetOfPossibleValues(r) csv = self.SetOfPossibleValues(cs)
def SolveOthers(self, rcb, rcbNum) : others = self.AllRelated(rcb, rcbNum) i = 0 # r is a powerset of integers (square numbers) for squares in a region that are not filled for r in powerset.powerset(others) : if 0 < len(r) < len(others) : # s is a set of the possible values of squares indexed by r s = self.SetOfPossibleValues(r) if len(s) == len(r) : cs = self.ComplementSet(others, i) for c in cs : valuesToRemove = s.intersection(set(self.possibleValues[c])) if len(valuesToRemove) : for ss in valuesToRemove : self.possibleValues[c].remove(ss) self.AppendToQueue(c) if len(r) >= 3 : self.RemoveTriples(rcb, rcbNum, r) i += 1
lowers = 0 uppers = 0 for char in s: if char.islower(): lowers += 1 elif char.isupper(): uppers += 1 return lowers, uppers # Exercise 46 def ten_base_number(n): result = [] # create an empty set while (n > 0): # while our number is positive result |= {n % 10} # we add the modulo 10 of the number IF he's not already in our set n = n // 10 # now we can divide our number by 10 to "split it" return result # Exercise 47 def words_matches(text1, text2): words1 = text1.split(' ') words2 = text2.split(' ') result = set(words1) & set(words2) print(result) import powerset if __name__ == '__main__': print([x for x in powerset.powerset([1, 2, 3])])
l = [i] superdict[tuple(l)]=[csupport([items[hash_table[i]]]),1] true_candidates = [] candidates= [] for i in hash_table: l=[i] candidates.append(l) for i in range(1,max_length): true_candidates=[] candidates=k_itemset(candidates,i) for j in candidates: flag = 1 l = powerset(j, len(j)-1) for p in l: if tuple(p) in superdict: continue else: flag=0 break if flag==1: sup=[] for k in j: sup.append(items[hash_table[k]]) if csupport(sup)>min_sup: superdict[tuple(j)]=[csupport(sup),len(j)] true_candidates.append(j) candidates=true_candidates
import itertools import Unlockattempt import powerset import time start_time = time.time() persons = 4 number_for_access = 3 # Puzzle: # Can one lock a box with multiple locks and distribute keys to the boxes (potentially more than one key per box) to a number of persons, such that the box can be unlocked [number_for_access] persons, but cannot be unlocked by [number_for_access]-1 persons? locks = 1 while True: key_list = powerset.powerset(range(persons)) for i in itertools.product(key_list, repeat=locks): attempt = Unlockattempt.Unlockattempt(i,persons,number_for_access) #print(i) if attempt.check_unlockable_combination(): print (attempt.locks) print("--- %s seconds ---" % (time.time() - start_time)) exit() locks=locks+1
s0 = min(homeConnects[0]) s2 = 6 - s0 - s1 return [s0 - 1, s2 - 1, s1 - 1] if __name__ == '__main__': class Spam: def __init__(self, size): self.size = size class Foo: def __init__(self, sizes): self.markers = [Spam(x) for x in sizes] def __str__(self): return '(' + ','.join([str(x.size) for x in self.markers]) + ')' print( "This is bad style, but if you aren't me (the programmer) this file doesn't work as a script" ) import sys sys.path.append('/home/jonathan/whome/GoogleDrive/ProjectEuler/lib/') from powerset import powerset for m0 in powerset(range(1, 4), b=2): h0 = Foo(m0) for m1 in powerset(range(1, 4), b=2): h1 = Foo(m1) p = optPermute([h0, h1]) print(h0, [x + 1 for x in p], h1)
def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument("values", nargs="+") args = parser.parse_args() print(powerset(args.values))