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: 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

#goalObject (readonly)

Returns the value of attribute goal.



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

def goal
  @goal
end

#lower_boundObject

Returns the value of attribute lower_bound.



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

def lower_bound
  @lower_bound
end

#time_valueObject (readonly)

Returns the value of attribute time_value.



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

def time_value
  @time_value
end

#upper_boundObject

Returns the value of attribute upper_bound.



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

def upper_bound
  @upper_bound
end

#upper_cap_metObject

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