Class: SuperInstance::Equipment::SwarmCoordinator::AgentOrchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/equipment/swarm_coordinator/agent_orchestrator.rb

Overview

AgentOrchestrator manages agent registration, hierarchy, and task distribution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ AgentOrchestrator

Creates a new AgentOrchestrator

Parameters:

  • config (Hash) (defaults to: {})

    Configuration options



75
76
77
78
79
80
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 75

def initialize(config = {})
  @config = OrchestratorConfig.new(**config)
  @agents = {}
  @hierarchy = {}
  @agent_weights = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



71
72
73
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 71

def config
  @config
end

Instance Method Details

#adjust_agent_weight(agent_id, performance_score) ⇒ Object

Adjust agent weight for task allocation

Parameters:

  • agent_id (String)

    Agent identifier

  • performance_score (Float)

    Performance score (0-1)



213
214
215
216
217
218
219
220
221
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 213

def adjust_agent_weight(agent_id, performance_score)
  current_weight = @agent_weights[agent_id] || 1.0
  new_weight = current_weight * 0.7 + performance_score * 0.3

  @agent_weights[agent_id] = new_weight

  agent = @agents[agent_id]
  @agents[agent_id] = agent.with(weight: new_weight) if agent
end

#broadcast(message, exclude_agent_ids = []) ⇒ Hash<String, OrchestrationResult>

Broadcast message to all agents

Parameters:

  • message (Object)

    Message to broadcast

  • exclude_agent_ids (Array<String>) (defaults to: [])

    Agents to exclude

Returns:



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 253

def broadcast(message, exclude_agent_ids = [])
  results = {}

  @agents.each do |agent_id, agent|
    next if exclude_agent_ids.include?(agent_id)

    results[agent_id] = OrchestrationResult.new(
      success: true,
      data: message,
      agents_involved: [agent_id],
      execution_time: 0,
      timestamp: DateTime.now
    )
  end

  results
end

#get_agent(agent_id) ⇒ AgentProfile?

Get agent profile

Parameters:

  • agent_id (String)

    Agent identifier

Returns:



150
151
152
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 150

def get_agent(agent_id)
  @agents[agent_id]
end

#get_agent_weight(agent_id) ⇒ Float

Get agent weight

Parameters:

  • agent_id (String)

    Agent identifier

Returns:

  • (Float)

    Agent weight



226
227
228
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 226

def get_agent_weight(agent_id)
  @agent_weights[agent_id] || 1.0
end

#get_agents_by_capability(capability) ⇒ Array<AgentProfile>

Get agents by capability

Parameters:

  • capability (String)

    Capability to filter by

Returns:

  • (Array<AgentProfile>)

    Agents with specified capability



170
171
172
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 170

def get_agents_by_capability(capability)
  @agents.values.select { |agent| agent.capabilities.include?(capability) }
end

#get_agents_by_role(role) ⇒ Array<AgentProfile>

Get agents by role

Parameters:

  • role (Symbol)

    Role to filter by

Returns:



163
164
165
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 163

def get_agents_by_role(role)
  @agents.values.select { |agent| agent.role == role }
end

#get_all_agentsArray<AgentProfile>

Get all agents

Returns:



156
157
158
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 156

def get_all_agents
  @agents.values
end

#get_available_agentsArray<AgentProfile>

Get available agents (idle status)

Returns:



176
177
178
179
180
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 176

def get_available_agents
  @agents.values.select do |agent|
    agent.status == Types::ExecutionStatus::IDLE && agent.current_load < 1
  end
end

#get_hierarchy(agent_id) ⇒ AgentHierarchyNode?

Get agent hierarchy

Parameters:

  • agent_id (String)

    Root agent ID

Returns:



185
186
187
188
189
190
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 185

def get_hierarchy(agent_id)
  agent = @agents[agent_id]
  return nil unless agent

  build_hierarchy_node(agent)
end

#get_statisticsOrchestratorStatistics

Get orchestrator statistics

Returns:



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 295

def get_statistics
  agents = @agents.values

  {
    total_agents: agents.size,
    agents_by_role: group_by_role(agents),
    agents_by_status: group_by_status(agents),
    average_load: calculate_average_load(agents),
    hierarchy_depth: calculate_max_depth,
    top_performers: get_top_performers(5)
  }
