controllers¶
Module of all the controllers used in M-LOOP. The controllers, as the name suggests, control the interface to the experiment and all the learners employed to find optimal parameters.
-
class
mloop.controllers.
Controller
(interface, max_num_runs=inf, target_cost=-inf, max_num_runs_without_better_params=inf, controller_archive_filename='controller_archive', controller_archive_file_type='txt', archive_extra_dict=None, start_datetime=None, **kwargs)¶ Bases:
object
Abstract class for controllers. The controller controls the entire M-LOOP process. The controller for each algorithm all inherit from this class. The class stores a variety of data which all algorithms use and also all of the achiving and saving features. In order to implement your own controller class the minimum requirement is to add a learner to the learner variable. And implement the next_parameters method, where you provide the appropriate information to the learner and get the next parameters. See the RandomController for a simple implementation of a controller. Note the first three keywords are all possible halting conditions for the controller. If any of them are satisfied the controller will halt (meaning an and condition is used). Also creates an empty variable learner. The simplest way to make a working controller is to assign a learner of some kind to this variable, and add appropriate queues and events from it. :param interface: The interface process. Is run by learner. :type interface: interface
Keyword Arguments: - max_num_runs (Optional [float]) – The number of runs before the controller stops. If set to float(‘+inf’) the controller will run forever. Default float(‘inf’), meaning the controller will run until another condition is met.
- target_cost (Optional [float]) – The target cost for the run. If a run achieves a cost lower than the target, the controller is stopped. Default float(‘-inf’), meaning the controller will run until another condition is met.
- max_num_runs_without_better_params (Otional [float]) – Puts a limit on the number of runs are allowed before a new better set of parameters is found. Default float(‘inf’), meaning the controller will run until another condition is met.
- controller_archive_filename (Optional [string]) – Filename for archive. Contains costs, parameter history and other details depending on the controller type. Default ‘ControllerArchive.mat’
- controller_archive_file_type (Optional [string]) – File type for archive. Can be either ‘txt’ a human readable text file, ‘pkl’ a python dill file, ‘mat’ a matlab file or None if there is no archive. Default ‘mat’.
- archive_extra_dict (Optional [dict]) – A dictionary with any extra variables that are to be saved to the archive. If None, nothing is added. Default None.
- start_datetime (Optional datetime) – Datetime for when controller was started.
-
params_out_queue
¶ Queue for parameters to next be run by experiment.
Type: queue
-
costs_in_queue
¶ Queue for costs (and other details) that have been returned by experiment.
Type: queue
-
end_interface
¶ Event used to trigger the end of the interface
Type: event
-
learner
¶ The placeholder for the learner, creating this variable is the minimum requirement to make a working controller class.
Type: None
-
learner_params_queue
¶ The parameters queue for the learner
Type: queue
-
learner_costs_queue
¶ The costs queue for the learner
Type: queue
-
end_learner
¶ Event used to trigger the end of the learner
Type: event
-
num_in_costs
¶ Counter for the number of costs received.
Type: int
-
num_out_params
¶ Counter for the number of parameters received.
Type: int
-
out_params
¶ List of all parameters sent out by controller.
Type: list
-
out_extras
¶ Any extras associated with the output parameters.
Type: list
-
in_costs
¶ List of costs received by controller.
Type: list
-
in_uncers
¶ List of uncertainties receieved by controller.
Type: list
-
best_cost
¶ The lowest, and best, cost received by the learner.
Type: float
-
best_uncer
¶ The uncertainty associated with the best cost.
Type: float
-
best_params
¶ The best parameters recieved by the learner.
Type: array
-
best_index
¶ The run number that produced the best cost.
Type: float
-
_first_params
()¶ Checks queue to get first parameters. :returns: Parameters for first experiment
-
_get_cost_and_in_dict
()¶ Get cost, uncertainty, parameters, bad and extra data from experiment. Stores in a list of history and also puts variables in their appropriate ‘current’ variables Note returns nothing, stores everything in the internal storage arrays and the curr_variables
-
_next_params
()¶ Abstract method. When implemented should send appropriate information to learner and get next parameters. :returns: Parameters for next experiment.
-
_optimization_routine
()¶ Runs controller main loop. Gives parameters to experiment and saves costs returned.
-
_put_params_and_out_dict
(params, param_type=None, **kwargs)¶ Send parameters to queue and whatever additional keywords. Saves sent variables in appropriate storage arrays. :param params: array of values to be sent to file :type params: array
Keyword Arguments: **kwargs – any additional data to be attached to file sent to experiment
-
_shut_down
()¶ Shutdown and clean up resources of the controller. end the learners, queue_listener and make one last save of archive.
-
_start_up
()¶ Start the learner and interface threads/processes.
-
_update_controller_with_learner_attributes
()¶ Update the controller with properties from the learner.
-
check_end_conditions
()¶ Check whether either of the three end contions have been met: number_of_runs, target_cost or max_num_runs_without_better_params. :returns: True, if the controlled should continue, False if the controller should end. :rtype: bool
-
optimize
()¶ Optimize the experiment. This code learner and interface processes/threads are launched and appropriately ended. Starts both threads and catches kill signals and shuts down appropriately.
-
print_results
()¶ Print results from optimization run to the logs
-
save_archive
()¶ Save the archive associated with the controller class. Only occurs if the filename for the archive is not None. Saves with the format previously set.
-
exception
mloop.controllers.
ControllerInterrupt
¶ Bases:
Exception
Exception that is raised when the controlled is ended with the end flag or event.
-
class
mloop.controllers.
DifferentialEvolutionController
(interface, **kwargs)¶ Bases:
mloop.controllers.Controller
Controller for the differential evolution learner. :param params_out_queue: Queue for parameters to next be run by experiment. :type params_out_queue: queue :param costs_in_queue: Queue for costs (and other details) that have been returned by experiment. :type costs_in_queue: queue :param **kwargs: Dictionary of options to be passed to Controller parent class and differential evolution learner. :type **kwargs: Optional [dict]
-
_next_params
()¶ Gets next parameters from differential evolution learner.
-
-
class
mloop.controllers.
GaussianProcessController
(interface, num_params=None, min_boundary=None, max_boundary=None, trust_region=None, learner_archive_filename='learner_archive', learner_archive_file_type='txt', **kwargs)¶ Bases:
mloop.controllers.MachineLearnerController
Controller for the Gaussian Process solver. Primarily suggests new points from the Gaussian Process learner. However, during the initial few runs it must rely on a different optimization algorithm to get some points to seed the learner. :param interface: The interface to the experiment under optimization. :type interface: Interface :param **kwargs: Dictionary of options to be passed to MachineLearnerController parent class and Gaussian Process learner. :type **kwargs: Optional [dict]
Keyword Args:
-
class
mloop.controllers.
MachineLearnerController
(interface, training_type='differential_evolution', machine_learner_type='machine_learner', num_training_runs=None, no_delay=True, num_params=None, min_boundary=None, max_boundary=None, trust_region=None, learner_archive_filename='learner_archive', learner_archive_file_type='txt', **kwargs)¶ Bases:
mloop.controllers.Controller
Abstract Controller class for the machine learning based solvers. :param interface: The interface to the experiment under optimization. :type interface: Interface :param **kwargs: Dictionary of options to be passed to Controller parent class and initial training learner. :type **kwargs: Optional [dict]
Keyword Arguments: - training_type (Optional [string]) – The type for the initial training source can be ‘random’ for the random learner, ‘nelder_mead’ for the Nelder-Mead learner or ‘differential_evolution’ for the Differential Evolution learner. This learner is also called if the machine learning learner is too slow and a new point is needed. Default ‘differential_evolution’.
- num_training_runs (Optional [int]) – The number of training runs to before starting the learner. If None, will be ten or double the number of parameters, whatever is larger.
- no_delay (Optional [bool]) – If True, there is never any delay between a returned cost and the next parameters to run for the experiment. In practice, this means if the machine learning learner has not prepared the next parameters in time the learner defined by the initial training source is used instead. If false, the controller will wait for the machine learning learner to predict the next parameters and there may be a delay between runs.
-
_get_cost_and_in_dict
()¶ Call _get_cost_and_in_dict() of parent Controller class. But also sends cost to machine learning learner and saves the cost if the parameters came from a trainer.
-
_next_params
()¶ Gets next parameters from training learner.
-
_optimization_routine
()¶ Overrides _optimization_routine. Uses the parent routine for the training runs. Implements a customized _optimization_routine when running the machine learning learner.
-
_put_params_and_out_dict
(params)¶ Override _put_params_and_out_dict function, used when the training learner creates parameters. Makes the defualt param_type the training type and sets last_training_run_flag.
-
_shut_down
()¶ Shutdown and clean up resources of the machine learning controller.
-
_start_up
()¶ Runs pararent method and also starts training_learner.
-
print_results
()¶ Adds some additional output to the results specific to controller.
-
class
mloop.controllers.
NelderMeadController
(interface, **kwargs)¶ Bases:
mloop.controllers.Controller
Controller for the Nelder-Mead solver. Suggests new parameters based on the Nelder-Mead algorithm. Can take no boundaries or hard boundaries. More details for the Nelder-Mead options are in the learners section. :param params_out_queue: Queue for parameters to next be run by experiment. :type params_out_queue: queue :param costs_in_queue: Queue for costs (and other details) that have been returned by experiment. :type costs_in_queue: queue :param **kwargs: Dictionary of options to be passed to Controller parent class and Nelder-Mead learner. :type **kwargs: Optional [dict]
-
_next_params
()¶ Gets next parameters from Nelder-Mead learner.
-
-
class
mloop.controllers.
NeuralNetController
(interface, num_params=None, min_boundary=None, max_boundary=None, trust_region=None, learner_archive_filename='learner_archive', learner_archive_file_type='txt', **kwargs)¶ Bases:
mloop.controllers.MachineLearnerController
Controller for the Neural Net solver. Primarily suggests new points from the Neural Net learner. However, during the initial few runs it must rely on a different optimization algorithm to get some points to seed the learner. :param interface: The interface to the experiment under optimization. :type interface: Interface :param **kwargs: Dictionary of options to be passed to MachineLearnerController parent class and Neural Net learner. :type **kwargs: Optional [dict]
Keyword Args:
-
class
mloop.controllers.
RandomController
(interface, **kwargs)¶ Bases:
mloop.controllers.Controller
Controller that simply returns random variables for the next parameters. Costs are stored but do not influence future points picked. :param params_out_queue: Queue for parameters to next be run by experiment. :type params_out_queue: queue :param costs_in_queue: Queue for costs (and other details) that have been returned by experiment. :type costs_in_queue: queue :param **kwargs: Dictionary of options to be passed to Controller and Random Learner. :type **kwargs: Optional [dict]
-
_next_params
()¶ Sends cost uncer and bad tuple to learner then gets next parameters. :returns: Parameters for next experiment.
-
-
mloop.controllers.
create_controller
(interface, controller_type='gaussian_process', **controller_config_dict)¶ Start the controller with the options provided. :param interface: Interface with queues and events to be passed to controller :type interface: interface
Keyword Arguments: - controller_type (Optional [str]) – Defines the type of controller can be ‘random’, ‘nelder’, ‘gaussian_process’ or ‘neural_net’. Defaults to ‘gaussian_process’.
- **controller_config_dict – Options to be passed to controller.
Returns: threadible object which must be started with start() to get the controller running.
Return type: Raises: ValueError
– if controller_type is an unrecognized string