Class: Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/solver.rb

Constant Summary collapse

PRECISION =
3
INTERVAL =
10**(-1 * PRECISION)
MAX_ITERATIONS =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_value:, lower_bound: 0.00, upper_bound: nil, guess: 10.00) ⇒ Solver

Returns a new instance of Solver.



9
10
11
12
13
14
15
16
17
# File 'lib/solver.rb', line 9

def initialize(time_value:, lower_bound: 0.00, upper_bound: nil, guess: 10.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
  @iteration_count = 0
end

Instance Attribute Details

#goalObject (readonly)

Returns the value of attribute goal.



6
7
8
# File 'lib/solver.rb', line 6

def goal
  @goal
end

#iteration_countObject

Returns the value of attribute iteration_count.



7
8
9
# File 'lib/solver.rb', line 7

def iteration_count
  @iteration_count
end

#lower_boundObject

Returns the value of attribute lower_bound.



7
8
9
# File 'lib/solver.rb', line 7

def lower_bound
  @lower_bound
end

#time_valueObject (readonly)

Returns the value of attribute time_value.



6
7
8
# File 'lib/solver.rb', line 6

def time_value
  @time_value
end

#upper_boundObject

Returns the value of attribute upper_bound.



7
8
9
# File 'lib/solver.rb', line 7

def upper_bound
  @upper_bound
end

#upper_cap_metObject

Returns the value of attribute upper_cap_met.



7
8
9
# File 'lib/solver.rb', line 7

def upper_cap_met
  @upper_cap_met
end

Instance Method Details

#solve!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/solver.rb', line 19

def solve!
  while !within_range? && iteration_count < MAX_ITERATIONS
    result = time_value.calc_fv
    adjust_bounds!(result: result)
    time_value.i = new_guess
    self.iteration_count += 1
  end
  # TODO: This will not handle the case where the 20th iteration
  # finds the solution
  return rounded_rate if iteration_count < MAX_ITERATIONS
rescue FloatDomainError
  return nil
end