1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module for the multi-processor command system."""
25
26
27 import sys
28 from re import match
29
30
31 from maths_fns.mf import Mf
32 from minfx.generic import generic_minimise
33 from minfx.grid import grid, grid_point_array
34 from multi import Memo, Result_command, Slave_command
35
36
37
39 """Print out some header text for the spin.
40
41 @param spin_id: The spin ID string.
42 @type spin_id: str
43 @param verbosity: The amount of information to print. The higher the value, the greater the verbosity.
44 @type verbosity: int
45 """
46
47
48 if verbosity >= 2:
49 print("\n\n")
50
51
52 string = "Fitting to spin " + repr(spin_id)
53 print("\n\n" + string)
54 print(len(string) * '~')
55
56
57
59 """The model-free memo class.
60
61 Not quite a momento so a memo.
62 """
63
64 - def __init__(self, model_free=None, model_type=None, spin=None, sim_index=None, scaling=None, scaling_matrix=None):
65 """Initialise the model-free memo class.
66
67 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.
68
69 @keyword model_free: The model-free class instance.
70 @type model_free: specific_fns.model_free.Model_free instance
71 @keyword spin: The spin data container. If this argument is supplied, then the spin_id argument will be ignored.
72 @type spin: SpinContainer instance
73 @keyword sim_index: The optional MC simulation index.
74 @type sim_index: int
75 @keyword scaling: If True, diagonal scaling is enabled.
76 @type scaling: bool
77 @keyword scaling_matrix: The diagonal, square scaling matrix.
78 @type scaling_matrix: numpy diagonal matrix
79 """
80
81
82 super(MF_memo, self).__init__()
83
84
85 self.model_free = model_free
86 self.model_type = model_type
87 self.spin = spin
88 self.sim_index = sim_index
89 self.scaling = scaling
90 self.scaling_matrix = scaling_matrix
91
92
93
95 """Command class for standard model-free minimisation."""
96
102
103
105 """Model-free optimisation.
106
107 @return: The optimisation results consisting of the parameter vector, function value, iteration count, function count, gradient count, Hessian count, and warnings.
108 @rtype: tuple of numpy array, float, int, int, int, int, str
109 """
110
111
112 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)
113
114
115 return results
116
117
118 - def run(self, processor, completed):
119 """Setup and perform the model-free optimisation."""
120
121
122 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)
123
124
125 if self.opt_params.verbosity >= 1 and (self.data.model_type == 'mf' or self.data.model_type == 'local_tm'):
126 spin_print(self.data.spin_id, self.opt_params.verbosity)
127
128
129 results = self.optimise()
130
131
132 param_vector, func, iter, fc, gc, hc, warning = results
133
134 processor.return_object(MF_result_command(processor, self.memo_id, param_vector, func, iter, fc, gc, hc, warning, completed=False))
135
136
138 """Store all the data required for model-free optimisation.
139
140 @param data: The data used to initialise the model-free target function class.
141 @type data: class instance
142 @param opt_params: The parameters and data required for optimisation using minfx.
143 @type opt_params: class instance
144 """
145
146
147 self.data = data
148 self.opt_params = opt_params
149
150
151
153 """Command class for the model-free grid search."""
154
160
161
163 """Model-free grid search.
164
165 @return: The optimisation results consisting of the parameter vector, function value, iteration count, function count, gradient count, Hessian count, and warnings.
166 @rtype: tuple of numpy array, float, int, int, int, int, str
167 """
168
169
170 if not hasattr(self.opt_params, 'subdivision'):
171 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)
172
173
174 else:
175 results = grid_point_array(func=self.mf.func, args=(), points=self.opt_params.subdivision, verbosity=self.opt_params.verbosity)
176
177
178 param_vector, func, iter, warning = results
179 fc = iter
180 gc = 0.0
181 hc = 0.0
182
183
184 return param_vector, func, iter, fc, gc, hc, warning
185
186
187
189 """Class for processing the model-free results."""
190
191 - def __init__(self, processor, memo_id, param_vector, func, iter, fc, gc, hc, warning, completed):
192 """Set up the class, placing the minimisation results here."""
193
194
195 super(MF_result_command, self).__init__(processor=processor, completed=completed)
196
197
198 self.memo_id = memo_id
199 self.param_vector = param_vector
200 self.func = func
201 self.iter = iter
202 self.fc = fc
203 self.gc = gc
204 self.hc = hc
205 self.warning = warning
206
207
208 - def run(self, processor, memo):
209 """Disassemble the model-free optimisation results.
210
211 @param processor: Unused!
212 @type processor: None
213 @param memo: The model-free memo.
214 @type memo: memo
215 """
216
217
218 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)
219