Class: Legion::Extensions::Agentic::Affect::Resilience::Helpers::ResilienceModel

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResilienceModel

Returns a new instance of ResilienceModel.



12
13
14
15
16
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 12

def initialize
  @dimensions = Constants::DIMENSIONS.keys.to_h { |d| [d, 0.5] }
  @growth_bonus = 0.0
  @history = []
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



10
11
12
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 10

def dimensions
  @dimensions
end

#growth_bonusObject (readonly)

Returns the value of attribute growth_bonus.



10
11
12
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 10

def growth_bonus
  @growth_bonus
end

#historyObject (readonly)

Returns the value of attribute history.



10
11
12
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 10

def history
  @history
end

Instance Method Details

#classificationObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 35

def classification
  score = composite_score
  if score >= Constants::ANTIFRAGILITY_THRESHOLD
    :antifragile
  elsif score >= 0.5
    :resilient
  elsif score >= Constants::FRAGILITY_THRESHOLD
    :fragile
  else
    :brittle
  end
end

#composite_scoreObject



27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 27

def composite_score
  total = 0.0
  Constants::DIMENSIONS.each do |dim, config|
    total += (@dimensions[dim] + (dim == :growth ? @growth_bonus : 0.0)) * config[:weight]
  end
  total.clamp(0.0, 1.0)
end

#dimension_detail(name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 48

def dimension_detail(name)
  return nil unless @dimensions.key?(name)

  {
    name:    name,
    value:   @dimensions[name].round(4),
    config:  Constants::DIMENSIONS[name],
    trend:   dimension_trend(name),
    healthy: @dimensions[name] >= 0.5
  }
end

#to_hObject



78
79
80
81
82
83
84
85
86
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 78

def to_h
  {
    dimensions:   @dimensions.transform_values { |v| v.round(4) },
    growth_bonus: @growth_bonus.round(4),
    composite:    composite_score.round(4),
    class:        classification,
    trend:        trend
  }
end

#trendObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 60

def trend
  return :insufficient_data if @history.size < 5

  recent = @history.last(10)
  scores = recent.map { |h| h[:composite] }
  first_half = scores[0...(scores.size / 2)]
  second_half = scores[(scores.size / 2)..]
  diff = mean(second_half) - mean(first_half)

  if diff > 0.03
    :strengthening
  elsif diff < -0.03
    :weakening
  else
    :stable
  end
end

#update_from_tracker(tracker) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/affect/resilience/helpers/resilience_model.rb', line 18

def update_from_tracker(tracker)
  update_elasticity(tracker)
  update_robustness(tracker)
  update_adaptability(tracker)
  update_growth(tracker)

  record_snapshot
end