def test_eq_one_agent(): worlds = [World('1', {'p': True}), World('2', {'p': True})] relations = {'a': {('1', '2')}} ks = KripkeStructure(worlds, relations) ks_expected = KripkeStructure( [World('1', {'p': True}), World('2', {'p': True})], {'a': {('1', '2')}}) assert ks_expected.__eq__(ks)
def test_get_power_set_of_worlds(): worlds = [ World('1', {'p': True}), World('2', {'p': True}), World('3', {'p': True}) ] relations = {('1', '2'), ('1', '1'), ('2', '2')} ks = KripkeStructure(worlds, relations) expected_result = [{}, {'1'}, {'2'}, {'3'}, {'1', '2'}, {'1', '3'}, {'2', '3'}, {'1', '2', '3'}] result = ks.get_power_set_of_worlds() for i, j in zip(result, expected_result): assert i == j
def preprocess(self): self.game_state = "Generating Kripke Models" #print("start") combination_numbers = combinations(range(self.n_players), self.n_fascists) self.possible_combinations = [] for combo in combination_numbers: atoms = ["{}=fascist".format(p) for p in combo] + [ "{}=liberal".format(p) for p in range(self.n_players) if p not in combo ] self.possible_combinations.append(atoms) #print("end") self.worlds = [ World("{}".format(idx), {atom: True for atom in atoms}) for idx, atoms in enumerate(self.possible_combinations) ] self.n_worlds = len(self.worlds) self.models = [] for player in range(self.n_players): worlds = copy.deepcopy(self.worlds) relations = { # Just the liberal relations (str(x), str(y)) for x, y in list(combinations((range(self.n_worlds)), 2)) } if self.is_liberal(player) else set() relations.update(self.add_reflexive_edges(worlds, relations)) relations.update(self.add_symmetric_edges(relations)) model = KripkeStructure(worlds, relations) atom = Atom('{}=fascist'.format(player)) if self.is_fascist( player) else Atom('{}=liberal'.format(player)) model = model.solve(atom) self.models.append(model) for player in range(self.n_players): if self.is_fascist(player): for p in range(self.n_players): if self.is_liberal(p): a = Atom('{}=liberal'.format(p)) self.models[player] = self.models[player].solve(a) #print("done") t1 = threading.Thread(target=self.run_game) t1.start()
def test_solve_with_model_first_ann(): wise_men_model = WiseMenWithHat() ks = wise_men_model.ks model = ks.solve(wise_men_model.knowledge_base[1]) worlds_expected = [ World('RRW', { '1:R': True, '2:R': True, '3:W': True }), World('RRR', { '1:R': True, '2:R': True, '3:R': True }), World('WRR', { '1:W': True, '2:R': True, '3:R': True }), World('WWR', { '1:W': True, '2:W': True, '3:R': True }), World('RWR', { '1:R': True, '2:W': True, '3:R': True }), World('WRW', { '1:W': True, '2:R': True, '3:W': True }), ] relations_expected = { '1': {('RRW', 'WRW'), ('RWR', 'WWR'), ('WRR', 'RRR')}, '2': {('RWR', 'RRR'), ('WRR', 'WWR')}, '3': {('RRR', 'RRW'), ('WRW', 'WRR')} } relations_expected.update( Model.add_reflexive_edges(worlds_expected, relations_expected)) relations_expected.update(Model.add_symmetric_edges(relations_expected)) ks_expected = KripkeStructure(worlds_expected, relations_expected) assert ks_expected.__eq__(model)
def test_kripke_structure_init(): worlds = [World('1', {'p': True})] relations = {} ks = KripkeStructure(worlds, relations) assert ks.relations == {} assert ks.worlds[0].name == '1' assert ks.worlds[0].assignment == {'p': True}
def test_semantic_box_a_true(): worlds = [World('1', {'p': False}), World('2', {'p': True})] relations = {'a': {('1', '2'), ('1', '1')}} ks = KripkeStructure(worlds, relations) mpl = Diamond_a('a', Atom('p')) assert True == mpl.semantic(ks, '1')
def test_semantic_box_a_two_agents(): worlds = [World('1', {'p': False}), World('2', {'p': False})] relations = {'a': {('1', '2')}, 'b': {}} ks = KripkeStructure(worlds, relations) mpl = Diamond_a('b', Atom('p')) assert False == mpl.semantic(ks, '1')
def test_semantic_box_a_empty_relations(): worlds = [World('1', {'p': False}), World('2', {'p': True})] relations = {} ks = KripkeStructure(worlds, relations) mpl = Diamond_a('a', Atom('p')) assert False == mpl.semantic(ks, '1')
def kripke_model(game): deck = game.deck.dealt_cards # Worlds worlds = [] for w, p in game.world.possible_worlds.items(): worlds.append( World( w, { str(0) + str(p[0]): True, str(1) + str(p[1]): True, str(2) + str(p[2]): True })) # Relations relations = {0: set([]), 1: set([]), 2: set([])} colors = game.players[0].colors for r_id, r in game.world.relations.items(): relations[colors.index(r[2])].add((r[0], r[1])) # relations.update(add_reflexive_edges(worlds, relations)) # relations.update(add_symmetric_edges(relations)) # Make kripke model ks = KripkeStructure(worlds, relations) # This doesn't work # formula = str(2) + str(('1S', '2S')) # g = Box_a('0', Not(Atom(formula))) # # model = ks.solve(g) print(relations)
def __init__(self, n): self.build_agents(n) print("Agents: ", self.agents) worlds, propositions = self.build_worlds(n) #print("Worlds:", worlds) kripke_worlds = [] self.propositions = propositions #print("Propositions:", propositions) #print() # create World objects for the Kripke structure for world in worlds: kripke_worlds.append(World(world, worlds[world])) # initialize the agent world relations relations = {} for i in range(n): id = str(i) relations[id] = [] for world in worlds: for i in range(n): id = str(i) # if the agent has no murderer in a world, the agent cannot possibly access this world (because the # agent is dead in this case) if (id not in world): continue # an agent only has an accessibility relation to a world where their target is the same # find the current agent's target formulas = worlds[world] for formula in formulas: if (formula[0]) == str(i): break # look for other worlds where the agent has the same target for other_world in worlds: if (formula in worlds[other_world] and id in other_world): relations[id].append((world, other_world)) for r in relations: relations[r] = set(relations[r]) #print("Relations:") self.ks = KripkeStructure(kripke_worlds, relations)
def test_semantic_not_q(): worlds = [ World('1', {'q': False}) ] relations = {('1', '2'), ('1', '3')} ks = KripkeStructure(worlds, relations) mpl = Not(Atom('q')) assert True == mpl.semantic(ks, '1')
def test_remove_node_trivial_case(): worlds = [World('1', {'p': True}), World('2', {'p': True})] relations = {('1', '2')} ks = KripkeStructure(worlds, relations) ks_expected = KripkeStructure([World('2', {'p': True})], {}) ks.remove_node_by_name('1') assert ks_expected.__eq__(ks)
def test_remove_node_one_agent(): worlds = [World('1', {'p': True}), World('2', {'p': True})] relations = {'a': {('1', '2')}} ks = KripkeStructure(worlds, relations) ks_expected = KripkeStructure([World('2', {'p': True})], {'a': set()}) ks.remove_node_by_name('1') assert ks_expected.__eq__(ks)
def test_nodes_not_follow_formula(): worlds = [ World('RWW', { '1:R': True, '2:W': True, '3:W': True }), World('RRW', { '1:R': True, '2:R': True, '3:W': True }) ] relations = {} ks = KripkeStructure(worlds, relations) formula = And(Atom('2:W'), Atom('3:W')) expected_result = ['RRW'] result = ks.nodes_not_follow_formula(formula) assert expected_result == result
def test_semantic_diamond_p_three_worlds_false(): worlds = [ World('1', {'p': False}), World('2', {'p': True}), World('3', {'p': False}), ] relations = {('1', '2'), ('1', '3')} ks = KripkeStructure(worlds, relations) mpl = Diamond(Atom('p')) assert True == mpl.semantic(ks, '1')
def test_semantic_box_p_implies_p(): worlds = [ World('1', {'p': False}), World('2', {'p': True}), World('3', {'p': True}), ] relations = {('1', '2'), ('1', '3')} ks = KripkeStructure(worlds, relations) mpl = Implies(Box(Atom('p')), Atom('p')) assert False == mpl.semantic(ks, '1')
def initialize_model(num_agents, played_cards, top_card, stacks): #Ml solver objects worlds = [] #worlds identifiable by their numbers for building the relations worldsNumValues = [] # All cards that fit on the first stack for cardP1S1 in range(stacks[0][0][-1] + 1, top_card): # Card wasn't seen before if not played_cards.get(cardP1S1, False): # All cards that fit on the second stack for cardP1S2 in range(2, stacks[1][0][-1]): #cannot be played already, and cannot better for stack 1. if (not played_cards.get(cardP1S2, False) and not (cardP1S2 > stacks[0][0][-1] and cardP1S2 < cardP1S1)): for cardP2S1 in range(stacks[0][0][-1] + 1, top_card): if (not played_cards.get(cardP2S1, False) and (cardP2S1 != cardP1S1) and (cardP2S1 != cardP1S2)): for cardP2S2 in range(2, stacks[1][0][-1]): #cannot be played already, cannot better for stack 1 and no duplicates if (not played_cards.get(cardP2S2, False) and not (cardP2S2 > stacks[0][0][-1] and cardP2S2 < cardP2S1) and (cardP2S2 != cardP1S1) and (cardP2S2 != cardP1S2)): #Create the worlds and add them to the numiric value list worlds.append( createWorld(cardP1S1, cardP1S2, cardP2S1, cardP2S2)) worldsNumValues.append([ cardP1S1, cardP1S2, cardP2S1, cardP2S2 ]) #make relations P1relations = set() P2relations = set() #Use numeric value list for relations for world1 in worldsNumValues: for world2 in worldsNumValues: if (world1[0] == world2[0] and world1[1] == world2[1]): P1relations.add( (createNameFromArray(world1), createNameFromArray(world2))) if (world1[2] == world2[2] and world1[3] == world2[3]): P2relations.add( (createNameFromArray(world1), createNameFromArray(world2))) relations = {'1': P1relations, '2': P2relations} return KripkeStructure(worlds, relations)
def __init__(self): # There are only two possible configurations of killer-targer pairs # Note that world 231, for example, stands for the world where 1 targets 2, 2 targets 3, 3 targets 1 worlds = [ World('231', { 't12': True, 't23': True, 't31': True }), World('312', { 't13': True, 't21': True, 't32': True }), ] # In the 3-agent case, from each world only the world itself is accessible for each agents relations = { '1': {('231', '231'), ('312', '312')}, '2': {('231', '231'), ('312', '312')}, '3': {('231', '231'), ('312', '312')} } # Build the Kripke model and store it in ks self.ks = KripkeStructure(worlds, relations)
def test_ks_one_world(): worlds = [World('1', {'p': True})] relations = {} ks = KripkeStructure(worlds, relations) atom = Atom('p') assert True == atom.semantic(ks, '1')
def test_semantic_p_and_q(): worlds = [World('1', {'p': True, 'q': True})] relations = {} ks = KripkeStructure(worlds, relations) mpl = And(Atom('p'), Atom('q')) assert True == mpl.semantic(ks, '1')
def test_atom_is_false_if_q_not_in_V(): worlds = [World('1', {'p': True})] relations = {} ks = KripkeStructure(worlds, relations) atom = Atom('q') assert False == atom.semantic(ks, '1')
def test_semantic_diamond_p_one_world_reflex_edge_false(): worlds = [World('1', {'p': False})] relations = {('1', '1')} ks = KripkeStructure(worlds, relations) mpl = Diamond(Atom('p')) assert False == mpl.semantic(ks, '1')
def test_semantic_diamond_p_one_world_true(): worlds = [World('1', {'p': True})] relations = {} ks = KripkeStructure(worlds, relations) mpl = Diamond(Atom('p')) assert False == mpl.semantic(ks, '1')
def test_semantic_box_p_two_worlds_false(): worlds = [World('1', {'p': False}), World('2', {'p': False})] relations = {('1', '2')} ks = KripkeStructure(worlds, relations) mpl = Box(Atom('p')) assert False == mpl.semantic(ks, '1')
def test_semantic_box_p_one_world_true(): worlds = [World('1', {'p': True})] relations = {} ks = KripkeStructure(worlds, relations) mpl = Box(Atom('p')) assert True == mpl.semantic(ks, '1')
def test_eq_empty_set(): ks_one = KripkeStructure([World('2', {'p': True})], {'a': set()}) ks_two = KripkeStructure([World('2', {'p': True})], {'a': set()}) assert ks_one.__eq__(ks_two)
def test_box_star_three_agents_box_phi_not_hold(): ks = KripkeStructure( [World('1', {'p': True}), World('2', {'p': False}), World('3', {'p': True}), World('4', {'p': True})], {'agent_1': {('1', '2'), ('1', '1')}, 'agent_2': {('1', '3'), ('1', '1')}, 'agent_3': {('1', '4'), ('1', '1')}}) formula = Box_star(Atom('p')) assert not formula.semantic(ks, '1')
def test_box_star_empty_relations(): ks = KripkeStructure([World('1', {'p': True})], {}) formula = Box_star(Atom('p')) assert formula.semantic(ks, '1')
def test_eq_empty_one_empty_world(): ks_one = KripkeStructure([World('s', {'p': True})], {}) ks_two = KripkeStructure([], {}) assert not ks_one == ks_two
def test_ks_wrong_type(): with pytest.raises(TypeError): KripkeStructure("p", []) assert True