Author: bugman Date: Fri Jul 18 15:51:10 2014 New Revision: 24570 URL: http://svn.gna.org/viewcvs/relax?rev=24570&view=rev Log: Modified the analysis specific API optimisation method. The base calculate(), grid_search() and minimise() methods now all accept the scaling_matrix argument, and the minimise() scaling argument has been removed. This scaling_matrix argument should be a per-model list of scaling matrices. To handle the change, the pipe_control.minimise.assemble_scaling_matrix() function has been created. This uses the new parameter object scaling values to create the list of scaling matrices. This will in the end replace all of the analysis specific assemble_scaling_matrix() functions and simplify their optimisation code paths. Modified: branches/zooming_grid_search/pipe_control/minimise.py branches/zooming_grid_search/specific_analyses/api_base.py Modified: branches/zooming_grid_search/pipe_control/minimise.py URL: http://svn.gna.org/viewcvs/relax/branches/zooming_grid_search/pipe_control/minimise.py?rev=24570&r1=24569&r2=24570&view=diff ============================================================================== --- branches/zooming_grid_search/pipe_control/minimise.py (original) +++ branches/zooming_grid_search/pipe_control/minimise.py Fri Jul 18 15:51:10 2014 @@ -22,6 +22,9 @@ # Module docstring. """Module for model minimisation/optimisation.""" +# Python module imports. +from numpy import float64, identity + # relax module imports. from lib.errors import RelaxError, RelaxIntListIntError, RelaxLenError from multi import Processor_box @@ -32,6 +35,47 @@ from user_functions.data import Uf_tables; uf_tables = Uf_tables() +def assemble_scaling_matrix(scaling=True): + """Create and return the per-model scaling matrices. + + @keyword scaling: If True, diagonal scaling is enabled during optimisation to allow the problem to be better conditioned. + @type scaling: bool + @return: The list of diagonal and square scaling matrices. + @rtype: list of numpy rank-2, float64 array or list of None + """ + + # The specific analysis API object and parameter object. + api = return_api() + param_object = return_parameter_object() + + # Initialise. + scaling_matrix = [] + + # Loop over the models. + for model_info in api.model_loop(): + # No diagonal scaling. + if not scaling: + scaling_matrix.append(None) + continue + + # Get the parameter names. + names = api.get_param_names(model_info) + + # The parameter number. + n = len(names) + + # Initialise. + scaling_matrix.append(identity(n, float64)) + i = 0 + + # Update the diagonal with the parameter specific scaling factor. + for i in range(n): + scaling_matrix[-1][i, i] = param_object.scaling(names[i], model_info=model_info) + + # Return the matrix. + return scaling_matrix + + def calc(verbosity=1): """Function for calculating the function value. @@ -44,6 +88,9 @@ # The specific analysis API object. api = return_api() + + # Create the scaling matrix. + scaling_matrix = assemble_scaling_matrix() # Deselect spins lacking data: api.overfit_deselect() @@ -63,7 +110,7 @@ status.mc_number = i # Calculation. - api.calculate(verbosity=verbosity-1, sim_index=i) + api.calculate(verbosity=verbosity-1, sim_index=i, scaling_matrix=scaling_matrix) # Print out. if verbosity and not processor.is_queued(): @@ -77,7 +124,7 @@ # Minimisation. else: - api.calculate(verbosity=verbosity) + api.calculate(verbosity=verbosity, scaling_matrix=scaling_matrix) # Execute any queued commands. processor.run_queue() @@ -107,6 +154,9 @@ # Determine the model specific grid bounds, and allow for the zooming grid search, and check the inc argument. model_lower, model_upper, model_inc = grid_setup(lower, upper, inc) + # Create the scaling matrix. + scaling_matrix = assemble_scaling_matrix() + # Deselect spins lacking data: api.overfit_deselect() @@ -125,7 +175,7 @@ status.mc_number = i # Optimisation. - api.grid_search(lower=model_lower, upper=model_upper, inc=model_inc, constraints=constraints, verbosity=verbosity-1, sim_index=i) + api.grid_search(lower=model_lower, upper=model_upper, inc=model_inc, scaling_matrix=scaling_matrix, constraints=constraints, verbosity=verbosity-1, sim_index=i) # Print out. if verbosity and not processor.is_queued(): @@ -139,7 +189,7 @@ # Grid search. else: - api.grid_search(lower=model_lower, upper=model_upper, inc=model_inc, constraints=constraints, verbosity=verbosity) + api.grid_search(lower=model_lower, upper=model_upper, inc=model_inc, scaling_matrix=scaling_matrix, constraints=constraints, verbosity=verbosity) # Execute any queued commands. processor.run_queue() @@ -310,6 +360,9 @@ min_options.append(hessian_type) min_options = tuple(min_options) + # Create the scaling matrix. + scaling_matrix = assemble_scaling_matrix(scaling) + # Deselect spins lacking data: api.overfit_deselect() @@ -319,7 +372,7 @@ # Single Monte Carlo simulation. if sim_index != None: - api.minimise(min_algor=min_algor, min_options=min_options, func_tol=func_tol, grad_tol=grad_tol, max_iterations=max_iter, constraints=constraints, scaling=scaling, verbosity=verbosity, sim_index=sim_index) + api.minimise(min_algor=min_algor, min_options=min_options, func_tol=func_tol, grad_tol=grad_tol, max_iterations=max_iter, constraints=constraints, scaling_matrix=scaling_matrix, verbosity=verbosity, sim_index=sim_index) # Monte Carlo simulation minimisation. elif hasattr(cdp, 'sim_state') and cdp.sim_state == 1: @@ -331,7 +384,7 @@ status.mc_number = i # Optimisation. - api.minimise(min_algor=min_algor, min_options=min_options, func_tol=func_tol, grad_tol=grad_tol, max_iterations=max_iter, constraints=constraints, scaling=scaling, verbosity=verbosity-1, sim_index=i) + api.minimise(min_algor=min_algor, min_options=min_options, func_tol=func_tol, grad_tol=grad_tol, max_iterations=max_iter, constraints=constraints, scaling_matrix=scaling_matrix, verbosity=verbosity-1, sim_index=i) # Print out. if verbosity and not processor.is_queued(): @@ -345,7 +398,7 @@ # Standard minimisation. else: - api.minimise(min_algor=min_algor, min_options=min_options, func_tol=func_tol, grad_tol=grad_tol, max_iterations=max_iter, constraints=constraints, scaling=scaling, verbosity=verbosity) + api.minimise(min_algor=min_algor, min_options=min_options, func_tol=func_tol, grad_tol=grad_tol, max_iterations=max_iter, constraints=constraints, scaling_matrix=scaling_matrix, verbosity=verbosity) # Execute any queued commands. processor.run_queue() Modified: branches/zooming_grid_search/specific_analyses/api_base.py URL: http://svn.gna.org/viewcvs/relax/branches/zooming_grid_search/specific_analyses/api_base.py?rev=24570&r1=24569&r2=24570&view=diff ============================================================================== --- branches/zooming_grid_search/specific_analyses/api_base.py (original) +++ branches/zooming_grid_search/specific_analyses/api_base.py Fri Jul 18 15:51:10 2014 @@ -104,15 +104,17 @@ raise RelaxImplementError('bmrb_write') - def calculate(self, spin_id=None, verbosity=1, sim_index=None): + def calculate(self, spin_id=None, scaling_matrix=None, verbosity=1, sim_index=None): """Calculate the chi-squared value. - @keyword spin_id: The spin identification string. - @type spin_id: None or str - @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. - @type verbosity: int - @keyword sim_index: The optional MC simulation index. - @type sim_index: None or int + @keyword spin_id: The spin ID string. + @type spin_id: None or str + @keyword scaling_matrix: The per-model list of diagonal and square scaling matrices. + @type scaling_matrix: list of numpy rank-2, float64 array or list of None + @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. + @type verbosity: int + @keyword sim_index: The optional MC simulation index. + @type sim_index: None or int """ # Not implemented. @@ -293,21 +295,23 @@ raise RelaxImplementError('get_param_values') - def grid_search(self, lower=None, upper=None, inc=None, constraints=True, verbosity=1, sim_index=None): + def grid_search(self, lower=None, upper=None, inc=None, scaling_matrix=None, constraints=True, verbosity=1, sim_index=None): """Grid search method. - @keyword lower: The lower bounds of the grid search which must be equal to the number of parameters in the model. - @type lower: array of numbers - @keyword upper: The upper bounds of the grid search which must be equal to the number of parameters in the model. - @type upper: array of numbers - @keyword inc: The increments for each dimension of the space for the grid search. The number of elements in the array must equal to the number of parameters in the model. - @type inc: array of int - @keyword constraints: If True, constraints are applied during the grid search (eliminating parts of the grid). If False, no constraints are used. - @type constraints: bool - @keyword verbosity: A flag specifying the amount of information to print. The higher the value, the greater the verbosity. - @type verbosity: int - @keyword sim_index: The index of the simulation to apply the grid search to. If None, the normal model is optimised. - @type sim_index: int + @keyword lower: The per-model lower bounds of the grid search which must be equal to the number of parameters in the model. + @type lower: list of lists of floats + @keyword upper: The per-model upper bounds of the grid search which must be equal to the number of parameters in the model. + @type upper: list of lists of floats + @keyword inc: The per-model increments for each dimension of the space for the grid search. The number of elements in the array must equal to the number of parameters in the model. + @type inc: list of lists of int + @keyword scaling_matrix: The per-model list of diagonal and square scaling matrices. + @type scaling_matrix: list of numpy rank-2, float64 array or list of None + @keyword constraints: If True, constraints are applied during the grid search (eliminating parts of the grid). If False, no constraints are used. + @type constraints: bool + @keyword verbosity: A flag specifying the amount of information to print. The higher the value, the greater the verbosity. + @type verbosity: int + @keyword sim_index: The index of the simulation to apply the grid search to. If None, the normal model is optimised. + @type sim_index: int """ # Not implemented. @@ -353,7 +357,7 @@ raise RelaxImplementError('map_bounds') - def minimise(self, min_algor=None, min_options=None, func_tol=None, grad_tol=None, max_iterations=None, constraints=False, scaling=True, verbosity=0, sim_index=None, lower=None, upper=None, inc=None): + def minimise(self, min_algor=None, min_options=None, func_tol=None, grad_tol=None, max_iterations=None, constraints=False, scaling_matrix=None, verbosity=0, sim_index=None, lower=None, upper=None, inc=None): """Minimisation method. @keyword min_algor: The minimisation algorithm to use. @@ -368,18 +372,18 @@ @type max_iterations: int @keyword constraints: If True, constraints are used during optimisation. @type constraints: bool - @keyword scaling: If True, diagonal scaling is enabled during optimisation to allow the problem to be better conditioned. - @type scaling: bool + @keyword scaling_matrix: The per-model list of diagonal and square scaling matrices. + @type scaling_matrix: list of numpy rank-2, float64 array or list of None @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. @type verbosity: int @keyword sim_index: The index of the simulation to optimise. This should be None if normal optimisation is desired. @type sim_index: None or int - @keyword lower: The lower bounds of the grid search which must be equal to the number of parameters in the model. This optional argument is only used when doing a grid search. - @type lower: array of numbers - @keyword upper: The upper bounds of the grid search which must be equal to the number of parameters in the model. This optional argument is only used when doing a grid search. - @type upper: array of numbers - @keyword inc: The increments for each dimension of the space for the grid search. The number of elements in the array must equal to the number of parameters in the model. This argument is only used when doing a grid search. - @type inc: array of int + @keyword lower: The per-model lower bounds of the grid search which must be equal to the number of parameters in the model. This optional argument is only used when doing a grid search. + @type lower: list of lists of float + @keyword upper: The per-model upper bounds of the grid search which must be equal to the number of parameters in the model. This optional argument is only used when doing a grid search. + @type upper: list of lists of float + @keyword inc: The per-model increments for each dimension of the space for the grid search. The number of elements in the array must equal to the number of parameters in the model. This argument is only used when doing a grid search. + @type inc: list of lists of int """ # Not implemented.