神译局是36氪旗下编译团队,关注科技、商业、职场、生活等领域,重点介绍国外的新技术、新观点、新风向。
class SimpleReflexVacuumAgent:
def __init__(self):
self.location = "A"
self.actions = []
def perceive_and_act(self, current_location, is_dirty):
if is_dirty:
self.actions.append("Suck")
print(f"Cleaned {current_location}")
else:
if current_location == "A":
self.actions.append("MoveRight")
self.location = "B"
else:
self.actions.append("MoveLeft")
self.location = "A"
print(f"Moved to {self.location}")
# 执行
agent = SimpleReflexVacuumAgent()
percepts = [("A", True), ("A", False), ("B", True), ("B", False)]
for loc, dirt in percepts:
agent.perceive_and_act(loc, dirt)
class ModelBasedVacuumAgent:
def __init__(self):
self.model = {"A": "Unknown", "B": "Unknown"}
self.location = "A"
def update_model(self, loc, status):
self.model[loc] = "Clean" if status else "Dirty"
def decide_action(self, current_loc, is_dirty):
self.update_model(current_loc, is_dirty)
if is_dirty:
return "Suck"
elif self.model["A"] == "Clean" and self.model["B"] == "Clean":
return "Shutdown"
else:
return "MoveRight" if current_loc == "A" else "MoveLeft"
# 使用示例
agent = ModelBasedVacuumAgent()
print(agent.decide_action("A", True)) # 输出:Suck
class GoalBasedAgent:
def __init__(self, target):
self.goal = target
self.actions = []
def path_planning(self, current_state):
# 简化的A*路径规划逻辑
if current_state == self.goal:
return "Goal achieved"
return "Move closer" if current_state < self.goal else "Adjust path"
agent = GoalBasedAgent(100)
print(agent.path_planning(75)) # 输出:Move closer
def utility_function(cost, time, risk):
return (0.5 * (1/cost)) + (0.3 * (1/time)) - (0.2 * risk)
actions = [
{"cost": 200, "time": 5, "risk": 0.1},
{"cost": 300, "time": 3, "risk": 0.2}
]
best_action = max(actions, key=lambda x: utility_function(x['cost'], x['time'], x['risk']))
print(f"Optimal action: {best_action}")
import numpy as np
class QLearningAgent:
def __init__(self, states, actions, alpha=0.1, gamma=0.9):
self.q_table = np.zeros((states, actions))
self.alpha = alpha
self.gamma = gamma
def learn(self, state, action, reward, next_state):
max_future_q = np.max(self.q_table[next_state])
current_q = self.q_table[state, action]
new_q = (1 - self.alpha) * current_q + self.alpha * (reward + self.gamma * max_future_q)
self.q_table[state, action] = new_q
# 初始化智能体(5种状态,4种动作)
agent = QLearningAgent(5, 4)
agent.learn(1, 2, 10, 3)
class SupervisorAgent:
def __init__(self):
self.subagents = {
"security": SecurityAgent(),
"climate": ClimateAgent()
}
def coordinate(self, sensor_data):
if sensor_data["intruder"]:
self.subagents["security"].activate()
else:
self.subagents["climate"].adjust(sensor_data["temp"])
class SecurityAgent:
def activate(self):
print("Security protocols engaged")
class ClimateAgent:
def adjust(self, temp):
action = "Cool" if temp > 72 else "Heat"
print(f"Climate system: {action} activated")
# 系统执行
smart_home = SupervisorAgent()
smart_home.coordinate({"intruder": True, "temp": 68})
通信人家园 (https://test.txrjy.com/) | Powered by C114 |