Class: Legion::Extensions::Agentic::Social::Symbiosis::Helpers::Ecosystem

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb

Instance Method Summary collapse

Constructor Details

#initializeEcosystem

Returns a new instance of Ecosystem.



10
11
12
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 10

def initialize
  @bonds = {}
end

Instance Method Details

#activate_bond(bond_id, amount: 0.05) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 22

def activate_bond(bond_id, amount: 0.05)
  bond = @bonds[bond_id]
  return { found: false, bond_id: bond_id } unless bond

  bond.activate!(amount: amount)
  { found: true, bond_id: bond_id, strength: bond.strength, activation_count: bond.activation_count }
end

#active_bondsObject



104
105
106
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 104

def active_bonds
  @bonds.values.reject(&:dormant?)
end

#all_bondsObject



96
97
98
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 96

def all_bonds
  @bonds.values
end

#bond_countObject



100
101
102
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 100

def bond_count
  @bonds.size
end

#decay_all!Object



66
67
68
69
70
71
72
73
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 66

def decay_all!
  decayed = 0
  @bonds.each_value do |bond|
    bond.decay!
    decayed += 1
  end
  decayed
end

#find_bond(subsystem_a, subsystem_b) ⇒ Object



89
90
91
92
93
94
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 89

def find_bond(subsystem_a, subsystem_b)
  @bonds.values.find do |b|
    (b.subsystem_a == subsystem_a && b.subsystem_b == subsystem_b) ||
      (b.subsystem_a == subsystem_b && b.subsystem_b == subsystem_a)
  end
end

#health_labelObject



46
47
48
49
50
51
52
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 46

def health_label
  score = measure_health
  Constants::ECOSYSTEM_HEALTH_LABELS.each do |range, label|
    return label if range.cover?(score)
  end
  :critical
end

#measure_healthObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 30

def measure_health
  return 0.0 if @bonds.empty?

  active = @bonds.values.reject(&:dormant?)
  return 0.0 if active.empty?

  mutualistic = active.count { |b| b.relationship_type == :mutualistic }
  parasitic   = active.count { |b| b.relationship_type == :parasitic }
  total_active = active.size

  mutualistic_ratio = mutualistic.to_f / total_active
  parasite_penalty  = parasitic.to_f / total_active * 0.5

  (mutualistic_ratio - parasite_penalty).clamp(0.0, 1.0).round(10)
end

#most_beneficialObject



54
55
56
57
58
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 54

def most_beneficial
  @bonds.values
        .select { |b| b.relationship_type == :mutualistic && !b.dormant? }
        .max_by { |b| b.benefit_ratio * b.strength }
end

#most_parasiticObject



60
61
62
63
64
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 60

def most_parasitic
  @bonds.values
        .select { |b| b.relationship_type == :parasitic && !b.dormant? }
        .min_by { |b| b.benefit_ratio * b.strength }
end

#network_densityObject



75
76
77
78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 75

def network_density
  return 0.0 if @bonds.empty?

  active = @bonds.values.reject(&:dormant?)
  return 0.0 if active.empty?

  avg_strength = active.sum(&:strength) / active.size.to_f
  avg_strength.clamp(0.0, 1.0).round(10)
end

#register_bond(bond) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 14

def register_bond(bond)
  raise ArgumentError, 'bond must be a SymbioticBond' unless bond.is_a?(SymbioticBond)
  raise ArgumentError, "MAX_BONDS (#{Constants::MAX_BONDS}) exceeded" if @bonds.size >= Constants::MAX_BONDS

  @bonds[bond.bond_id] = bond
  bond
end

#symbiotic_web(subsystem_id) ⇒ Object



85
86
87
# File 'lib/legion/extensions/agentic/social/symbiosis/helpers/ecosystem.rb', line 85

def symbiotic_web(subsystem_id)
  @bonds.values.select { |b| b.involves?(subsystem_id) }
end