
AI Computing
Lesson Content
Advanced Agent Architectures
Multi-Agent System Design
Agent Communication Patterns
1. Direct Communication
codeclass Agent: def __init__(self, agent_id): self.id = agent_id self.inbox = [] def send_message(self, recipient, message): recipient.receive_message({ 'from': self.id, 'content': message, 'timestamp': time.time() }) def receive_message(self, message): self.inbox.append(message) self.process_message(message)
2. Pub/Sub Pattern
codeclass MessageBroker: def __init__(self): self.subscribers = defaultdict(list) def subscribe(self, topic, agent): self.subscribers[topic].append(agent) def publish(self, topic, message): for agent in self.subscribers[topic]: agent.receive_message(topic, message) # Usage broker = MessageBroker() coordinator_agent.subscribe('task_completed', broker) worker_agent.publish('task_completed', result)
Agent Coordination Strategies
1. Hierarchical Coordination
codeMaster Agent ├── Coordinator Agent 1 │ ├── Worker Agent 1a │ └── Worker Agent 1b └── Coordinator Agent 2 ├── Worker Agent 2a └── Worker Agent 2b
2. Market-Based Coordination
codeclass TaskMarket: def __init__(self): self.tasks = [] self.bids = defaultdict(list) def post_task(self, task): self.tasks.append(task) return task.id def submit_bid(self, task_id, agent_id, cost, quality): self.bids[task_id].append({ 'agent': agent_id, 'cost': cost, 'quality': quality, 'score': quality / cost # Simple scoring }) def award_task(self, task_id): bids = self.bids[task_id] winner = max(bids, key=lambda b: b['score']) return winner['agent']
3. Consensus-Based Coordination
codeclass ConsensusAgent: def __init__(self, agent_id, peers): self.id = agent_id self.peers = peers self.proposals = [] def propose_action(self, action): votes = self.collect_votes(action) if votes['agree'] > len(self.peers) / 2: self.execute_action(action) return True return False def collect_votes(self, action): votes = {'agree': 0, 'disagree': 0} for peer in self.peers: response = peer.evaluate_proposal(action) votes[response] += 1 return votes
Specialized Agent Types
Research Agent
codeclass ResearchAgent(BaseAgent): def __init__(self): super().__init__() self.tools = [ 'web_search', 'academic_database', 'data_analyzer' ] async def research_topic(self, query): # Phase 1: Information gathering search_results = await self.web_search(query) papers = await self.search_academic_db(query) # Phase 2: Analysis insights = await self.analyze_findings( search_results + papers ) # Phase 3: Synthesis report = await self.generate_report(insights) return report
Planning Agent
codeclass PlanningAgent(BaseAgent): def __init__(self): super().__init__() self.world_model = WorldModel() def create_plan(self, goal, constraints): # Generate possible actions actions = self.generate_actions(goal) # Simulate outcomes best_plan = None best_score = -inf for action_sequence in actions: outcome = self.simulate(action_sequence) score = self.evaluate(outcome, goal, constraints) if score > best_score: best_score = score best_plan = action_sequence return best_plan
Memory Agent
codeclass MemoryAgent(BaseAgent): def __init__(self): super().__init__() self.short_term = deque(maxlen=100) self.long_term = ChromaDB() # Vector database def store_memory(self, memory, importance): # Short-term storage self.short_term.append(memory) # Long-term storage for important memories if importance > 0.7: embedding = self.embed(memory) self.long_term.add( id=memory['id'], embedding=embedding, metadata=memory ) def recall(self, query, k=5): # Search both storages recent = [m for m in self.short_term if self.is_relevant(m, query)] embedding = self.embed(query) long_term = self.long_term.query( query_embedding=embedding, n_results=k ) return self.merge_and_rank(recent, long_term)
Agent Learning and Adaptation
Reinforcement Learning Agent
codeclass RLAgent(BaseAgent): def __init__(self, state_size, action_size): super().__init__() self.q_table = np.zeros((state_size, action_size)) self.epsilon = 1.0 # Exploration rate self.epsilon_decay = 0.995 self.learning_rate = 0.1 self.discount_factor = 0.95 def choose_action(self, state): # Epsilon-greedy policy if random.random() < self.epsilon: return random.choice(range(len(self.q_table[0]))) return np.argmax(self.q_table[state]) def learn(self, state, action, reward, next_state): # Q-learning update current_q = self.q_table[state][action] max_next_q = np.max(self.q_table[next_state]) new_q = current_q + self.learning_rate * ( reward + self.discount_factor * max_next_q - current_q ) self.q_table[state][action] = new_q # Decay exploration self.epsilon *= self.epsilon_decay
Problem-Solving Scenarios
Scenario 1: Task Delegation System
codeclass TaskDelegationSystem: def __init__(self): self.manager = ManagerAgent() self.workers = [WorkerAgent(i) for i in range(5)] self.task_queue = Queue() async def process_complex_task(self, task): # 1. Decompose task subtasks = self.manager.decompose(task) # 2. Assign to workers assignments = self.manager.assign_tasks( subtasks, self.workers ) # 3. Execute in parallel results = await asyncio.gather(*[ worker.execute(subtask) for worker, subtask in assignments ]) # 4. Aggregate results final_result = self.manager.aggregate(results) return final_result
Scenario 2: Negotiation Between Agents
codeclass NegotiationAgent(BaseAgent): def __init__(self, resources, preferences): super().__init__() self.resources = resources self.preferences = preferences self.offers_made = [] def negotiate(self, other_agent, rounds=10): for round_num in range(rounds): # Make offer offer = self.generate_offer( other_agent, round_num / rounds # Urgency ) # Get response response = other_agent.evaluate_offer(offer) if response['accept']: return self.finalize_deal(offer) # Adjust strategy based on counter-offer self.update_strategy(response['counter_offer']) return None # No deal reached