lex-agentic-learning 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +5 -0
- data/LICENSE +21 -0
- data/README.md +13 -0
- data/lex-agentic-learning.gemspec +30 -0
- data/lib/legion/extensions/agentic/learning/anchoring/client.rb +26 -0
- data/lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb +65 -0
- data/lib/legion/extensions/agentic/learning/anchoring/helpers/anchor_store.rb +132 -0
- data/lib/legion/extensions/agentic/learning/anchoring/helpers/constants.rb +31 -0
- data/lib/legion/extensions/agentic/learning/anchoring/runners/anchoring.rb +100 -0
- data/lib/legion/extensions/agentic/learning/anchoring/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/anchoring.rb +19 -0
- data/lib/legion/extensions/agentic/learning/catalyst/client.rb +15 -0
- data/lib/legion/extensions/agentic/learning/catalyst/helpers/catalyst.rb +87 -0
- data/lib/legion/extensions/agentic/learning/catalyst/helpers/catalyst_engine.rb +153 -0
- data/lib/legion/extensions/agentic/learning/catalyst/helpers/constants.rb +55 -0
- data/lib/legion/extensions/agentic/learning/catalyst/helpers/reaction.rb +87 -0
- data/lib/legion/extensions/agentic/learning/catalyst/runners/cognitive_catalyst.rb +103 -0
- data/lib/legion/extensions/agentic/learning/catalyst/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/catalyst.rb +22 -0
- data/lib/legion/extensions/agentic/learning/chrysalis/client.rb +22 -0
- data/lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb +137 -0
- data/lib/legion/extensions/agentic/learning/chrysalis/helpers/cocoon.rb +89 -0
- data/lib/legion/extensions/agentic/learning/chrysalis/helpers/constants.rb +49 -0
- data/lib/legion/extensions/agentic/learning/chrysalis/helpers/metamorphosis_engine.rb +157 -0
- data/lib/legion/extensions/agentic/learning/chrysalis/runners/cognitive_chrysalis.rb +129 -0
- data/lib/legion/extensions/agentic/learning/chrysalis/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/chrysalis.rb +21 -0
- data/lib/legion/extensions/agentic/learning/curiosity/client.rb +28 -0
- data/lib/legion/extensions/agentic/learning/curiosity/helpers/constants.rb +30 -0
- data/lib/legion/extensions/agentic/learning/curiosity/helpers/gap_detector.rb +167 -0
- data/lib/legion/extensions/agentic/learning/curiosity/helpers/wonder.rb +73 -0
- data/lib/legion/extensions/agentic/learning/curiosity/helpers/wonder_store.rb +149 -0
- data/lib/legion/extensions/agentic/learning/curiosity/runners/curiosity.rb +163 -0
- data/lib/legion/extensions/agentic/learning/curiosity/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/curiosity.rb +21 -0
- data/lib/legion/extensions/agentic/learning/epistemic_curiosity/client.rb +28 -0
- data/lib/legion/extensions/agentic/learning/epistemic_curiosity/helpers/constants.rb +31 -0
- data/lib/legion/extensions/agentic/learning/epistemic_curiosity/helpers/curiosity_engine.rb +122 -0
- data/lib/legion/extensions/agentic/learning/epistemic_curiosity/helpers/knowledge_gap.rb +70 -0
- data/lib/legion/extensions/agentic/learning/epistemic_curiosity/runners/epistemic_curiosity.rb +106 -0
- data/lib/legion/extensions/agentic/learning/epistemic_curiosity/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/epistemic_curiosity.rb +19 -0
- data/lib/legion/extensions/agentic/learning/fermentation/client.rb +19 -0
- data/lib/legion/extensions/agentic/learning/fermentation/helpers/batch.rb +75 -0
- data/lib/legion/extensions/agentic/learning/fermentation/helpers/constants.rb +78 -0
- data/lib/legion/extensions/agentic/learning/fermentation/helpers/fermentation_engine.rb +147 -0
- data/lib/legion/extensions/agentic/learning/fermentation/helpers/substrate.rb +108 -0
- data/lib/legion/extensions/agentic/learning/fermentation/runners/cognitive_fermentation.rb +60 -0
- data/lib/legion/extensions/agentic/learning/fermentation/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/fermentation.rb +22 -0
- data/lib/legion/extensions/agentic/learning/habit/client.rb +26 -0
- data/lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb +120 -0
- data/lib/legion/extensions/agentic/learning/habit/helpers/constants.rb +44 -0
- data/lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb +148 -0
- data/lib/legion/extensions/agentic/learning/habit/runners/habit.rb +86 -0
- data/lib/legion/extensions/agentic/learning/habit/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/habit.rb +19 -0
- data/lib/legion/extensions/agentic/learning/hebbian/actors/decay.rb +45 -0
- data/lib/legion/extensions/agentic/learning/hebbian/client.rb +29 -0
- data/lib/legion/extensions/agentic/learning/hebbian/helpers/assembly.rb +82 -0
- data/lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb +190 -0
- data/lib/legion/extensions/agentic/learning/hebbian/helpers/constants.rb +50 -0
- data/lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb +94 -0
- data/lib/legion/extensions/agentic/learning/hebbian/runners/hebbian_assembly.rb +94 -0
- data/lib/legion/extensions/agentic/learning/hebbian/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/hebbian.rb +20 -0
- data/lib/legion/extensions/agentic/learning/learning_rate/client.rb +25 -0
- data/lib/legion/extensions/agentic/learning/learning_rate/helpers/constants.rb +35 -0
- data/lib/legion/extensions/agentic/learning/learning_rate/helpers/rate_model.rb +133 -0
- data/lib/legion/extensions/agentic/learning/learning_rate/runners/learning_rate.rb +85 -0
- data/lib/legion/extensions/agentic/learning/learning_rate/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/learning_rate.rb +18 -0
- data/lib/legion/extensions/agentic/learning/meta_learning/client.rb +27 -0
- data/lib/legion/extensions/agentic/learning/meta_learning/helpers/constants.rb +46 -0
- data/lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb +85 -0
- data/lib/legion/extensions/agentic/learning/meta_learning/helpers/meta_learning_engine.rb +202 -0
- data/lib/legion/extensions/agentic/learning/meta_learning/helpers/strategy.rb +62 -0
- data/lib/legion/extensions/agentic/learning/meta_learning/runners/meta_learning.rb +118 -0
- data/lib/legion/extensions/agentic/learning/meta_learning/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/meta_learning.rb +20 -0
- data/lib/legion/extensions/agentic/learning/plasticity/client.rb +15 -0
- data/lib/legion/extensions/agentic/learning/plasticity/helpers/constants.rb +45 -0
- data/lib/legion/extensions/agentic/learning/plasticity/helpers/neural_pathway.rb +85 -0
- data/lib/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine.rb +130 -0
- data/lib/legion/extensions/agentic/learning/plasticity/runners/cognitive_plasticity.rb +85 -0
- data/lib/legion/extensions/agentic/learning/plasticity/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/plasticity.rb +19 -0
- data/lib/legion/extensions/agentic/learning/preference_learning/actors/decay.rb +45 -0
- data/lib/legion/extensions/agentic/learning/preference_learning/client.rb +28 -0
- data/lib/legion/extensions/agentic/learning/preference_learning/helpers/constants.rb +35 -0
- data/lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb +78 -0
- data/lib/legion/extensions/agentic/learning/preference_learning/helpers/preference_engine.rb +121 -0
- data/lib/legion/extensions/agentic/learning/preference_learning/runners/preference_learning.rb +84 -0
- data/lib/legion/extensions/agentic/learning/preference_learning/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/preference_learning.rb +19 -0
- data/lib/legion/extensions/agentic/learning/procedural/client.rb +19 -0
- data/lib/legion/extensions/agentic/learning/procedural/helpers/constants.rb +46 -0
- data/lib/legion/extensions/agentic/learning/procedural/helpers/learning_engine.rb +160 -0
- data/lib/legion/extensions/agentic/learning/procedural/helpers/production.rb +66 -0
- data/lib/legion/extensions/agentic/learning/procedural/helpers/skill.rb +101 -0
- data/lib/legion/extensions/agentic/learning/procedural/runners/procedural_learning.rb +96 -0
- data/lib/legion/extensions/agentic/learning/procedural/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/procedural.rb +20 -0
- data/lib/legion/extensions/agentic/learning/scaffolding/client.rb +26 -0
- data/lib/legion/extensions/agentic/learning/scaffolding/helpers/constants.rb +42 -0
- data/lib/legion/extensions/agentic/learning/scaffolding/helpers/scaffold.rb +136 -0
- data/lib/legion/extensions/agentic/learning/scaffolding/helpers/scaffolding_engine.rb +112 -0
- data/lib/legion/extensions/agentic/learning/scaffolding/runners/cognitive_scaffolding.rb +107 -0
- data/lib/legion/extensions/agentic/learning/scaffolding/version.rb +13 -0
- data/lib/legion/extensions/agentic/learning/scaffolding.rb +19 -0
- data/lib/legion/extensions/agentic/learning/version.rb +11 -0
- data/lib/legion/extensions/agentic/learning.rb +31 -0
- data/spec/legion/extensions/agentic/learning/anchoring/client_spec.rb +32 -0
- data/spec/legion/extensions/agentic/learning/anchoring/helpers/anchor_spec.rb +130 -0
- data/spec/legion/extensions/agentic/learning/anchoring/helpers/anchor_store_spec.rb +201 -0
- data/spec/legion/extensions/agentic/learning/anchoring/helpers/constants_spec.rb +63 -0
- data/spec/legion/extensions/agentic/learning/anchoring/runners/anchoring_spec.rb +199 -0
- data/spec/legion/extensions/agentic/learning/catalyst/client_spec.rb +58 -0
- data/spec/legion/extensions/agentic/learning/catalyst/cognitive_catalyst_spec.rb +49 -0
- data/spec/legion/extensions/agentic/learning/catalyst/helpers/catalyst_engine_spec.rb +263 -0
- data/spec/legion/extensions/agentic/learning/catalyst/helpers/catalyst_spec.rb +214 -0
- data/spec/legion/extensions/agentic/learning/catalyst/helpers/reaction_spec.rb +223 -0
- data/spec/legion/extensions/agentic/learning/catalyst/runners/cognitive_catalyst_spec.rb +217 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/client_spec.rb +83 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/cognitive_chrysalis_spec.rb +15 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis_engine_spec.rb +57 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis_spec.rb +305 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/helpers/cocoon_spec.rb +206 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/helpers/constants_spec.rb +109 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/helpers/metamorphic_cycle_spec.rb +76 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/helpers/metamorphosis_engine_spec.rb +247 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/helpers/transformation_phase_spec.rb +98 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/runners/cognitive_chrysalis_spec.rb +180 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/runners/reporting_spec.rb +81 -0
- data/spec/legion/extensions/agentic/learning/chrysalis/runners/transformation_spec.rb +74 -0
- data/spec/legion/extensions/agentic/learning/curiosity/client_spec.rb +27 -0
- data/spec/legion/extensions/agentic/learning/curiosity/helpers/gap_detector_spec.rb +118 -0
- data/spec/legion/extensions/agentic/learning/curiosity/helpers/wonder_spec.rb +130 -0
- data/spec/legion/extensions/agentic/learning/curiosity/helpers/wonder_store_spec.rb +136 -0
- data/spec/legion/extensions/agentic/learning/curiosity/runners/curiosity_spec.rb +159 -0
- data/spec/legion/extensions/agentic/learning/epistemic_curiosity/client_spec.rb +47 -0
- data/spec/legion/extensions/agentic/learning/epistemic_curiosity/helpers/constants_spec.rb +45 -0
- data/spec/legion/extensions/agentic/learning/epistemic_curiosity/helpers/curiosity_engine_spec.rb +229 -0
- data/spec/legion/extensions/agentic/learning/epistemic_curiosity/helpers/knowledge_gap_spec.rb +188 -0
- data/spec/legion/extensions/agentic/learning/epistemic_curiosity/runners/epistemic_curiosity_spec.rb +175 -0
- data/spec/legion/extensions/agentic/learning/fermentation/client_spec.rb +36 -0
- data/spec/legion/extensions/agentic/learning/fermentation/helpers/batch_spec.rb +72 -0
- data/spec/legion/extensions/agentic/learning/fermentation/helpers/fermentation_engine_spec.rb +138 -0
- data/spec/legion/extensions/agentic/learning/fermentation/helpers/substrate_spec.rb +146 -0
- data/spec/legion/extensions/agentic/learning/habit/client_spec.rb +50 -0
- data/spec/legion/extensions/agentic/learning/habit/helpers/action_sequence_spec.rb +276 -0
- data/spec/legion/extensions/agentic/learning/habit/helpers/constants_spec.rb +115 -0
- data/spec/legion/extensions/agentic/learning/habit/helpers/habit_store_spec.rb +274 -0
- data/spec/legion/extensions/agentic/learning/habit/runners/habit_spec.rb +228 -0
- data/spec/legion/extensions/agentic/learning/hebbian/client_spec.rb +38 -0
- data/spec/legion/extensions/agentic/learning/hebbian/helpers/assembly_network_spec.rb +142 -0
- data/spec/legion/extensions/agentic/learning/hebbian/helpers/assembly_spec.rb +89 -0
- data/spec/legion/extensions/agentic/learning/hebbian/helpers/unit_spec.rb +119 -0
- data/spec/legion/extensions/agentic/learning/hebbian/runners/hebbian_assembly_spec.rb +109 -0
- data/spec/legion/extensions/agentic/learning/learning_rate/client_spec.rb +51 -0
- data/spec/legion/extensions/agentic/learning/learning_rate/helpers/constants_spec.rb +29 -0
- data/spec/legion/extensions/agentic/learning/learning_rate/helpers/rate_model_spec.rb +151 -0
- data/spec/legion/extensions/agentic/learning/learning_rate/runners/learning_rate_spec.rb +92 -0
- data/spec/legion/extensions/agentic/learning/meta_learning/client_spec.rb +27 -0
- data/spec/legion/extensions/agentic/learning/meta_learning/helpers/constants_spec.rb +43 -0
- data/spec/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain_spec.rb +146 -0
- data/spec/legion/extensions/agentic/learning/meta_learning/helpers/meta_learning_engine_spec.rb +309 -0
- data/spec/legion/extensions/agentic/learning/meta_learning/helpers/strategy_spec.rb +82 -0
- data/spec/legion/extensions/agentic/learning/meta_learning/runners/meta_learning_spec.rb +185 -0
- data/spec/legion/extensions/agentic/learning/plasticity/helpers/constants_spec.rb +54 -0
- data/spec/legion/extensions/agentic/learning/plasticity/helpers/neural_pathway_spec.rb +136 -0
- data/spec/legion/extensions/agentic/learning/plasticity/helpers/plasticity_engine_spec.rb +157 -0
- data/spec/legion/extensions/agentic/learning/plasticity/runners/cognitive_plasticity_spec.rb +83 -0
- data/spec/legion/extensions/agentic/learning/preference_learning/client_spec.rb +17 -0
- data/spec/legion/extensions/agentic/learning/preference_learning/helpers/constants_spec.rb +67 -0
- data/spec/legion/extensions/agentic/learning/preference_learning/helpers/option_spec.rb +104 -0
- data/spec/legion/extensions/agentic/learning/preference_learning/helpers/preference_engine_spec.rb +151 -0
- data/spec/legion/extensions/agentic/learning/preference_learning/runners/preference_learning_spec.rb +86 -0
- data/spec/legion/extensions/agentic/learning/procedural/client_spec.rb +22 -0
- data/spec/legion/extensions/agentic/learning/procedural/helpers/learning_engine_spec.rb +135 -0
- data/spec/legion/extensions/agentic/learning/procedural/helpers/production_spec.rb +66 -0
- data/spec/legion/extensions/agentic/learning/procedural/helpers/skill_spec.rb +102 -0
- data/spec/legion/extensions/agentic/learning/procedural/runners/procedural_learning_spec.rb +94 -0
- data/spec/legion/extensions/agentic/learning/scaffolding/client_spec.rb +20 -0
- data/spec/legion/extensions/agentic/learning/scaffolding/helpers/constants_spec.rb +36 -0
- data/spec/legion/extensions/agentic/learning/scaffolding/helpers/scaffold_spec.rb +187 -0
- data/spec/legion/extensions/agentic/learning/scaffolding/helpers/scaffolding_engine_spec.rb +159 -0
- data/spec/legion/extensions/agentic/learning/scaffolding/runners/cognitive_scaffolding_spec.rb +163 -0
- data/spec/spec_helper.rb +46 -0
- metadata +277 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/learning/meta_learning/version'
|
|
4
|
+
require 'legion/extensions/agentic/learning/meta_learning/helpers/constants'
|
|
5
|
+
require 'legion/extensions/agentic/learning/meta_learning/helpers/learning_domain'
|
|
6
|
+
require 'legion/extensions/agentic/learning/meta_learning/helpers/strategy'
|
|
7
|
+
require 'legion/extensions/agentic/learning/meta_learning/helpers/meta_learning_engine'
|
|
8
|
+
require 'legion/extensions/agentic/learning/meta_learning/runners/meta_learning'
|
|
9
|
+
require 'legion/extensions/agentic/learning/meta_learning/client'
|
|
10
|
+
|
|
11
|
+
module Legion
|
|
12
|
+
module Extensions
|
|
13
|
+
module Agentic
|
|
14
|
+
module Learning
|
|
15
|
+
module MetaLearning
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Learning
|
|
7
|
+
module Plasticity
|
|
8
|
+
module Helpers
|
|
9
|
+
module Constants
|
|
10
|
+
MAX_PATHWAYS = 300
|
|
11
|
+
MAX_EVENTS = 500
|
|
12
|
+
|
|
13
|
+
DEFAULT_LEARNING_RATE = 0.5
|
|
14
|
+
STRENGTHENING_RATE = 0.08
|
|
15
|
+
PRUNING_THRESHOLD = 0.15
|
|
16
|
+
CRITICAL_PERIOD_MULTIPLIER = 2.5
|
|
17
|
+
MATURATION_RATE = 0.01
|
|
18
|
+
|
|
19
|
+
PLASTICITY_LABELS = {
|
|
20
|
+
(0.8..) => :highly_plastic,
|
|
21
|
+
(0.6...0.8) => :plastic,
|
|
22
|
+
(0.4...0.6) => :moderate,
|
|
23
|
+
(0.2...0.4) => :rigid,
|
|
24
|
+
(..0.2) => :crystallized
|
|
25
|
+
}.freeze
|
|
26
|
+
|
|
27
|
+
STRENGTH_LABELS = {
|
|
28
|
+
(0.8..) => :robust,
|
|
29
|
+
(0.6...0.8) => :strong,
|
|
30
|
+
(0.4...0.6) => :moderate,
|
|
31
|
+
(0.2...0.4) => :weak,
|
|
32
|
+
(..0.2) => :nascent
|
|
33
|
+
}.freeze
|
|
34
|
+
|
|
35
|
+
PATHWAY_TYPES = %i[
|
|
36
|
+
synaptic structural functional
|
|
37
|
+
homeostatic metaplastic
|
|
38
|
+
].freeze
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Learning
|
|
9
|
+
module Plasticity
|
|
10
|
+
module Helpers
|
|
11
|
+
class NeuralPathway
|
|
12
|
+
include Constants
|
|
13
|
+
|
|
14
|
+
attr_reader :id, :label, :pathway_type, :strength, :plasticity,
|
|
15
|
+
:activation_count, :created_at
|
|
16
|
+
|
|
17
|
+
def initialize(label:, pathway_type: :synaptic, initial_strength: 0.3)
|
|
18
|
+
@id = SecureRandom.uuid
|
|
19
|
+
@label = label
|
|
20
|
+
@pathway_type = pathway_type.to_sym
|
|
21
|
+
@strength = initial_strength.to_f.clamp(0.0, 1.0)
|
|
22
|
+
@plasticity = DEFAULT_LEARNING_RATE
|
|
23
|
+
@activation_count = 0
|
|
24
|
+
@created_at = Time.now.utc
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def strengthen!(amount: STRENGTHENING_RATE, critical_period: false)
|
|
28
|
+
multiplier = critical_period ? CRITICAL_PERIOD_MULTIPLIER : 1.0
|
|
29
|
+
effective = (amount * multiplier * @plasticity).round(10)
|
|
30
|
+
@strength = (@strength + effective).clamp(0.0, 1.0).round(10)
|
|
31
|
+
@activation_count += 1
|
|
32
|
+
mature!
|
|
33
|
+
self
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def weaken!(amount: STRENGTHENING_RATE)
|
|
37
|
+
effective = (amount * @plasticity).round(10)
|
|
38
|
+
@strength = (@strength - effective).clamp(0.0, 1.0).round(10)
|
|
39
|
+
self
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def prune_candidate?
|
|
43
|
+
@strength < PRUNING_THRESHOLD
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def mature!
|
|
47
|
+
@plasticity = (@plasticity - MATURATION_RATE).clamp(0.1, 1.0).round(10)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def rejuvenate!(amount: 0.1)
|
|
51
|
+
@plasticity = (@plasticity + amount).clamp(0.1, 1.0).round(10)
|
|
52
|
+
self
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def strength_label
|
|
56
|
+
match = STRENGTH_LABELS.find { |range, _| range.cover?(@strength) }
|
|
57
|
+
match ? match.last : :nascent
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def plasticity_label
|
|
61
|
+
match = PLASTICITY_LABELS.find { |range, _| range.cover?(@plasticity) }
|
|
62
|
+
match ? match.last : :crystallized
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def to_h
|
|
66
|
+
{
|
|
67
|
+
id: @id,
|
|
68
|
+
label: @label,
|
|
69
|
+
pathway_type: @pathway_type,
|
|
70
|
+
strength: @strength,
|
|
71
|
+
strength_label: strength_label,
|
|
72
|
+
plasticity: @plasticity,
|
|
73
|
+
plasticity_label: plasticity_label,
|
|
74
|
+
activation_count: @activation_count,
|
|
75
|
+
prune_candidate: prune_candidate?,
|
|
76
|
+
created_at: @created_at
|
|
77
|
+
}
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Learning
|
|
7
|
+
module Plasticity
|
|
8
|
+
module Helpers
|
|
9
|
+
class PlasticityEngine
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@pathways = {}
|
|
14
|
+
@critical_period = false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def create_pathway(label:, pathway_type: :synaptic, initial_strength: 0.3)
|
|
18
|
+
prune_if_needed
|
|
19
|
+
pathway = NeuralPathway.new(label: label, pathway_type: pathway_type,
|
|
20
|
+
initial_strength: initial_strength)
|
|
21
|
+
@pathways[pathway.id] = pathway
|
|
22
|
+
pathway
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def strengthen_pathway(pathway_id:, amount: STRENGTHENING_RATE)
|
|
26
|
+
pathway = @pathways[pathway_id]
|
|
27
|
+
return nil unless pathway
|
|
28
|
+
|
|
29
|
+
pathway.strengthen!(amount: amount, critical_period: @critical_period)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def weaken_pathway(pathway_id:, amount: STRENGTHENING_RATE)
|
|
33
|
+
pathway = @pathways[pathway_id]
|
|
34
|
+
return nil unless pathway
|
|
35
|
+
|
|
36
|
+
pathway.weaken!(amount: amount)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def rejuvenate_pathway(pathway_id:, amount: 0.1)
|
|
40
|
+
pathway = @pathways[pathway_id]
|
|
41
|
+
return nil unless pathway
|
|
42
|
+
|
|
43
|
+
pathway.rejuvenate!(amount: amount)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def enter_critical_period!
|
|
47
|
+
@critical_period = true
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def exit_critical_period!
|
|
51
|
+
@critical_period = false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def critical_period?
|
|
55
|
+
@critical_period
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def prune_weak_pathways!
|
|
59
|
+
candidates = @pathways.values.select(&:prune_candidate?)
|
|
60
|
+
candidates.each { |p| @pathways.delete(p.id) }
|
|
61
|
+
candidates.size
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def pathways_by_type(pathway_type:)
|
|
65
|
+
pt = pathway_type.to_sym
|
|
66
|
+
@pathways.values.select { |p| p.pathway_type == pt }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def strongest_pathways(limit: 5)
|
|
70
|
+
@pathways.values.sort_by { |p| -p.strength }.first(limit)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def weakest_pathways(limit: 5)
|
|
74
|
+
@pathways.values.sort_by(&:strength).first(limit)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def average_strength
|
|
78
|
+
return 0.0 if @pathways.empty?
|
|
79
|
+
|
|
80
|
+
strengths = @pathways.values.map(&:strength)
|
|
81
|
+
(strengths.sum / strengths.size).round(10)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def average_plasticity
|
|
85
|
+
return DEFAULT_LEARNING_RATE if @pathways.empty?
|
|
86
|
+
|
|
87
|
+
plasticities = @pathways.values.map(&:plasticity)
|
|
88
|
+
(plasticities.sum / plasticities.size).round(10)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def prune_candidates_count
|
|
92
|
+
@pathways.values.count(&:prune_candidate?)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def plasticity_report
|
|
96
|
+
{
|
|
97
|
+
total_pathways: @pathways.size,
|
|
98
|
+
critical_period: @critical_period,
|
|
99
|
+
average_strength: average_strength,
|
|
100
|
+
average_plasticity: average_plasticity,
|
|
101
|
+
prune_candidates: prune_candidates_count,
|
|
102
|
+
strongest: strongest_pathways(limit: 3).map(&:to_h)
|
|
103
|
+
}
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def to_h
|
|
107
|
+
{
|
|
108
|
+
total_pathways: @pathways.size,
|
|
109
|
+
critical_period: @critical_period,
|
|
110
|
+
average_strength: average_strength,
|
|
111
|
+
average_plasticity: average_plasticity,
|
|
112
|
+
prune_candidates: prune_candidates_count
|
|
113
|
+
}
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
def prune_if_needed
|
|
119
|
+
return if @pathways.size < MAX_PATHWAYS
|
|
120
|
+
|
|
121
|
+
weakest = @pathways.values.min_by(&:strength)
|
|
122
|
+
@pathways.delete(weakest.id) if weakest
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Learning
|
|
7
|
+
module Plasticity
|
|
8
|
+
module Runners
|
|
9
|
+
module CognitivePlasticity
|
|
10
|
+
include Helpers::Constants
|
|
11
|
+
|
|
12
|
+
include Legion::Extensions::Helpers::Lex if defined?(Legion::Extensions::Helpers::Lex)
|
|
13
|
+
|
|
14
|
+
def create_pathway(label:, engine: nil, pathway_type: :synaptic, initial_strength: 0.3, **)
|
|
15
|
+
eng = engine || default_engine
|
|
16
|
+
pathway = eng.create_pathway(label: label, pathway_type: pathway_type,
|
|
17
|
+
initial_strength: initial_strength)
|
|
18
|
+
{ success: true, pathway: pathway.to_h }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def strengthen_pathway(pathway_id:, engine: nil, amount: STRENGTHENING_RATE, **)
|
|
22
|
+
eng = engine || default_engine
|
|
23
|
+
result = eng.strengthen_pathway(pathway_id: pathway_id, amount: amount)
|
|
24
|
+
return { success: false, error: 'pathway not found' } unless result
|
|
25
|
+
|
|
26
|
+
{ success: true, pathway: result.to_h }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def weaken_pathway(pathway_id:, engine: nil, amount: STRENGTHENING_RATE, **)
|
|
30
|
+
eng = engine || default_engine
|
|
31
|
+
result = eng.weaken_pathway(pathway_id: pathway_id, amount: amount)
|
|
32
|
+
return { success: false, error: 'pathway not found' } unless result
|
|
33
|
+
|
|
34
|
+
{ success: true, pathway: result.to_h }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def rejuvenate_pathway(pathway_id:, engine: nil, amount: 0.1, **)
|
|
38
|
+
eng = engine || default_engine
|
|
39
|
+
result = eng.rejuvenate_pathway(pathway_id: pathway_id, amount: amount)
|
|
40
|
+
return { success: false, error: 'pathway not found' } unless result
|
|
41
|
+
|
|
42
|
+
{ success: true, pathway: result.to_h }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def enter_critical_period(engine: nil, **)
|
|
46
|
+
eng = engine || default_engine
|
|
47
|
+
eng.enter_critical_period!
|
|
48
|
+
{ success: true, critical_period: true }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def exit_critical_period(engine: nil, **)
|
|
52
|
+
eng = engine || default_engine
|
|
53
|
+
eng.exit_critical_period!
|
|
54
|
+
{ success: true, critical_period: false }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def prune_weak_pathways(engine: nil, **)
|
|
58
|
+
eng = engine || default_engine
|
|
59
|
+
pruned = eng.prune_weak_pathways!
|
|
60
|
+
{ success: true, pruned_count: pruned }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def strongest_pathways(engine: nil, limit: 5, **)
|
|
64
|
+
eng = engine || default_engine
|
|
65
|
+
pathways = eng.strongest_pathways(limit: limit).map(&:to_h)
|
|
66
|
+
{ success: true, pathways: pathways, count: pathways.size }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def plasticity_report(engine: nil, **)
|
|
70
|
+
eng = engine || default_engine
|
|
71
|
+
{ success: true, report: eng.plasticity_report }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def default_engine
|
|
77
|
+
@default_engine ||= Helpers::PlasticityEngine.new
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'plasticity/version'
|
|
4
|
+
require_relative 'plasticity/helpers/constants'
|
|
5
|
+
require_relative 'plasticity/helpers/neural_pathway'
|
|
6
|
+
require_relative 'plasticity/helpers/plasticity_engine'
|
|
7
|
+
require_relative 'plasticity/runners/cognitive_plasticity'
|
|
8
|
+
require_relative 'plasticity/client'
|
|
9
|
+
|
|
10
|
+
module Legion
|
|
11
|
+
module Extensions
|
|
12
|
+
module Agentic
|
|
13
|
+
module Learning
|
|
14
|
+
module Plasticity
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/actors/every'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Learning
|
|
9
|
+
module PreferenceLearning
|
|
10
|
+
module Actor
|
|
11
|
+
class Decay < Legion::Extensions::Actors::Every
|
|
12
|
+
def runner_class
|
|
13
|
+
Legion::Extensions::Agentic::Learning::PreferenceLearning::Runners::PreferenceLearning
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def runner_function
|
|
17
|
+
'update_preference_learning'
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def time
|
|
21
|
+
300
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def run_now?
|
|
25
|
+
false
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def use_runner?
|
|
29
|
+
false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def check_subtask?
|
|
33
|
+
false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def generate_task?
|
|
37
|
+
false
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/learning/preference_learning/helpers/constants'
|
|
4
|
+
require 'legion/extensions/agentic/learning/preference_learning/helpers/option'
|
|
5
|
+
require 'legion/extensions/agentic/learning/preference_learning/helpers/preference_engine'
|
|
6
|
+
require 'legion/extensions/agentic/learning/preference_learning/runners/preference_learning'
|
|
7
|
+
|
|
8
|
+
module Legion
|
|
9
|
+
module Extensions
|
|
10
|
+
module Agentic
|
|
11
|
+
module Learning
|
|
12
|
+
module PreferenceLearning
|
|
13
|
+
class Client
|
|
14
|
+
include Runners::PreferenceLearning
|
|
15
|
+
|
|
16
|
+
def initialize(**)
|
|
17
|
+
@preference_engine = Helpers::PreferenceEngine.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :preference_engine
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Learning
|
|
7
|
+
module PreferenceLearning
|
|
8
|
+
module Helpers
|
|
9
|
+
module Constants
|
|
10
|
+
PREFERENCE_LABELS = {
|
|
11
|
+
(0.8..) => :strongly_preferred,
|
|
12
|
+
(0.6...0.8) => :preferred,
|
|
13
|
+
(0.4...0.6) => :neutral,
|
|
14
|
+
(0.2...0.4) => :disliked,
|
|
15
|
+
(..0.2) => :strongly_disliked
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
MAX_OPTIONS = 200
|
|
19
|
+
MAX_COMPARISONS = 1000
|
|
20
|
+
MAX_HISTORY = 500
|
|
21
|
+
|
|
22
|
+
DEFAULT_PREFERENCE = 0.5
|
|
23
|
+
PREFERENCE_FLOOR = 0.0
|
|
24
|
+
PREFERENCE_CEILING = 1.0
|
|
25
|
+
|
|
26
|
+
WIN_BOOST = 0.08
|
|
27
|
+
LOSS_PENALTY = 0.06
|
|
28
|
+
DECAY_RATE = 0.01
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Learning
|
|
9
|
+
module PreferenceLearning
|
|
10
|
+
module Helpers
|
|
11
|
+
class Option
|
|
12
|
+
attr_reader :id, :label, :domain, :created_at
|
|
13
|
+
attr_accessor :preference_score, :wins, :losses, :times_seen
|
|
14
|
+
|
|
15
|
+
def initialize(label:, domain: :general)
|
|
16
|
+
@id = SecureRandom.uuid
|
|
17
|
+
@label = label
|
|
18
|
+
@domain = domain
|
|
19
|
+
@preference_score = Constants::DEFAULT_PREFERENCE
|
|
20
|
+
@wins = 0
|
|
21
|
+
@losses = 0
|
|
22
|
+
@times_seen = 0
|
|
23
|
+
@created_at = Time.now.utc
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def win!
|
|
27
|
+
@wins += 1
|
|
28
|
+
@times_seen += 1
|
|
29
|
+
@preference_score = clamp(@preference_score + Constants::WIN_BOOST)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def lose!
|
|
33
|
+
@losses += 1
|
|
34
|
+
@times_seen += 1
|
|
35
|
+
@preference_score = clamp(@preference_score - Constants::LOSS_PENALTY)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def win_rate
|
|
39
|
+
total = @wins + @losses
|
|
40
|
+
return 0.0 if total.zero?
|
|
41
|
+
|
|
42
|
+
@wins.to_f / (total + 1)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def preference_label
|
|
46
|
+
Constants::PREFERENCE_LABELS.each do |range, label|
|
|
47
|
+
return label if range.cover?(@preference_score)
|
|
48
|
+
end
|
|
49
|
+
:neutral
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def to_h
|
|
53
|
+
{
|
|
54
|
+
id: @id,
|
|
55
|
+
label: @label,
|
|
56
|
+
domain: @domain,
|
|
57
|
+
preference_score: @preference_score,
|
|
58
|
+
wins: @wins,
|
|
59
|
+
losses: @losses,
|
|
60
|
+
times_seen: @times_seen,
|
|
61
|
+
win_rate: win_rate,
|
|
62
|
+
preference_label: preference_label,
|
|
63
|
+
created_at: @created_at
|
|
64
|
+
}
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def clamp(value)
|
|
70
|
+
value.clamp(Constants::PREFERENCE_FLOOR, Constants::PREFERENCE_CEILING)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|