end

#multicast(agent_ids, message) ⇒ Hash<String, OrchestrationResult>

Send message to specific agents

Parameters:

  • agent_ids (Array<String>)

    Target agent IDs

  • message (Object)

    Message to send

Returns:



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 275

def multicast(agent_ids, message)
  results = {}

  agent_ids.each do |agent_id|
    if @agents.key?(agent_id)
      results[agent_id] = OrchestrationResult.new(
        success: true,
        data: message,
        agents_involved: [agent_id],
        execution_time: 0,
        timestamp: DateTime.now
      )
    end
  end

  results
end

#register_agent(profile) ⇒ Boolean

Register a new agent

Parameters:

Returns:

  • (Boolean)

    True if registration successful



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 85

def register_agent(profile)
  return false if @agents.size >= @config.max_agents
  return false if @agents.key?(profile.id)

  if profile.hierarchy_level > @config.max_hierarchy_depth
    return false
  end

  full_profile = profile.with(
    child_agent_ids: [],
    current_load: 0,
    weight: 1.0
  )

  @agents[profile.id] = full_profile
  @agent_weights[profile.id] = 1.0
  @hierarchy[profile.id] = Set.new

  if profile.parent_agent_id && @agents.key?(profile.parent_agent_id)
    parent = @agents[profile.parent_agent_id]
    updated_children = parent.child_agent_ids + [profile.id]
    @agents[profile.parent_agent_id] = parent.with(child_agent_ids: updated_children)
    @hierarchy[profile.parent_agent_id].add(profile.id)
  end

  true
end

#select_best_agent(required_capabilities, preferred_role = nil) ⇒ AgentProfile?

Select best agent for a task

Parameters:

  • required_capabilities (Array<String>)

    Required capabilities

  • preferred_role (Symbol, nil) (defaults to: nil)

    Preferred agent role

Returns:



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 234

def select_best_agent(required_capabilities, preferred_role = nil)
  candidates = get_available_agents.select do |agent|
    required_capabilities.all? { |cap| agent.capabilities.include?(cap) }
  end

  return nil if candidates.empty?

  role_matched = preferred_role ? candidates.select { |a| a.role == preferred_role } : candidates
  pool = role_matched.empty? ? candidates : role_matched

  pool.sort_by do |a|
    [-a.weight, a.current_load]
  end.first
end

#unregister_agent(agent_id) ⇒ Boolean

Unregister an agent

Parameters:

  • agent_id (String)

    Agent identifier

Returns:

  • (Boolean)

    True if unregistration successful



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 116

def unregister_agent(agent_id)
  return false unless @agents.key?(agent_id)

  agent = @agents[agent_id]

  agent.child_agent_ids.each do |child_id|
    child = @agents[child_id]
    if child
      @agents[child_id] = child.with(
        parent_agent_id: agent.parent_agent_id,
        hierarchy_level: [0, child.hierarchy_level - 1].max
      )
    end
  end

  if agent.parent_agent_id
    parent = @agents[agent.parent_agent_id]
    if parent
      updated_children = parent.child_agent_ids.reject { |id| id == agent_id }
      @agents[agent.parent_agent_id] = parent.with(child_agent_ids: updated_children)
      @hierarchy[agent.parent_agent_id].delete(agent_id)
    end
  end

  @agents.delete(agent_id)
  @agent_weights.delete(agent_id)
  @hierarchy.delete(agent_id)

  true
end

#update_agent_load(agent_id, load) ⇒ Object

Update agent load

Parameters:

  • agent_id (String)

    Agent identifier

  • load (Float)

    Current load (0-1)



203
204
205
206
207
208
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 203

def update_agent_load(agent_id, load)
  agent = @agents[agent_id]
  if agent
    @agents[agent_id] = agent.with(current_load: [0, [1, load].min].max)
  end
end

#update_agent_status(agent_id, status) ⇒ Object

Update agent status

Parameters:

  • agent_id (String)

    Agent identifier

  • status (Symbol)

    New status



195
196
197
198
# File 'lib/equipment/swarm_coordinator/agent_orchestrator.rb', line 195

def update_agent_status(agent_id, status)
  agent = @agents[agent_id]
  @agents[agent_id] = agent.with(status: status) if agent
end