Class: Legion::Extensions::Agentic::Learning::Plasticity::Helpers::PlasticityEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb

Constant Summary

Constants included from Constants

Constants::CRITICAL_PERIOD_MULTIPLIER, Constants::DEFAULT_LEARNING_RATE, Constants::MATURATION_RATE, Constants::MAX_EVENTS, Constants::MAX_PATHWAYS, Constants::PATHWAY_TYPES, Constants::PLASTICITY_LABELS, Constants::PRUNING_THRESHOLD, Constants::STRENGTHENING_RATE, Constants::STRENGTH_LABELS

Instance Method Summary collapse

Constructor Details

#initializePlasticityEngine

Returns a new instance of PlasticityEngine.



12
13
14
15
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 12

def initialize
  @pathways        = {}
  @critical_period = false
end

Instance Method Details

#average_plasticityObject



84
85
86
87
88
89
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 84

def average_plasticity
  return DEFAULT_LEARNING_RATE if @pathways.empty?

  plasticities = @pathways.values.map(&:plasticity)
  (plasticities.sum / plasticities.size).round(10)
end

#average_strengthObject



77
78
79
80
81
82
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 77

def average_strength
  return 0.0 if @pathways.empty?

  strengths = @pathways.values.map(&:strength)
  (strengths.sum / strengths.size).round(10)
end

#create_pathway(label:, pathway_type: :synaptic, initial_strength: 0.3) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 17

def create_pathway(label:, pathway_type: :synaptic, initial_strength: 0.3)
  prune_if_needed
  pathway = NeuralPathway.new(label: label, pathway_type: pathway_type,
                              initial_strength: initial_strength)
  @pathways[pathway.id] = pathway
  pathway
end

#critical_period?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 54

def critical_period?
  @critical_period
end

#enter_critical_period!Object



46
47
48
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 46

def enter_critical_period!
  @critical_period = true
end

#exit_critical_period!Object



50
51
52
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 50

def exit_critical_period!
  @critical_period = false
end

#pathways_by_type(pathway_type:) ⇒ Object



64
65
66
67
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 64

def pathways_by_type(pathway_type:)
  pt = pathway_type.to_sym
  @pathways.values.select { |p| p.pathway_type == pt }
end

#plasticity_reportObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 95

def plasticity_report
  {
    total_pathways:     @pathways.size,
    critical_period:    @critical_period,
    average_strength:   average_strength,
    average_plasticity: average_plasticity,
    prune_candidates:   prune_candidates_count,
    strongest:          strongest_pathways(limit: 3).map(&:to_h)
  }
end

#prune_candidates_countObject



91
92
93
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 91

def prune_candidates_count
  @pathways.values.count(&:prune_candidate?)
end

#prune_weak_pathways!Object



58
59
60
61
62
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 58

def prune_weak_pathways!
  candidates = @pathways.values.select(&:prune_candidate?)
  candidates.each { |p| @pathways.delete(p.id) }
  candidates.size
end

#rejuvenate_pathway(pathway_id:, amount: 0.1) ⇒ Object



39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 39

def rejuvenate_pathway(pathway_id:, amount: 0.1)
  pathway = @pathways[pathway_id]
  return nil unless pathway

  pathway.rejuvenate!(amount: amount)
end

#strengthen_pathway(pathway_id:, amount: STRENGTHENING_RATE) ⇒ Object



25
26
27
28
29
30
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 25

def strengthen_pathway(pathway_id:, amount: STRENGTHENING_RATE)
  pathway = @pathways[pathway_id]
  return nil unless pathway

  pathway.strengthen!(amount: amount, critical_period: @critical_period)
end

#strongest_pathways(limit: 5) ⇒ Object



69
70
71
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 69

def strongest_pathways(limit: 5)
  @pathways.values.sort_by { |p| -p.strength }.first(limit)
end

#to_hObject



106
107
108
109
110
111
112
113
114
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 106

def to_h
  {
    total_pathways:     @pathways.size,
    critical_period:    @critical_period,
    average_strength:   average_strength,
    average_plasticity: average_plasticity,
    prune_candidates:   prune_candidates_count
  }
end

#weaken_pathway(pathway_id:, amount: STRENGTHENING_RATE) ⇒ Object



32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 32

def weaken_pathway(pathway_id:, amount: STRENGTHENING_RATE)
  pathway = @pathways[pathway_id]
  return nil unless pathway

  pathway.weaken!(amount: amount)
end

#weakest_pathways(limit: 5) ⇒ Object



73
74
75
# File 'lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb', line 73

def weakest_pathways(limit: 5)
  @pathways.values.sort_by(&:strength).first(limit)
end