Class: Legion::Extensions::Agentic::Learning::PreferenceLearning::Helpers::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, domain: :general) ⇒ Option

Returns a new instance of Option.



15
16
17
18
19
20
21
22
23
24
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 15

def initialize(label:, domain: :general)
  @id               = SecureRandom.uuid
  @label            = label
  @domain           = domain
  @preference_score = Constants::DEFAULT_PREFERENCE
  @wins             = 0
  @losses           = 0
  @times_seen       = 0
  @created_at       = Time.now.utc
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 12

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



12
13
14
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 12

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 12

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



12
13
14
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 12

def label
  @label
end

#lossesObject

Returns the value of attribute losses.



13
14
15
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 13

def losses
  @losses
end

#preference_scoreObject

Returns the value of attribute preference_score.



13
14
15
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 13

def preference_score
  @preference_score
end

#times_seenObject

Returns the value of attribute times_seen.



13
14
15
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 13

def times_seen
  @times_seen
end

#winsObject

Returns the value of attribute wins.



13
14
15
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 13

def wins
  @wins
end

Instance Method Details

#lose!Object



32
33
34
35
36
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 32

def lose!
  @losses       += 1
  @times_seen   += 1
  @preference_score = clamp(@preference_score - Constants::LOSS_PENALTY)
end

#preference_labelObject



45
46
47
48
49
50
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 45

def preference_label
  Constants::PREFERENCE_LABELS.each do |range, label|
    return label if range.cover?(@preference_score)
  end
  :neutral
end

#to_hObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 52

def to_h
  {
    id:               @id,
    label:            @label,
    domain:           @domain,
    preference_score: @preference_score,
    wins:             @wins,
    losses:           @losses,
    times_seen:       @times_seen,
    win_rate:         win_rate,
    preference_label: preference_label,
    created_at:       @created_at
  }
end

#win!Object



26
27
28
29
30
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 26

def win!
  @wins         += 1
  @times_seen   += 1
  @preference_score = clamp(@preference_score + Constants::WIN_BOOST)
end

#win_rateObject



38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/learning/preference_learning/helpers/option.rb', line 38

def win_rate
  total = @wins + @losses
  return 0.0 if total.zero?

  @wins.to_f / (total + 1)
end