def scipy_slsqp_sparse(prb_map12_init, y_dm1, x_dm2, cs_idxs, max_nfe=50000, vis=False): #test the gradient from scipy.optimize import check_grad err = check_grad(f_1D_slsqp_sparse, gradient_f_1D_slsqp_sparse, prb_map12_init.flatten(), y_dm1, x_dm2, cs_idxs) #correct print 'Check grad: ', err, err1 stop from scipy.optimize import minimize, fmin_slsqp size1 = np.shape(y_dm1)[0] size2 = np.shape(x_dm2)[0] bnds = create_bounds_sparse(size1,size2, cs_idxs) cons = create_constrains(size1) #stop t0 = cpu_time() #bounds and constrains #if vis: # print 'Optimizing based on slsqp with bounds and constrains' #res = minimize(f_1D_slsqp, prb_map12_init.flatten(),args=(y_dm1, x_dm2), method='SLSQP', bounds=bnds, # constraints = cons, options = ({'maxiter' : max_nfe, 'disp': True})) if vis: print 'Optimizing based on slsqp with bounds and constrains. Also gradient is provided' res = minimize(f_1D_slsqp_sparse, prb_map12_init.flatten(), args=(y_dm1, x_dm2, cs_idxs), method='SLSQP', jac = gradient_f_1D_slsqp_sparse, bounds=bnds, constraints = cons, callback = inter_loss, options = ({'maxiter' : max_nfe, 'disp': True})) t_opt = cpu_time() - t0 plsq = res.x prb_map12 = np.reshape(plsq,(size1,-1)) #print 'Number of iteration', res.nit #print 'Final value of object func:', res.fun #print 'Exit mode', res.status, res.message if vis: #print plsq #print 'Probability mapping: ', prb_map12 begin_err = f_1D_slsqp(prb_map12_init.flatten(), y_dm1, x_dm2) print 'Loss function before optimizing: ', begin_err final_err = f_1D_slsqp(plsq, y_dm1, x_dm2) print 'Loss function after optimizing: ', final_err print 'Optimizing cpu time: ', t_opt return t_opt, prb_map12