Package specific_fns :: Package model_free :: Module multi_processor_commands
[hide private]
[frames] | no frames]

Source Code for Module specific_fns.model_free.multi_processor_commands

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2007 Gary S Thompson (https://gna.org/users/varioustoxins)    # 
  4  # Copyright (C) 2008-2012 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax is free software; you can redistribute it and/or modify               # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation; either version 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax is distributed in the hope that it will be useful,                    # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """Module for the multi-processor command system.""" 
 26   
 27  # Python module imports. 
 28  import sys 
 29  from re import match 
 30   
 31  # relax module imports. 
 32  from maths_fns.mf import Mf 
 33  from minfx.generic import generic_minimise 
 34  from minfx.grid import grid, grid_point_array 
 35  from multi import Memo, Result_command, Slave_command 
 36   
 37   
 38   
39 -def spin_print(spin_id, verbosity):
40 """Print out some header text for the spin. 41 42 @param spin_id: The spin ID string. 43 @type spin_id: str 44 @param verbosity: The amount of information to print. The higher the value, the greater the verbosity. 45 @type verbosity: int 46 """ 47 48 # Some extra spacing for verbose printouts. 49 if verbosity >= 2: 50 print("\n\n") 51 52 # The header. 53 string = "Fitting to spin " + repr(spin_id) 54 print("\n\n" + string) 55 print(len(string) * '~')
56 57 58
59 -class MF_memo(Memo):
60 """The model-free memo class. 61 62 Not quite a momento so a memo. 63 """ 64
65 - def __init__(self, model_free=None, model_type=None, spin=None, sim_index=None, scaling=None, scaling_matrix=None):
66 """Initialise the model-free memo class. 67 68 This memo stores the model-free class instance so that the _disassemble_result() method can be called to store the optimisation results. The other args are those required by this method but not generated through optimisation. 69 70 @keyword model_free: The model-free class instance. 71 @type model_free: specific_fns.model_free.Model_free instance 72 @keyword spin: The spin data container. If this argument is supplied, then the spin_id argument will be ignored. 73 @type spin: SpinContainer instance 74 @keyword sim_index: The optional MC simulation index. 75 @type sim_index: int 76 @keyword scaling: If True, diagonal scaling is enabled. 77 @type scaling: bool 78 @keyword scaling_matrix: The diagonal, square scaling matrix. 79 @type scaling_matrix: numpy diagonal matrix 80 """ 81 82 # Execute the base class __init__() method. 83 super(MF_memo, self).__init__() 84 85 # Store the arguments. 86 self.model_free = model_free 87 self.model_type = model_type 88 self.spin = spin 89 self.sim_index = sim_index 90 self.scaling = scaling 91 self.scaling_matrix = scaling_matrix
92 93 94
95 -class MF_minimise_command(Slave_command):
96 """Command class for standard model-free minimisation.""" 97
98 - def __init__(self):
99 """Initialise the base class.""" 100 101 # Execute the base class __init__() method. 102 super(MF_minimise_command, self).__init__()
103 104
105 - def optimise(self):
106 """Model-free optimisation. 107 108 @return: The optimisation results consisting of the parameter vector, function value, iteration count, function count, gradient count, Hessian count, and warnings. 109 @rtype: tuple of numpy array, float, int, int, int, int, str 110 """ 111 112 # Minimisation. 113 results = generic_minimise(func=self.mf.func, dfunc=self.mf.dfunc, d2func=self.mf.d2func, args=(), x0=self.opt_params.param_vector, min_algor=self.opt_params.min_algor, min_options=self.opt_params.min_options, func_tol=self.opt_params.func_tol, grad_tol=self.opt_params.grad_tol, maxiter=self.opt_params.max_iterations, A=self.opt_params.A, b=self.opt_params.b, full_output=True, print_flag=self.opt_params.verbosity) 114 115 # Return the minfx results unmodified. 116 return results
117 118
119 - def run(self, processor, completed):
120 """Setup and perform the model-free optimisation.""" 121 122 # Initialise the function to minimise. 123 self.mf = Mf(init_params=self.opt_params.param_vector, model_type=self.data.model_type, diff_type=self.data.diff_type, diff_params=self.data.diff_params, scaling_matrix=self.data.scaling_matrix, num_spins=self.data.num_spins, equations=self.data.equations, param_types=self.data.param_types, param_values=self.data.param_values, relax_data=self.data.ri_data, errors=self.data.ri_data_err, bond_length=self.data.r, csa=self.data.csa, num_frq=self.data.num_frq, frq=self.data.frq, num_ri=self.data.num_ri, remap_table=self.data.remap_table, noe_r1_table=self.data.noe_r1_table, ri_labels=self.data.ri_types, gx=self.data.gx, gh=self.data.gh, h_bar=self.data.h_bar, mu0=self.data.mu0, num_params=self.data.num_params, vectors=self.data.xh_unit_vectors) 124 125 # Print out. 126 if self.opt_params.verbosity >= 1 and (self.data.model_type == 'mf' or self.data.model_type == 'local_tm'): 127 spin_print(self.data.spin_id, self.opt_params.verbosity) 128 129 # Preform optimisation. 130 results = self.optimise() 131 132 # Disassemble the results list. 133 param_vector, func, iter, fc, gc, hc, warning = results 134 135 processor.return_object(MF_result_command(processor, self.memo_id, param_vector, func, iter, fc, gc, hc, warning, completed=False))
136 137
138 - def store_data(self, data, opt_params):
139 """Store all the data required for model-free optimisation. 140 141 @param data: The data used to initialise the model-free target function class. 142 @type data: class instance 143 @param opt_params: The parameters and data required for optimisation using minfx. 144 @type opt_params: class instance 145 """ 146 147 # Store the data. 148 self.data = data 149 self.opt_params = opt_params
150 151 152
153 -class MF_grid_command(MF_minimise_command):
154 """Command class for the model-free grid search.""" 155
156 - def __init__(self):
157 """Initialise all the data.""" 158 159 # Execute the base class __init__() method. 160 super(MF_grid_command, self).__init__()
161 162
163 - def optimise(self):
164 """Model-free grid search. 165 166 @return: The optimisation results consisting of the parameter vector, function value, iteration count, function count, gradient count, Hessian count, and warnings. 167 @rtype: tuple of numpy array, float, int, int, int, int, str 168 """ 169 170 # Normal grid search. 171 if not hasattr(self.opt_params, 'subdivision'): 172 results = grid(func=self.mf.func, args=(), num_incs=self.opt_params.inc, lower=self.opt_params.lower, upper=self.opt_params.upper, A=self.opt_params.A, b=self.opt_params.b, verbosity=self.opt_params.verbosity) 173 174 # Subdivided grid. 175 else: 176 results = grid_point_array(func=self.mf.func, args=(), points=self.opt_params.subdivision, verbosity=self.opt_params.verbosity) 177 178 # Unpack the results. 179 param_vector, func, iter, warning = results 180 fc = iter 181 gc = 0.0 182 hc = 0.0 183 184 # Return everything. 185 return param_vector, func, iter, fc, gc, hc, warning
186 187 188
189 -class MF_result_command(Result_command):
190 """Class for processing the model-free results.""" 191
192 - def __init__(self, processor, memo_id, param_vector, func, iter, fc, gc, hc, warning, completed):
193 """Set up the class, placing the minimisation results here.""" 194 195 # Execute the base class __init__() method. 196 super(MF_result_command, self).__init__(processor=processor, completed=completed) 197 198 # Store the arguments. 199 self.memo_id = memo_id 200 self.param_vector = param_vector 201 self.func = func 202 self.iter = iter 203 self.fc = fc 204 self.gc = gc 205 self.hc = hc 206 self.warning = warning
207 208
209 - def run(self, processor, memo):
210 """Disassemble the model-free optimisation results. 211 212 @param processor: Unused! 213 @type processor: None 214 @param memo: The model-free memo. 215 @type memo: memo 216 """ 217 218 # Disassemble the results. 219 memo.model_free._disassemble_result(param_vector=self.param_vector, func=self.func, iter=self.iter, fc=self.fc, gc=self.gc, hc=self.hc, warning=self.warning, spin=memo.spin, sim_index=memo.sim_index, model_type=memo.model_type, scaling=memo.scaling, scaling_matrix=memo.scaling_matrix)
220