Class: Solver
- Inherits:
-
Object
- Object
- Solver
- Defined in:
- lib/solver.rb
Constant Summary collapse
- PRECISION =
3- INTERVAL =
10 ** (-1 * PRECISION)
- MAX_ITERATIONS =
20
Instance Attribute Summary collapse
-
#goal ⇒ Object
readonly
Returns the value of attribute goal.
-
#lower_bound ⇒ Object
Returns the value of attribute lower_bound.
-
#time_value ⇒ Object
readonly
Returns the value of attribute time_value.
-
#upper_bound ⇒ Object
Returns the value of attribute upper_bound.
-
#upper_cap_met ⇒ Object
Returns the value of attribute upper_cap_met.
Instance Method Summary collapse
-
#initialize(time_value:, lower_bound: 0.00, upper_bound: nil, guess: 0.00) ⇒ Solver
constructor
A new instance of Solver.
- #solve! ⇒ Object
Constructor Details
#initialize(time_value:, lower_bound: 0.00, upper_bound: nil, guess: 0.00) ⇒ Solver
Returns a new instance of Solver.
8 9 10 11 12 13 14 15 |
# File 'lib/solver.rb', line 8 def initialize(time_value:, lower_bound: 0.00, upper_bound: nil, guess: 0.00) @upper_bound = upper_bound || guess @lower_bound = lower_bound @time_value = time_value.dup @time_value.i = guess @goal = time_value.fv @upper_cap_met = false end |
Instance Attribute Details
#goal ⇒ Object (readonly)
Returns the value of attribute goal.
5 6 7 |
# File 'lib/solver.rb', line 5 def goal @goal end |
#lower_bound ⇒ Object
Returns the value of attribute lower_bound.
6 7 8 |
# File 'lib/solver.rb', line 6 def lower_bound @lower_bound end |
#time_value ⇒ Object (readonly)
Returns the value of attribute time_value.
5 6 7 |
# File 'lib/solver.rb', line 5 def time_value @time_value end |
#upper_bound ⇒ Object
Returns the value of attribute upper_bound.
6 7 8 |
# File 'lib/solver.rb', line 6 def upper_bound @upper_bound end |
#upper_cap_met ⇒ Object
Returns the value of attribute upper_cap_met.
6 7 8 |
# File 'lib/solver.rb', line 6 def upper_cap_met @upper_cap_met end |
Instance Method Details
#solve! ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/solver.rb', line 17 def solve! iteration_count = 0 while (upper_bound - lower_bound).round(PRECISION) > INTERVAL && iteration_count < MAX_ITERATIONS iteration_count += 1 begin result = time_value.calc_fv rescue FloatDomainError return nil end adjust_bounds!(result) time_value.i = new_guess end # TODO: This will not handle the case where the 20th iteration # finds the solution rate if iteration_count < MAX_ITERATIONS end |