Class: Legion::Extensions::Agentic::Homeostasis::TemporalDiscounting::Helpers::Reward

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, amount:, delay:, domain: :general, discount_rate: DEFAULT_DISCOUNT_RATE) ⇒ Reward

Returns a new instance of Reward.



14
15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 14

def initialize(label:, amount:, delay:, domain: :general, discount_rate: DEFAULT_DISCOUNT_RATE)
  @id            = SecureRandom.uuid
  @label         = label
  @amount        = amount.clamp(0.0, 1.0)
  @delay         = delay.to_f
  @domain        = domain
  @discount_rate = discount_rate.clamp(MIN_DISCOUNT_RATE, MAX_DISCOUNT_RATE)
  @created_at    = Time.now.utc
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 12

def amount
  @amount
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 12

def created_at
  @created_at
end

#delayObject (readonly)

Returns the value of attribute delay.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 12

def delay
  @delay
end

#discount_rateObject (readonly)

Returns the value of attribute discount_rate.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 12

def discount_rate
  @discount_rate
end

#domainObject (readonly)

Returns the value of attribute domain.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 12

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 12

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 12

def label
  @label
end

Instance Method Details

#adjust_delay!(new_delay:) ⇒ Object



54
55
56
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 54

def adjust_delay!(new_delay:)
  @delay = new_delay.to_f
end

#adjust_discount_rate!(new_rate:) ⇒ Object



58
59
60
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 58

def adjust_discount_rate!(new_rate:)
  @discount_rate = new_rate.clamp(MIN_DISCOUNT_RATE, MAX_DISCOUNT_RATE)
end

#impulsivity_labelObject



42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 42

def impulsivity_label
  k = @discount_rate
  IMPULSIVITY_LABELS.each do |range, label|
    return label if range.cover?(k)
  end
  :extreme
end

#subjective_valueObject



24
25
26
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 24

def subjective_value
  (@amount / (1.0 + (@discount_rate * @delay))).round(10)
end

#to_hObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 62

def to_h
  {
    id:                @id,
    label:             @label,
    amount:            @amount,
    delay:             @delay,
    domain:            @domain,
    discount_rate:     @discount_rate,
    subjective_value:  subjective_value,
    value_ratio:       value_ratio,
    value_label:       value_label,
    impulsivity_label: impulsivity_label,
    worth_waiting:     worth_waiting?,
    created_at:        @created_at
  }
end

#value_labelObject



34
35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 34

def value_label
  ratio = value_ratio
  VALUE_LABELS.each do |range, label|
    return label if range.cover?(ratio)
  end
  :negligible
end

#value_ratioObject



28
29
30
31
32
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 28

def value_ratio
  return 0.0 if @amount.zero?

  (subjective_value / @amount).round(10)
end

#worth_waiting?(threshold: 0.3) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/helpers/reward.rb', line 50

def worth_waiting?(threshold: 0.3)
  subjective_value >= threshold
end