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_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_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_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_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 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_add_reflexive_edges_two_agents(): worlds = [World('WR', {}), World('RW', {})] relations = {'1': {('WR', 'RW')}, '2': {('RW', 'WR')}} expected_relations = { '1': {('WR', 'RW'), ('WR', 'WR'), ('RW', 'RW')}, '2': {('RW', 'WR'), ('RW', 'RW'), ('WR', 'WR')} } relations = Model.add_reflexive_edges(worlds, relations) assert expected_relations == relations
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 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_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 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 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 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 createWorld(CardP1S1, CardP1S2, CardP2S1, CardP2S2): #create a world object for ML mlsolver name = createName(CardP1S1, CardP1S2, CardP2S1, CardP2S2) truth_values = {} truth_values["P1S1" + str(CardP1S1)] = True truth_values["P1S2" + str(CardP1S2)] = True truth_values["P2S1" + str(CardP2S1)] = True truth_values["P2S2" + str(CardP2S2)] = True return World(name, truth_values)
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_solve_with_model_second_ann(): wise_men_model = WiseMenWithHat() ks = wise_men_model.ks model = ks.solve(wise_men_model.knowledge_base[1]) model = model.solve(wise_men_model.knowledge_base[3]) worlds_expected = [ 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 }), ] relations_expected = { '1': {('RWR', 'WWR'), ('WRR', 'RRR')}, '2': {('RWR', 'RRR'), ('WRR', 'WWR')}, '3': set() } 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 __init__(self, n_players, simulation=True, n_f_wins=5, n_l_wins=5): self.n_players = n_players self.n_fascists = fascist_dist[self.n_players] self.fascist_wins_needed = n_f_wins self.liberal_wins_needed = n_l_wins self.president = -1 self.chancellor = -1 self.liberal_wins = 0 self.fascist_wins = 0 self.game_state = 'Start' self.winner = 0 self.done = False self.lock = 0 self.simulation = simulation self.votes = {n: False for n in range(n_players)} 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) 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.relations = { # Just the liberal relations n: [(str(x), str(y)) for x, y in list(combinations((range(self.n_worlds)), 2))] for n in range(self.n_fascists, self.n_players) } # Add the fascist relations for n in range(self.n_fascists): self.relations.update({n: []}) self.relations.update( self.add_reflexive_edges(self.worlds, self.relations)) self.relations.update(self.add_symmetric_edges(self.relations)) self.fascists = sample(list(range(self.n_players)), self.n_fascists) t1 = threading.Thread(target=self.run_game) t1.start()
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 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 __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_ks_one_world(): worlds = [World('1', {'p': True})] relations = {} ks = KripkeStructure(worlds, relations) atom = Atom('p') assert True == atom.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_true(): worlds = [World('1', {'p': True})] relations = {} ks = KripkeStructure(worlds, relations) mpl = Diamond(Atom('p')) assert False == mpl.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_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_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_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_eq_empty_one_empty_world(): ks_one = KripkeStructure([World('s', {'p': True})], {}) ks_two = KripkeStructure([], {}) assert not ks_one == ks_two
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')