def __init__(self, fmodel, selections, r_initial, t_initial, refine_r, refine_t, max_iterations, euler_angle_convention, lbfgs_maxfev): adopt_init_args(self, locals()) self.fmodel_copy = self.fmodel.deep_copy() self.target_functor = self.fmodel_copy.target_functor() self.target_functor.prepare_for_minimization() self.atomic_weights = self.fmodel.xray_structure.atomic_weights() self.sites_cart = self.fmodel.xray_structure.sites_cart() self.sites_frac = self.fmodel.xray_structure.sites_frac() self.n_groups = len(self.selections) assert self.n_groups > 0 self.counter = 0 assert len(self.r_initial) == len(self.t_initial) assert len(self.selections) == len(self.t_initial) self.dim_r = 3 self.dim_t = 3 self.r_min = copy.deepcopy(self.r_initial) self.t_min = copy.deepcopy(self.t_initial) for i in range(len(self.r_min)): self.r_min[i] = tuple(self.r_min[i]) self.t_min[i] = tuple(self.t_min[i]) self.x = self.pack(self.r_min, self.t_min) self.n = self.x.size() self.minimizer = lbfgs.run( target_evaluator=self, core_params=lbfgs.core_parameters(maxfev=lbfgs_maxfev), termination_params=lbfgs.termination_parameters( max_iterations=max_iterations), exception_handling_params=lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound=True, ignore_line_search_failed_step_at_upper_bound=True)) self.compute_functional_and_gradients(suppress_gradients=True) del self.x
def __init__(self, d_i, psi_i, eta_rad, Deff): import sys self.safelog = -1. + math.log(sys.float_info.max) self.S = StringIO.StringIO() pickle.dump([d_i, psi_i, eta_rad, Deff],self.S,0) assert len(d_i) == len(psi_i) self.d_i = d_i self.psi_i = psi_i self.Nobs = len(d_i) self.escalate = 10. # 10 is a soft switch; 50-100 a hard switch self.x = flex.double([log(2./Deff), log(eta_rad)]) # parameters alpha, eta self.minimizer = run( target_evaluator=self, core_params=core_parameters( gtol=0.1 # increasing the accuracy of the line search technique (default=0.9) # as suggested by source code. Otherwise Deff is set unreasonably high # and the exponential blows up. ), termination_params = termination_parameters( traditional_convergence_test=False, drop_convergence_test_max_drop_eps=1.e-5, min_iterations=0, max_iterations = 100, max_calls=200), exception_handling_params=exception_handling_parameters( ignore_line_search_failed_rounding_errors=True, ignore_line_search_failed_step_at_lower_bound=True,#the only change from default ignore_line_search_failed_step_at_upper_bound=False, ignore_line_search_failed_maxfev=False, ignore_line_search_failed_xtol=False, ignore_search_direction_not_descent=False) ) self.x=flex.exp(self.x)
def __init__(self, d_pos, psi_pos, d_neg, psi_neg, eta_rad, Deff): adopt_init_args(self, locals()) import sys self.safelog = -1. + math.log(sys.float_info.max) assert len(d_pos) == len(psi_pos) assert len(d_neg) == len(psi_neg) self.Nobs = len(d_pos) + len(d_neg) self.escalate = 10. # 10 is a soft switch; 50-100 a hard switch self.x = flex.double([log(2. / Deff), log(eta_rad)]) # parameters alpha, eta self.minimizer = run( target_evaluator=self, core_params=core_parameters( gtol=0.1 # increasing the accuracy of the line search technique (default=0.9) # as suggested by source code. Otherwise Deff is set unreasonably high # and the exponential blows up. ), termination_params=termination_parameters( traditional_convergence_test=False, drop_convergence_test_max_drop_eps=1.e-5, min_iterations=0, max_iterations=100, max_calls=200), exception_handling_params=exception_handling_parameters( ignore_line_search_failed_rounding_errors=True, ignore_line_search_failed_step_at_lower_bound= True, #the only change from default ignore_line_search_failed_step_at_upper_bound=False, ignore_line_search_failed_maxfev=False, ignore_line_search_failed_xtol=False, ignore_search_direction_not_descent=False)) self.x = flex.exp(self.x)
def _core_param(self): core_param = core_parameters() core_param.gtol = self.gtol core_param.xtol = self.xtol core_param.stpmin = self.stpmin core_param.stpmax = self.stpmax core_param.maxfev = self.maxfev core_param.m = self.m return core_param
def __init__(self, fmodel, tlsos_initial, refine_T, refine_L, refine_S, selections, selections_1d, max_iterations, run_finite_differences_test = False, correct_adp = True): adopt_init_args(self, locals()) fmodel.xray_structure.scatterers().flags_set_grads(state=False) xray.set_scatterer_grad_flags(scatterers = fmodel.xray_structure.scatterers(), u_aniso = True) if(self.run_finite_differences_test): self.correct_adp = False self.fmodel_copy = self.fmodel.deep_copy() self.target_functor = self.fmodel_copy.target_functor() self.run_finite_differences_test_counter = 0 self.T_initial = [] self.L_initial = [] self.S_initial = [] self.origins = [] for tlso_ in tlsos_initial: self.T_initial.append(tlso_.t) self.L_initial.append(tlso_.l) self.S_initial.append(tlso_.s) self.origins.append(tlso_.origin) self.counter = 0 self.n_groups = len(self.T_initial) self.dim_T = len(self.T_initial[0]) self.dim_L = len(self.L_initial[0]) self.dim_S = len(self.S_initial[0]) self.T_min = self.T_initial self.L_min = self.L_initial self.S_min = self.S_initial self.x = self.pack(self.T_min, self.L_min, self.S_min) self.minimizer = lbfgs.run( target_evaluator = self, core_params = lbfgs.core_parameters(maxfev = 10), termination_params = lbfgs.termination_parameters( min_iterations = max_iterations, max_calls = int(max_iterations*1.5)), exception_handling_params = lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound = True, ignore_line_search_failed_step_at_upper_bound = True, ignore_line_search_failed_maxfev = True)) self.compute_functional_and_gradients() del self.x self.tlsos_result = generate_tlsos( selections = self.selections, xray_structure = self.fmodel.xray_structure, T = self.T_min, L = self.L_min, S = self.S_min)
def __init__(self, fmodel, tlsos_initial, refine_T, refine_L, refine_S, selections, selections_1d, max_iterations, run_finite_differences_test=False, correct_adp=True): adopt_init_args(self, locals()) fmodel.xray_structure.scatterers().flags_set_grads(state=False) xray.set_scatterer_grad_flags( scatterers=fmodel.xray_structure.scatterers(), u_aniso=True) if (self.run_finite_differences_test): self.correct_adp = False self.fmodel_copy = self.fmodel.deep_copy() self.target_functor = self.fmodel_copy.target_functor() self.run_finite_differences_test_counter = 0 self.T_initial = [] self.L_initial = [] self.S_initial = [] self.origins = [] for tlso_ in tlsos_initial: self.T_initial.append(tlso_.t) self.L_initial.append(tlso_.l) self.S_initial.append(tlso_.s) self.origins.append(tlso_.origin) self.counter = 0 self.n_groups = len(self.T_initial) self.dim_T = len(self.T_initial[0]) self.dim_L = len(self.L_initial[0]) self.dim_S = len(self.S_initial[0]) self.T_min = self.T_initial self.L_min = self.L_initial self.S_min = self.S_initial self.x = self.pack(self.T_min, self.L_min, self.S_min) self.minimizer = lbfgs.run( target_evaluator=self, core_params=lbfgs.core_parameters(maxfev=10), termination_params=lbfgs.termination_parameters( min_iterations=max_iterations, max_calls=int(max_iterations * 1.5)), exception_handling_params=lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound=True, ignore_line_search_failed_step_at_upper_bound=True, ignore_line_search_failed_maxfev=True)) self.compute_functional_and_gradients() del self.x self.tlsos_result = generate_tlsos( selections=self.selections, xray_structure=self.fmodel.xray_structure, T=self.T_min, L=self.L_min, S=self.S_min)
def __init__(self, fmodel, model, xs_initial, adp_nmas, fsub_adp, selections, selections_1d, max_iterations, n_modes, weight_nmre=1.0, run_finite_differences_test=False, correct_adp=True, zero_mode_flag=True): adopt_init_args(self, locals()) fmodel.xray_structure.scatterers().flags_set_grads(state=False) xray.set_scatterer_grad_flags( scatterers=fmodel.xray_structure.scatterers(), u_aniso=True) if (self.run_finite_differences_test): self.correct_adp = False self.fmodel_copy = self.fmodel.deep_copy() self.target_functor = self.fmodel_copy.target_functor() self.run_finite_differences_test_counter = 0 self.counter = 0 self.n_groups = len(self.xs_initial) self.dim_x = len(self.xs_initial[0]) self.xs_min = self.xs_initial self.x = self.pack(self.xs_min) # for adp_nma_selected, selection in zip(adp_nmas, selections): # weights_selected = self.fmodel.xray_structure.atomic_weights.select(selection) # modes1d_selected = selected_modes_to_1D(modes = self.modes, n_modes = self.n_modes, # selection = self.selection) # assert len(modes1d_selected)/n_modes == len(weights_selected) # adp_nma_selected = init_nm_adp(modes = modes1d_selected, # weights = weights_selected, # n_modes = n_modes, # zero_mode_flag = zero_mode_flag) # self.adp_nmas.append(adp_nma_selected) self.minimizer = lbfgs.run( target_evaluator=self, core_params=lbfgs.core_parameters(maxfev=10), termination_params=lbfgs.termination_parameters( min_iterations=max_iterations, max_calls=int(max_iterations * 1.5)), exception_handling_params=lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound=True, ignore_line_search_failed_step_at_upper_bound=True, ignore_line_search_failed_maxfev=True)) self.compute_functional_and_gradients() del self.x self.xs_result = self.xs_min
def __init__(self, fmodel, model, xs_initial, adp_nmas, selections, selections_1d, max_iterations, n_modes, weight_nmre = 1.0, run_finite_differences_test = False, correct_adp = True, zero_mode_flag = True): adopt_init_args(self, locals()) fmodel.xray_structure.scatterers().flags_set_grads(state=False) xray.set_scatterer_grad_flags(scatterers = fmodel.xray_structure.scatterers(), u_aniso = True) if(self.run_finite_differences_test): self.correct_adp = False self.fmodel_copy = self.fmodel.deep_copy() self.target_functor = self.fmodel_copy.target_functor() self.run_finite_differences_test_counter = 0 self.counter = 0 self.n_groups = len(self.xs_initial) self.dim_x = len(self.xs_initial[0]) self.xs_min = self.xs_initial self.x = self.pack(self.xs_min) # for adp_nma_selected, selection in zip(adp_nmas, selections): # weights_selected = self.fmodel.xray_structure.atomic_weights.select(selection) # modes1d_selected = selected_modes_to_1D(modes = self.modes, n_modes = self.n_modes, # selection = self.selection) # assert len(modes1d_selected)/n_modes == len(weights_selected) # adp_nma_selected = init_nm_adp(modes = modes1d_selected, # weights = weights_selected, # n_modes = n_modes, # zero_mode_flag = zero_mode_flag) # self.adp_nmas.append(adp_nma_selected) self.minimizer = lbfgs.run( target_evaluator = self, core_params = lbfgs.core_parameters(), termination_params = lbfgs.termination_parameters( min_iterations = max_iterations, max_calls = int(max_iterations*1.5)), exception_handling_params = lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound = False, ignore_line_search_failed_step_at_upper_bound = True, ignore_line_search_failed_maxfev = False)) self.compute_functional_and_gradients() del self.x self.xs_result = self.xs_min
def __init__(self, calculator, stpmax, max_iterations, gradient_only): core_params = lbfgs_core.core_parameters(stpmax=stpmax) termination_params = lbfgs_core.termination_parameters( max_iterations=max_iterations) M = lbfgs_core.run(core_params=core_params, termination_params=termination_params, exception_handling_params=None, target_evaluator=calculator, gradient_only=gradient_only, line_search=True, log=None) # items to store self.M = M self.calculator = calculator self.nfev = self.M.nfun()
def __init__(self, gaussian_fit, target_power, use_sigmas, shift_sqrt_b, lbfgs_termination_params=None, lbfgs_core_params=lbfgs.core_parameters(m=20), hard_b_min=-1): adopt_init_args(self, locals()) minimize_multi_histogram.setdefault(str(self), 0) assert target_power in [2,4] self.x = flex.double(gaussian_fit.n_parameters(), 0) self.first_target_value = None self.minimizer = lbfgs.run( target_evaluator=self, termination_params=lbfgs_termination_params, core_params=lbfgs_core_params) self.finalize()
def __init__(self, fmodel, selections, r_initial, t_initial, refine_r, refine_t, max_iterations, euler_angle_convention, lbfgs_maxfev): adopt_init_args(self, locals()) self.fmodel_copy = self.fmodel.deep_copy() self.target_functor = self.fmodel_copy.target_functor() self.target_functor.prepare_for_minimization() self.atomic_weights = self.fmodel.xray_structure.atomic_weights() self.sites_cart = self.fmodel.xray_structure.sites_cart() self.sites_frac = self.fmodel.xray_structure.sites_frac() self.n_groups = len(self.selections) assert self.n_groups > 0 self.counter=0 assert len(self.r_initial) == len(self.t_initial) assert len(self.selections) == len(self.t_initial) self.dim_r = 3 self.dim_t = 3 self.r_min = copy.deepcopy(self.r_initial) self.t_min = copy.deepcopy(self.t_initial) for i in xrange(len(self.r_min)): self.r_min[i] = tuple(self.r_min[i]) self.t_min[i] = tuple(self.t_min[i]) self.x = self.pack(self.r_min, self.t_min) self.n = self.x.size() self.minimizer = lbfgs.run( target_evaluator = self, core_params = lbfgs.core_parameters( maxfev = lbfgs_maxfev), termination_params = lbfgs.termination_parameters( max_iterations = max_iterations), exception_handling_params = lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound = True, ignore_line_search_failed_step_at_upper_bound = True) ) self.compute_functional_and_gradients(suppress_gradients=True) del self.x
def __init__( self, reciprocal_lattice_points, vector, lbfgs_termination_params=None, lbfgs_core_params=lbfgs.core_parameters(m=20), ): self.reciprocal_lattice_points = reciprocal_lattice_points if not isinstance(vector, flex.double): self.x = flex.double(vector) else: self.x = vector.deep_copy() self.n = len(self.x) assert self.n == 3 self.target = BasisVectorTarget(self.reciprocal_lattice_points) self.minimizer = lbfgs.run( target_evaluator=self, termination_params=lbfgs_termination_params, core_params=lbfgs_core_params, )
def __init__(self, fmodel_core_data, f_obs, u_initial=[0,0,0,0,0,0], refine_u=True, min_iterations=500, max_iterations=500, symmetry_constraints_on_b_cart = True, u_min_max = 500., u_min_min =-500.): adopt_init_args(self, locals()) self.u_min = self.u_initial self.u_factor = self.fmodel_core_data.uc.volume()**(2/3.) if(self.symmetry_constraints_on_b_cart): self.adp_constraints = self.f_obs.space_group().adp_constraints() u_star = self.f_obs.space_group().average_u_star(u_star = self.u_initial) self.dim_u = self.adp_constraints.n_independent_params() assert self.dim_u <= 6 independent_params = self.adp_constraints.independent_params(u_star) self.x = self.pack( u=independent_params, u_factor=self.u_factor) else: self.dim_u = len(self.u_initial) assert self.dim_u == 6 self.x = self.pack( u=flex.double(self.u_min), u_factor=self.u_factor) lbfgs_exception_handling_params = lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound = True, ignore_line_search_failed_step_at_upper_bound = True, ignore_line_search_failed_maxfev = True) self.minimizer = lbfgs.run( target_evaluator = self, core_params = lbfgs.core_parameters(), termination_params = lbfgs.termination_parameters( min_iterations = min_iterations, max_iterations = max_iterations), exception_handling_params = lbfgs_exception_handling_params) self.compute_functional_and_gradients() del self.x
def mpi_split_evaluator_run(target_evaluator, termination_params=None, core_params=None, exception_handling_params=None, log=None, #---> Insertion starts gradient_only=False, line_search=True): #<--- Insertion ends """The supported scenario is that each MPI worker rank has a target evaluator that has part of the data. Each rank calculates a bit of the functional and gradients, but then mpi reduce is used to sum them all up. There has been no low-level redesign to support MPI. In particular, the ext.minimizer is run (wastefully) by every worker rank, using the same data. It is assumed that the calculation of compute_functional_and_gradients() is overwhelmingly the rate limiting step, and that is what MPI parallelism is intended to distribute here.""" from libtbx.mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() if (termination_params is None): termination_params = termination_parameters() if (core_params is None): core_params = core_parameters() if (exception_handling_params is None): exception_handling_params = exception_handling_parameters() x = target_evaluator.x if (log is not None): print >> log, "lbfgs minimizer():" print >> log, " x.size():", x.size() print >> log, " m:", core_params.m print >> log, " maxfev:", core_params.maxfev print >> log, " gtol:", core_params.gtol print >> log, " xtol:", core_params.xtol print >> log, " stpmin:", core_params.stpmin print >> log, " stpmax:", core_params.stpmax print >> log, "lbfgs traditional_convergence_test:", \ termination_params.traditional_convergence_test minimizer = ext.minimizer( x.size(), core_params.m, core_params.maxfev, core_params.gtol, core_params.xtol, core_params.stpmin, core_params.stpmax) if (termination_params.traditional_convergence_test): is_converged = ext.traditional_convergence_test( x.size(), termination_params.traditional_convergence_test_eps) else: is_converged = ext.drop_convergence_test( n_test_points=termination_params.drop_convergence_test_n_test_points, max_drop_eps=termination_params.drop_convergence_test_max_drop_eps, iteration_coefficient =termination_params.drop_convergence_test_iteration_coefficient) callback_after_step = getattr(target_evaluator, "callback_after_step", None) diag_mode = getattr(target_evaluator, "diag_mode", None) if (diag_mode is not None): assert diag_mode in ["once", "always"] f_min, x_min = None, None f, g = None, None try: while 1: if (diag_mode is None): #XXX Only the diag_mode==None case is currently implemented, just as example f_term, g_term = target_evaluator.compute_functional_and_gradients() f_total = comm.reduce(f_term, MPI.SUM, 0) g_total = comm.reduce(g_term, MPI.SUM, 0) if rank==0: transmit = (f_total,g_total) else: transmit = None f, g = comm.bcast(transmit, root=0) if False and rank==0: # for debug print ("%s %10.4f"%("MPI stp",f),"["," ".join(["%10.4f"%a for a in x]),"]") d = None else: f, g, d = target_evaluator.compute_functional_gradients_diag() if (diag_mode == "once"): diag_mode = None if (f_min is None): if (not termination_params.traditional_convergence_test): is_converged(f) f_min, x_min = f, x.deep_copy() elif (f_min > f): f_min, x_min = f, x.deep_copy() if (log is not None): print >> log, "lbfgs minimizer.run():" \ " f=%.6g, |g|=%.6g, x_min=%.6g, x_mean=%.6g, x_max=%.6g" % ( f, g.norm(), flex.min(x), flex.mean(x), flex.max(x)) if (d is None): #---> Insertion starts if (minimizer.run(x, f, g, gradient_only,line_search)): continue #<--- Insertion ends else: #---> Insertion starts if (minimizer.run(x, f, g, d, gradient_only,line_search)): continue #<--- Insertion ends if (log is not None): print >> log, "lbfgs minimizer step" if (callback_after_step is not None): if (callback_after_step(minimizer) is True): if (log is not None): print >> log, "lbfgs minimizer stop: callback_after_step is True" break if (termination_params.traditional_convergence_test): if ( minimizer.iter() >= termination_params.min_iterations and is_converged(x, g)): if (log is not None): print >> log, "lbfgs minimizer stop: traditional_convergence_test" break else: if (is_converged(f)): if (log is not None): print >> log, "lbfgs minimizer stop: drop_convergence_test" break if ( termination_params.max_iterations is not None and minimizer.iter() >= termination_params.max_iterations): if (log is not None): print >> log, "lbfgs minimizer stop: max_iterations" break if ( termination_params.max_calls is not None and minimizer.nfun() > termination_params.max_calls): if (log is not None): print >> log, "lbfgs minimizer stop: max_calls" break if (d is None): #---> Insertion starts if (not minimizer.run(x, f, g, gradient_only,line_search)): break #<--- Insertion ends else: #---> Insertion starts if (not minimizer.run(x, f, g, d, gradient_only,line_search)): break #<--- Insertion ends except RuntimeError as e: minimizer.error = str(e) if (log is not None): print >> log, "lbfgs minimizer exception:", str(e) if (x_min is not None): x.clear() x.extend(x_min) error_classification = exception_handling_params.filter( minimizer.error, x.size(), x, g) if (error_classification > 0): raise elif (error_classification < 0): minimizer.is_unusual_error = True else: minimizer.is_unusual_error = False else: minimizer.error = None minimizer.is_unusual_error = None if (log is not None): print >> log, "lbfgs minimizer done." return minimizer
def __init__(self, fmodel_core_data, f_obs, k_initial, b_initial, u_initial, refine_k, refine_b, refine_u, min_iterations, max_iterations, fmodel_core_data_twin = None, twin_fraction = None, symmetry_constraints_on_b_cart = True, u_min_max = 500., u_min_min =-500., k_sol_max = 10., k_sol_min =-10., b_sol_max = 500., b_sol_min =-500.): adopt_init_args(self, locals()) if(twin_fraction == 0): twin_fraction = None self.twin_fraction = None fmodel_core_data_twin=None self.fmodel_core_data_twin=None assert [fmodel_core_data_twin,twin_fraction].count(None) in [0,2] self.n_shells = self.fmodel_core_data.data.n_shells() if not self.fmodel_core_data_twin is None: assert self.fmodel_core_data_twin.data.n_shells() == self.n_shells assert self.n_shells > 0 and self.n_shells <= 10 self.k_min = self.k_initial assert len(self.k_min) == self.n_shells self.b_min = self.b_initial self.u_min = self.u_initial self.u_factor = self.fmodel_core_data.uc.volume()**(2/3.) if(self.symmetry_constraints_on_b_cart): self.adp_constraints = self.f_obs.space_group().adp_constraints() u_star = self.f_obs.space_group().average_u_star(u_star = self.u_initial) self.dim_u = self.adp_constraints.n_independent_params() assert self.dim_u <= 6 independent_params = self.adp_constraints.independent_params(u_star) self.x = self.pack( u=independent_params, k=self.k_min, b=self.b_min, u_factor=self.u_factor) else: self.dim_u = len(self.u_initial) assert self.dim_u == 6 self.x = self.pack( u=flex.double(self.u_min), k=self.k_min, b=self.b_min, u_factor=self.u_factor) lbfgs_exception_handling_params = lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound = True, ignore_line_search_failed_step_at_upper_bound = True, ignore_line_search_failed_maxfev = True) self.minimizer = lbfgs.run( target_evaluator = self, core_params = lbfgs.core_parameters(), termination_params = lbfgs.termination_parameters( min_iterations = min_iterations, max_iterations = max_iterations), exception_handling_params = lbfgs_exception_handling_params) self.compute_functional_and_gradients() del self.x