_actions.add_condition('eat', hungry=True, has_food=True, in_kitchen=False) _actions.add_reaction('eat', hungry=False) _actions.add_condition('cook', hungry=True, has_food=False, in_kitchen=True) _actions.add_reaction('cook', has_food=True) _actions.add_condition('sleep', tired=True, in_bed=True) _actions.add_reaction('sleep', tired=False) _actions.add_condition('go_to_bed', in_bed=False, hungry=False) _actions.add_reaction('go_to_bed', in_bed=True) _actions.add_condition('go_to_kitchen', in_kitchen=False) _actions.add_reaction('go_to_kitchen', in_kitchen=True) _actions.add_condition('leave_kitchen', in_kitchen=True) _actions.add_reaction('leave_kitchen', in_kitchen=False) _actions.add_condition('order_pizza', has_food=False, hungry=True) _actions.add_reaction('order_pizza', has_food=True) _actions.set_weight('go_to_kitchen', 20) _actions.set_weight('order_pizza', 1) _world.set_action_list(_actions) _t = time.time() _path = _world.calculate() _took_time = time.time() - _t for path in _path: print(_path.index(path) + 1, path['name']) print('\nTook:', _took_time)
_world.set_start_state(hungry=True, has_food=False, in_kitchen=False, tired=True, in_bed=False) _world.set_goal_state(tired=False) _actions = Action_List() _actions.add_condition('eat', hungry=True, has_food=True, in_kitchen=False) _actions.add_reaction('eat', hungry=False) _actions.add_condition('cook', hungry=True, has_food=False, in_kitchen=True) _actions.add_reaction('cook', has_food=True) _actions.add_condition('sleep', tired=True, in_bed=True) _actions.add_reaction('sleep', tired=False) _actions.add_condition('go_to_bed', in_bed=False, hungry=False) _actions.add_reaction('go_to_bed', in_bed=True) _actions.add_condition('go_to_kitchen', in_kitchen=False) _actions.add_reaction('go_to_kitchen', in_kitchen=True) _actions.add_condition('leave_kitchen', in_kitchen=True) _actions.add_reaction('leave_kitchen', in_kitchen=False) _actions.add_condition('order_pizza', has_food=False, hungry=True) _actions.add_reaction('order_pizza', has_food=True) _actions.set_weight('go_to_kitchen', 20) _actions.set_weight('order_pizza', 1) _world.set_action_list(_actions) _t = time.time() _path = _world.calculate() _took_time = time.time() - _t for path in _path: print _path.index(path)+1, path['name'] print '\nTook:', _took_time
_combat_actions.add_condition('arm', weapon_loaded=True, weapon_armed=False) _combat_actions.add_reaction('arm', weapon_armed=True) _combat_actions.add_condition('shoot', weapon_loaded=True, weapon_armed=True, is_near=True) _combat_actions.add_reaction('shoot', in_engagement=False) _combat_actions.add_condition('get_cover', in_cover=False) _combat_actions.add_reaction('get_cover', in_cover=True) _combat_actions.set_weight('unpack_ammo', 3) _combat_actions.set_weight('search_for_ammo', 4) _combat_actions.set_weight('track', 20) _combat_brain.set_action_list(_combat_actions) _food_brain = Planner('is_hungry', 'has_food') _food_actions = Action_List() _food_brain.set_action_list(_food_actions) _food_brain.set_start_state(has_food=False, is_hungry=True) _food_brain.set_goal_state(is_hungry=False) _food_actions.add_condition('find_food', has_food=False) _food_actions.add_reaction('find_food', has_food=True) _food_actions.add_condition('eat_food', has_food=True) _food_actions.add_reaction('eat_food', is_hungry=False) _food_actions.set_weight('find_food', 20)
_movement_actions.add_condition('move_right', canMoveRight=True, moved_down=True) _movement_actions.add_reaction( 'move_right', x=-1, movedHorizontal=True, moved_down=False, #chicken = fallingPiece['x'], canMoveRight=canMoveRight()) _movement_actions.add_condition('rotate_left', rotLeftGoal=True) _movement_actions.add_reaction('rotate_left', rotation=(fallingPiece['rotation'] + 1) % len(tetris.PIECES[fallingPiece['shape']])) _movement_actions.add_condition('move_down', canMoveDown=True, movingHorizontal=True) _movement_actions.add_reaction('move_down', lowerPiece=fallingPiece['y'] + 1) _movement_actions.add_condition('drop', can_drop=True) _movement_actions.add_reaction('drop', ) _movement_brain.set_action_list(_movement_actions)
def step(self): selforders = self.getOrders(c_selforder) passorders = self.getOrders(c_passorder) print(selforders, passorders) world = Planner('hungry', 'tired', "has_noorder", "has_order", "has_passorder", "get_order", "give_order", "execute_order", "bored") world.set_start_state(hungry=self.need_hunger > 70, tired=self.need_sleep > 70, has_order=len(selforders), has_passorder=len(passorders)>0, has_noorder=len(selforders)==0, bored=self.bored) world.set_goal_state(tired=False, has_passorder=False, has_noorder=True, has_order=True, bored=True) actions = Action_List() actions.add_condition('eat', hungry=True) actions.add_reaction('eat', hungry=False) actions.add_condition('sleep', tired=True) actions.add_reaction('sleep', tired=False) actions.add_condition("get_order", has_order=False) actions.add_reaction("get_order", has_order=True) actions.set_weight('get_order', 10)#dynamically adjust these weights actions.add_condition("give_order", has_passorder=True) actions.add_reaction("give_order", has_passorder=False) actions.set_weight('give_order', 5) actions.add_condition("execute_order", has_noorder=False) actions.add_reaction("execute_order", has_noorder=True) actions.set_weight('execute_order', 1) actions.add_condition("idle", bored=False) actions.add_reaction("idle", bored=True) actions.set_weight('idle', 20) world.set_action_list(actions) _t = time.time() path = world.calculate() _took_time = time.time() - _t #print(path) for pi, p in enumerate(path): print(pi, p['name']) if path is None or len(path) == 0: print("no path found") return self.listening = False pn = path[0]["name"] self.history.append(pn) if pn == "eat": self.need_hunger = max(0, self.need_hunger-20) elif pn == "sleep": self.need_sleep = max(0, self.need_sleep-20) elif pn == "give_order": po = choice(passorders) if po.target.listening: self.orders.remove(po) po.target.orders.append(po) elif pn == "get_order": self.listening = True print("listening") elif pn == "execute_order": order = selforders.pop(0) print("EXEC", order.message) self.orders.remove(order) if pn == "idle": self.bored = True else: self.bored = False #print('\nTook:', _took_time) self.need_hunger += 1 self.need_sleep += 1
_combat_actions.add_condition('arm', weapon_loaded=True, weapon_armed=False) _combat_actions.add_reaction('arm', weapon_armed=True) _combat_actions.add_condition('shoot', weapon_loaded=True, weapon_armed=True, is_near=True) _combat_actions.add_reaction('shoot', in_engagement=False) _combat_actions.add_condition('get_cover', in_cover=False) _combat_actions.add_reaction('get_cover', in_cover=True) _combat_actions.set_weight('unpack_ammo', 3) _combat_actions.set_weight('search_for_ammo', 4) _combat_actions.set_weight('track', 20) _combat_brain.set_action_list(_combat_actions) _food_brain = Planner('is_hungry', 'has_food') _food_actions = Action_List() _food_brain.set_action_list(_food_actions) _food_brain.set_start_state(has_food=False, is_hungry=True) _food_brain.set_goal_state(is_hungry=False) _food_actions.add_condition('find_food', has_food=False) _food_actions.add_reaction('find_food', has_food=True) _food_actions.add_condition('eat_food', has_food=True) _food_actions.add_reaction('eat_food', is_hungry=False) _food_actions.set_weight('find_food', 20) _food_actions.set_weight('eat_food', 10)