def _optimize_polyfit(server_address, periodic_callback): settings_0 = np.zeros(len(template.CURRENT_FIELDS)) sets = np.identity(len(settings_0)) M = np.zeros(sets.shape) sets_a = np.linspace(-5, 5, 5) * 0.01 readings = np.zeros([len(settings_0), 5]) for i in range(0, len(settings_0)): for j in range(0, len(sets_a)): is_steering, steering = _check_beam_steering(True) if not is_steering: raise AbortOptimizationException() setting_test = sets[i,:] * sets_a[j] template.write_epics_values(server_address, template.CURRENT_FIELDS, setting_test) periodic_callback(server_address) readings[:,j] = template.read_epics_values(server_address, template.BPM_FIELDS) for k in range(0, len(settings_0)): M[i,k] = np.polyfit(sets_a, readings[k,:], 1)[0] # inverse response matrix MI = np.linalg.pinv(M.T) # reset the beam-line template.write_epics_values(server_address, template.CURRENT_FIELDS, settings_0) periodic_callback(server_address) readings_1 = np.asarray(template.read_epics_values(server_address, template.BPM_FIELDS)) # create settings to cancel out offsets new_sets = np.dot(MI, -readings_1) return { 'message': '', 'success': True, 'result': new_sets, }
def _cost_function(values, server_address, periodic_callback): is_steering, steering = _check_beam_steering(True) if not is_steering: raise AbortOptimizationException() template.write_epics_values(server_address, template.CURRENT_FIELDS, values) # periodic_callback() either waits for the remote EPICS or runs a local sim which populates local EPICS periodic_callback(server_address) readings = template.read_epics_values(server_address, template.BPM_FIELDS) #cost = np.sum((np.array(readings) * 1000.) ** 2) cost = np.sum((np.array( [readings[4] - readings[6], readings[5] - readings[7], readings[6], readings[7]] ) * 1000.) ** 2) return cost
def _run_beam_steering(server_address, steering, periodic_callback): method = steering.steeringMethod try: if method == 'nmead': res = _optimize_nelder_mead(server_address, periodic_callback) elif method == 'polyfit': res = _optimize_polyfit(server_address, periodic_callback) if 'result' in res: template.write_epics_values(server_address, template.CURRENT_FIELDS, res['result']) simulation_db.write_json(template.OPTIMIZER_RESULT_FILE, { 'message': res['message'], 'success': res['success'], }) except AbortOptimizationException as e: pass