Package pipe_control :: Module minimise
[hide private]
[frames] | no frames]

Source Code for Module pipe_control.minimise

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2014 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for model minimisation/optimisation.""" 
 24   
 25  # relax module imports. 
 26  from multi import Processor_box 
 27  from pipe_control.mol_res_spin import return_spin, spin_loop 
 28  from pipe_control import pipes 
 29  from specific_analyses.api import return_api 
 30  from status import Status; status = Status() 
 31  from user_functions.data import Uf_tables; uf_tables = Uf_tables() 
 32   
 33   
34 -def calc(verbosity=1):
35 """Function for calculating the function value. 36 37 @param verbosity: The amount of information to print. The higher the value, the greater 38 the verbosity. 39 @type verbosity: int 40 """ 41 42 # Test if the current data pipe exists. 43 pipes.test() 44 45 # The specific analysis API object. 46 api = return_api() 47 48 # Deselect spins lacking data: 49 api.overfit_deselect() 50 51 # Get the Processor box singleton (it contains the Processor instance) and alias the Processor. 52 processor_box = Processor_box() 53 processor = processor_box.processor 54 55 # Monte Carlo simulation calculation. 56 if hasattr(cdp, 'sim_state') and cdp.sim_state == 1: 57 # Loop over the simulations. 58 for i in range(cdp.sim_number): 59 # Status. 60 if status.current_analysis: 61 status.auto_analysis[status.current_analysis].mc_number = i 62 else: 63 status.mc_number = i 64 65 # Calculation. 66 api.calculate(verbosity=verbosity-1, sim_index=i) 67 68 # Print out. 69 if verbosity and not processor.is_queued(): 70 print("Simulation " + repr(i+1)) 71 72 # Unset the status. 73 if status.current_analysis: 74 status.auto_analysis[status.current_analysis].mc_number = None 75 else: 76 status.mc_number = None 77 78 # Minimisation. 79 else: 80 api.calculate(verbosity=verbosity) 81 82 # Execute any queued commands. 83 processor.run_queue()
84 85
86 -def grid_search(lower=None, upper=None, inc=None, constraints=True, verbosity=1):
87 """The grid search function. 88 89 @param lower: The lower bounds of the grid search which must be equal to the number of 90 parameters in the model. 91 @type lower: array of numbers 92 @param upper: The upper bounds of the grid search which must be equal to the number of 93 parameters in the model. 94 @type upper: array of numbers 95 @param inc: The increments for each dimension of the space for the grid search. The 96 number of elements in the array must equal to the number of parameters in 97 the model. 98 @type inc: array of int 99 @param constraints: If True, constraints are applied during the grid search (elinating parts of 100 the grid). If False, no constraints are used. 101 @type constraints: bool 102 @param verbosity: The amount of information to print. The higher the value, the greater 103 the verbosity. 104 @type verbosity: int 105 """ 106 107 # Test if the current data pipe exists. 108 pipes.test() 109 110 # The specific analysis API object. 111 api = return_api() 112 113 # Deselect spins lacking data: 114 api.overfit_deselect() 115 116 # Get the Processor box singleton (it contains the Processor instance) and alias the Processor. 117 processor_box = Processor_box() 118 processor = processor_box.processor 119 120 # Monte Carlo simulation grid search. 121 if hasattr(cdp, 'sim_state') and cdp.sim_state == 1: 122 # Loop over the simulations. 123 for i in range(cdp.sim_number): 124 # Status. 125 if status.current_analysis: 126 status.auto_analysis[status.current_analysis].mc_number = i 127 else: 128 status.mc_number = i 129 130 # Optimisation. 131 api.grid_search(lower=lower, upper=upper, inc=inc, constraints=constraints, verbosity=verbosity-1, sim_index=i) 132 133 # Print out. 134 if verbosity and not processor.is_queued(): 135 print("Simulation " + repr(i+1)) 136 137 # Unset the status. 138 if status.current_analysis: 139 status.auto_analysis[status.current_analysis].mc_number = None 140 else: 141 status.mc_number = None 142 143 # Grid search. 144 else: 145 api.grid_search(lower=lower, upper=upper, inc=inc, constraints=constraints, verbosity=verbosity) 146 147 # Execute any queued commands. 148 processor.run_queue()
149 150
151 -def minimise(min_algor=None, line_search=None, hessian_mod=None, hessian_type=None, func_tol=None, grad_tol=None, max_iter=None, constraints=True, scaling=True, verbosity=1, sim_index=None):
152 """Minimisation function. 153 154 @keyword min_algor: The minimisation algorithm to use. 155 @type min_algor: str 156 @keyword line_search: The line search algorithm which will only be used in combination with the line search and conjugate gradient methods. This will default to the More and Thuente line search. 157 @type line_search: str or None 158 @keyword hessian_mod: The Hessian modification. This will only be used in the algorithms which use the Hessian, and defaults to Gill, Murray, and Wright modified Cholesky algorithm. 159 @type hessian_mod: str or None 160 @keyword hessian_type: The Hessian type. This will only be used in a few trust region algorithms, and defaults to BFGS. 161 @type hessian_type: str or None 162 @keyword func_tol: The function tolerance which, when reached, terminates optimisation. Setting this to None turns of the check. 163 @type func_tol: None or float 164 @keyword grad_tol: The gradient tolerance which, when reached, terminates optimisation. Setting this to None turns of the check. 165 @type grad_tol: None or float 166 @keyword max_iter: The maximum number of iterations for the algorithm. 167 @type max_iter: int 168 @keyword constraints: If True, constraints are used during optimisation. 169 @type constraints: bool 170 @keyword scaling: If True, diagonal scaling is enabled during optimisation to allow the problem to be better conditioned. 171 @type scaling: bool 172 @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. 173 @type verbosity: int 174 @keyword sim_index: The index of the simulation to optimise. This should be None if normal optimisation is desired. 175 @type sim_index: None or int 176 """ 177 178 # Test if the current data pipe exists. 179 pipes.test() 180 181 # The specific analysis API object. 182 api = return_api() 183 184 # Re-package the minimisation algorithm, options, and constraints for the generic_minimise() calls within the specific code. 185 if constraints: 186 min_options = [min_algor] 187 188 # Determine the constraint algorithm to use. 189 min_algor = api.constraint_algorithm() 190 else: 191 min_options = [] 192 if line_search != None: 193 min_options.append(line_search) 194 if hessian_mod != None: 195 min_options.append(hessian_mod) 196 if hessian_type != None: 197 min_options.append(hessian_type) 198 min_options = tuple(min_options) 199 200 # Deselect spins lacking data: 201 api.overfit_deselect() 202 203 # Get the Processor box singleton (it contains the Processor instance) and alias the Processor. 204 processor_box = Processor_box() 205 processor = processor_box.processor 206 207 # Single Monte Carlo simulation. 208 if sim_index != None: 209 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) 210 211 # Monte Carlo simulation minimisation. 212 elif hasattr(cdp, 'sim_state') and cdp.sim_state == 1: 213 for i in range(cdp.sim_number): 214 # Status. 215 if status.current_analysis: 216 status.auto_analysis[status.current_analysis].mc_number = i 217 else: 218 status.mc_number = i 219 220 # Optimisation. 221 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) 222 223 # Print out. 224 if verbosity and not processor.is_queued(): 225 print("Simulation " + repr(i+1)) 226 227 # Unset the status. 228 if status.current_analysis: 229 status.auto_analysis[status.current_analysis].mc_number = None 230 else: 231 status.mc_number = None 232 233 # Standard minimisation. 234 else: 235 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) 236 237 # Execute any queued commands. 238 processor.run_queue()
239 240
241 -def reset_min_stats(data_pipe=None, spin=None):
242 """Function for resetting the minimisation statistics. 243 244 @param data_pipe: The name of the data pipe to reset the minimisation statisics of. This 245 defaults to the current data pipe. 246 @type data_pipe: str 247 @param spin: The spin data container if spin specific data is to be reset. 248 @type spin: SpinContainer 249 """ 250 251 # The data pipe. 252 if data_pipe == None: 253 data_pipe = pipes.cdp_name() 254 255 # Get the data pipe. 256 dp = pipes.get_pipe(data_pipe) 257 258 259 # Global minimisation statistics. 260 ################################# 261 262 # Chi-squared. 263 if hasattr(dp, 'chi2'): 264 dp.chi2 = None 265 266 # Iteration count. 267 if hasattr(dp, 'iter'): 268 dp.iter = None 269 270 # Function count. 271 if hasattr(dp, 'f_count'): 272 dp.f_count = None 273 274 # Gradient count. 275 if hasattr(dp, 'g_count'): 276 dp.g_count = None 277 278 # Hessian count. 279 if hasattr(dp, 'h_count'): 280 dp.h_count = None 281 282 # Warning. 283 if hasattr(dp, 'warning'): 284 dp.warning = None 285 286 287 # Sequence specific minimisation statistics. 288 ############################################ 289 290 # Loop over all spins. 291 for spin in spin_loop(): 292 # Chi-squared. 293 if hasattr(spin, 'chi2'): 294 spin.chi2 = None 295 296 # Iteration count. 297 if hasattr(spin, 'iter'): 298 spin.iter = None 299 300 # Function count. 301 if hasattr(spin, 'f_count'): 302 spin.f_count = None 303 304 # Gradient count. 305 if hasattr(spin, 'g_count'): 306 spin.g_count = None 307 308 # Hessian count. 309 if hasattr(spin, 'h_count'): 310 spin.h_count = None 311 312 # Warning. 313 if hasattr(spin, 'warning'): 314 spin.warning = None
315 316
317 -def set(val=None, error=None, param=None, scaling=None, spin_id=None):
318 """Set global or spin specific minimisation parameters. 319 320 @keyword val: The parameter values. 321 @type val: number 322 @keyword param: The parameter names. 323 @type param: str 324 @keyword scaling: Unused. 325 @type scaling: float 326 @keyword spin_id: The spin identification string. 327 @type spin_id: str 328 """ 329 330 # Global minimisation stats. 331 if spin_id == None: 332 # Chi-squared. 333 if param == 'chi2': 334 cdp.chi2 = val 335 336 # Iteration count. 337 elif param == 'iter': 338 cdp.iter = val 339 340 # Function call count. 341 elif param == 'f_count': 342 cdp.f_count = val 343 344 # Gradient call count. 345 elif param == 'g_count': 346 cdp.g_count = val 347 348 # Hessian call count. 349 elif param == 'h_count': 350 cdp.h_count = val 351 352 # Residue specific minimisation. 353 else: 354 # Get the spin. 355 spin = return_spin(spin_id) 356 357 # Chi-squared. 358 if param == 'chi2': 359 spin.chi2 = val 360 361 # Iteration count. 362 elif param == 'iter': 363 spin.iter = val 364 365 # Function call count. 366 elif param == 'f_count': 367 spin.f_count = val 368 369 # Gradient call count. 370 elif param == 'g_count': 371 spin.g_count = val 372 373 # Hessian call count. 374 elif param == 'h_count': 375 spin.h_count = val
376