HARK.parallel

Early version of multithreading in HARK. To use most of this module, you should first install dill and joblib. Packages can be installed by typing “conda install dill” (etc) at a command prompt.

Functions

loadNelderMeadData(name) Reads the progress of a parallel Nelder-Mead search from a text file, as created by saveNelderMeadData().
main()
multiThreadCommands(agent_list, command_list) Executes the list of commands in command_list for each AgentType in agent_list using a multithreaded system.
multiThreadCommandsFake(agent_list, command_list) Executes the list of commands in command_list for each AgentType in agent_list in an ordinary, single-threaded loop.
parallelNelderMead(objFunc, guess[, …]) A parallel implementation of the Nelder-Mead minimization algorithm, as described in Lee and Wiswall.
parallelNelderMeadWorker(objFunc, simplex, …) A worker process for the parallel Nelder-Mead algorithm.
raiseImportError(moduleStr)
runCommands(agent, command_list) Executes each command in command_list on a given AgentType.
saveNelderMeadData(name, simplex, fvals, …) Stores the progress of a parallel Nelder-Mead search in a text file so that it can be resumed later (after manual termination or a crash).
HARK.parallel.loadNelderMeadData(name)

Reads the progress of a parallel Nelder-Mead search from a text file, as created by saveNelderMeadData().

Parameters:
name : string

Name of the txt file from which to read search progress.

Returns:
simplex : np.array

The current state of the simplex of parameter guesses.

fvals : np.array

The objective function value at each row of simplex.

iters : int

The number of completed Nelder-Mead iterations.

evals : int

The cumulative number of function evaluations in the search process.

HARK.parallel.multiThreadCommands(agent_list, command_list, num_jobs=None)

Executes the list of commands in command_list for each AgentType in agent_list using a multithreaded system. Each command should be a method of that AgentType subclass.

Parameters:
agent_list : [AgentType]

A list of instances of AgentType on which the commands will be run.

command_list : [string]

A list of commands to run for each AgentType in agent_list.

Returns:
None
HARK.parallel.multiThreadCommandsFake(agent_list, command_list, num_jobs=None)

Executes the list of commands in command_list for each AgentType in agent_list in an ordinary, single-threaded loop. Each command should be a method of that AgentType subclass. This function exists so as to easily disable multithreading, as it uses the same syntax as multithreadCommands.

Parameters:
agent_list : [AgentType]

A list of instances of AgentType on which the commands will be run.

command_list : [string]

A list of commands to run for each AgentType.

num_jobs : None

Dummy input to match syntax of multiThreadCommands. Does nothing.

Returns:
none
HARK.parallel.parallelNelderMead(objFunc, guess, perturb=None, P=1, ftol=1e-06, xtol=1e-08, maxiter=inf, maxeval=inf, r_param=1.0, e_param=1.0, c_param=0.5, s_param=0.5, maxcores=None, name=None, resume=False, savefreq=None, verbose=1)

A parallel implementation of the Nelder-Mead minimization algorithm, as described in Lee and Wiswall. For long optimization procedures, it can save progress between iterations and resume later.

Parameters:
objFunc : function

The objective function to be minimized. Takes a single 1D array as input.

guess : np.array

Initial starting point for the simplex, representing an input for objFunc.

perturb : np.array

Perturbation vector for the simplex, of the same length as an input to objFunc. If perturb[j] is non-zero, a simplex point will be created that perturbs the j-th element of guess by perturb[j]; if it is zero, then the j-th parameter of objFunc will not be optimized over. By default, guess=None, indicating that all parameters should be optimized, with an initial perturbation of 0.1*guess.

P : int

Degree of parallelization: the number of vertices of the simplex to try to update on each iteration of the process.

ftol : float

Absolute tolerance of the objective function for convergence. If suc- cessive iterations return minimum function values that differ by less than ftol, the process terminates successfully.

xtol : float

Absolute tolerance of the input values for convergence. If the maximum distance between the current minimum point and the worst point in the simplex is less than xtol, then the process terminates successfully.

maxiter : int

Maximum number of Nelder-Mead iterations; reaching iters=maxiter is reported as an “unsuccessful” minimization.

maxeval : int

Maximum number of evaluations of objFunc (across all processes); reaching evals=maxeval is reported as an “unsuccessful” minimization.

r_param: float

Parameter indicating magnitude of the reflection point calculation.

e_param: float

Parameter indicating magnitude of the expansion point calculation.

c_param: float

Parameter indicating magnitude of the contraction point calculation.

s_param: float

Parameter indicating magnitude of the shrink calculation.

maxcores : int

The maximum number of CPU cores that the optimization should use, regardless of the size of the problem.

name : string

A filename for (optionally) saving the progress of the Nelder-Mead search, and for resuming a previous search (when resume=True). Useful for long searches that could potentially be interrupted by computer down time.

resume : boolean

An indicator for whether the search should resume from earlier progress. When True, the process will load a progress file named in input name.

savefreq : int

When not None, search progress will be saved to name.txt every savefreq iterations, to be loaded later with resume=True).

verbose : int

Indicator for the verbosity of the optimization routine. Higher values generate more text output; verbose=0 produces no text output.

Returns:
min_point : np.array

The input that minimizes objFunc, as found by the minimization.

fmin : float

The minimum of objFunc; fmin = objFunc(min_point).

HARK.parallel.parallelNelderMeadWorker(objFunc, simplex, f_vals, j, P, opt_params)

A worker process for the parallel Nelder-Mead algorithm. Updates one point in the simplex, returning its function value as well. Should basically never be called directly, only by parallelNelderMead().

Parameters:
objFunc : function

The function to be minimized; takes a single 1D array as input.

simplex : numpy.array

The current simplex for minimization; simplex[k,:] is an input for objFunc.

f_vals : numpy.array

The values of the objective function at each point of the simplex: f_vals[k] = objFunc(simplex[k,:])

j : int

Index of the point in the simplex to update: simplex[j,:]

P : int

Degree of parallelization of the algorithm.

opt_params : numpy.array

Three element array with parameters for reflection, contraction, expansion.

Returns:
new_point : numpy.array

An updated point for the simplex; might be the same as simplex[j,:].

new_val : float

The value of the objective function at the new point: objFunc(new_point).

evals : int

Number of evaluations of objFunc by this worker.

HARK.parallel.runCommands(agent, command_list)

Executes each command in command_list on a given AgentType. The commands should be methods of that AgentType’s subclass.

Parameters:
agent : AgentType

An instance of AgentType on which the commands will be run.

command_list : [string]

A list of commands that the agent should run, as methods.

Returns:
agent : AgentType

The same AgentType instance passed as input, after running the commands.

HARK.parallel.saveNelderMeadData(name, simplex, fvals, iters, evals)

Stores the progress of a parallel Nelder-Mead search in a text file so that it can be resumed later (after manual termination or a crash).

Parameters:
name : string

Name of the txt file in which to store search progress.

simplex : np.array

The current state of the simplex of parameter guesses.

fvals : np.array

The objective function value at each row of simplex.

iters : int

The number of completed Nelder-Mead iterations.

evals : int

The cumulative number of function evaluations in the search process.

Returns:
none