def ask_kb(percept, move, agent): """ Ask the kb stuff """ oz_kb = agent.oz_kb sentence = pit_iff(move, agent.some_number) satisfy_dpll = dpll_satisfiable(expr(sentence)) isin_kb = lambda item_s: oz_kb.has_key(item_s) contradicts_kb = lambda item_s, obj: oz_kb[item_s] != satisfy_dpll[obj] has_all = True for key in satisfy_dpll.keys(): var = key.__repr__() if isin_kb(var) and contradicts_kb(var, key): return True if not isin_kb(var): has_all = False if has_all: agent.explored[move] = True return False
#add goal KB.tell(logic.expr("At_1(Flat, Ground)")) #some manual cnf string = "" for elem in KB.clauses: # print(elem) elem = logic.to_cnf(str(elem)) string = string + str(elem) + " & " string = string[:-2] # print(string) #print only true values answer = logic.dpll_satisfiable(string) if answer != False: for elem in answer: if answer[elem]: print(str(elem)+ " : " +str(answer[elem])) else: print(answer) # print(logic.dpll_satisfiable(string))
def ask(self, sentence): satisfiable = dpll_satisfiable(self.knows & ~sentence) if (satisfiable): return False else: return True
print "Rules:" for r in rules: print r print init = logic.PropSymbolExpr(agent_str,1,1,0) goals = [] for t in xrange(max_time): goals += [logic.PropSymbolExpr(agent_str,0,0,t)] model = False for t in xrange(max_time): print "Planning with t=%d" % t s = init & reduce((lambda a,b: a&b), rules) & goals[t] print s model = logic.dpll_satisfiable(s) if model : break print if model : model_list = [(val,sym) for (sym, val) in model.items()] print sorted(model_list, reverse=True) else : print "No can do."
def get_next_action(self, observation): # get observation for the current state and return next action to apply (and None if no action is applicable) #observation is a dictionary with spaceship_name:number_of_laser_around it. global my_world print("my_world_is:") print(my_world) print("this is the observation:") print(observation) spaceships = my_world[0] devices = my_world[1] all_targets = my_world[2] #update spaceships and devices if there is spaceship that exploded for device in devices: if device[0] in observation: ship_name = device[0] if observation[ship_name] == -1: #ship exploded temp_list = list(devices) temp_list.remove(device) devices = tuple(temp_list) for ship in spaceships: if ship[0] in observation: ship_name = ship[0] if observation[ship_name] == -1: # ship exploded temp_list = list(spaceships) temp_list.remove(ship) spaceships = tuple(temp_list) #updated world after removing ships that exploded. important for the GBFS running in the next lines my_world = (spaceships, devices, all_targets) #all ships exploded if not devices or not spaceships: return None ########################## #running GBFS from current world p = SpaceshipProblem(my_world) timeout = 60 result = check_problem( p, (lambda p: search.best_first_graph_search(p, p.h)), timeout) print("GBFS ", result) ########################## #updating the world representation next_action = result[2][0] if next_action[0] in ("turn_on", "use", "calibrate"): self.update_my_world(next_action) return next_action #for all locations without lasers, make a tuple of their neighbors and mark them as safe - without lasers. all_neighbors = () all_neighbors = (next_action[2], ) + all_neighbors for sname, nb_lasers in observation.items(): if nb_lasers == 0: for shipname, location in spaceships: if shipname == sname: ship_all_neighbors = self.get_neighbors(location) for i in ship_all_neighbors: if i not in all_neighbors: all_neighbors = (i, ) + all_neighbors for i in all_neighbors: self.lasers_logic.tell(~(self.grid_dict[i])) #if the action is move for some ship and there isn't lasers around it, go ahead and do it. for sname, nb_lasers in observation.items(): if next_action[1] == sname and nb_lasers == 0 and next_action[ 0] == "move": self.update_my_world(next_action) return next_action #if the code is here, then the gbfs wants to move a ship with lasers around it #next_action[0] = "move", next_action[1] = ship name, next_action[2] = from, next_action[3] = to n_combinations = [] for ship_name, ship_location in spaceships: if ship_name == next_action[1]: near_by_n = self.get_neighbors(ship_location) near_by_n = (ship_location, ) + near_by_n nb_lasers = observation[ship_name] n_combinations = (itertools.combinations( list(near_by_n), len(near_by_n) - nb_lasers)) combination_list = list(n_combinations) small_combinations = (combination_list[0], combination_list[1], combination_list[2]) logic_list = [] for combination in small_combinations: temp = [] for coord in combination: temp.append(~self.grid_dict[coord]) logic_list.append(logic.associate('&', temp)) self.lasers_logic.tell(logic.associate('|', logic_list)) logic_answer = logic.dpll_satisfiable( logic.to_cnf(logic.associate('&', self.lasers_logic.clauses))) #logic answer contains a dict of Lxyz = Flase/True. when False means that the (x,y,z) location is safe, and unsafe #(contains lasers) otherwise #if "to_location" of GBFS is in logic_answer and is false - use it, #else we would like to choose a random one that is not our current location print("logic answer:", logic_answer) # gbfs_to_location = self.grid_dict[next_action[3]] # logic_answer.delete(self.grid_dict[next_action[2]]) for k, v in logic_answer.items(): if v == True: logic_answer = self.minus_key(k, logic_answer) #remove from logic answer not neighbors nears = () neighbors = self.get_neighbors(next_action[2]) for n in neighbors: temp = self.grid_dict[n] nears = (temp, ) + nears for l in logic_answer: if l not in nears: logic_answer = self.minus_key(l, logic_answer) #remove targets locations for t in all_targets: temp = self.grid_dict[t] if temp in logic_answer: logic_answer = self.minus_key(temp, logic_answer) #remove spaceships locations for ship_name, location in spaceships: temp = self.grid_dict[location] if temp in logic_answer: logic_answer = self.minus_key(temp, logic_answer) print("whats left for random: ", list(logic_answer.keys())) random_to_location = random.choice(list(logic_answer.keys())) print("random choise:", random_to_location) if not random_to_location: #empty return None #return Lxyz form to (x,y,z) new_to_location = () for k, v in self.grid_dict.items(): if v == random_to_location: new_to_location = k print("new_to_location:", new_to_location) new_next_action = ("move", next_action[1], next_action[2], new_to_location) print("new next location", new_next_action) self.update_my_world(new_next_action) return new_next_action
# rules += [(A111 & N1) >> (A212 & ~A112), (A211 & N1) >> A212, (A121 & N1) >> (A222 & ~A122), (A221 & N1) >> A222] print "Rules:" for r in rules: print r print init = A111 goals = [A222, A223] model = False time = 2 for goal in goals: print "Planning with t=%d" % time s = init & reduce((lambda a, b: a & b), rules) & goal print s model = logic.dpll_satisfiable(s) if model: break time += 1 print if model: model_list = [(val, sym) for (sym, val) in model.items()] print sorted(model_list, reverse=True) else: print "No can do."
import logic if __name__ == '__main__': wumpus_kb = logic.PropKB() P11, P12, P21, P22, P31, B11, B21 = logic.expr( 'P11, P12, P21, P22, P31, B11, B21') #B100 = logic.expr('B100') wumpus_kb.tell(~P11) wumpus_kb.tell(B11 | '<=>' | ((P12 | P21))) wumpus_kb.tell(B21 | '<=>' | ((P11 | P22 | P31))) wumpus_kb.tell(~B11) ship = (1, 23, 4) shipstr = 'P' + str((ship[0] + 1, ship[1], ship[2])) #for v in ship: # shipstr += str(v) print(shipstr) wumpus_kb.tell(~logic.expr(shipstr)) result = logic.dpll_satisfiable( logic.to_cnf( logic.associate('&', wumpus_kb.clauses + [logic.expr(P22)]))) print(result) result = logic.dpll_satisfiable( logic.to_cnf( logic.associate('&', wumpus_kb.clauses + [logic.expr(shipstr)]))) print(result)
KB.tell(logic.expr("At_2(Flat,Ground)")) KB.tell(logic.expr("At_2(Spare,Ground)")) #some manual cnf string = "" for elem in KB.clauses: elem = logic.to_cnf(str(elem)) string = string + str(elem) + " & " string = string[:-2] # print(string) #print only true values # answer = logic.dpll_satisfiable(string) # if answer != False: # for elem in answer: # # if answer[elem]: # print(str(elem)+ " : " +str(answer[elem])) # else: # print(answer) print(logic.dpll_satisfiable(string))
KB.tell(logic.expr("At_6(C2, SFO) & At_6(P2, SFO)")) #some manual cnf string = "" for elem in KB.clauses: # print(elem) # elem = logic.to_cnf(str(elem)) string = string + str(elem) + " & " string = string[:-2] action_stubs = ["Load", "Unload", "Fly"] #print only true values answer = logic.dpll_satisfiable(logic.expr(string)) #print out only true actions, leave out facts if answer != False: for elem in answer: if answer[elem]: if any(sub in str(elem) for sub in action_stubs): print(str(elem)+ " : " +str(answer[elem])) else: print(answer) # print(logic.dpll_satisfiable(logic.expr(string)))
# KB.tell(logic.expr("At_2(Flat, Ground) & At_2(Spare, Ground)")) KB.tell(logic.expr("At_3(Spare, Axle)")) # KB.tell(logic.expr("At_2(Flat, Ground)")) # KB.tell(logic.expr("At_2(Spare, Ground)")) # some manual cnf string = "" for elem in KB.clauses: elem = logic.to_cnf(str(elem)) string = string + str(elem) + " & " string = string[:-2] # print(string) # print only true values # answer = logic.dpll_satisfiable(string) # if answer != False: # for elem in answer: # # if answer[elem]: # print(str(elem)+ " : " +str(answer[elem])) # else: # print(answer) print(logic.dpll_satisfiable(logic.expr(string)))
def solve(steps): for moves in range(0, steps+1): print("trying with "+str(moves)+" moves") KB = logic.PropKB() #initial state KB.tell(logic.expr("~At_0(Spare, Ground)")) KB.tell(logic.expr("At_0(Spare, Trunk)")) KB.tell(logic.expr("~At_0(Spare, Axle)")) KB.tell(logic.expr("At_0(Flat, Axle)")) KB.tell(logic.expr("~At_0(Flat, Ground)")) KB.tell(logic.expr("~At_0(Flat, Trunk)")) #first preconditions for i in range(0,moves): KB.tell(logic.expr("~Remove_"+str(i)+"(Spare, Trunk) | At_"+str(i)+"(Spare, Trunk)")) KB.tell(logic.expr("~PutOn_"+str(i)+"(Spare, Axle) | At_"+str(i)+"(Spare, Ground)")) KB.tell(logic.expr("~PutOn_"+str(i)+"(Spare, Axle) | ~At_"+str(i)+"(Flat, Axle) ")) KB.tell(logic.expr("~Remove_"+str(i)+"(Spare, Axle) | At_"+str(i)+"(Spare, Axle)")) KB.tell(logic.expr("~Remove_"+str(i)+"(Flat, Axle) | At_"+str(i)+"(Flat, Axle)")) KB.tell(logic.expr("~PutOn_"+str(i)+"(Flat, Axle) | At_"+str(i)+"(Flat, Ground) ")) KB.tell(logic.expr("~PutOn_"+str(i)+"(Flat, Axle) | ~At_"+str(i)+"(Flat, Axle) ")) KB.tell(logic.expr("~Remove_"+str(i)+"(Flat, Trunk) | At_"+str(i)+"(Flat, Trunk)")) #second positive effects for i in range(0,moves): KB.tell(logic.expr("~Remove_"+str(i)+"(Spare, Trunk) | At_"+str(i+1)+"(Spare, Ground)")) KB.tell(logic.expr("~PutOn_"+str(i)+"(Spare, Axle) | At_"+str(i+1)+"(Spare, Axle)")) KB.tell(logic.expr("~Remove_"+str(i)+"(Spare, Axle) | At_"+str(i+1)+"(Spare, Ground)")) KB.tell(logic.expr("~Remove_"+str(i)+"(Flat, Axle) | At_"+str(i+1)+"(Flat, Ground)")) KB.tell(logic.expr("~PutOn_"+str(i)+"(Flat, Axle) | At_"+str(i+1)+"(Flat, Axle)")) KB.tell(logic.expr("~Remove_"+str(i)+"(Flat, Trunk) | At_"+str(i+1)+"(Flat, Ground)")) #third negative effects for i in range(0,moves): KB.tell(logic.expr("~Remove_"+str(i)+"(Spare, Trunk) | ~At_"+str(i+1)+"(Spare, Trunk)")) KB.tell(logic.expr("~PutOn_"+str(i)+"(Spare, Axle) | ~At_"+str(i+1)+"(Spare, Ground)")) KB.tell(logic.expr("~Remove_"+str(i)+"(Spare, Axle) | ~At_"+str(i+1)+"(Spare, Axle)")) KB.tell(logic.expr("~Remove_"+str(i)+"(Flat, Axle) | ~At_"+str(i+1)+"(Flat, Axle)")) KB.tell(logic.expr("~PutOn_"+str(i)+"(Flat, Axle) | ~At_"+str(i+1)+"(Flat, Ground)")) KB.tell(logic.expr("~Remove_"+str(i)+"(Flat, Trunk) | ~At_"+str(i+1)+"(Flat, Trunk)")) KB.tell(logic.expr("~LeaveOvernight_"+str(i)+" | ~At_"+str(i+1)+"(Spare, Ground)")) KB.tell(logic.expr("~LeaveOvernight_"+str(i)+" | ~At_"+str(i+1)+"(Spare, Axle)")) KB.tell(logic.expr("~LeaveOvernight_"+str(i)+" | ~At_"+str(i+1)+"(Spare, Trunk)")) KB.tell(logic.expr("~LeaveOvernight_"+str(i)+" | ~At_"+str(i+1)+"(Flat, Ground)")) KB.tell(logic.expr("~LeaveOvernight_"+str(i)+" | ~At_"+str(i+1)+"(Flat, Axle)")) KB.tell(logic.expr("~LeaveOvernight_"+str(i)+" | ~At_"+str(i+1)+"(Flat, Trunk)")) #fourth from false to true for i in range(0,moves): KB.tell(logic.expr("At_"+str(i)+"(Spare, Ground) | ~At_"+str(i+1)+"(Spare, Ground) | Remove_"+str(i)+"(Spare, Trunk) | Remove_"+str(i)+"(Spare, Axle)")) KB.tell(logic.expr("At_"+str(i)+"(Spare, Trunk) | ~At_"+str(i+1)+"(Spare, Trunk)")) KB.tell(logic.expr("At_"+str(i)+"(Spare, Axle) | ~At_"+str(i+1)+"(Spare, Axle) | PutOn_"+str(i)+"(Spare, Axle)")) KB.tell(logic.expr("At_"+str(i)+"(Flat, Axle) | ~At_"+str(i+1)+"(Flat, Axle) | PutOn_"+str(i)+"(Flat, Axle)")) KB.tell(logic.expr("At_"+str(i)+"(Flat, Ground) | ~At_"+str(i+1)+"(Flat, Ground) | Remove_"+str(i)+"(Flat, Axle) | Remove_"+str(i)+"(Flat, Trunk)")) KB.tell(logic.expr("At_"+str(i)+"(Flat, Trunk) | ~At_"+str(i+1)+"(Flat, Trunk)")) #fifth from true to false for i in range(0,moves): KB.tell(logic.expr("~At_"+str(i)+"(Spare, Ground) | At_"+str(i+1)+"(Spare, Ground) | PutOn_"+str(i)+"(Spare, Axle) | LeaveOvernight_"+str(i)+"")) KB.tell(logic.expr("~At_"+str(i)+"(Spare, Trunk) | At_"+str(i+1)+"(Spare, Trunk) | Remove_"+str(i)+"(Spare, Trunk) | LeaveOvernight_"+str(i)+"")) KB.tell(logic.expr("~At_"+str(i)+"(Spare, Axle) | At_"+str(i+1)+"(Spare, Axle) | Remove_"+str(i)+"(Spare, Axle) | LeaveOvernight_"+str(i)+"")) KB.tell(logic.expr("~At_"+str(i)+"(Flat, Axle) | At_"+str(i+1)+"(Flat, Axle) | Remove_"+str(i)+"(Flat, Axle) | LeaveOvernight_"+str(i)+"")) KB.tell(logic.expr("~At_"+str(i)+"(Flat, Ground) | At_"+str(i+1)+"(Flat, Ground) | PutOn_"+str(i)+"(Flat, Axle) | LeaveOvernight_"+str(i)+"")) KB.tell(logic.expr("~At_"+str(i)+"(Flat, Trunk) | At_"+str(i+1)+"(Flat, Trunk) | Remove_"+str(i)+"(Flat, Trunk) | LeaveOvernight_"+str(i)+"")) #list of all possible actions #actions without timestamp, will be added later actions = ["Remove_(Spare, Trunk)", "PutOn_(Spare, Axle)", "Remove_(Spare, Axle)", "Remove_(Flat, Axle)", "PutOn_(Flat, Axle)", "Remove_(Flat, Trunk)", "LeaveOvernight_"] #sixth, only one action can take place for i in range(0,moves): for elem in itertools.combinations(actions, 2): KB.tell(logic.expr("~"+elem[0].replace("_","_"+str(i))+" | ~"+elem[1].replace("_","_"+str(i)))) #seventh, one action must take place for i in range(0,moves): seventh = "" for elem in actions: seventh += elem.replace("_","_"+str(i)) + " | " seventh = seventh[:-2] KB.tell(logic.expr(seventh)) #add goal KB.tell(logic.expr("At_"+str(moves)+"(Spare, Axle)")) #some manual cnf just to be sure string = "" for elem in KB.clauses: elem = logic.to_cnf(str(elem)) string = string + str(elem) + " & " string = string[:-2] action_stubs = ["Remove", "PutOn", "LeaveOvernight"] #print only true values answer = logic.dpll_satisfiable(logic.expr(string)) if answer == False: print("Couldn't solve problem in "+str(moves)+" turns") else: print("Found solution with "+str(moves)+" turns") for elem in answer: if answer[elem]: if any(sub in str(elem) for sub in action_stubs): print(str(elem)+ " : " +str(answer[elem])) break
def get_next_action(self, observation): # TODO : COMPLETE BY STUDENTS # get observation for the current state and return next action to apply (and None if no action is applicable) #self.space_kb.tell() cleared = False #print(self.state) for prev_tar in self.problem[3]: for tar in self.state[3]: if prev_tar[0] == tar[0]: if len(prev_tar[1]) != len(tar[1]): self.states=[] self.prev_fit = None cleared=True if cleared: print("CLEARED") self.problem=self.state # Take a ship and look for the closest target+calibration actions = self.actions(self.state) #print(actions) next_action = None next_target = None ship_missions=[] for ship in self.state[1]: for action in actions: if observation.get(ship[0]) != -1: if observation.get(ship[0]) == 1: pos_str = 'P' + str(ship[1]) self.space_kb.tell(~logic.expr(pos_str)) elif observation.get(ship[0])==0: pos_str='P'+str(ship[1]) self.space_kb.tell(~logic.expr(pos_str)) pos_str='P'+str((ship[1][0]+1,ship[1][1],ship[1][2])) self.space_kb.tell(~logic.expr(pos_str)) pos_str = 'P' + str((ship[1][0]-1, ship[1][1], ship[1][2])) self.space_kb.tell(~logic.expr(pos_str)) pos_str = 'P' + str((ship[1][0], ship[1][1]+1, ship[1][2])) self.space_kb.tell(~logic.expr(pos_str)) pos_str = 'P' + str((ship[1][0], ship[1][1]-1, ship[1][2])) self.space_kb.tell(~logic.expr(pos_str)) pos_str = 'P' + str((ship[1][0], ship[1][1], ship[1][2]+1)) self.space_kb.tell(~logic.expr(pos_str)) pos_str = 'P' + str((ship[1][0], ship[1][1], ship[1][2]-1)) self.space_kb.tell(~logic.expr(pos_str)) if action[0] == "use" and action [1] == ship[0]: self.state = self.result(self.state, action) self.states.append(self.state) return action if action[0] == "calibrate" and action [1] == ship[0] and ship[2][1]==False: self.state = self.result(self.state, action) self.states.append(self.state) return action if ship[2][0] is None: if action[0] == "turn_on" and action[1] == ship[0]: need_for_weapon = False for tar in self.state[3]: if action[2] in tar[1]: need_for_weapon = True if need_for_weapon: self.state = self.result(self.state, action) self.states.append(self.state) return action fitness = -1 tmp_min_fit=None if observation.get(ship[0]) != -1: ntarget = best_next_target(ship, self.state) if ntarget != (): fitness = ntarget[0]+observation.get(ship[0])+1 if tmp_min_fit is None: tmp_min_fit = fitness next_target = ntarget if 0 < fitness < tmp_min_fit: tmp_min_fit = fitness next_target = ntarget if next_target != (): ship_missions.append((next_target,ship)) tmp_best = None for mission in ship_missions: if tmp_best is None: tmp_best = mission if 0<mission[0][0]<tmp_best[0][0]: tmp_best = mission '''if self.prev_fit is None: print (mission[0][0]) self.prev_fit = mission[0][0] elif self.prev_fit<mission[0][0]: self.prev_fit=None self.state = self.result(self.state, self.move_back) return self.move_back else: self.prev_fit=mission[0][0]''' if mission[1][2][0] not in mission[0][1][1]: next_action = ("turn_on", mission[1][0], mission[0][2][0]) self.state=self.result(self.state,next_action) return next_action next_action = self.next_move_to_mission(mission, self.state) if next_action is None: return self.move_back allow=logic.dpll_satisfiable(logic.to_cnf(logic.associate('&',self.space_kb.clauses + [logic.expr('P'+str(next_action[3]))]))) #print(allow) tmp_state = self.result(self.state, next_action) self.states.append(tmp_state) while allow != False: print('Achtung Laser') print(allow) next_action = self.next_move_to_mission(mission, self.state) tmp_state = self.result(self.state, next_action) self.states.append(tmp_state) if len(self.states)>6: self.states.clear() allow = logic.dpll_satisfiable( logic.to_cnf(logic.associate('&', self.space_kb.clauses + [logic.expr('P' + str(next_action[3]))]))) #print(allow) self.state=tmp_state self.move_back = (next_action[0], next_action[1], next_action[3],next_action[2]) return next_action