def kb_trump(self, state, move): # type: (State,move) -> bool kb = KB() load.general_information(kb) load.strategy_knowledge(kb) p_card = move[0] p_card_suit = "" if p_card < 5: p_card_suit = "C" elif p_card < 10: p_card_suit = "D" elif p_card < 15: p_card_suit = "H" elif p_card < 20: p_card_suit = "S" # p_card_suit = Deck.get_suit(p_card) trump_suit = state.get_trump_suit() variable_string = "wtt" + str(p_card_suit) + str(trump_suit) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def kb_acecard(self, state, move): # type: (State, move) -> bool # each time we check for consistency we initialise a new knowledge-base kb = KB() # Add general information about the game load.general_information(kb) # Add the necessary knowledge about the strategy load.strategy_knowledge(kb) # This line stores the index of the card in the deck. # If this doesn't make sense, refer to _deck.py for the card index mapping index = move[0] # This creates the string which is used to make the strategy_variable. # Note that as far as kb.py is concerned, two objects created with the same # string in the constructor are equivalent, and are seen as the same symbol. # Here we use "pj" to indicate that the card with index "index" should be played with the # PlayJack heuristics that was defined in class. Initialise a different variable if # you want to apply a different strategy (that you will have to define in load.py) variable_string = "pa" + str(index) strategy_variable = Boolean(variable_string) # Add the relevant clause to the loaded knowledge base kb.add_clause(~strategy_variable) # If the knowledge base is not satisfiable, the strategy variable is # entailed (proof by refutation) return kb.satisfiable()
def kb_consistent_trumpmarriage(self, state, move): # type: (State, move) -> bool # each time we check for consistency we initialise a new knowledge-base kb = KB() # Add general information about the game suit = State.get_trump_suit(state) if suit == "C": card1 = 2 card2 = 3 variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(strategy_variable) elif suit == "D": card1 = 7 card2 = 8 variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(strategy_variable) elif suit == "H": card1 = 12 card2 = 13 variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(strategy_variable) elif suit == "S": card1 = 17 card2 = 18 variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(strategy_variable) load.general_information(kb) # Add the necessary knowledge about the strategy load.strategy_knowledge(kb) # This line stores the index of the card in the deck. # If this doesn't make sense, refer to _deck.py for the card index mapping index = move[0] variable_string = "pm" + str(index) strategy_variable = Boolean(variable_string) # Add the relevant clause to the loaded knowledge base kb.add_clause(~strategy_variable) # If the knowledge base is not satisfiable, the strategy variable is # entailed (proof by refutation) return kb.satisfiable()
def kb_consistent_marriage(self, state, move): # type: (State, move) -> bool kb = KB() load.general_information(kb) load.strategy_knowledge(kb) card1 = move[0] card2 = move[1] variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def kb_consistent_low_non_trump(self, state, move): # type: (State, move) -> bool kb = KB() load.general_information(kb) load.strategy_knowledge(kb) card = move[0] trump_suit = state.get_trump_suit() variable_string = "pc" + str(card) + str(trump_suit) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def kb_consistent_matching_win(self, state, move): # type: (State,move) -> bool kb = KB() load_justabout.general_information(kb) load_justabout.strategy_knowledge(kb) opp_card = state.get_opponents_played_card() opp_card_suit = Deck.get_suit(opp_card) opp_card_rank = opp_card % 5 p_card = move[0] p_card_suit = Deck.get_suit(p_card) p_card_rank = opp_card % 5 variable_string = "wt" + str(p_card_rank) + str(opp_card_rank) + str(p_card_suit) + str(opp_card_suit) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def kb_consistent_trump_win(self,state,move): # type: (State,move) -> bool kb = KB() load_justabout.general_information(kb) load_justabout.strategy_knowledge(kb) opp_card = state.get_opponents_played_card() opp_card_suit = Deck.get_suit(opp_card) opp_card_rank = opp_card & 5 p_card = move[0] p_card_suit = Deck.get_suit(p_card) p_card_rank = p_card % 5 trump_suit = state.get_trump_suit() constraint_a = Integer('me') > Integer('op') constraint_b = Integer('op') > Integer('me') if opp_card_suit == trump_suit: if p_card_suit == trump_suit: if opp_card_rank < p_card_rank: strategy_variable = constraint_b else: strategy_variable = constraint_a else: strategy_variable = constraint_b else: variable_string = "wtt" + str(p_card_suit) + str(trump_suit) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
import sys from kb import KB, Boolean, Integer, Constant # Define our symbols Q = Boolean('Q') P = Boolean('P') R = Boolean('R') # Create a new knowledge base kb = KB() # Add clauses kb.add_clause(P, Q) kb.add_clause(~Q, R) kb.add_clause(~R, ~P) kb.add_clause(Q, ~P) kb.add_clause(P, ~Q) # Print all models of the knowledge base for model in kb.models(): print(model) # Print out whether the KB is satisfiable (if there are no models, it is not satisfiable) print(kb.satisfiable())
# Create a new knowledge base kb = KB() # Add clauses # kb.add_clause(A, B, C) # kb.add_clause(~A, B) # kb.add_clause(C) # kb.add_clause(C) # # Question 3 # kb.add_clause(A) # kb.add_clause(C) # kb.add_clause(D) # kb.add_clause(~A) # Question 4 kb.add_clause(A, B) kb.add_clause(B, ~C) kb.add_clause(~C, ~A) # This needs to be unsatisfiable for entailment kb.add_clause(A, ~B) kb.add_clause(~A, B) # Print all models of the knowledge base for model in kb.models(): print(model) # Print out whether the KB is satisfiable (if there are no models, it is not satisfiable) print("It is satisfiable" if kb.satisfiable() else "It is not satisfiable")
def kb_consistent(self, state, move): # type: (State, move) -> bool # each time we check for consistency we initialise a new knowledge-base kb = KB() # Add general information about the game load.general_information(kb) # Add the necessary knowledge about the strategy load.strategy_knowledge(kb) # This line stores the index of the card in the deck. # If this doesn't make sense, refer to _deck.py for the card index mapping index = move[0] # This creates the string which is used to make the strategy_variable. # Note that as far as kb.py is concerned, two objects created with the same # string in the constructor are equivalent, and are seen as the same symbol. # Here we use "pj" to indicate that the card with index "index" should be played with the # PlayJack heuristics that was defined in class. Initialise a different variable if # you want to apply a different strategy (that you will have to define in load.py) # # Always play trump trump_suit = state.get_trump_suit() trump = trump_suit.lower() if trump == 'c': pj = Boolean('pj4') pq = Boolean('pq3') pk = Boolean('pk2') pt = Boolean('pt1') pa = Boolean('pa0') if trump == 'd': pj = Boolean('pj9') pq = Boolean('pq8') pk = Boolean('pk7') pt = Boolean('pt6') pa = Boolean('pa5') if trump == 'h': pj = Boolean('pj14') pq = Boolean('pq13') pk = Boolean('pk12') pt = Boolean('pt11') pa = Boolean('pa10') if trump == 's': pj = Boolean('pj19') pq = Boolean('pq18') pk = Boolean('pk17') pt = Boolean('pt16') pa = Boolean('pa15') kb.add_clause(~pj, ~pq, ~pk, ~pt, ~pa) # # always play Jack # variable_string = "pj" + str(index) # strategy_variable = Boolean(variable_string) # Add the relevant clause to the loaded knowledge base # kb.add_clause(~strategy_variable) # If the knowledge base is not satisfiable, the strategy variable is # entailed (proof by refutation) return kb.satisfiable()