Skip to content

Commit

Permalink
Move the seterr statement to solve and simulate and use error states …
Browse files Browse the repository at this point in the history
…instead, as these will automatically reset upon exit.
  • Loading branch information
pkofod committed Mar 19, 2019
1 parent 29d1202 commit 960b633
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions HARK/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
from time import clock
from .parallel import multiThreadCommands, multiThreadCommandsFake

# Ignore floating point "errors". Numpy calls it "errors", but really it's excep-
# tions with well-defined answers such as 1.0/0.0 that is np.inf, -1.0/0.0 that is
# -np.inf, np.inf/np.inf is np.nan and so on.
np.seterr(all='ignore')

def distanceMetric(thing_A,thing_B):
'''
A "universal distance" metric that can be used as a default in many settings.
Expand Down Expand Up @@ -377,12 +372,16 @@ def solve(self,verbose=False):
none
'''

self.preSolve() # Do pre-solution stuff
self.solution = solveAgent(self,verbose) # Solve the model by backward induction
if self.time_flow: # Put the solution in chronological order if this instance's time flow runs that way
self.solution.reverse()
self.addToTimeVary('solution') # Add solution to the list of time-varying attributes
self.postSolve() # Do post-solution stuff
# Ignore floating point "errors". Numpy calls it "errors", but really it's excep-
# tions with well-defined answers such as 1.0/0.0 that is np.inf, -1.0/0.0 that is
# -np.inf, np.inf/np.inf is np.nan and so on.
with np.errstate(divide='ignore', over='ignore', under='ignore', invalid='ignore'):
self.preSolve() # Do pre-solution stuff
self.solution = solveAgent(self,verbose) # Solve the model by backward induction
if self.time_flow: # Put the solution in chronological order if this instance's time flow runs that way
self.solution.reverse()
self.addToTimeVary('solution') # Add solution to the list of time-varying attributes
self.postSolve() # Do post-solution stuff

def resetRNG(self):
'''
Expand Down Expand Up @@ -685,19 +684,23 @@ def simulate(self,sim_periods=None):
-------
None
'''
orig_time = self.time_flow
self.timeFwd()
if sim_periods is None:
sim_periods = self.T_sim

for t in range(sim_periods):
self.simOnePeriod()
for var_name in self.track_vars:
exec('self.' + var_name + '_hist[self.t_sim,:] = self.' + var_name)
self.t_sim += 1

if not orig_time:
self.timeRev()
# Ignore floating point "errors". Numpy calls it "errors", but really it's excep-
# tions with well-defined answers such as 1.0/0.0 that is np.inf, -1.0/0.0 that is
# -np.inf, np.inf/np.inf is np.nan and so on.
with np.errstate(divide='ignore', over='ignore', under='ignore', invalid='ignore'):
orig_time = self.time_flow
self.timeFwd()
if sim_periods is None:
sim_periods = self.T_sim

for t in range(sim_periods):
self.simOnePeriod()
for var_name in self.track_vars:
exec('self.' + var_name + '_hist[self.t_sim,:] = self.' + var_name)
self.t_sim += 1

if not orig_time:
self.timeRev()

def clearHistory(self):
'''
Expand Down

0 comments on commit 960b633

Please sign in to comment.