Class: Legion::Extensions::Agentic::Homeostasis::Metabolism::Helpers::EnergyReserve
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Homeostasis::Metabolism::Helpers::EnergyReserve
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb
Constant Summary
Constants included from Constants
Constants::EFFICIENCY_DECAY, Constants::MAX_ENERGY, Constants::METABOLIC_STATES, Constants::OPERATION_COSTS, Constants::RECOVERY_RATE, Constants::RESTING_METABOLIC_RATE, Constants::STATE_THRESHOLDS
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#current_energy ⇒ Object
readonly
Returns the value of attribute current_energy.
-
#efficiency ⇒ Object
readonly
Returns the value of attribute efficiency.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#max_energy ⇒ Object
readonly
Returns the value of attribute max_energy.
-
#metabolic_rate ⇒ Object
readonly
Returns the value of attribute metabolic_rate.
Instance Method Summary collapse
- #anabolize!(energy_cost: 5.0) ⇒ Object
- #catabolize!(complexity: 1.0) ⇒ Object
- #depleted? ⇒ Boolean
- #energy_ratio ⇒ Object
-
#initialize(max_energy: MAX_ENERGY, metabolic_rate: RESTING_METABOLIC_RATE, efficiency: 1.0) ⇒ EnergyReserve
constructor
A new instance of EnergyReserve.
- #recover!(amount) ⇒ Object
- #spend!(amount) ⇒ Object
- #state ⇒ Object
- #to_h ⇒ Object
Methods included from Constants
Constructor Details
#initialize(max_energy: MAX_ENERGY, metabolic_rate: RESTING_METABOLIC_RATE, efficiency: 1.0) ⇒ EnergyReserve
Returns a new instance of EnergyReserve.
16 17 18 19 20 21 22 23 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 16 def initialize(max_energy: MAX_ENERGY, metabolic_rate: RESTING_METABOLIC_RATE, efficiency: 1.0) @id = SecureRandom.uuid @max_energy = max_energy.clamp(1.0, Float::INFINITY) @current_energy = @max_energy @metabolic_rate = .clamp(0.0, Float::INFINITY) @efficiency = efficiency.clamp(0.0, 1.0) @created_at = Time.now.utc end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
14 15 16 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 14 def created_at @created_at end |
#current_energy ⇒ Object (readonly)
Returns the value of attribute current_energy.
14 15 16 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 14 def current_energy @current_energy end |
#efficiency ⇒ Object (readonly)
Returns the value of attribute efficiency.
14 15 16 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 14 def efficiency @efficiency end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 14 def id @id end |
#max_energy ⇒ Object (readonly)
Returns the value of attribute max_energy.
14 15 16 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 14 def max_energy @max_energy end |
#metabolic_rate ⇒ Object (readonly)
Returns the value of attribute metabolic_rate.
14 15 16 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 14 def @metabolic_rate end |
Instance Method Details
#anabolize!(energy_cost: 5.0) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 58 def anabolize!(energy_cost: 5.0) raise ArgumentError, 'insufficient energy for anabolism' if @current_energy < energy_cost @current_energy = (@current_energy - energy_cost).clamp(0.0, @max_energy) structure_value = (energy_cost * @efficiency).round(10) { energy_spent: energy_cost, structure_value: structure_value, current_energy: @current_energy.round(10), state: state } end |
#catabolize!(complexity: 1.0) ⇒ Object
52 53 54 55 56 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 52 def catabolize!(complexity: 1.0) energy_gained = (complexity * 10.0).round(10) @current_energy = (@current_energy + energy_gained).clamp(0.0, @max_energy) { energy_gained: energy_gained, current_energy: @current_energy.round(10), state: state } end |
#depleted? ⇒ Boolean
33 34 35 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 33 def depleted? state == :depleted end |
#energy_ratio ⇒ Object
25 26 27 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 25 def energy_ratio (@current_energy / @max_energy).clamp(0.0, 1.0) end |
#recover!(amount) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 45 def recover!(amount) before = @current_energy @current_energy = (@current_energy + amount).clamp(0.0, @max_energy) @efficiency = (@efficiency + (EFFICIENCY_DECAY * 0.5)).clamp(0.0, 1.0) @current_energy - before end |
#spend!(amount) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 37 def spend!(amount) effective_cost = amount / @efficiency.clamp(0.01, 1.0) before = @current_energy @current_energy = (@current_energy - effective_cost).clamp(0.0, @max_energy) @efficiency = (@efficiency - EFFICIENCY_DECAY).clamp(0.0, 1.0) before - @current_energy end |
#state ⇒ Object
29 30 31 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 29 def state Constants.label_for(energy_ratio) end |
#to_h ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/legion/extensions/agentic/homeostasis/metabolism/helpers/energy_reserve.rb', line 66 def to_h { id: @id, current_energy: @current_energy.round(10), max_energy: @max_energy, metabolic_rate: @metabolic_rate, efficiency: @efficiency.round(10), energy_ratio: energy_ratio.round(10), state: state, depleted: depleted?, created_at: @created_at } end |