Class: Legion::Extensions::Agentic::Inference::EnactiveCognition::Helpers::SensorimotorLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb

Constant Summary collapse

MAX_COUPLINGS =
200
MAX_ACTIONS =
500
MAX_PERCEPTIONS =
500
MAX_HISTORY =
300
DEFAULT_COUPLING_STRENGTH =
0.5
COUPLING_FLOOR =
0.0
COUPLING_CEILING =
1.0
REINFORCEMENT_RATE =
0.1
DECOUPLING_RATE =
0.15
PREDICTION_ACCURACY_THRESHOLD =
0.6
COUPLING_DECAY =
0.02
STALE_THRESHOLD =
120
COUPLING_LABELS =
{
  (0.8..)     => :entrained,
  (0.6...0.8) => :coupled,
  (0.4...0.6) => :forming,
  (0.2...0.4) => :weak,
  (..0.2)     => :decoupled
}.freeze
LOOP_TYPES =
%i[sensorimotor cognitive social].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action:, perception:, domain:, loop_type: :sensorimotor) ⇒ SensorimotorLoop

Returns a new instance of SensorimotorLoop.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 44

def initialize(action:, perception:, domain:, loop_type: :sensorimotor)
  @id                  = SecureRandom.uuid
  @action              = action
  @perception          = perception
  @domain              = domain
  @loop_type           = LOOP_TYPES.include?(loop_type) ? loop_type : :sensorimotor
  @coupling_strength   = DEFAULT_COUPLING_STRENGTH
  @execution_count     = 0
  @accurate_predictions = 0
  @prediction_accuracy = 0.0
  @created_at          = Time.now.utc
  @last_executed_at    = nil
end

Instance Attribute Details

#accurate_predictionsObject (readonly)

Returns the value of attribute accurate_predictions.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def accurate_predictions
  @accurate_predictions
end

#actionObject (readonly)

Returns the value of attribute action.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def action
  @action
end

#coupling_strengthObject (readonly)

Returns the value of attribute coupling_strength.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def coupling_strength
  @coupling_strength
end

#created_atObject (readonly)

Returns the value of attribute created_at.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def domain
  @domain
end

#execution_countObject (readonly)

Returns the value of attribute execution_count.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def execution_count
  @execution_count
end

#idObject (readonly)

Returns the value of attribute id.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def id
  @id
end

#last_executed_atObject (readonly)

Returns the value of attribute last_executed_at.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def last_executed_at
  @last_executed_at
end

#loop_typeObject (readonly)

Returns the value of attribute loop_type.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def loop_type
  @loop_type
end

#perceptionObject (readonly)

Returns the value of attribute perception.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def perception
  @perception
end

#prediction_accuracyObject (readonly)

Returns the value of attribute prediction_accuracy.



39
40
41
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 39

def prediction_accuracy
  @prediction_accuracy
end

Instance Method Details

#adapt_perception!(new_perception:) ⇒ Object



85
86
87
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 85

def adapt_perception!(new_perception:)
  @perception = new_perception
end

#coupled?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 74

def coupled?
  coupling_strength >= 0.6
end

#coupling_labelObject



78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 78

def coupling_label
  COUPLING_LABELS.each do |range, label|
    return label if range.cover?(coupling_strength)
  end
  :decoupled
end

#decay!Object



89
90
91
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 89

def decay!
  @coupling_strength = (@coupling_strength - COUPLING_DECAY).clamp(COUPLING_FLOOR, COUPLING_CEILING)
end

#execute!(actual_perception:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 58

def execute!(actual_perception:)
  @execution_count    += 1
  @last_executed_at    = Time.now.utc
  match = actual_perception.to_s == @perception.to_s

  if match
    @accurate_predictions += 1
    @coupling_strength     = (@coupling_strength + REINFORCEMENT_RATE).clamp(COUPLING_FLOOR, COUPLING_CEILING)
  else
    @coupling_strength = (@coupling_strength - DECOUPLING_RATE).clamp(COUPLING_FLOOR, COUPLING_CEILING)
  end

  @prediction_accuracy = @accurate_predictions.to_f / @execution_count
  { match: match, coupling_strength: @coupling_strength, prediction_accuracy: @prediction_accuracy }
end

#stale?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 93

def stale?
  return false if @last_executed_at.nil?

  (Time.now.utc - @last_executed_at) > STALE_THRESHOLD
end

#to_hObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/legion/extensions/agentic/inference/enactive_cognition/helpers/sensorimotor_loop.rb', line 99

def to_h
  {
    id:                   @id,
    action:               @action,
    perception:           @perception,
    domain:               @domain,
    loop_type:            @loop_type,
    coupling_strength:    @coupling_strength,
    coupling_label:       coupling_label,
    prediction_accuracy:  @prediction_accuracy,
    execution_count:      @execution_count,
    accurate_predictions: @accurate_predictions,
    coupled:              coupled?,
    created_at:           @created_at,
    last_executed_at:     @last_executed_at
  }
end