def _erase_facts_by_ids(fact_ids, author=None): if eternity_dbutils.is_any_type_facts(fact_ids): raise exceptions.InvalidInputValueError( 'Not allowed to delete any to:type') else: ers = Eraser(author=author) ers.erase_facts(fact_ids)
def test_eraser_erases_the_next_occurence_of_text(): initial_text = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" erase_text = "chuck" paper = Paper(initial_text) eraser = Eraser() eraser.erase(paper, erase_text) eraser.erase(paper, erase_text) expected_text = "How much wood would a woodchuck chuck if a wood could wood?" assert paper.buffer == expected_text
def test_erasing_should_erase_opposite_direction_of_the_written_order( self): eraser = Eraser(durability=3) pencil = Pencil(eraser=eraser) pencil.write(self.paper, "I am related to Buffalo Bill") pencil.erase(self.paper, "Bill") self.assertEqual("I am related to Buffalo B ", self.paper.read())
def test_when_pencil_eraser_degrades_fully_it_should_stop_erasing(self): eraser = Eraser(durability=4) pencil = Pencil(eraser=eraser) pencil.write(self.paper, "I am related to Buffalo Bill") pencil.erase(self.paper, "Bill") pencil.erase(self.paper, "Buffalo") self.assertEqual("I am related to Buffalo ", self.paper.read())
def test_when_the_word_to_be_erased_is_not_found_nothing_should_be_erased( self): eraser = Eraser() pencil = Pencil(eraser=eraser) text = "Nothing to erase here" pencil.write(self.paper, text) pencil.erase(self.paper, "Something") self.assertEqual(text, self.paper.read())
def __init__(self, initial_length: int=10, point_value: int=100, eraser_durability: int=10): self.eraser = Eraser(eraser_durability) self.length = initial_length self.sharpened_value = point_value self.point = point_value
class Pencil: def __init__(self, initial_length: int=10, point_value: int=100, eraser_durability: int=10): self.eraser = Eraser(eraser_durability) self.length = initial_length self.sharpened_value = point_value self.point = point_value def erase(self, paper, text: str): self.eraser.erase(paper, text) def edit(self, paper, text: str): sub_size = len(text) pattern = make_re(sub_size) if re.search(pattern, paper.buffer): paper.buffer = re.sub(pattern, r"\1" + text + r"\2", paper.buffer) else: found_spaces = 0 output = "" sub_pos = -1 for c in paper.buffer: if c != " " and sub_pos > 0 and sub_pos < len(text) - 1: output += "@" sub_pos += 1 elif c == " " and found_spaces > 0 and sub_pos <= len(text) - 1: sub_pos += 1 output += text[sub_pos] found_spaces += 1 elif c == " " and found_spaces == 0: output += c found_spaces += 1 elif c != " " and sub_pos == len(text) - 1: output += c elif c == " " and sub_pos <= 0: output += c else: output += c if sub_pos == len(text) - 1: found_spaces = 0 paper.buffer = output def sharpen(self): if self.length < 1: return self.point = self.sharpened_value self.length -= 1 def dull(self, char): if char in (' ', '\n'): return elif char in ascii_lowercase: self.point -= 1 elif char in ascii_uppercase: self.point -= 2 def write(self, paper, text): for char in text: if self.point > 0: paper.write(char) self.dull(char) else: paper.write(" ")
def __init__(self, point_durability=100, length=5, eraser=Eraser(durability=100)): self.point_durability = point_durability self.length = length self.eraser = eraser self._point = point_durability
yToDraw, screen, basicX, basicY) #board.printGrid() if event.type == pygame.MOUSEBUTTONUP: clicking = 0 if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: print("Now using Pincel") ferramenta = Pincel(color) board.ferramenta = ferramenta elif event.key == pygame.K_2: print("Now using Fill") pass elif event.key == pygame.K_3: print("Now using Eraser") ferramenta = Eraser(color) board.ferramenta = ferramenta if event.type == pygame.VIDEORESIZE: width = event.w height = event.h basicX = width // rows basicY = height // columns modifierX = width / columns modifierY = height / rows pygame.display.flip() pygame.quit()
# partisan_type = [False] # attack_list = [democrat_states] # partisan_type = [True] # Get reward matrices att_reward_array, def_reward_array = get_reward_matrices( clean_election_results, state_list, attack_list, electoral_votes, partisan_type) # print(att_reward_array) sec_game = ElectionGame(max_coverage=2, num_attacker_types=len(attack_list), att_reward=att_reward_array, def_reward=def_reward_array) eraser_solver = Eraser(sec_game) tic = time.perf_counter() eraser_solver.solve() toc = time.perf_counter() print(eraser_solver.opt_defender_payoff) print(eraser_solver.opt_coverage) print(f"Completed in {toc - tic:0.4f} seconds ") # tic = time.perf_counter() # for i in range(51): # sec_game = ElectionGame(max_coverage=i+1, num_attacker_types=len( # attack_list), att_reward=att_reward_array, def_reward=def_reward_array) # eraser_solver = Eraser(sec_game) # eraser_solver.solve() # eraser_solver.opt_defender_payoff # toc = time.perf_counter()
def _solve_game_using_eraser(): eraser = Eraser(testing_game) eraser.solve() return eraser
def test_eraser_can_run_out(): paper = Paper("Buffalo Bill") eraser = Eraser(3) eraser.erase(paper, "Bill") assert paper.buffer == "Buffalo B "
def test_eraser(input_text, erase_text, output_text): paper = Paper(input_text) eraser = Eraser() eraser.erase(paper, erase_text) assert paper.buffer == output_text
def setUpClass(self): """ Create Games: 1) Set up a security game, generate corresponding norm_form and harsanyi-transformed norm_form game. 2) Set up a large non-bayesian security game 3) Set up bayesian norm_form, generate corresponding harsanyi- transformed norm_form game. 4) Set up norm_form game. Solve Games: """ # construct games # part 1 self.sec_game = SecurityGame(num_targets=5, max_coverage=3, num_attacker_types=1) self.sec_norm_game = NormalFormGame(game=self.sec_game, harsanyi=False) self.sec_norm_hars_game = NormalFormGame(game=self.sec_norm_game) # part 2 self.large_sec_game = SecurityGame(num_targets=100, max_coverage=30, num_attacker_types=1) # part 3 self.bayse_sec_game = SecurityGame(num_targets=5, max_coverage=3, num_attacker_types=2) self.bayse_sec_norm_game = NormalFormGame(game=self.bayse_sec_game, harsanyi=False) self.bayse_sec_norm_hars_game = NormalFormGame( game=self.bayse_sec_norm_game) # part 4 self.bayse_norm_game = NormalFormGame(num_defender_strategies=10, num_attacker_strategies=3, num_attacker_types=3) self.bayse_norm_hars_game = NormalFormGame(game=self.bayse_norm_game) self.bayse_norm_partial_full_game = NormalFormGame( partial_game_from=self.bayse_norm_game, attacker_types=(0, 1, 2)) self.bayse_norm_partial_game = NormalFormGame( partial_game_from=self.bayse_norm_game, attacker_types=(1, 2)) # part 5 self.norm_game = NormalFormGame(num_defender_strategies=20, num_attacker_strategies=10, num_attacker_types=1) # solve games: # part 1 (non-bayesian security games) print("solving part 1") self.p1_eraser = Eraser(self.sec_game) self.p1_origami = Origami(self.sec_game) self.p1_origami_milp = OrigamiMILP(self.sec_game) self.p1_dobbs = Dobbs(self.sec_norm_game) self.p1_multLP = MultipleLP(self.sec_norm_hars_game) self.p1_multSingLP_sec_game = Multiple_SingleLP(self.sec_game) self.p1_multSingLP_sec_norm_game = Multiple_SingleLP( self.sec_norm_game) self.p1_multSingLP_sec_norm_hars_game = Multiple_SingleLP( self.sec_norm_hars_game) self.p1_eraser.solve() self.p1_origami.solve() self.p1_origami_milp.solve() self.p1_dobbs.solve() self.p1_multLP.solve() self.p1_multSingLP_sec_game.solve() self.p1_multSingLP_sec_norm_game.solve() self.p1_multSingLP_sec_norm_hars_game.solve() # part 2 (large security game) print("solving part 2") self.p2_large_origami = Origami(self.large_sec_game) self.p2_large_origami_milp = OrigamiMILP(self.large_sec_game) self.p2_large_eraser = Eraser(self.large_sec_game) self.p2_large_origami.solve() self.p2_large_origami_milp.solve() self.p2_large_eraser.solve() # part 3 (bayseian security games) print("solving part 3") self.p3_dobbs = Dobbs(self.bayse_sec_norm_game) self.p3_multLP = MultipleLP(self.bayse_sec_norm_hars_game) self.p3_multSingLP = Multiple_SingleLP(self.bayse_sec_game) self.p3_hbgs = HBGS(self.bayse_sec_game) self.p3_hbgs_origami = HBGS(self.bayse_sec_game, True) self.p3_hbgs_norm = HBGS(self.bayse_sec_norm_game) self.p3_dobbs.solve() self.p3_multLP.solve() self.p3_multSingLP.solve() self.p3_hbgs.solve() self.p3_hbgs_origami.solve() self.p3_hbgs_norm.solve() # part 4 (bayesian norm_form game) print("solving part 4") self.p4_dobbs = Dobbs(self.bayse_norm_game) self.p4_multLP = MultipleLP(self.bayse_norm_hars_game) self.p4_multSingLP = Multiple_SingleLP(self.bayse_norm_game) self.p4_dobbs_partial_full = Dobbs(self.bayse_norm_partial_full_game) self.p4_multSingLP_partial_full = \ Multiple_SingleLP(self.bayse_norm_partial_full_game) self.p4_dobbs_partial = Dobbs(self.bayse_norm_partial_game) self.p4_multSingLP_partial = \ Multiple_SingleLP(self.bayse_norm_partial_game) self.p4_hbgs = HBGS(self.bayse_norm_game) self.p4_dobbs.solve() self.p4_multLP.solve() self.p4_multSingLP.solve() self.p4_dobbs_partial.solve() self.p4_dobbs_partial_full.solve() self.p4_multSingLP_partial_full.solve() self.p4_dobbs_partial.solve() self.p4_multSingLP_partial.solve() self.p4_hbgs.solve() # part 5 print("solving part 5") self.p5_dobbs = Dobbs(self.norm_game) self.p5_multLP = MultipleLP(self.norm_game) self.p5_multSingLP = Multiple_SingleLP(self.norm_game) self.p5_dobbs.solve() self.p5_multLP.solve() self.p5_multSingLP.solve()
class TestSolvers(unittest.TestCase): @classmethod def setUpClass(self): """ Create Games: 1) Set up a security game, generate corresponding norm_form and harsanyi-transformed norm_form game. 2) Set up a large non-bayesian security game 3) Set up bayesian norm_form, generate corresponding harsanyi- transformed norm_form game. 4) Set up norm_form game. Solve Games: """ # construct games # part 1 self.sec_game = SecurityGame(num_targets=5, max_coverage=3, num_attacker_types=1) self.sec_norm_game = NormalFormGame(game=self.sec_game, harsanyi=False) self.sec_norm_hars_game = NormalFormGame(game=self.sec_norm_game) # part 2 self.large_sec_game = SecurityGame(num_targets=100, max_coverage=30, num_attacker_types=1) # part 3 self.bayse_sec_game = SecurityGame(num_targets=5, max_coverage=3, num_attacker_types=2) self.bayse_sec_norm_game = NormalFormGame(game=self.bayse_sec_game, harsanyi=False) self.bayse_sec_norm_hars_game = NormalFormGame( game=self.bayse_sec_norm_game) # part 4 self.bayse_norm_game = NormalFormGame(num_defender_strategies=10, num_attacker_strategies=3, num_attacker_types=3) self.bayse_norm_hars_game = NormalFormGame(game=self.bayse_norm_game) self.bayse_norm_partial_full_game = NormalFormGame( partial_game_from=self.bayse_norm_game, attacker_types=(0, 1, 2)) self.bayse_norm_partial_game = NormalFormGame( partial_game_from=self.bayse_norm_game, attacker_types=(1, 2)) # part 5 self.norm_game = NormalFormGame(num_defender_strategies=20, num_attacker_strategies=10, num_attacker_types=1) # solve games: # part 1 (non-bayesian security games) print("solving part 1") self.p1_eraser = Eraser(self.sec_game) self.p1_origami = Origami(self.sec_game) self.p1_origami_milp = OrigamiMILP(self.sec_game) self.p1_dobbs = Dobbs(self.sec_norm_game) self.p1_multLP = MultipleLP(self.sec_norm_hars_game) self.p1_multSingLP_sec_game = Multiple_SingleLP(self.sec_game) self.p1_multSingLP_sec_norm_game = Multiple_SingleLP( self.sec_norm_game) self.p1_multSingLP_sec_norm_hars_game = Multiple_SingleLP( self.sec_norm_hars_game) self.p1_eraser.solve() self.p1_origami.solve() self.p1_origami_milp.solve() self.p1_dobbs.solve() self.p1_multLP.solve() self.p1_multSingLP_sec_game.solve() self.p1_multSingLP_sec_norm_game.solve() self.p1_multSingLP_sec_norm_hars_game.solve() # part 2 (large security game) print("solving part 2") self.p2_large_origami = Origami(self.large_sec_game) self.p2_large_origami_milp = OrigamiMILP(self.large_sec_game) self.p2_large_eraser = Eraser(self.large_sec_game) self.p2_large_origami.solve() self.p2_large_origami_milp.solve() self.p2_large_eraser.solve() # part 3 (bayseian security games) print("solving part 3") self.p3_dobbs = Dobbs(self.bayse_sec_norm_game) self.p3_multLP = MultipleLP(self.bayse_sec_norm_hars_game) self.p3_multSingLP = Multiple_SingleLP(self.bayse_sec_game) self.p3_hbgs = HBGS(self.bayse_sec_game) self.p3_hbgs_origami = HBGS(self.bayse_sec_game, True) self.p3_hbgs_norm = HBGS(self.bayse_sec_norm_game) self.p3_dobbs.solve() self.p3_multLP.solve() self.p3_multSingLP.solve() self.p3_hbgs.solve() self.p3_hbgs_origami.solve() self.p3_hbgs_norm.solve() # part 4 (bayesian norm_form game) print("solving part 4") self.p4_dobbs = Dobbs(self.bayse_norm_game) self.p4_multLP = MultipleLP(self.bayse_norm_hars_game) self.p4_multSingLP = Multiple_SingleLP(self.bayse_norm_game) self.p4_dobbs_partial_full = Dobbs(self.bayse_norm_partial_full_game) self.p4_multSingLP_partial_full = \ Multiple_SingleLP(self.bayse_norm_partial_full_game) self.p4_dobbs_partial = Dobbs(self.bayse_norm_partial_game) self.p4_multSingLP_partial = \ Multiple_SingleLP(self.bayse_norm_partial_game) self.p4_hbgs = HBGS(self.bayse_norm_game) self.p4_dobbs.solve() self.p4_multLP.solve() self.p4_multSingLP.solve() self.p4_dobbs_partial.solve() self.p4_dobbs_partial_full.solve() self.p4_multSingLP_partial_full.solve() self.p4_dobbs_partial.solve() self.p4_multSingLP_partial.solve() self.p4_hbgs.solve() # part 5 print("solving part 5") self.p5_dobbs = Dobbs(self.norm_game) self.p5_multLP = MultipleLP(self.norm_game) self.p5_multSingLP = Multiple_SingleLP(self.norm_game) self.p5_dobbs.solve() self.p5_multLP.solve() self.p5_multSingLP.solve() def test_p1(self): """ Test if opt_defender_payoff is the same across all solutions """ self.assertAlmostEqual(self.p1_dobbs.opt_defender_payoff, self.p1_eraser.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p1_dobbs.opt_defender_payoff, self.p1_multLP.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p1_dobbs.opt_defender_payoff, self.p1_origami.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p1_dobbs.opt_defender_payoff, self.p1_origami_milp.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p1_dobbs.opt_defender_payoff, self.p1_multSingLP_sec_game.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p1_dobbs.opt_defender_payoff, self.p1_multSingLP_sec_norm_game.\ opt_defender_payoff, places=1) self.assertAlmostEqual(self.p1_dobbs.opt_defender_payoff, self.p1_multSingLP_sec_norm_hars_game.\ opt_defender_payoff, places=1) def test_p2(self): """ Test if solvers taking compact representations of large games are in agreement. """ # test that every opt_target is the same for all solvers ori_target = self.p2_large_origami.opt_attacked_target ori_milp_target = self.p2_large_origami_milp.opt_attacked_target ers_target = self.p2_large_eraser.opt_attacked_target print("ORIGAMI SOL: {}".format(self.p2_large_origami.solution_time)) print("ORIGAMI OH SOL: {}".format( self.p2_large_origami.solution_time_with_overhead)) print("ORI_MILP SOL: {}".format( self.p2_large_origami_milp.solution_time)) print("ORI_MILP OH SOL: {}".format( self.p2_large_origami_milp.solution_time_with_overhead)) self.assertEqual(ori_target, ori_milp_target, msg="opt target disagree: origami vs. origami_milp") self.assertEqual(ori_milp_target, ers_target, msg="opt target disagree: origami_milp vs. eraser") # test that coverage is the same for attacked target self.assertAlmostEqual( self.p2_large_origami.opt_coverage[ori_target], self.p2_large_origami_milp.opt_coverage[ori_milp_target], places=1, msg="cov for attacked target disagree: origami vs. origami_milp") self.assertAlmostEqual( self.p2_large_origami.opt_coverage[ori_target], self.p2_large_eraser.opt_coverage[ers_target], places=1, msg="cov for attacked target disagree: origami vs. eraser") # test that payoff is the same self.assertAlmostEqual(self.p2_large_origami.opt_defender_payoff, self.p2_large_origami_milp.opt_defender_payoff, places=1, msg="payoff disagreement: orig vs. orig-milp") self.assertAlmostEqual(self.p2_large_origami.opt_defender_payoff, self.p2_large_eraser.opt_defender_payoff, places=1, msg="payoff disagreement: ori vs. eraser") # test that attackset are the same for origami and origami-milp self.assertEqual( len(self.p2_large_origami.opt_attack_set), len(self.p2_large_origami_milp.opt_attack_set), msg="attackset diff. length: origami vs. origami-milp") def test_p3(self): """ Test if opt_defender_payoff is the same across all solutions """ self.assertAlmostEqual(self.p3_dobbs.opt_defender_payoff, self.p3_multLP.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p3_dobbs.opt_defender_payoff, self.p3_multSingLP.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p3_hbgs.opt_defender_payoff, self.p3_dobbs.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p3_hbgs_origami.opt_defender_payoff, self.p3_dobbs.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p3_hbgs_norm.opt_defender_payoff, self.p3_dobbs.opt_defender_payoff, places=1) def test_p4(self): """ Test if bayesian normal form game solvers are in agreement. """ # print("sol tim, dob, mltLP vs. Singl") # print(self.p4_dobbs.solution_time) # print(self.p4_multLP.solution_time) # print(self.p4_multSingLP.solution_time) # test that the expected opt defender payoff is the same self.assertAlmostEqual(self.p4_dobbs.opt_defender_payoff, self.p4_multLP.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p4_dobbs.opt_defender_payoff, self.p4_multSingLP.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p4_dobbs_partial_full.opt_defender_payoff, self.p4_multSingLP_partial_full.\ opt_defender_payoff, places=1) self.assertAlmostEqual(self.p4_dobbs_partial.opt_defender_payoff, self.p4_multSingLP_partial.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p4_hbgs.opt_defender_payoff, self.p4_dobbs.opt_defender_payoff, places=1) # test that opt defender strat. yields the same attacker pure strategy self.assertSequenceEqual(self.p4_dobbs.opt_attacker_pure_strategy, self.p4_multSingLP.opt_attacker_pure_strategy) # self.assertSequenceEqual(self.p4_dobbs.opt_attacker_pure_strategy, # self.bayse_norm_hars_game.\ # attacker_pure_strategy_tuples[self.p4_multLP.\ # opt_attacker_pure_strategy]) def test_p5(self): """ Test agreement between all solvers of non-bayesian normal form games. """ # test that the expected opt defender payoff is the same self.assertAlmostEqual(self.p5_dobbs.opt_defender_payoff, self.p5_multLP.opt_defender_payoff, places=1) self.assertAlmostEqual(self.p5_dobbs.opt_defender_payoff, self.p5_multSingLP.opt_defender_payoff, places=1) # test that opt defender strat. yields the same attacker pure strategy self.assertSequenceEqual(self.p5_dobbs.opt_attacker_pure_strategy, self.p5_multSingLP.opt_attacker_pure_strategy) self.assertEqual(self.p5_dobbs.opt_attacker_pure_strategy[0], self.p5_multLP.opt_attacker_pure_strategy)
def _erase_nodes_by_ids(node_ids, author=None): ers = Eraser(author=author) ers.erase_nodes(node_ids)