def quantileReg(t,y,alpha): # Données de la regression m=y.size n=2 A=np.zeros((m,n)) b=np.zeros((m,1)) A[:,0]=t A[:,1]=np.ones((m,)) b[:,0]=y # Variables x = cp.Variable(n) ep = cp.Variable(m) em = cp.Variable(m) # Constitution du problème objective = cp.Minimize(cp.sum_entries(cp.square(alpha*ep +(1.-alpha)*em))) constraints = [A*x-b+em-ep==0,ep>=0,em>=0] prob = cp.Problem(objective, constraints) # Résolution prob.solve() xres=np.array(x.value) return xres
def solve_sparse(self, y, w, x_mask=None): assert y.ndim == 1 and w.ndim == 1 and y.shape == w.shape assert w.shape[0] == self.n x = cp.Variable(np.count_nonzero(x_mask)) inv_mask = np.logical_not(x_mask) term1 = cp.square(cp.norm(x-y[x_mask])) term2 = self.c * cp.norm1(cp.diag(w[x_mask]) * x) objective = cp.Minimize(term1 + term2) constraints = [cp.quad_form(x, self.B[np.ix_(x_mask, x_mask)]) <= 1] problem = cp.Problem(objective, constraints) result = problem.solve(solver=cp.SCS) if problem.status != cp.OPTIMAL: warnings.warn(problem.status) if problem.status not in (cp.OPTIMAL, cp.OPTIMAL_INACCURATE, cp.UNBOUNDED_INACCURATE): raise ValueError(problem.status) out = np.zeros(self.n) x = np.asarray(x.value).flatten() out[x_mask] = x return out
def find_ideal_pt_for_person_in_ball(center, radius, idealpt_and_radius, constraint="l1"): X = cvxpy.Variable(5) # 1 point for each item fun = 0 y = idealpt_and_radius[0] w = idealpt_and_radius[1] sumsq = math.sqrt(sum([math.pow(w[i], 2) for i in range(5)])) w = [w[i] / sumsq for i in range(5)] for slider in range(5): fun += w[slider] * cvxpy.abs(X[slider] - y[slider]) obj = cvxpy.Minimize(fun) constraints = [X >= 0, X[0] + X[1] + X[2] - X[3] + 162 == X[4]] if constraint == "l1": constraints += [cvxpy.sum_entries( cvxpy.abs(X[0:4] - center[0:4])) <= radius] else: constraints += [cvxpy.sum_entries( cvxpy.square(X[0:4] - center[0:4])) <= radius**2] prob = cvxpy.Problem(obj, constraints) result = prob.solve() items = [X.value[i, 0] for i in range(5)] if constraint == "l1": credits = [abs(items[i] - center[i]) / radius for i in range(4)] else: credits = [(items[i] - center[i])**2 / radius**2 for i in range(4)] deficit = calculate_deficit(items) items.append(deficit) return items, credits
def cbp_taylor(y, F, Delta, penalty=0.1, order=1): """ 1st order taylor approximation """ # generate derivative matrices dF = list() current = F for i in range(order): dF.append( deriv(current,t) ) current = dF[-1] # Construct the problem. Fp = cvxopt.matrix(F) dFp = cvxopt.matrix(dF[0]) yp = cvxopt.matrix(y) gamma = cp.Parameter(sign="positive", name='gamma') gamma.value = penalty x = cp.Variable(F.shape[1],name='x') d = cp.Variable(F.shape[1],name='d') objective = cp.Minimize(sum(cp.square(yp - Fp*x - dFp*d)) + gamma*cp.norm(x, 1)) constraints = [0 <= x, cp.abs(d) <= 0.5*Delta*x] p = cp.Problem(objective, constraints) # solve result = p.solve() # reconstruct yhat = F.dot(np.array(x.value)) + dF[0].dot(np.array(d.value)) return np.array(x.value), yhat, np.array(d.value), p.value
def test_signed_curvature(self): # Convex argument. expr = cvx.abs(1 + cvx.exp(cvx.Variable()) ) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.abs( -cvx.entr(cvx.Variable()) ) self.assertEqual(expr.curvature, s.UNKNOWN) expr = cvx.abs( -cvx.log(cvx.Variable()) ) self.assertEqual(expr.curvature, s.UNKNOWN) # Concave argument. expr = cvx.abs( cvx.log(cvx.Variable()) ) self.assertEqual(expr.curvature, s.UNKNOWN) expr = cvx.abs( -cvx.square(cvx.Variable()) ) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.abs( cvx.entr(cvx.Variable()) ) self.assertEqual(expr.curvature, s.UNKNOWN) # Affine argument. expr = cvx.abs( cvx.NonNegative() ) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.abs( -cvx.NonNegative() ) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.abs( cvx.Variable() ) self.assertEqual(expr.curvature, s.CONVEX)
def constraint_norm1(Ftilde, Sigma_F, positivity=False): """ Constrain with optimization strategy """ import cvxpy as cvx m, n = Sigma_F.shape Sigma_F_inv = np.linalg.inv(Sigma_F) zeros_F = np.zeros_like(Ftilde[:, np.newaxis]) F = cvx.Variable(n) # Construct the problem. PRIMAL expression = cvx.quad_form(F - Ftilde[:, np.newaxis], Sigma_F_inv) objective = cvx.Minimize(expression) if positivity: constraints = [F[0] == 0, F[-1] == 0, F >= zeros_F, cvx.square(cvx.norm(F, 2)) <= 1] else: constraints = [F[0] == 0, F[-1] == 0, cvx.square(cvx.norm(F, 2)) <= 1] prob = cvx.Problem(objective, constraints) prob.solve(verbose=0, solver=cvx.CVXOPT) return np.squeeze(np.array((F.value)))
def runndCVXPy(A, b, clambda): n_vars = A.shape[1] x = cvxpy.Variable(n_vars) obj = cvxpy.Minimize( clambda * cvxpy.sum_entries(cvxpy.abs(x)) + 0.5 * cvxpy.sum_entries(cvxpy.square(A * x - b))) prob = cvxpy.Problem(obj, []) prob.solve(verbose=False) return x.value
def updateEz2(self,sigmaz,ez_): ez=cvx.Variable() constraints=[0<=ez] objective = cvx.Maximize(np.array(sigmaz)*ez-ez*self.omegabg-rho/2*cvx.square(ez-ez_)) prob = cvx.Problem(objective, constraints) result = prob.solve(solver=slv) #print(ez.value) self.ez=ez.value
def l1_solution(A, b, lam=0.5): N = A.shape[0] x = Variable(N) objective = Minimize(sum_entries(square(A * x - b)) + lam * norm(x, 1)) constraints = [] prob = Problem(objective, constraints) prob.solve() xhat = x.value return xhat
def test_linearize(self): """ Test the linearize function. """ z = cvx.Variable((1,5)) expr = cvx.square(z) z.value = np.reshape(np.array([1,2,3,4,5]), (1,5)) lin = linearize(expr) self.assertEqual(lin.shape, (1,5)) self.assertItemsAlmostEqual(lin.value, [1,4,9,16,25])
def transition_to_action(s0,s1, dynamics, max_dtheta, max_ddx, max_iter=30, max_line_searches=10): """recovers a control signal u that can cause a transition from s0 to s1. specifically, minimize || f(s0,u) - s1 ||^2 as a Sequential Quadratic Program. """ # the current step size along the search direction recovered by the QP step = 1. # initial guess s0 = array(s0) s1 = array(s1) u0 = array((0,0)) def cost(u): "|| f(s0,u) - s1 ||^2" return sum((dynamics(s0,u)['val'] - s1)**2) # the value of the initial guess best_cost = cost(u0) for it in xrange(max_iter): f = dynamics(s0, u0, derivs={'du'}) # linearize || f(s0,u) - s1 ||^2 about u0 and solve as a QP u = CX.Variable(len(u0), name='u') objective = CX.square(CX.norm( array(f['val']) + vstack(f['du'])*(u-u0) - s1 )) p = CX.Problem(CX.Minimize(objective), [CX.abs(u[0]) <= max_ddx, CX.abs(u[1]) <= max_dtheta]) r = p.solve() unew = array(u.value.flat) # line search along unew-u0 from u0 line_search_success = False for line_searches in xrange(max_line_searches): new_cost = cost(u0 + step*(unew-u0)) if new_cost < best_cost: # accept the step best_cost = new_cost u0 = u0 + step*(unew-u0) # grow the step for the next iteration step *= 1.2 line_search_success = True else: # shrink the step size and try again step *= 0.5 if not line_search_success: # convergence is when line search fails. return u0 print 'Warning: failed to converge' return u0
def cbp_polar(y, F, Fprev, Fnext, Delta, penalty=0.1, order=1): """ CBP with polar interpolation """ # compute r and theta a = 0.5 * np.linalg.norm(Fnext-Fprev, axis=0)[int(F.shape[1]/2)] b = np.linalg.norm(F-Fprev, axis=0)[int(F.shape[1]/2)] theta = np.pi - 2 * np.arcsin(a/b) # radians r = a / np.sin(theta) # build the polar transformation matrix P = np.array([[1,r*np.cos(theta),-r*np.sin(theta)], [1,r,0], [1,r*np.cos(theta),r*np.sin(theta)]]) # get C, U, and V pol = np.linalg.inv(P).dot(np.vstack((Fprev.ravel(),F.ravel(),Fnext.ravel()))) C = pol[0,:].reshape(F.shape) U = pol[1,:].reshape(F.shape) V = pol[2,:].reshape(F.shape) ## construct the problem # discretized matrices Cp = cvxopt.matrix(C) Up = cvxopt.matrix(U) Vp = cvxopt.matrix(V) yp = cvxopt.matrix(y) # sparsity penalty gamma = cp.Parameter(sign="positive", name='gamma') gamma.value = penalty # variables dx = cp.Variable(F.shape[1],name='x') dy = cp.Variable(F.shape[1],name='y') dz = cp.Variable(F.shape[1],name='z') # objective and constraints objective = cp.Minimize(sum(cp.square(yp - Cp*dx - Up*dy - Vp*dz)) + gamma*cp.norm(dx, 1)) #constraints = [0 <= x, cp.sqrt(cp.square(y)+cp.square(z)) <= r*x, r*np.cos(theta)*x <= y, y <= r*x] sqcon = [cp.norm(cp.vstack(yi,zi),2) <= xi*r for xi, yi, zi in zip(dx,dy,dz)] constraints = [0 <= dx, dy <= r*dx, r*np.cos(theta)*dx <= dy] constraints.extend(sqcon) p = cp.Problem(objective, constraints) # solve result = p.solve() # reconstruct yhat = C.dot(np.array(dx.value)) + U.dot(np.array(dy.value)) + V.dot(np.array(dz.value)) return np.array(dx.value), yhat, p.value
def solveX(data): inputs = int(data[len(data)-1]) lamb = data[len(data)-2] rho = data[len(data)-3] x = data[0:inputs] y = data[inputs:2*inputs] z = data[2*inputs:3*inputs] a = data[3*inputs:4*inputs] neighs = data[4*inputs:len(data)-3] xnew = cp.Variable(inputs, 1) g = 0.5*cp.square(norm(xnew - a)) h = 0 for i in range(inputs): #This can be written better... h = h + y[i]*(xnew[i] - z[i]) s = cp.square(norm(xnew - z)) w = 0 #TODO fill in later for i in range(len(neighs)/(inputs+1)): w = w + neighs[i*(inputs+1)]*norm(xnew - neighs[i*(inputs+1)+1:i*(inputs+1)+(inputs+1)]) objective = cp.Minimize(g + lamb/2*w + h + rho/2*s) constraints = [] p = cp.Problem(objective, constraints) result = p.solve() return xnew.value
def cvx_test(*args): n = 5000 p = 100 X = np.random.uniform(-1,1,(n,p)) C = 1e-3 y = np.random.uniform(-1,1, n) w = cvx.Variable(p) loss = cvx.sum_entries(cvx.square(X*w - y)) reg = cvx.norm2(w)**2 obj = cvx.Minimize(loss + C*reg) prob = cvx.Problem(obj, []) tic() prob.solve(solver=cvx.SCS, verbose=False) toc()
def feasibility_regression(X, pairwise_constraints_indices, bag_indices,upper_p_bound_bags, diff_upper_bound_pairs,diff_lower_bound_pairs, lower_p_bound_bags): theta = cp.Variable(X.shape[1]) reg = cp.square(cp.norm(theta, 2)) constraints = [] added_pairs = [] pair_ind = 0 for pair in pairwise_constraints_indices: bag_high = bag_indices[pair[0]] bag_low = bag_indices[pair[1]] scores_high = (1./len(bag_high))*X[bag_high]*theta scores_low = (1./len(bag_low))*X[bag_low]*theta if pair in diff_upper_bound_pairs: constraints.append(cp.sum_entries(scores_high) - cp.sum_entries(scores_low) < diff_upper_bound_pairs[pair]) if pair in diff_lower_bound_pairs: constraints.append(cp.sum_entries(scores_high) - cp.sum_entries(scores_low) > diff_lower_bound_pairs[pair]) else: constraints.append(cp.sum_entries(scores_high) - cp.sum_entries(scores_low) > 0) if pair[0] not in added_pairs: if pair[0] in upper_p_bound_bags: constraints.append(cp.sum_entries(scores_high)<=upper_p_bound_bags[pair[0]]) if pair[0] in lower_p_bound_bags: constraints.append(cp.sum_entries(scores_high)>=lower_p_bound_bags[pair[0]]) added_pairs.append(pair[0]) if pair[1] not in added_pairs: if pair[1] in upper_p_bound_bags: constraints.append(cp.sum_entries(scores_low)<=upper_p_bound_bags[pair[1]]) if pair[1] in lower_p_bound_bags: constraints.append(cp.sum_entries(scores_low)>=lower_p_bound_bags[pair[1]]) added_pairs.append(pair[1]) pair_ind+=1 prob = cp.Problem(cp.Minimize(1*reg),constraints = constraints) try: prob.solve() except: prob.solve(solver="SCS") w_t = np.squeeze(np.asarray(np.copy(theta.value))) return w_t
def test_convexify_obj(self): """ Test convexify objective """ obj = cvx.Maximize(cvx.sum(cvx.square(self.x))) self.x.value = [1,1] obj_conv = convexify_obj(obj) prob_conv = cvx.Problem(obj_conv, [self.x <= -1]) prob_conv.solve() self.assertAlmostEqual(prob_conv.value,-6) obj = cvx.Minimize(cvx.sqrt(self.a)) self.a.value = [1] obj_conv = convexify_obj(obj) prob_conv = cvx.Problem(obj_conv,cvx.sqrt(self.a).domain) prob_conv.solve() self.assertAlmostEqual(prob_conv.value,0.5)
def cvxpy_sigma_trace(b, model): xDim = model.xDim SqrtSigma = np.zeros([xDim,xDim]).tolist() idx = xDim for j in xrange(0,xDim): for i in xrange(j,xDim): SqrtSigma[i][j] = b[idx] SqrtSigma[j][i] = SqrtSigma[i][j] idx = idx+1 trace = 0 for i in xrange(0,xDim): trace += sum(cvxpy.square(SqrtSigma[i][j]) for j in xrange(0,xDim)) return trace
def admm(self, rho=0.5, iterations=5, *args, **kwargs): noncvx_vars = [] for var in self.variables(): if getattr(var, "noncvx", False): noncvx_vars += [var] # Form ADMM problem. obj = self.objective._expr for var in noncvx_vars: obj = obj + (rho/2)*cvx.sum_entries(cvx.square(var - var.z + var.u)) prob = cvx.Problem(cvx.Minimize(obj), self.constraints) # ADMM loop for i in range(iterations): result = prob.solve(*args, **kwargs) for var in noncvx_vars: var.z.value = var.round(var.value + var.u.value) var.u.value += var.value - var.z.value return polish(self, noncvx_vars, *args, **kwargs)
def test_problem_penalty(self): """ Compare cvxpy solutions to cvxopt ground truth """ from cvxpy import (matrix,variable,program,minimize, sum,abs,norm2,log,square,zeros,max, hstack,vstack) m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # set tolerance to 5 significant digits tol_exp = 5 # l1 approximation x = variable(n) p = program(minimize(sum(abs(A*x + b)))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x1,tol_exp) # l2 approximation x = variable(n) p = program(minimize(norm2(A*x + b))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x2,tol_exp) # Deadzone approximation - implementation is currently ugly (need max along axis) x = variable(n) Axbm = abs(A*x+b)-0.5 Axbm_deadzone = vstack([max(hstack((Axbm[i,0],0.0))) for i in range(m)]) p = program(minimize(sum(Axbm_deadzone))) p.solve(True) obj_dz_cvxpy = np.sum(np.max([np.abs(A*x.value+b)-0.5,np.zeros((m,1))],axis=0)) np.testing.assert_array_almost_equal(obj_dz_cvxpy,self.obj_dz,tol_exp) # Log barrier x = variable(n) p = program(minimize(-sum(log(1.0-square(A*x + b))))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.cxlb,tol_exp)
def admm(self, rho=0.5, iterations=5, solver=cvx.ECOS): vars_ = self.objective.variables() for constr in self.constraints: vars_ += constr.variables() noncvx_vars = [obj for obj in vars_ if isinstance(obj, NonCvxVariable)] # Form ADMM problem. obj = self.objective._expr for var in noncvx_vars: obj = obj + (rho / 2) * cvx.sum_entries(cvx.square(var - var.z + var.u)) p = cvx.Problem(cvx.Minimize(obj), self.constraints) # ADMM loop for i in range(iterations): result = p.solve(solver=solver) for var in noncvx_vars: var.z.value = var.round(var.value + var.u.value) var.u.value = var.value - var.z.value # Fix noncvx variables and solve. fix_constr = [] for var in noncvx_vars: fix_constr += var.fix(var.z.value) p = cvx.Problem(self.objective, self.constraints + fix_constr) return p.solve(solver=solver)
def admm(self, rho=0.5, iterations=5, solver=cvx.ECOS): noncvx_vars = [] for var in self.variables(): if getattr(var, "noncvx", False): noncvx_vars += [var] # Form ADMM problem. obj = self.objective._expr for var in noncvx_vars: obj = obj + (rho/2)*cvx.sum_entries(cvx.square(var - var.z + var.u)) prob = cvx.Problem(cvx.Minimize(obj), self.constraints) # ADMM loop for i in range(iterations): result = prob.solve(solver=solver) for var in noncvx_vars: var.z.value = var.round(var.value + var.u.value) var.u.value += var.value - var.z.value # Fix noncvx variables and solve. fix_constr = [] for var in noncvx_vars: fix_constr += var.fix(var.z.value) prob = cvx.Problem(self.objective, self.constraints + fix_constr) return prob.solve(solver=solver)
def admm(self, rho=0.5, iterations=5, solver=cp.ECOS): objective,constr_map,dims = self.canonicalize() var_offsets,x_length = self.variables(objective, constr_map[s.EQ] + constr_map[s.INEQ]) noncvx_vars = [obj for obj in var_offsets.keys() if isinstance(obj, NonCvxVariable)] # Form ADMM problem. obj = self.objective.expr for var in noncvx_vars: obj = obj + (rho/2)*sum(cp.square(var - var.z + var.u)) p = cp.Problem(cp.Minimize(obj), self.constraints) # ADMM loop for i in range(iterations): result = p.solve(solver=solver) for var in noncvx_vars: var.z.value = var.round(var.value + var.u.value) var.u.value = var.value - var.z.value # Fix noncvx variables and solve. fix_constr = [] for var in noncvx_vars: fix_constr += var.fix(var.z.value) p = cp.Problem(self.objective, self.constraints + fix_constr) return p.solve(solver=solver)
def admm(self, rho=0.5, iterations=5, solver=cp.ECOS): objective,constr_map,dims = self.canonicalize() new_constr_map = constr_map[s.EQ] for c in constr_map[s.INEQ]: new_constr_map.add(c) vars_ = objective.variables() for constr in new_constr_map: vars_ += constr.variables() var_offsets = OrderedDict() vert_offset = 0 for var in set(vars_): var_offsets[var] = vert_offset vert_offset += var.size[0]*var.size[1] #var_offsets,x_length = self.variables(objective, # new_constr_map) noncvx_vars = [obj for obj in var_offsets.keys() if isinstance(obj, NonCvxVariable)] #import pdb; pdb.set_trace() # Form ADMM problem. obj = self.objective._expr for var in noncvx_vars: obj = obj + (rho/2)*sum(cp.square(var - var.z + var.u)) p = cp.Problem(cp.Minimize(obj), self.constraints) # ADMM loop for i in range(iterations): result = p.solve(solver=solver) for var in noncvx_vars: var.z.value = var.round(var.value + var.u.value) var.u.value = var.value - var.z.value # Fix noncvx variables and solve. fix_constr = [] for var in noncvx_vars: fix_constr += var.fix(var.z.value) p = cp.Problem(self.objective, self.constraints + fix_constr) return p.solve(solver=solver)
def lasso_cvxpy(dict, target, gamma): """ Computes Lasso optimization :param dict: dictionnary :type dict: np.array :param target: image :typetarget: np.array :param gamma: regularization factor :type gamma: float :rtype: np.array """ num_samples = target.shape[1] patch_size = dict.shape[0] dic_size = dict.shape[1] alpha = cp.Variable(dic_size, num_samples) D = cp.Parameter(patch_size, dic_size, value=dict) x = cp.Parameter(patch_size, num_samples, value=target) objective = cp.Minimize(sum(cp.square(D * alpha - x)) / (2 * num_samples) + gamma * cp.norm(alpha, 1)) P = cp.Problem(objective) P.solve() # print(P.value) return alpha.value
def run_MPC(traj_des, cur_state_vec, mpc_acc, mpc_steer, steer_des, goal): for iter in range(3): traj_pred = calc_predicted_trajectory(mpc_acc, mpc_steer, cur_state_vec) x = cp.Variable([Nx, H + 1]) u = cp.Variable([Nu, H]) cost = 0.0 constraints = [] for i in range(H): cost += cp.sum(W1 * cp.square(u[:, i])) # input weightage cost += cp.sum( W2 * cp.square(traj_des[:, i] - x[:, i])) # state error weightage #cost += cp.sum(W2 * cp.square([goal[0],goal[1],0,0] - x[:, i])) # terminal cost if i < (H - 1): cost += cp.sum( W3 * cp.square(u[:, i + 1] - u[:, i])) # rate of input change weightage constraints += [ cp.abs(u[1, i + 1] - u[1, i]) <= max_steer_rate * dt ] A, B, C = dynamic_model(traj_pred[3, i], traj_pred[2, i], mpc_steer[i]) constraints += [x[:, i + 1] == A * x[:, i] + B * u[:, i] + C] cost += cp.sum( W4 * cp.square(traj_des[:, H] - x[:, H])) # final state error weightage #cost += cp.sum(10 * cp.square([goal[0],goal[1]] - x[:2, H])) # terminal cost constraints += [x[:, 0] == cur_state_vec] constraints += [x[3, :] <= max_speed] constraints += [x[3, :] >= -max_reverse_speed] constraints += [u[1, :] <= max_steer_angle] constraints += [u[1, :] >= -max_steer_angle] constraints += [u[0, :] <= max_acc] constraints += [u[0, :] >= -3 * max_acc] prob = cp.Problem(cp.Minimize(cost), constraints) prob.solve() mpc_x = x.value[0, :] mpc_y = x.value[1, :] mpc_yaw = x.value[2, :] mpc_v = x.value[3, :] mpc_acc = u.value[0, :] mpc_steer = u.value[1, :] lyap_val = 0 lap_u = 0 lap_x = 0 lap_du = 0 for i in range(H): lyap_val += np.sum(W1 * np.square(u.value[:, i])) # input weightage lap_u += np.sum(W1 * np.square(u.value[:, i])) lyap_val += np.sum( W2 * np.square(traj_des[:, i] - x.value[:, i])) # state error weightage lap_x += np.sum(W2 * np.square(traj_des[:, i] - x.value[:, i])) if i < (H - 1): lyap_val += np.sum( W3 * np.square(u.value[:, i + 1] - u.value[:, i])) # rate of input change weightage lap_du += np.sum(W3 * np.square(u.value[:, i + 1] - u.value[:, i])) lyap_val += np.sum(W4 * np.square(traj_des[:, H] - x.value[:, H])) lap_x += np.sum(W4 * np.square(traj_des[:, H] - x.value[:, H])) #yap_val += np.sum(W2 * np.square(x.value[:, 1])) aaaa = 5 return mpc_x, mpc_y, mpc_yaw, mpc_v, mpc_acc, mpc_steer, lyap_val, lap_u, lap_x, lap_du
def Main_vbjde_Extension_constrained(graph, Y, Onsets, Thrf, K, TR, beta, dt, scale=1, estimateSigmaH=True, sigmaH=0.05, NitMax=-1, NitMin=1, estimateBeta=True, PLOT=False, contrasts=[], computeContrast=False, gamma_h=0, estimateHRF=True, TrueHrfFlag=False, HrfFilename='hrf.nii', estimateLabels=True, LabelsFilename='labels.nii', MFapprox=False, InitVar=0.5, InitMean=2.0, MiniVEMFlag=False, NbItMiniVem=5): # VBJDE Function for BOLD with contraints logger.info("Fast EM with C extension started ...") np.random.seed(6537546) ########################################################################## # INITIALIZATIONS # Initialize parameters tau1 = 0.0 tau2 = 0.0 S = 100 Init_sigmaH = sigmaH Nb2Norm = 1 NormFlag = False if NitMax < 0: NitMax = 100 gamma = 7.5 #gamma_h = 1000 gradientStep = 0.003 MaxItGrad = 200 Thresh = 1e-5 Thresh_FreeEnergy = 1e-5 estimateLabels = True # WARNING!! They should be estimated # Initialize sizes vectors D = int(np.ceil(Thrf / dt)) + 1 # D = int(np.ceil(Thrf/dt)) M = len(Onsets) N = Y.shape[0] J = Y.shape[1] l = int(np.sqrt(J)) condition_names = [] # Neighbours maxNeighbours = max([len(nl) for nl in graph]) neighboursIndexes = np.zeros((J, maxNeighbours), dtype=np.int32) neighboursIndexes -= 1 for i in xrange(J): neighboursIndexes[i, :len(graph[i])] = graph[i] # Conditions X = OrderedDict([]) for condition, Ons in Onsets.iteritems(): X[condition] = vt.compute_mat_X_2(N, TR, D, dt, Ons) condition_names += [condition] XX = np.zeros((M, N, D), dtype=np.int32) nc = 0 for condition, Ons in Onsets.iteritems(): XX[nc, :, :] = X[condition] nc += 1 # Covariance matrix order = 2 D2 = vt.buildFiniteDiffMatrix(order, D) R = np.dot(D2, D2) / pow(dt, 2 * order) invR = np.linalg.inv(R) Det_invR = np.linalg.det(invR) Gamma = np.identity(N) Det_Gamma = np.linalg.det(Gamma) p_Wtilde = np.zeros((M, K), dtype=np.float64) p_Wtilde1 = np.zeros((M, K), dtype=np.float64) p_Wtilde[:, 1] = 1 Crit_H = 1 Crit_Z = 1 Crit_A = 1 Crit_AH = 1 AH = np.zeros((J, M, D), dtype=np.float64) AH1 = np.zeros((J, M, D), dtype=np.float64) Crit_FreeEnergy = 1 cA = [] cH = [] cZ = [] cAH = [] FreeEnergy_Iter = [] cTime = [] cFE = [] SUM_q_Z = [[] for m in xrange(M)] mu1 = [[] for m in xrange(M)] h_norm = [] h_norm2 = [] CONTRAST = np.zeros((J, len(contrasts)), dtype=np.float64) CONTRASTVAR = np.zeros((J, len(contrasts)), dtype=np.float64) Q_barnCond = np.zeros((M, M, D, D), dtype=np.float64) XGamma = np.zeros((M, D, N), dtype=np.float64) m1 = 0 for k1 in X: # Loop over the M conditions m2 = 0 for k2 in X: Q_barnCond[m1, m2, :, :] = np.dot( np.dot(X[k1].transpose(), Gamma), X[k2]) m2 += 1 XGamma[m1, :, :] = np.dot(X[k1].transpose(), Gamma) m1 += 1 if MiniVEMFlag: logger.info("MiniVEM to choose the best initialisation...") """InitVar, InitMean, gamma_h = MiniVEM_CompMod(Thrf,TR,dt,beta,Y,K, gamma,gradientStep, MaxItGrad,D,M,N,J,S, maxNeighbours, neighboursIndexes, XX,X,R,Det_invR,Gamma, Det_Gamma, scale,Q_barnCond,XGamma, NbItMiniVem, sigmaH,estimateHRF)""" InitVar, InitMean, gamma_h = vt.MiniVEM_CompMod(Thrf, TR, dt, beta, Y, K, gamma, gradientStep, MaxItGrad, D, M, N, J, S, maxNeighbours, neighboursIndexes, XX, X, R, Det_invR, Gamma, Det_Gamma, p_Wtilde, scale, Q_barnCond, XGamma, tau1, tau2, NbItMiniVem, sigmaH, estimateHRF) sigmaH = Init_sigmaH sigma_epsilone = np.ones(J) logger.info( "Labels are initialized by setting active probabilities to ones ...") q_Z = np.zeros((M, K, J), dtype=np.float64) q_Z[:, 1, :] = 1 q_Z1 = np.zeros((M, K, J), dtype=np.float64) Z_tilde = q_Z.copy() # TT,m_h = getCanoHRF(Thrf-dt,dt) #TODO: check TT, m_h = getCanoHRF(Thrf, dt) # TODO: check m_h = m_h[:D] m_H = np.array(m_h).astype(np.float64) m_H1 = np.array(m_h) sigmaH1 = sigmaH if estimateHRF: Sigma_H = np.ones((D, D), dtype=np.float64) else: Sigma_H = np.zeros((D, D), dtype=np.float64) Beta = beta * np.ones((M), dtype=np.float64) P = vt.PolyMat(N, 4, TR) L = vt.polyFit(Y, TR, 4, P) PL = np.dot(P, L) y_tilde = Y - PL Ndrift = L.shape[0] sigma_M = np.ones((M, K), dtype=np.float64) sigma_M[:, 0] = 0.5 sigma_M[:, 1] = 0.6 mu_M = np.zeros((M, K), dtype=np.float64) for k in xrange(1, K): mu_M[:, k] = InitMean Sigma_A = np.zeros((M, M, J), np.float64) for j in xrange(0, J): Sigma_A[:, :, j] = 0.01 * np.identity(M) m_A = np.zeros((J, M), dtype=np.float64) m_A1 = np.zeros((J, M), dtype=np.float64) for j in xrange(0, J): for m in xrange(0, M): for k in xrange(0, K): m_A[j, m] += np.random.normal(mu_M[m, k], np.sqrt(sigma_M[m, k])) * q_Z[m, k, j] m_A1 = m_A t1 = time.time() ########################################################################## # VBJDE num. iter. minimum ni = 0 while ((ni < NitMin) or (((Crit_FreeEnergy > Thresh_FreeEnergy) or (Crit_AH > Thresh)) and (ni < NitMax))): logger.info("------------------------------ Iteration n° " + str(ni + 1) + " ------------------------------") ##################### # EXPECTATION ##################### # A logger.info("E A step ...") UtilsC.expectation_A(q_Z, mu_M, sigma_M, PL, sigma_epsilone, Gamma, Sigma_H, Y, y_tilde, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N, K) val = np.reshape(m_A, (M * J)) val[np.where((val <= 1e-50) & (val > 0.0))] = 0.0 val[np.where((val >= -1e-50) & (val < 0.0))] = 0.0 # crit. A DIFF = np.reshape(m_A - m_A1, (M * J)) # To avoid numerical problems DIFF[np.where((DIFF < 1e-50) & (DIFF > 0.0))] = 0.0 # To avoid numerical problems DIFF[np.where((DIFF > -1e-50) & (DIFF < 0.0))] = 0.0 Crit_A = ( np.linalg.norm(DIFF) / np.linalg.norm(np.reshape(m_A1, (M * J)))) ** 2 cA += [Crit_A] m_A1[:, :] = m_A[:, :] # HRF h if estimateHRF: ################################ # HRF ESTIMATION ################################ UtilsC.expectation_H(XGamma, Q_barnCond, sigma_epsilone, Gamma, R, Sigma_H, Y, y_tilde, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N, scale, sigmaH) import cvxpy as cvx m, n = Sigma_H.shape Sigma_H_inv = np.linalg.inv(Sigma_H) zeros_H = np.zeros_like(m_H[:, np.newaxis]) # Construct the problem. PRIMAL h = cvx.Variable(n) expression = cvx.quad_form(h - m_H[:, np.newaxis], Sigma_H_inv) objective = cvx.Minimize(expression) #constraints = [h[0] == 0, h[-1]==0, h >= zeros_H, cvx.square(cvx.norm(h,2))<=1] constraints = [ h[0] == 0, h[-1] == 0, cvx.square(cvx.norm(h, 2)) <= 1] prob = cvx.Problem(objective, constraints) result = prob.solve(verbose=0, solver=cvx.CVXOPT) # Now we update the mean of h m_H_old = m_H Sigma_H_old = Sigma_H m_H = np.squeeze(np.array((h.value))) Sigma_H = np.zeros_like(Sigma_H) h_norm += [np.linalg.norm(m_H)] # print 'h_norm = ', h_norm # Plotting HRF if PLOT and ni >= 0: import matplotlib.pyplot as plt plt.figure(M + 1) plt.plot(m_H) plt.hold(True) else: if TrueHrfFlag: #TrueVal, head = read_volume(HrfFilename) TrueVal, head = read_volume(HrfFilename)[:, 0, 0, 0] print TrueVal print TrueVal.shape m_H = TrueVal # crit. h Crit_H = (np.linalg.norm(m_H - m_H1) / np.linalg.norm(m_H1)) ** 2 cH += [Crit_H] m_H1[:] = m_H[:] # crit. AH for d in xrange(0, D): AH[:, :, d] = m_A[:, :] * m_H[d] DIFF = np.reshape(AH - AH1, (M * J * D)) # To avoid numerical problems DIFF[np.where((DIFF < 1e-50) & (DIFF > 0.0))] = 0.0 # To avoid numerical problems DIFF[np.where((DIFF > -1e-50) & (DIFF < 0.0))] = 0.0 if np.linalg.norm(np.reshape(AH1, (M * J * D))) == 0: Crit_AH = 1000000000. else: Crit_AH = ( np.linalg.norm(DIFF) / np.linalg.norm(np.reshape(AH1, (M * J * D)))) ** 2 cAH += [Crit_AH] AH1[:, :, :] = AH[:, :, :] # Z labels if estimateLabels: logger.info("E Z step ...") # WARNING!!! ParsiMod gives better results, but we need the other # one. if MFapprox: UtilsC.expectation_Z(Sigma_A, m_A, sigma_M, Beta, Z_tilde, mu_M, q_Z, neighboursIndexes.astype( np.int32), M, J, K, maxNeighbours) if not MFapprox: UtilsC.expectation_Z_ParsiMod_RVM_and_CompMod( Sigma_A, m_A, sigma_M, Beta, mu_M, q_Z, neighboursIndexes.astype(np.int32), M, J, K, maxNeighbours) else: logger.info("Using True Z ...") TrueZ = read_volume(LabelsFilename) for m in xrange(M): q_Z[m, 1, :] = np.reshape(TrueZ[0][:, :, :, m], J) q_Z[m, 0, :] = 1 - q_Z[m, 1, :] # crit. Z val = np.reshape(q_Z, (M * K * J)) val[np.where((val <= 1e-50) & (val > 0.0))] = 0.0 DIFF = np.reshape(q_Z - q_Z1, (M * K * J)) # To avoid numerical problems DIFF[np.where((DIFF < 1e-50) & (DIFF > 0.0))] = 0.0 # To avoid numerical problems DIFF[np.where((DIFF > -1e-50) & (DIFF < 0.0))] = 0.0 if np.linalg.norm(np.reshape(q_Z1, (M * K * J))) == 0: Crit_Z = 1000000000. else: Crit_Z = ( np.linalg.norm(DIFF) / np.linalg.norm(np.reshape(q_Z1, (M * K * J)))) ** 2 cZ += [Crit_Z] q_Z1 = q_Z ##################### # MAXIMIZATION ##################### # HRF: Sigma_h if estimateHRF: if estimateSigmaH: logger.info("M sigma_H step ...") if gamma_h > 0: sigmaH = vt.maximization_sigmaH_prior( D, Sigma_H_old, R, m_H_old, gamma_h) else: sigmaH = vt.maximization_sigmaH(D, Sigma_H, R, m_H) logger.info('sigmaH = %s', str(sigmaH)) # (mu,sigma) logger.info("M (mu,sigma) step ...") mu_M, sigma_M = vt.maximization_mu_sigma( mu_M, sigma_M, q_Z, m_A, K, M, Sigma_A) for m in xrange(M): SUM_q_Z[m] += [sum(q_Z[m, 1, :])] mu1[m] += [mu_M[m, 1]] # Drift L UtilsC.maximization_L( Y, m_A, m_H, L, P, XX.astype(np.int32), J, D, M, Ndrift, N) PL = np.dot(P, L) y_tilde = Y - PL # Beta if estimateBeta: logger.info("estimating beta") for m in xrange(0, M): if MFapprox: Beta[m] = UtilsC.maximization_beta(beta, q_Z[m, :, :].astype(np.float64), Z_tilde[m, :, :].astype( np.float64), J, K, neighboursIndexes.astype(np.int32), gamma, maxNeighbours, MaxItGrad, gradientStep) if not MFapprox: #Beta[m] = UtilsC.maximization_beta(beta,q_Z[m,:,:].astype(np.float64),q_Z[m,:,:].astype(np.float64),J,K,neighboursIndexes.astype(int32),gamma,maxNeighbours,MaxItGrad,gradientStep) Beta[m] = UtilsC.maximization_beta_CB(beta, q_Z[m, :, :].astype( np.float64), J, K, neighboursIndexes.astype(np.int32), gamma, maxNeighbours, MaxItGrad, gradientStep) logger.info("End estimating beta") logger.info(Beta) # Sigma noise logger.info("M sigma noise step ...") UtilsC.maximization_sigma_noise( Gamma, PL, sigma_epsilone, Sigma_H, Y, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N) #### Computing Free Energy #### if ni > 0: FreeEnergy1 = FreeEnergy """FreeEnergy = vt.Compute_FreeEnergy(y_tilde,m_A,Sigma_A,mu_M,sigma_M, m_H,Sigma_H,R,Det_invR,sigmaH, p_Wtilde,q_Z,neighboursIndexes, maxNeighbours,Beta,sigma_epsilone, XX,Gamma,Det_Gamma,XGamma,J,D,M, N,K,S,"CompMod")""" FreeEnergy = vt.Compute_FreeEnergy(y_tilde, m_A, Sigma_A, mu_M, sigma_M, m_H, Sigma_H, R, Det_invR, sigmaH, p_Wtilde, tau1, tau2, q_Z, neighboursIndexes, maxNeighbours, Beta, sigma_epsilone, XX, Gamma, Det_Gamma, XGamma, J, D, M, N, K, S, "CompMod") if ni > 0: Crit_FreeEnergy = (FreeEnergy1 - FreeEnergy) / FreeEnergy1 FreeEnergy_Iter += [FreeEnergy] cFE += [Crit_FreeEnergy] # Update index ni += 1 t02 = time.time() cTime += [t02 - t1] t2 = time.time() ########################################################################## # PLOTS and SNR computation FreeEnergyArray = np.zeros((ni), dtype=np.float64) for i in xrange(ni): FreeEnergyArray[i] = FreeEnergy_Iter[i] SUM_q_Z_array = np.zeros((M, ni), dtype=np.float64) mu1_array = np.zeros((M, ni), dtype=np.float64) h_norm_array = np.zeros((ni), dtype=np.float64) for m in xrange(M): for i in xrange(ni): SUM_q_Z_array[m, i] = SUM_q_Z[m][i] mu1_array[m, i] = mu1[m][i] h_norm_array[i] = h_norm[i] if PLOT and 0: import matplotlib.pyplot as plt import matplotlib font = {'size': 15} matplotlib.rc('font', **font) plt.savefig('./HRF_Iter_CompMod.png') plt.hold(False) plt.figure(2) plt.plot(cAH[1:-1], 'lightblue') plt.hold(True) plt.plot(cFE[1:-1], 'm') plt.hold(False) #plt.legend( ('CA','CH', 'CZ', 'CAH', 'CFE') ) plt.legend(('CAH', 'CFE')) plt.grid(True) plt.savefig('./Crit_CompMod.png') plt.figure(3) plt.plot(FreeEnergyArray) plt.grid(True) plt.savefig('./FreeEnergy_CompMod.png') plt.figure(4) for m in xrange(M): plt.plot(SUM_q_Z_array[m]) plt.hold(True) plt.hold(False) #plt.legend( ('m=0','m=1', 'm=2', 'm=3') ) #plt.legend( ('m=0','m=1') ) plt.savefig('./Sum_q_Z_Iter_CompMod.png') plt.figure(5) for m in xrange(M): plt.plot(mu1_array[m]) plt.hold(True) plt.hold(False) plt.savefig('./mu1_Iter_CompMod.png') plt.figure(6) plt.plot(h_norm_array) plt.savefig('./HRF_Norm_CompMod.png') Data_save = xndarray(h_norm_array, ['Iteration']) Data_save.save('./HRF_Norm_Comp.nii') CompTime = t2 - t1 cTimeMean = CompTime / ni sigma_M = np.sqrt(np.sqrt(sigma_M)) logger.info("Nb iterations to reach criterion: %d", ni) logger.info("Computational time = %s min %s s", str( int(CompTime // 60)), str(int(CompTime % 60))) # print "Computational time = " + str(int( CompTime//60 ) ) + " min " + str(int(CompTime%60)) + " s" # print "sigma_H = " + str(sigmaH) logger.info('mu_M: %f', mu_M) logger.info('sigma_M: %f', sigma_M) logger.info("sigma_H = %s" + str(sigmaH)) logger.info("Beta = %s" + str(Beta)) StimulusInducedSignal = vt.computeFit(m_H, m_A, X, J, N) SNR = 20 * \ np.log( np.linalg.norm(Y) / np.linalg.norm(Y - StimulusInducedSignal - PL)) SNR /= np.log(10.) logger.info("SNR = %d", SNR) return ni, m_A, m_H, q_Z, sigma_epsilone, mu_M, sigma_M, Beta, L, PL, CONTRAST, CONTRASTVAR, cA[2:], cH[2:], cZ[2:], cAH[2:], cTime[2:], cTimeMean, Sigma_A, StimulusInducedSignal, FreeEnergyArray
r1 = np.sqrt(3.4 * np.random.rand(Nt1, 1)) # Radius t1 = 2 * np.pi * np.random.rand(Nt1, 1) # Angle testdata1 = np.concatenate((r1 * np.cos(t1), r1 * np.sin(t1)), axis=1) # Points r2 = np.sqrt(2.4 * np.random.rand(Nt2, 1)) # Radius t2 = 2 * np.pi * np.random.rand(Nt2, 1) # Angle testdata2 = np.concatenate((3 + r2 * np.cos(t2), r2 * np.sin(t2)), axis=1) # points ## training linear SVM based on CVX optimizer X = np.concatenate((data1, data2), axis=0) y = np.concatenate((np.ones((N1, 1)), -np.ones((N2, 1))), axis=0) w = cp.Variable((D, 1)) b = cp.Variable() objective = cp.Minimize(cp.sum(cp.square(w)) * 0.5) constraints = [cp.multiply(y, (X @ w + b)) >= 1] prob = cp.Problem(objective, constraints) prob.solve() print("status:", prob.status) print("optimal value", prob.value) print("optimal var w = {}, b = {}".format(w.value, b.value)) ## visualize decision boundary for training data d = 0.02 x1 = np.arange(np.min(X[:, 0]), np.max(X[:, 0]), d) x2 = np.arange(np.min(X[:, 1]), np.max(X[:, 1]), d) x1Grid, x2Grid = np.meshgrid(x1, x2) xGrid = np.stack((x1Grid.flatten('F'), x2Grid.flatten('F')), axis=1) scores1 = xGrid.dot(w.value) + b.value scores2 = -xGrid.dot(w.value) - b.value
X_normalized = MinMaxScaler().fit_transform(X.values) X = pd.DataFrame(X_normalized) relaxation = 100 points_per_agent = 200 var = 4 #defining van variables W = cp.Variable((var, 1)) b = cp.Variable() eps = cp.Variable((4000, 1), nonneg=True) loss = cp.sum(eps) reg = cp.square(cp.norm(W)) #cost function, kloopt waarschijnlijk niet opt = cp.Minimize(0.5 * reg + relaxation * loss) #loop om mijn agents te vullen met neighbours en X_train_total = np.zeros((1, 4)) X_test_total = np.zeros((1, 4)) y_test_total = np.zeros((1, 1)) y_train_total = np.zeros((1, 1)) for i in range(agents): X_train = X[400 * i:400 * i + 200] X_test = X[400 * i + 200:400 * (i + 1)] y_train = Y[400 * i:400 * i + 200]
def reachable(T, s0, sT, cost, dynamics, max_dtheta, max_theta, max_ddx, max_line_search=30, show_results=lambda *a: None): """Find control signals u_1...u_T, u_t=(ddx_t,dtheta_t) and the ensuing states s_1...s_T that minimize L(s_0,...,s_{T-1}) subject to s_t = f(s_{t-1}, u_t) | dtheta_t | < max_dtheta | ddx_t | < max_ddx One of sT or cost must be None. if sT is set, then L = || s_{t-1} - sT || otherwise, it is L = sum_{t=0}^{T-1} cost(s_t) Solves this as a Sequential Quadratic program by approximating L by a quadratic and f by an affine function. """ s0 = array(s0) if sT is not None: sT = array(sT) assert (cost is None) != (sT is None), 'only one of cost or sT may be specified' # initial iterates and objective terms Sv = [s0] * T Uv = [None] + [zeros(2)] * T L = None if cost: L = [cost(s0)['val']] * T last_obj = None # last objective value attained step = 1. # last line search step size iters = 0 n_line_searches = 0 while True: show_results( Sv, L, '%d, %d line searches so far. step size %g' % (iters, n_line_searches, step)) iters += 1 # variables, objective, and constraints of the quadratic problem S = [None] * T U = [None] * T S[0] = CX.Parameter(5, name='s0') S[0].value = s0 constraints = [] if cost: objective = zeros(1) # define the QP for t in xrange(1, T): # f(u_t, s_{t-1}) and its derivatives f = dynamics(Sv[t - 1], Uv[t], {'du', 'ds'}) dfds = vstack(f['ds']) dfdu = vstack(f['du']) # define u_t and s_t U[t] = CX.Variable(2, name='u%d' % t) S[t] = CX.Variable(5, name='s%d' % t) # constraints: # s_t = linearized f(s_t-1, u_t) about previous iterate # and bounds on s_t and u_t constraints += [ S[t] == f['val'] + dfds * (S[t - 1] - Sv[t - 1]) + dfdu * (U[t] - Uv[t]), CX.abs(U[t][0]) <= max_ddx, CX.abs(U[t][1]) <= max_dtheta, CX.abs(S[t][4]) <= max_theta ] if cost: # accumulate objective c = cost(Sv[t], derivs={'ds', 'ds2'}) c['ds2'] = make_psd(c['ds2']) objective += c['val'] + ( S[t] - Sv[t]).T * c['ds'] + 0.5 * CX.quad_form( S[t] - Sv[t], c['ds2']) if sT is not None: # objective is || s_t - sT || objective = CX.square(CX.norm(S[T - 1] - sT)) # solve for S and U p = CX.Problem(CX.Minimize(objective), constraints) r = p.solve(solver=CX.CVXOPT, verbose=False) assert isfinite(r) # line search on U, from Uv along U-Uv line_search_failed = True while n_line_searches < max_line_search: n_line_searches += 1 # compute and apply the controls along the step Us = [] Svs = [s0] for u, u0 in zip(U[1:], Uv[1:]): # a step along the search direction us = u0 + step * (ravel(u.value) - u0) # make it feasible us[0] = clip(us[0], -max_ddx, max_ddx) us[1] = clip(us[1], -max_dtheta, max_dtheta) Us.append(us) # apply controls Svs.append(sim.apply_control(Svs[-1], us)['val']) # objective value based on the last state if cost: L = [cost(s)['val'] for s in Svs] obj = sum(L) else: obj = sum((Svs[-1] - sT)**2) if last_obj is None or obj < last_obj: step *= 1.1 # lengthen the step for the next round line_search_failed = False # converged break else: step *= 0.7 # shorten the step and try again if line_search_failed: # converged break # throw away this iterate else: # accept the iterate Sv = Svs Uv = [None] + Us last_obj = obj return Sv, Uv
def test_readme_examples(self): import cvxopt import numpy # Problem data. m = 30 n = 20 A = cvxopt.normal(m, n) b = cvxopt.normal(m) # Construct the problem. x = cp.Variable(n) objective = cp.Minimize(sum(cp.square(A * x - b))) constraints = [0 <= x, x <= 1] p = cp.Problem(objective, constraints) # The optimal objective is returned by p.solve(). result = p.solve() # The optimal value for x is stored in x.value. print x.value # The optimal Lagrange multiplier for a constraint # is stored in constraint.dual_value. print constraints[0].dual_value #################################################### # Scalar variable. a = cp.Variable() # Column vector variable of length 5. x = cp.Variable(5) # Matrix variable with 4 rows and 7 columns. A = cp.Variable(4, 7) #################################################### # Positive scalar parameter. m = cp.Parameter(sign="positive") # Column vector parameter with unknown sign (by default). c = cp.Parameter(5) # Matrix parameter with negative entries. G = cp.Parameter(4, 7, sign="negative") # Assigns a constant value to G. G.value = -numpy.ones((4, 7)) # Raises an error for assigning a value with invalid sign. with self.assertRaises(Exception) as cm: G.value = numpy.ones((4, 7)) self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.") #################################################### a = cp.Variable() x = cp.Variable(5) # expr is an Expression object after each assignment. expr = 2 * x expr = expr - a expr = sum(expr) + cp.norm(x, 2) #################################################### import numpy as np import cvxopt from multiprocessing import Pool # Problem data. n = 10 m = 5 A = cvxopt.normal(n, m) b = cvxopt.normal(n) gamma = cp.Parameter(sign="positive") # Construct the problem. x = cp.Variable(m) objective = cp.Minimize( sum(cp.square(A * x - b)) + gamma * cp.norm(x, 1)) p = cp.Problem(objective) # Assign a value to gamma and find the optimal x. def get_x(gamma_value): gamma.value = gamma_value result = p.solve() return x.value gammas = np.logspace(-1, 2, num=2) # Serial computation. x_values = [get_x(value) for value in gammas] #################################################### n = 10 mu = cvxopt.normal(1, n) sigma = cvxopt.normal(n, n) sigma = sigma.T * sigma gamma = cp.Parameter(sign="positive") gamma.value = 1 x = cp.Variable(n) # Constants: # mu is the vector of expected returns. # sigma is the covariance matrix. # gamma is a Parameter that trades off risk and return. # Variables: # x is a vector of stock holdings as fractions of total assets. expected_return = mu * x risk = cp.quad_form(x, sigma) objective = cp.Maximize(expected_return - gamma * risk) p = cp.Problem(objective, [sum(x) == 1]) result = p.solve() # The optimal expected return. print expected_return.value # The optimal risk. print risk.value ########################################### N = 50 M = 40 n = 10 data = [] for i in range(N): data += [(1, cvxopt.normal(n, mean=1.0, std=2.0))] for i in range(M): data += [(-1, cvxopt.normal(n, mean=-1.0, std=2.0))] # Construct problem. gamma = cp.Parameter(sign="positive") gamma.value = 0.1 # 'a' is a variable constrained to have at most 6 non-zero entries. a = cp.Variable(n) #mi.SparseVar(n, nonzeros=6) b = cp.Variable() slack = [ cp.pos(1 - label * (sample.T * a - b)) for (label, sample) in data ] objective = cp.Minimize(cp.norm(a, 2) + gamma * sum(slack)) p = cp.Problem(objective) # Extensions can attach new solve methods to the CVXPY Problem class. #p.solve(method="admm") p.solve() # Count misclassifications. errors = 0 for label, sample in data: if label * (sample.T * a - b).value < 0: errors += 1 print "%s misclassifications" % errors print a.value print b.value
def test_sum_of_qccv_not_dqcp(self): t = cp.Variable(5, pos=True) expr = cp.sum(cp.square(t) / t) self.assertFalse(expr.is_dqcp())
v = npy.random.random((m, 1)) * 2 - 1 b = npy.dot(A, x_known) + v r = cvx.Variable(m, 1) x = cvx.Variable(n, 1) z = npy.zeros((n, 1)) a = .25 cst = [r == A * x - b] f, (ax) = plt.subplots(4, sharex=True) bins = npy.linspace(-2, 2, 81) + .025 bins = bins[:-1] for idx in [0, 1, 2, 3]: if idx == 0: fcn = cvx.sum_entries(cvx.abs(r)) elif idx == 1: fcn = cvx.sum_entries(cvx.square(r)) elif idx == 2: fcn = cvx.sum_entries( cvx.max_entries(cvx.hstack(cvx.abs(r) - a, npy.zeros((m, 1))), axis=1)) elif idx == 3: fcn = cvx.sum_entries(-cvx.log(1 - cvx.square(r))) else: print('bad') print(idx) prb = cvx.Problem(cvx.Minimize(fcn), cst) prb.solve() print(prb.status) ax[idx].hist(r.value, bins) plt.show()
#!/usr/bin/python import numpy as np import cvxpy as cvx from qcqp import * n, m = 10, 15 np.random.seed(1) A = np.random.randn(m, n) b = np.random.randn(m, 1) x = cvx.Variable(n) obj = cvx.sum_squares(A*x - b) cons = [cvx.square(x) == 1] prob = cvx.Problem(cvx.Minimize(obj), cons) qcqp = QCQP(prob) # sample from the semidefinite relaxation qcqp.suggest(SDR) print("SDR lower bound: %.3f" % qcqp.sdr_bound) f_cd, v_cd = qcqp.improve(COORD_DESCENT) x_cd = x.value print("Coordinate descent: objective %.3f, violation %.3f" % (f_cd, v_cd)) # SDR solution is cached and not solved again qcqp.suggest(SDR) f_dccp, v_dccp = qcqp.improve(DCCP) print("Penalty CCP: objective %.3f, violation %.3f" % (f_dccp, v_dccp)) f_dccp, v_dccp = qcqp.improve(COORD_DESCENT, phase1=False) x_dccp = x.value
def main(): # x[0] == x(k) x0, y0 = (2, 3) ux0, uy0 = (3, 4) (x_target, y_target) = (22, 25) steps = 50 delta_t = 0.1 x_limits, y_limits = (1, 30), (1, 30) ux_limits, uy_limits = (1, 10), (1, 10) x = cvx.Variable(steps) ux = cvx.Variable(steps - 1) constraint_x = [x[0] == x0, ux[0] == ux0] constraint_x += [ x[index + 1] == x[index] + delta_t * ux[index] for index in xrange(0, steps - 1) ] constraint_x += [ x >= x_limits[0], x <= x_limits[1], ux >= ux_limits[0], ux <= ux_limits[1] ] objective_xpath = cvx.Minimize(cvx.square(x_target - x[steps - 1])) delta_xvelocity = np.sum([(ux[index] - ux[index + 1])**2 for index in xrange(0, steps - 2)]) objective_xvelocity = cvx.Minimize((delta_xvelocity)) objective_x = objective_xpath + objective_xvelocity y = cvx.Variable(steps) uy = cvx.Variable(steps - 1) constraint_y = [y[0] == y0, uy[0] == uy0] constraint_y += [ y[index + 1] == y[index] + delta_t * uy[index] for index in xrange(0, steps - 1) ] constraint_y += [ y >= y_limits[0], y <= y_limits[1], uy >= uy_limits[0], uy <= uy_limits[1] ] objective_ypath = cvx.Minimize(cvx.square(y_target - y[steps - 1])) delta_yvelocity = np.sum([(uy[index] - uy[index + 1])**2 for index in xrange(0, steps - 2)]) objective_yvelocity = cvx.Minimize((delta_yvelocity)) objective_y = objective_ypath + objective_yvelocity constraints = constraint_x + constraint_y objective = objective_x + objective_y prob = cvx.Problem(objective, constraints) print 'DCP followed:', prob.is_dcp() prob.solve() print prob.status print 'cost function: ', np.round(prob.value, 5) print 'x coordinates: ', np.round(x.value, 3) print 'y coordinates:', np.round(y.value, 3) # print 'ux velocity: ', np.round(ux.value, 3) # print 'uy velocity: ', np.round(uy.value, 3) plt.figure(1) plt.subplot(211) plt.plot(x.value, y.value, 'ro', x0, y0, 'go', x_target, y_target, 'go') plt.xlabel('x coordinate') plt.ylabel('y coordinate') plt.subplot(212) velocity = np.array( [np.sqrt(vx**2 + vy**2) for vx, vy in zip(ux.value, uy.value)]) time = np.linspace(0.1, 5, 49) print 'velocity:', velocity plt.plot(time, velocity, 'bo', time[0], np.sqrt(ux0**2 + uy0**2), 'go') plt.xlabel('Time') plt.ylabel('Velocity') plt.show()
import cvxpy as cp w = cp.Variable(2) d = cp.Variable() probconst = ([2 * w[0] + w[1] + d >= 1, 0.8 * w[0] - 0.6 * w[1] + d <= -1]) probobj = cp.Minimize(0.5 * cp.square(cp.norm(w))) prob = cp.Problem(probobj, probconst) prob.solve() print(prob.value) print(w.value) print(d.value)
def q3_partd(A, b): lambdValues = [.0001, .001, .01, .1, .5, 1, 5, 10, 50, 100] indexArray = list(range(0, A.shape[0])) subsetList = [] subsets = [] classes = [] #PART i print('Runing q3 partd, section i') #Make 5 random subset index lists of size 29. for i in range(0, 5): subsetList.append([]) for m in range(0, 29): randi = rand.randint(0, len(indexArray) - 1) subsetList[i].append(indexArray[randi]) del indexArray[randi] #Make 5 random subset index lists of size 30. for i in range(5, 10): subsetList.append([]) for m in range(0, 30): randi = rand.randint(0, len(indexArray) - 1) subsetList[i].append(indexArray[randi]) del indexArray[randi] #Make actual data subsets out of the for i in range(0, len(subsetList)): subsets.append(A[subsetList[i]]) classes.append(b[subsetList[i]]) #PART ii (data sets 0 through 7) print('Runing q3 partd, section ii') Atrain = None btrain = None for i in range(0, 8): if Atrain == None: Atrain = subsets[i] btrain = classes[i] else: Atrain = np.vstack((Atrain, subsets[i])) btrain = np.vstack((btrain, classes[i])) # Set up the parameters of the equation for LASSO. lambd = cvx.Parameter(sign="positive") lambd.value = 0.01 x = cvx.Variable(A.shape[1]) objective = cvx.Minimize( cvx.sum_squares(Atrain * x - btrain) + (lambd * cvx.norm1(x))) prob = cvx.Problem(objective) optimalRidgeX = [] optimalLassoX = [] #Find optimal lasso x's for val in lambdValues: print('Solving lambda: %f' % (val)) # Solve the problem with the new lambda. lambd.value = val prob.solve() optimalLassoX.append(np.array(x.value)) #Find optimal ridge x's prob.objective = cvx.Minimize( cvx.sum_squares(Atrain * x - btrain) + (lambd * cvx.sum_entries(cvx.square(x)))) for val in lambdValues: print('Solving lambda: %f' % (val)) # Solve the problem with the new lambda. lambd.value = val prob.solve() optimalRidgeX.append(np.array(x.value)) #PART iii print('Runing q3 partd, section iii') Atuning = subsets[8] btuning = classes[8] bestLassoX = optimalLassoX[0] bestLassoAccuracy = 0 bestRidgeX = optimalRidgeX[0] bestRidgeAccuracy = 0 #Find best Lasso for xopt in optimalLassoX: ypredicted = [] for data in Atuning: predVal = data.reshape(1, data.size).dot(xopt.reshape(xopt.size, 1)) if predVal > 0: predVal = 1 else: predVal = -1 ypredicted.append(predVal) accuracy = calculateAccuracy(btuning, ypredicted) if accuracy > bestLassoAccuracy: bestLassoAccuracy = accuracy bestLassoX = xopt #Find best Ridge for xopt in optimalRidgeX: ypredicted = [] for data in Atuning: predVal = data.reshape(1, data.size).dot(xopt.reshape(xopt.size, 1)) if predVal > 0: predVal = 1 else: predVal = -1 ypredicted.append(predVal) accuracy = calculateAccuracy(btuning, ypredicted) if accuracy > bestRidgeAccuracy: bestRidgeAccuracy = accuracy bestRidgeX = xopt #PART iv print('Runing q3 partd, section iv') #Calculate test accuracy for LASSO. Atest = subsets[9] btest = classes[9] ypredicted = [] for data in Atest: predVal = data.reshape(1, data.size).dot( bestLassoX.reshape(bestLassoX.size, 1)) if predVal > 0: predVal = 1 else: predVal = -1 ypredicted.append(predVal) lassoAccuracy = calculateAccuracy(btest, ypredicted) #Calculate test accuracy for RIDGE. ypredicted = [] for data in Atest: predVal = data.reshape(1, data.size).dot( bestRidgeX.reshape(bestRidgeX.size, 1)) if predVal > 0: predVal = 1 else: predVal = -1 ypredicted.append(predVal) ridgeAccuracy = calculateAccuracy(btest, ypredicted) print('Ridge test accuracy: %f' % (ridgeAccuracy)) print('LASSO test accuracy: %f' % (lassoAccuracy))
import cvxpy as cvx # Create two scalar optimization variables. x = cvx.Variable() y = cvx.Variable() constraints = [x + y == 1, x - y >= 1] # Form objective. obj = cvx.Minimize(cvx.square(x - y)) # Form and solve problem. prob = cvx.Problem(obj, constraints) prob.solve() # Returns the optimal value. print("status:", prob.status) print("optimal value", prob.value) print("optimal var", x.value, y.value)
def lasso_sure_cvxpy(X, y, alpha, sigma, random_state=42): # lambda_alpha = [alpha, alpha] n_samples, n_features = X.shape epsilon = 2 * sigma / n_samples**0.3 rng = check_random_state(random_state) delta = rng.randn(n_samples) y2 = y + epsilon * delta Xth, yth, y2th, deltath = map(torch.from_numpy, [X, y, y2, delta]) # set up variables and parameters beta_cp = cp.Variable(n_features) lambda_cp = cp.Parameter(nonneg=True) # set up objective loss = ((1 / (2 * n_samples)) * cp.sum(cp.square(Xth @ beta_cp - yth))) reg = lambda_cp * cp.norm1(beta_cp) objective = loss + reg # define problem problem1 = cp.Problem(cp.Minimize(objective)) assert problem1.is_dpp() # solve problem1 layer = CvxpyLayer(problem1, [lambda_cp], [beta_cp]) alpha_th1 = torch.tensor(alpha, requires_grad=True) beta1, = layer(alpha_th1) # get test loss and it's gradient test_loss1 = (Xth @ beta1 - yth).pow(2).sum() test_loss1 -= 2 * sigma**2 / epsilon * (Xth @ beta1) @ deltath test_loss1.backward() val1 = test_loss1.detach().numpy() grad1 = np.array(alpha_th1.grad) # set up variables and parameters beta_cp = cp.Variable(n_features) lambda_cp = cp.Parameter(nonneg=True) # set up objective loss = ((1 / (2 * n_samples)) * cp.sum(cp.square(Xth @ beta_cp - y2th))) reg = lambda_cp * cp.norm1(beta_cp) objective = loss + reg # define problem problem2 = cp.Problem(cp.Minimize(objective)) assert problem2.is_dpp() # solve problem2 layer = CvxpyLayer(problem2, [lambda_cp], [beta_cp]) alpha_th2 = torch.tensor(alpha, requires_grad=True) beta2, = layer(alpha_th2) # get test loss and it's gradient test_loss2 = 2 * sigma**2 / epsilon * (Xth @ beta2) @ deltath test_loss2.backward() val2 = test_loss2.detach().numpy() grad2 = np.array(alpha_th2.grad) val = val1 + val2 - len(y) * sigma**2 grad = grad1 + grad2 return val, grad
def sparse_kmeans(AllDataMatrix, s, niter, group): w = [1 / np.sqrt(AllDataMatrix.shape[1])] * AllDataMatrix.shape[1] wx = np.zeros((len(AllDataMatrix), AllDataMatrix.shape[1])) for j in range(AllDataMatrix.shape[1]): wx[:, j] = AllDataMatrix[:, j] * (np.sqrt(w)[j]) alpha_group = s s_orig = s nclust = 6 #kmt = KMeans(n_clusters=nclust, init='random', n_init=100,verbose=False, tol=0.0000000001) #kmt.fit(wx) #print kmt.labels_ #print adjusted_rand_score(labels, kmt.labels_) kmt = rkmeans(x=numpy2ri(wx), centers=nclust, nstart=100) kmlabels = np.array(kmt[0]) print adjusted_rand_score(labels, np.array(kmt[0])) #overall iterations for i in range(niter): #print i #1.get bcssj aj_list = [] for j in range(AllDataMatrix.shape[1]): dat_j = AllDataMatrix[:, j].reshape((len(AllDataMatrix), 1)) djall = euclidean_distances(dat_j, dat_j) sumd_all = np.sum(djall**2) / len(AllDataMatrix) nk_list = [] sumd_k_list = [] for k in range(nclust): dat_j = AllDataMatrix[kmlabels == k, j] dat_j = dat_j.reshape((len(dat_j), 1)) if (len(dat_j) < 1): d = 0 else: d = euclidean_distances(dat_j, dat_j) nk = len(dat_j) sumd_k = np.sum(d**2) nk_list.append(nk) sumd_k_list.append(sumd_k) nk_list = np.array(nk_list) sumd_k_list = np.array(sumd_k_list) #compute within-sum of squares over feature j nk_list[nk_list == 0] = -1 one_nk_list = 1. / nk_list one_nk_list[np.sign(one_nk_list) == -1] = 0 withinssj = np.sum(one_nk_list * sumd_k_list) #aj = totalss/n - wss/nk aj = sumd_all - withinssj aj_list.append(aj) #2. get w a = np.array(aj_list) lenseq = np.array([256, 128, 64, 32, 16, 8, 4, 2, 1, 1]) lenseq = np.array([256, 128, 64, 32, 16, 8, 8]) nlevels = len(lenseq) sqrtlenseq = np.sqrt(lenseq) indseq = np.cumsum(lenseq) wvar = cvx.Variable(len(a)) t = cvx.Variable(nlevels) ## Form objective. if group: ####GROUP SPARSE #obj = cvx.Minimize(sum(-1*a*wvar) + alpha_group*sum(t)) obj = cvx.Minimize(sum(-1 * a * wvar)) group0 = [cvx.norm(wvar[0:(indseq[0] - 1)], 2) <= t[0]] group1 = [cvx.norm(wvar[indseq[0]:(indseq[1])], 2) <= t[1]] group2 = [cvx.norm(wvar[indseq[1]:(indseq[2])], 2) <= t[2]] group3 = [cvx.norm(wvar[indseq[2]:(indseq[3])], 2) <= t[3]] group4 = [cvx.norm(wvar[indseq[3]:(indseq[4])], 2) <= t[4]] group5 = [cvx.norm(wvar[indseq[4]:(indseq[5])], 2) <= t[5]] group6 = [cvx.norm(wvar[indseq[5]:(indseq[6])], 2) <= t[6]] # group0 = [cvx.norm(wvar[0:indseq[0]],2)<=t[0]] # group1 = [cvx.norm(wvar[indseq[0]:indseq[1]],2)<=t[1]] # group0 = [sqrtlenseq[0]*cvx.norm(wvar[0:(indseq[0]-1)],2)<=t[0]] # group1 = [sqrtlenseq[1]*cvx.norm(wvar[indseq[0]:(indseq[1])],2)<=t[1]] # group2 = [sqrtlenseq[2]*cvx.norm(wvar[indseq[1]:(indseq[2])],2)<=t[2]] # group3 = [sqrtlenseq[3]*cvx.norm(wvar[indseq[2]:(indseq[3])],2)<=t[3]] # group4 = [sqrtlenseq[4]*cvx.norm(wvar[indseq[3]:(indseq[4])],2)<=t[4]] # group5 = [sqrtlenseq[5]*cvx.norm(wvar[indseq[4]:(indseq[5])],2)<=t[5]] # group6 = [sqrtlenseq[6]*cvx.norm(wvar[indseq[5]:(indseq[6])],2)<=t[6]] # #group7 = [cvx.norm(wvar[indseq[6]:(indseq[7])],2)<=t[7]] # group8 = [cvx.norm(wvar[indseq[7]:(indseq[8])],2)<=t[8]] # group9 = [cvx.norm(wvar[indseq[8]:(indseq[9])],2)<=t[9]] ###"correct" constraints #constr = [wvar>=0,sum(wvar)==1] + group0 + group1 + group2 + group3 + group4 + group5 + group6 ##l2 constraints #constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 + group7 + group8 + group9 #constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 + group7 constr = [ cvx.square(cvx.norm2(wvar)) <= 1, wvar >= 0 ] + group0 + group1 + group2 + group3 + group4 + group5 + group6 constr = constr + [sum(t) <= alpha_group] #cvx.norm1(wvar)<=s_orig ####GROUP NORM AS IN LASSO # groupnormvec = [cvx.norm(wvar[0:(indseq[0]-1)],2),cvx.norm(wvar[indseq[0]:(indseq[1])],2), # cvx.norm(wvar[indseq[1]:(indseq[2])],2),cvx.norm(wvar[indseq[2]:(indseq[3])],2), # cvx.norm(wvar[indseq[3]:(indseq[4])],2),cvx.norm(wvar[indseq[4]:(indseq[5])],2), # cvx.norm(wvar[indseq[5]:(indseq[6])],2)] # obj = cvx.Minimize(sum(-1*a*wvar) + alpha_group*sum(groupnormvec)) # constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] else: ####ORIGINAL SPARSE KMEANS PROBLEM #obj = cvx.Minimize(cvx.sum(cvx.mul_elemwise(-1*a,wvar))) obj = cvx.Minimize(sum(-1 * a * wvar)) constr = [ cvx.square(cvx.norm2(wvar)) <= 1, cvx.norm1(wvar) <= s_orig, wvar >= 0 ] #constr = [cvx.square(cvx.norm2(wvar))<=1, wvar>=0] prob = cvx.Problem(obj, constr) #prob.solve() try: prob.solve() #print "default solver" except: #print "SCS SOLVER" #prob.solve(solver =cvx.CVXOPT) prob.solve(solver=cvx.SCS, verbose=False) #use_indirect=True #print prob.value w = wvar.value #3. update kmeans wx = np.zeros((len(AllDataMatrix), AllDataMatrix.shape[1])) for j in range(AllDataMatrix.shape[1]): wj = np.sqrt(w[j][0, 0]) #wj = w[j][0,0] if np.isnan(wj): #print "bad" wj = 10**-20 # else: # #print "yes" # #print wj wx[:, j] = AllDataMatrix[:, j] * wj # kmt = KMeans(n_clusters=nclust, init='random', n_init=100,verbose=False,tol=0.0000000001) # kmt.fit(wx) kmt = rkmeans(x=numpy2ri(wx), centers=nclust, nstart=100) kmlabels = np.array(kmt[0]) return prob.value, kmlabels
x[2 * k + 2, :] - x[2 * k, :] == (linf(np_x[2 * k, :], np_u[k, :], x[2 * k, :], u[k, :]) + 4 * linf(np_x[2 * k + 1, :], (np_u[k, :] + np_u[k + 1, :]) / 2, x[2 * k + 1], (u[k, :] + u[k + 1, :]) / 2) + linf(np_x[2 * k + 2, :], np_u[k + 1, :], x[2 * k + 2], u[k, :])) * dt / 6 ] #const += [x[k+1,:] - x[k,:] == 0.5*dt*(linf(np_x[k+1,:],np_u[k+1,:],x[k+1,:],u[k+1,:])+linf(np_x[k,:],np_u[k,:],x[k,:],u[k,:]))] cost = cost + 0.5 * cvx.huber( x[k, 0] - np.pi, M=0.5) + 0.01 * cvx.huber( u[k, :]) + 0.5 * cvx.huber(x[k, 1] - 0, M=0.25) cost = cost + 100 * cvx.square(x[K, 0] - np.pi) + 100 * cvx.square(x[K, 1] - 0) objective = cvx.Minimize(cost) print("Iteration", j + 1) prob = cvx.Problem(objective, const) sol = prob.solve(verbose=True) np_x = x.value np_u = u.value #print((np_x)) # print(np.shape(np_u)) plt.plot(np_x[:, 0]) plt.plot(np_x[:, 1]) plt.plot(np_u[:, 0]) plt.show()
def cost(self): p = self.terminals[0].power_var return self.alpha * cvx.square(p) - self.beta * p + self.gamma
def reachable(T, s0, sT, cost, dynamics, max_dtheta, max_theta, max_ddx, max_line_search=30, show_results=lambda *a:None): """Find control signals u_1...u_T, u_t=(ddx_t,dtheta_t) and the ensuing states s_1...s_T that minimize L(s_0,...,s_{T-1}) subject to s_t = f(s_{t-1}, u_t) | dtheta_t | < max_dtheta | ddx_t | < max_ddx One of sT or cost must be None. if sT is set, then L = || s_{t-1} - sT || otherwise, it is L = sum_{t=0}^{T-1} cost(s_t) Solves this as a Sequential Quadratic program by approximating L by a quadratic and f by an affine function. """ s0 = array(s0) if sT is not None: sT = array(sT) assert (cost is None) != (sT is None), 'only one of cost or sT may be specified' # initial iterates and objective terms Sv = [s0] * T Uv = [None] + [zeros(2)]*T L = None if cost: L = [cost(s0)['val']] * T last_obj = None # last objective value attained step = 1. # last line search step size iters = 0 n_line_searches = 0 while True: show_results(Sv, L, '%d, %d line searches so far. step size %g'%(iters, n_line_searches, step)) iters += 1 # variables, objective, and constraints of the quadratic problem S = [None] * T U = [None] * T S[0] = CX.Parameter(5, name='s0') S[0].value = s0 constraints = [] if cost: objective = zeros(1) # define the QP for t in xrange(1,T): # f(u_t, s_{t-1}) and its derivatives f = dynamics(Sv[t-1], Uv[t], {'du','ds'}) dfds = vstack(f['ds']) dfdu = vstack(f['du']) # define u_t and s_t U[t] = CX.Variable(2, name='u%d'%t) S[t] = CX.Variable(5, name='s%d'%t) # constraints: # s_t = linearized f(s_t-1, u_t) about previous iterate # and bounds on s_t and u_t constraints += [ S[t] == f['val'] + dfds*(S[t-1]-Sv[t-1]) + dfdu*(U[t]-Uv[t]), CX.abs(U[t][0]) <= max_ddx, CX.abs(U[t][1]) <= max_dtheta, CX.abs(S[t][4]) <= max_theta ] if cost: # accumulate objective c = cost(Sv[t], derivs={'ds','ds2'}) c['ds2'] = make_psd(c['ds2']) objective += c['val'] + (S[t]-Sv[t]).T*c['ds'] + 0.5*CX.quad_form(S[t]-Sv[t], c['ds2']) if sT is not None: # objective is || s_t - sT || objective = CX.square(CX.norm(S[T-1] - sT)) # solve for S and U p = CX.Problem(CX.Minimize(objective), constraints) r = p.solve(solver=CX.CVXOPT, verbose=False) assert isfinite(r) # line search on U, from Uv along U-Uv line_search_failed = True while n_line_searches < max_line_search: n_line_searches += 1 # compute and apply the controls along the step Us = [] Svs = [s0] for u,u0 in zip(U[1:],Uv[1:]): # a step along the search direction us = u0 + step * (ravel(u.value)-u0) # make it feasible us[0] = clip(us[0], -max_ddx, max_ddx) us[1] = clip(us[1], -max_dtheta, max_dtheta) Us.append(us) # apply controls Svs.append( sim.apply_control(Svs[-1], us)['val'] ) # objective value based on the last state if cost: L = [ cost(s)['val'] for s in Svs ] obj = sum(L) else: obj = sum((Svs[-1]-sT)**2) if last_obj is None or obj < last_obj: step *= 1.1 # lengthen the step for the next round line_search_failed = False # converged break else: step *= 0.7 # shorten the step and try again if line_search_failed: # converged break # throw away this iterate else: # accept the iterate Sv = Svs Uv = [None] + Us last_obj = obj return Sv,Uv
def flow_x_travel_time(self, link, flow): return square(flow) * self.a + self.b * flow
def loss_x4(x, y, rho): norm = cp.norm(y - x[0], p=2, axis=0) return 1 / 2 * rho * cp.square(norm)
delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) writer.writerow( [epoch, epoch_w, loss_l_cum, loss_u_cum, loss_ent_cum]) # update unlabel estimates with cumulative loss: target_var_list = [{'params': estimated_y, 'lr': ETA_Y}] optimizer_y = optim.SGD(target_var_list, momentum=0, weight_decay=0) optimizer_y.step() optimizer_y.zero_grad() estimated_y.grad.data.zero_() # Project onto probability polytope est_y_num = estimated_y.data.cpu().numpy() U = cvxpy.Variable((est_y_num.shape[0], est_y_num.shape[1])) objective = cvxpy.Minimize(cvxpy.sum(cvxpy.square(U - est_y_num))) constraints = [U >= EPSILON, cvxpy.sum(U, axis=1) == 1] prob = cvxpy.Problem(objective, constraints) prob.solve() estimated_y.data = torch.from_numpy(np.float32(U.value)).cuda() # save estimated labels and labeled indexes on unlabeled data: state = { "lab_inds": lab_inds, "estimated_y": estimated_y.data.cpu().numpy(), "epoch": epoch, "y_acc": y_acc, } np.save(Y_U_FOLDER + file_name + '.npy', state)
def Main_vbjde_Extension_constrained_stable(graph, Y, Onsets, Thrf, K, TR, beta, dt, scale=1, estimateSigmaH=True, sigmaH=0.05, NitMax=-1, NitMin=1, estimateBeta=True, PLOT=False, contrasts=[], computeContrast=False, gamma_h=0): """ Version modified by Lofti from Christine's version """ logger.info( "Fast EM with C extension started ... Here is the stable version !") np.random.seed(6537546) # Initialize parameters S = 100 if NitMax < 0: NitMax = 100 gamma = 7.5 # 7.5 gradientStep = 0.003 MaxItGrad = 200 Thresh = 1e-5 # Initialize sizes vectors D = np.int(np.ceil(Thrf / dt)) + 1 M = len(Onsets) N = Y.shape[0] J = Y.shape[1] l = np.int(np.sqrt(J)) condition_names = [] # Neighbours maxNeighbours = max([len(nl) for nl in graph]) neighboursIndexes = np.zeros((J, maxNeighbours), dtype=np.int32) neighboursIndexes -= 1 for i in xrange(J): neighboursIndexes[i, :len(graph[i])] = graph[i] # Conditions X = OrderedDict([]) for condition, Ons in Onsets.iteritems(): X[condition] = vt.compute_mat_X_2(N, TR, D, dt, Ons) condition_names += [condition] XX = np.zeros((M, N, D), dtype=np.int32) nc = 0 for condition, Ons in Onsets.iteritems(): XX[nc, :, :] = X[condition] nc += 1 # Covariance matrix order = 2 D2 = vt.buildFiniteDiffMatrix(order, D) R = np.dot(D2, D2) / pow(dt, 2 * order) invR = np.linalg.inv(R) Det_invR = np.linalg.det(invR) Gamma = np.identity(N) Det_Gamma = np.linalg.det(Gamma) Crit_H = 1 Crit_Z = 1 Crit_A = 1 Crit_AH = 1 AH = np.zeros((J, M, D), dtype=np.float64) AH1 = np.zeros((J, M, D), dtype=np.float64) Crit_FreeEnergy = 1 cTime = [] cA = [] cH = [] cZ = [] cAH = [] CONTRAST = np.zeros((J, len(contrasts)), dtype=np.float64) CONTRASTVAR = np.zeros((J, len(contrasts)), dtype=np.float64) Q_barnCond = np.zeros((M, M, D, D), dtype=np.float64) XGamma = np.zeros((M, D, N), dtype=np.float64) m1 = 0 for k1 in X: # Loop over the M conditions m2 = 0 for k2 in X: Q_barnCond[m1, m2, :, :] = np.dot( np.dot(X[k1].transpose(), Gamma), X[k2]) m2 += 1 XGamma[m1, :, :] = np.dot(X[k1].transpose(), Gamma) m1 += 1 sigma_epsilone = np.ones(J) logger.info( "Labels are initialized by setting active probabilities to ones ...") q_Z = np.zeros((M, K, J), dtype=np.float64) q_Z[:, 1, :] = 1 q_Z1 = np.zeros((M, K, J), dtype=np.float64) Z_tilde = q_Z.copy() TT, m_h = getCanoHRF(Thrf, dt) # TODO: check m_h = m_h[:D] m_H = np.array(m_h).astype(np.float64) m_H1 = np.array(m_h) sigmaH1 = sigmaH Sigma_H = np.ones((D, D), dtype=np.float64) Beta = beta * np.ones((M), dtype=np.float64) P = vt.PolyMat(N, 4, TR) L = vt.polyFit(Y, TR, 4, P) PL = np.dot(P, L) y_tilde = Y - PL Ndrift = L.shape[0] sigma_M = np.ones((M, K), dtype=np.float64) sigma_M[:, 0] = 0.5 sigma_M[:, 1] = 0.6 mu_M = np.zeros((M, K), dtype=np.float64) for k in xrange(1, K): mu_M[:, k] = 1 # InitMean Sigma_A = np.zeros((M, M, J), np.float64) for j in xrange(0, J): Sigma_A[:, :, j] = 0.01 * np.identity(M) m_A = np.zeros((J, M), dtype=np.float64) m_A1 = np.zeros((J, M), dtype=np.float64) for j in xrange(0, J): for m in xrange(0, M): for k in xrange(0, K): m_A[j, m] += np.random.normal(mu_M[m, k], np.sqrt(sigma_M[m, k])) * q_Z[m, k, j] m_A1 = m_A t1 = time.time() ########################################################################## # VBJDE num. iter. minimum ni = 0 while ((ni < NitMin + 1) or ((Crit_AH > Thresh) and (ni < NitMax))): logger.info("------------------------------ Iteration n° " + str(ni + 1) + " ------------------------------") ##################### # EXPECTATION ##################### # A logger.info("E A step ...") UtilsC.expectation_A(q_Z, mu_M, sigma_M, PL, sigma_epsilone, Gamma, Sigma_H, Y, y_tilde, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N, K) # crit. A DIFF = np.reshape(m_A - m_A1, (M * J)) Crit_A = ( np.linalg.norm(DIFF) / np.linalg.norm(np.reshape(m_A1, (M * J)))) ** 2 cA += [Crit_A] m_A1[:, :] = m_A[:, :] # HRF h UtilsC.expectation_H(XGamma, Q_barnCond, sigma_epsilone, Gamma, R, Sigma_H, Y, y_tilde, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N, scale, sigmaH) #m_H[0] = 0 #m_H[-1] = 0 # Constrain with optimization strategy import cvxpy as cvx m, n = Sigma_H.shape Sigma_H_inv = np.linalg.inv(Sigma_H) zeros_H = np.zeros_like(m_H[:, np.newaxis]) # Construct the problem. PRIMAL h = cvx.Variable(n) expression = cvx.quad_form(h - m_H[:, np.newaxis], Sigma_H_inv) objective = cvx.Minimize(expression) #constraints = [h[0] == 0, h[-1]==0, h >= zeros_H, cvx.square(cvx.norm(h,2))<=1] constraints = [h[0] == 0, h[-1] == 0, cvx.square(cvx.norm(h, 2)) <= 1] prob = cvx.Problem(objective, constraints) result = prob.solve(verbose=0, solver=cvx.CVXOPT) # Now we update the mean of h m_H_old = m_H Sigma_H_old = Sigma_H m_H = np.squeeze(np.array((h.value))) Sigma_H = np.zeros_like(Sigma_H) # and the norm h_norm += [np.linalg.norm(m_H)] # crit. h Crit_H = (np.linalg.norm(m_H - m_H1) / np.linalg.norm(m_H1)) ** 2 cH += [Crit_H] m_H1[:] = m_H[:] # crit. AH for d in xrange(0, D): AH[:, :, d] = m_A[:, :] * m_H[d] DIFF = np.reshape(AH - AH1, (M * J * D)) Crit_AH = (np.linalg.norm( DIFF) / (np.linalg.norm(np.reshape(AH1, (M * J * D))) + eps)) ** 2 cAH += [Crit_AH] AH1[:, :, :] = AH[:, :, :] # Z labels logger.info("E Z step ...") UtilsC.expectation_Z(Sigma_A, m_A, sigma_M, Beta, Z_tilde, mu_M, q_Z, neighboursIndexes.astype(np.int32), M, J, K, maxNeighbours) # crit. Z DIFF = np.reshape(q_Z - q_Z1, (M * K * J)) Crit_Z = (np.linalg.norm(DIFF) / (np.linalg.norm(np.reshape(q_Z1, (M * K * J))) + eps)) ** 2 cZ += [Crit_Z] q_Z1[:, :, :] = q_Z[:, :, :] ##################### # MAXIMIZATION ##################### # HRF: Sigma_h if estimateSigmaH: logger.info("M sigma_H step ...") if gamma_h > 0: sigmaH = vt.maximization_sigmaH_prior( D, Sigma_H, R, m_H, gamma_h) else: sigmaH = vt.maximization_sigmaH(D, Sigma_H, R, m_H) logger.info('sigmaH = %s', str(sigmaH)) # (mu,sigma) logger.info("M (mu,sigma) step ...") mu_M, sigma_M = vt.maximization_mu_sigma( mu_M, sigma_M, q_Z, m_A, K, M, Sigma_A) # Drift L UtilsC.maximization_L( Y, m_A, m_H, L, P, XX.astype(np.int32), J, D, M, Ndrift, N) PL = np.dot(P, L) y_tilde = Y - PL # Beta if estimateBeta: logger.info("estimating beta") for m in xrange(0, M): Beta[m] = UtilsC.maximization_beta(beta, q_Z[m, :, :].astype(np.float64), Z_tilde[m, :, :].astype( np.float64), J, K, neighboursIndexes.astype(np.int32), gamma, maxNeighbours, MaxItGrad, gradientStep) logger.info("End estimating beta") logger.info(Beta) # Sigma noise logger.info("M sigma noise step ...") UtilsC.maximization_sigma_noise( Gamma, PL, sigma_epsilone, Sigma_H, Y, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N) t02 = time.time() cTime += [t02 - t1] t2 = time.time() ########################################################################## # PLOTS and SNR computation if PLOT and 0: font = {'size': 15} matplotlib.rc('font', **font) savefig('./HRF_Iter_CompMod.png') hold(False) figure(2) plot(cAH[1:-1], 'lightblue') hold(True) plot(cFE[1:-1], 'm') hold(False) legend(('CAH', 'CFE')) grid(True) savefig('./Crit_CompMod.png') figure(3) plot(FreeEnergyArray) grid(True) savefig('./FreeEnergy_CompMod.png') figure(4) for m in xrange(M): plot(SUM_q_Z_array[m]) hold(True) hold(False) savefig('./Sum_q_Z_Iter_CompMod.png') figure(5) for m in xrange(M): plot(mu1_array[m]) hold(True) hold(False) savefig('./mu1_Iter_CompMod.png') figure(6) plot(h_norm_array) savefig('./HRF_Norm_CompMod.png') Data_save = xndarray(h_norm_array, ['Iteration']) Data_save.save('./HRF_Norm_Comp.nii') CompTime = t2 - t1 cTimeMean = CompTime / ni """ Norm = np.linalg.norm(m_H) m_H /= Norm Sigma_H /= Norm**2 sigmaH /= Norm**2 m_A *= Norm Sigma_A *= Norm**2 mu_M *= Norm sigma_M *= Norm**2 sigma_M = np.sqrt(np.sqrt(sigma_M)) """ logger.info("Nb iterations to reach criterion: %d", ni) logger.info("Computational time = %s min %s s", str( np.int(CompTime // 60)), str(np.int(CompTime % 60))) logger.info('mu_M: %f', mu_M) logger.info('sigma_M: %f', sigma_M) logger.info("sigma_H = %s", str(sigmaH)) logger.info("Beta = %s", str(Beta)) StimulusInducedSignal = vt.computeFit(m_H, m_A, X, J, N) SNR = 20 * \ np.log( np.linalg.norm(Y) / np.linalg.norm(Y - StimulusInducedSignal - PL)) SNR /= np.log(10.) print 'SNR comp =', SNR return ni, m_A, m_H, q_Z, sigma_epsilone, mu_M, sigma_M, Beta, L, PL, CONTRAST, CONTRASTVAR, cA[2:], cH[2:], cZ[2:], cAH[2:], cTime[2:], cTimeMean, Sigma_A, StimulusInducedSignal
def test_intro(self): """Test examples from cvxpy.org introduction. """ import numpy # Problem data. m = 30 n = 20 numpy.random.seed(1) A = numpy.random.randn(m, n) b = numpy.random.randn(m, 1) # Construct the problem. x = Variable(n) objective = Minimize(sum_squares(A * x - b)) constraints = [0 <= x, x <= 1] prob = Problem(objective, constraints) # The optimal objective is returned by p.solve(). result = prob.solve() # The optimal value for x is stored in x.value. print(x.value) # The optimal Lagrange multiplier for a constraint # is stored in constraint.dual_value. print(constraints[0].dual_value) ######################################## # Create two scalar variables. x = Variable() y = Variable() # Create two constraints. constraints = [x + y == 1, x - y >= 1] # Form objective. obj = Minimize(square(x - y)) # Form and solve problem. prob = Problem(obj, constraints) prob.solve() # Returns the optimal value. print("status:", prob.status) print("optimal value", prob.value) print("optimal var", x.value, y.value) ######################################## import cvxpy as cvx # Create two scalar variables. x = cvx.Variable() y = cvx.Variable() # Create two constraints. constraints = [x + y == 1, x - y >= 1] # Form objective. obj = cvx.Minimize(cvx.square(x - y)) # Form and solve problem. prob = cvx.Problem(obj, constraints) prob.solve() # Returns the optimal value. print("status:", prob.status) print("optimal value", prob.value) print("optimal var", x.value, y.value) self.assertEqual(prob.status, OPTIMAL) self.assertAlmostEqual(prob.value, 1.0) self.assertAlmostEqual(x.value, 1.0) self.assertAlmostEqual(y.value, 0) ######################################## # Replace the objective. prob.objective = Maximize(x + y) print("optimal value", prob.solve()) self.assertAlmostEqual(prob.value, 1.0) # Replace the constraint (x + y == 1). prob.constraints[0] = (x + y <= 3) print("optimal value", prob.solve()) self.assertAlmostEqual(prob.value, 3.0) ######################################## x = Variable() # An infeasible problem. prob = Problem(Minimize(x), [x >= 1, x <= 0]) prob.solve() print("status:", prob.status) print("optimal value", prob.value) self.assertEqual(prob.status, INFEASIBLE) self.assertAlmostEqual(prob.value, np.inf) # An unbounded problem. prob = Problem(Minimize(x)) prob.solve() print("status:", prob.status) print("optimal value", prob.value) self.assertEqual(prob.status, UNBOUNDED) self.assertAlmostEqual(prob.value, -np.inf) ######################################## # A scalar variable. a = Variable() # Column vector variable of length 5. x = Variable(5) # Matrix variable with 4 rows and 7 columns. A = Variable(4, 7) ######################################## import numpy # Problem data. m = 10 n = 5 numpy.random.seed(1) A = numpy.random.randn(m, n) b = numpy.random.randn(m, 1) # Construct the problem. x = Variable(n) objective = Minimize(sum_entries(square(A * x - b))) constraints = [0 <= x, x <= 1] prob = Problem(objective, constraints) print("Optimal value", prob.solve()) print("Optimal var") print(x.value) # A numpy matrix. self.assertAlmostEqual(prob.value, 4.14133859146) ######################################## # Positive scalar parameter. m = Parameter(sign="positive") # Column vector parameter with unknown sign (by default). c = Parameter(5) # Matrix parameter with negative entries. G = Parameter(4, 7, sign="negative") # Assigns a constant value to G. G.value = -numpy.ones((4, 7)) ######################################## # Create parameter, then assign value. rho = Parameter(sign="positive") rho.value = 2 # Initialize parameter with a value. rho = Parameter(sign="positive", value=2) ######################################## import numpy # Problem data. n = 15 m = 10 numpy.random.seed(1) A = numpy.random.randn(n, m) b = numpy.random.randn(n, 1) # gamma must be positive due to DCP rules. gamma = Parameter(sign="positive") # Construct the problem. x = Variable(m) error = sum_squares(A * x - b) obj = Minimize(error + gamma * norm(x, 1)) prob = Problem(obj) # Construct a trade-off curve of ||Ax-b||^2 vs. ||x||_1 sq_penalty = [] l1_penalty = [] x_values = [] gamma_vals = numpy.logspace(-4, 6) for val in gamma_vals: gamma.value = val prob.solve() # Use expr.value to get the numerical value of # an expression in the problem. sq_penalty.append(error.value) l1_penalty.append(norm(x, 1).value) x_values.append(x.value) ######################################## import numpy X = Variable(5, 4) A = numpy.ones((3, 5)) # Use expr.size to get the dimensions. print("dimensions of X:", X.size) print("dimensions of sum_entries(X):", sum_entries(X).size) print("dimensions of A*X:", (A * X).size) # ValueError raised for invalid dimensions. try: A + X except ValueError as e: print(e)
def loss_y2(rho, x, y, workers): val = 1 / 2 * rho * cp.sum([ cp.square(cp.norm(y[i] - x[i + 1][0], p=2, axis=0)) for i in range(0, workers) ]) return val
print('Reading vecA ... done!') else: for f in trange(numFaces, desc='Computing vecA', ncols=100, leave=False): vA = [] for i in range(9): A = cvx.Variable(7) err = 0 for m in range(numMeshes_POSE): err += cvx.square((deltaR[m][faceBone[f]] * A) - np.matrix(matQ[m][f]).A1[i]) obj = cvx.Minimize(err) prob = cvx.Problem(obj) prob.solve() vA.append(A.value.A1) vecA[f] = np.array(vA) vecA = np.array(vecA) test_vecA() print('Computing vecA ... done!') if WRITE_VECA_TO_DISK: write_vecA(DATA_DIR + '/vecA.dat', vecA)
def Main_vbjde_Extension_constrained_stable(graph, Y, Onsets, Thrf, K, TR, beta, dt, scale=1, estimateSigmaH=True, sigmaH=0.05, NitMax=-1, NitMin=1, estimateBeta=True, PLOT=False, contrasts=[], computeContrast=False, gamma_h=0): """ Version modified by Lofti from Christine's version """ logger.info( "Fast EM with C extension started ... Here is the stable version !") np.random.seed(6537546) # Initialize parameters S = 100 if NitMax < 0: NitMax = 100 gamma = 7.5 # 7.5 gradientStep = 0.003 MaxItGrad = 200 Thresh = 1e-5 # Initialize sizes vectors D = np.int(np.ceil(Thrf / dt)) + 1 M = len(Onsets) N = Y.shape[0] J = Y.shape[1] l = np.int(np.sqrt(J)) condition_names = [] # Neighbours maxNeighbours = max([len(nl) for nl in graph]) neighboursIndexes = np.zeros((J, maxNeighbours), dtype=np.int32) neighboursIndexes -= 1 for i in xrange(J): neighboursIndexes[i, :len(graph[i])] = graph[i] # Conditions X = OrderedDict([]) for condition, Ons in Onsets.iteritems(): X[condition] = vt.compute_mat_X_2(N, TR, D, dt, Ons) condition_names += [condition] XX = np.zeros((M, N, D), dtype=np.int32) nc = 0 for condition, Ons in Onsets.iteritems(): XX[nc, :, :] = X[condition] nc += 1 # Covariance matrix order = 2 D2 = vt.buildFiniteDiffMatrix(order, D) R = np.dot(D2, D2) / pow(dt, 2 * order) invR = np.linalg.inv(R) Det_invR = np.linalg.det(invR) Gamma = np.identity(N) Det_Gamma = np.linalg.det(Gamma) Crit_H = 1 Crit_Z = 1 Crit_A = 1 Crit_AH = 1 AH = np.zeros((J, M, D), dtype=np.float64) AH1 = np.zeros((J, M, D), dtype=np.float64) Crit_FreeEnergy = 1 cTime = [] cA = [] cH = [] cZ = [] cAH = [] CONTRAST = np.zeros((J, len(contrasts)), dtype=np.float64) CONTRASTVAR = np.zeros((J, len(contrasts)), dtype=np.float64) Q_barnCond = np.zeros((M, M, D, D), dtype=np.float64) XGamma = np.zeros((M, D, N), dtype=np.float64) m1 = 0 for k1 in X: # Loop over the M conditions m2 = 0 for k2 in X: Q_barnCond[m1, m2, :, :] = np.dot( np.dot(X[k1].transpose(), Gamma), X[k2]) m2 += 1 XGamma[m1, :, :] = np.dot(X[k1].transpose(), Gamma) m1 += 1 sigma_epsilone = np.ones(J) logger.info( "Labels are initialized by setting active probabilities to ones ...") q_Z = np.zeros((M, K, J), dtype=np.float64) q_Z[:, 1, :] = 1 q_Z1 = np.zeros((M, K, J), dtype=np.float64) Z_tilde = q_Z.copy() TT, m_h = getCanoHRF(Thrf, dt) # TODO: check m_h = m_h[:D] m_H = np.array(m_h).astype(np.float64) m_H1 = np.array(m_h) sigmaH1 = sigmaH Sigma_H = np.ones((D, D), dtype=np.float64) Beta = beta * np.ones((M), dtype=np.float64) P = vt.PolyMat(N, 4, TR) L = vt.polyFit(Y, TR, 4, P) PL = np.dot(P, L) y_tilde = Y - PL Ndrift = L.shape[0] sigma_M = np.ones((M, K), dtype=np.float64) sigma_M[:, 0] = 0.5 sigma_M[:, 1] = 0.6 mu_M = np.zeros((M, K), dtype=np.float64) for k in xrange(1, K): mu_M[:, k] = 1 # InitMean Sigma_A = np.zeros((M, M, J), np.float64) for j in xrange(0, J): Sigma_A[:, :, j] = 0.01 * np.identity(M) m_A = np.zeros((J, M), dtype=np.float64) m_A1 = np.zeros((J, M), dtype=np.float64) for j in xrange(0, J): for m in xrange(0, M): for k in xrange(0, K): m_A[j, m] += np.random.normal(mu_M[m, k], np.sqrt(sigma_M[m, k])) * q_Z[m, k, j] m_A1 = m_A t1 = time.time() ########################################################################## # VBJDE num. iter. minimum ni = 0 while ((ni < NitMin + 1) or ((Crit_AH > Thresh) and (ni < NitMax))): logger.info("------------------------------ Iteration n° " + str(ni + 1) + " ------------------------------") ##################### # EXPECTATION ##################### # A logger.info("E A step ...") UtilsC.expectation_A(q_Z, mu_M, sigma_M, PL, sigma_epsilone, Gamma, Sigma_H, Y, y_tilde, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N, K) # crit. A DIFF = np.reshape(m_A - m_A1, (M * J)) Crit_A = ( np.linalg.norm(DIFF) / np.linalg.norm(np.reshape(m_A1, (M * J)))) ** 2 cA += [Crit_A] m_A1[:, :] = m_A[:, :] # HRF h UtilsC.expectation_H(XGamma, Q_barnCond, sigma_epsilone, Gamma, R, Sigma_H, Y, y_tilde, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N, scale, sigmaH) #m_H[0] = 0 #m_H[-1] = 0 # Constrain with optimization strategy import cvxpy as cvx m, n = Sigma_H.shape Sigma_H_inv = np.linalg.inv(Sigma_H) zeros_H = np.zeros_like(m_H[:, np.newaxis]) # Construct the problem. PRIMAL h = cvx.Variable(n) expression = cvx.quad_form(h - m_H[:, np.newaxis], Sigma_H_inv) objective = cvx.Minimize(expression) #constraints = [h[0] == 0, h[-1]==0, h >= zeros_H, cvx.square(cvx.norm(h,2))<=1] constraints = [h[0] == 0, h[-1] == 0, cvx.square(cvx.norm(h, 2)) <= 1] prob = cvx.Problem(objective, constraints) result = prob.solve(verbose=0, solver=cvx.CVXOPT) # Now we update the mean of h m_H_old = m_H Sigma_H_old = Sigma_H m_H = np.squeeze(np.array((h.value))) Sigma_H = np.zeros_like(Sigma_H) # and the norm h_norm += [np.linalg.norm(m_H)] # crit. h Crit_H = (np.linalg.norm(m_H - m_H1) / np.linalg.norm(m_H1)) ** 2 cH += [Crit_H] m_H1[:] = m_H[:] # crit. AH for d in xrange(0, D): AH[:, :, d] = m_A[:, :] * m_H[d] DIFF = np.reshape(AH - AH1, (M * J * D)) Crit_AH = (np.linalg.norm( DIFF) / (np.linalg.norm(np.reshape(AH1, (M * J * D))) + eps)) ** 2 cAH += [Crit_AH] AH1[:, :, :] = AH[:, :, :] # Z labels logger.info("E Z step ...") UtilsC.expectation_Z(Sigma_A, m_A, sigma_M, Beta, Z_tilde, mu_M, q_Z, neighboursIndexes.astype(np.int32), M, J, K, maxNeighbours) # crit. Z DIFF = np.reshape(q_Z - q_Z1, (M * K * J)) Crit_Z = (np.linalg.norm(DIFF) / (np.linalg.norm(np.reshape(q_Z1, (M * K * J))) + eps)) ** 2 cZ += [Crit_Z] q_Z1[:, :, :] = q_Z[:, :, :] ##################### # MAXIMIZATION ##################### # HRF: Sigma_h if estimateSigmaH: logger.info("M sigma_H step ...") if gamma_h > 0: sigmaH = vt.maximization_sigmaH_prior( D, Sigma_H, R, m_H, gamma_h) else: sigmaH = vt.maximization_sigmaH(D, Sigma_H, R, m_H) logger.info('sigmaH = %s', str(sigmaH)) # (mu,sigma) logger.info("M (mu,sigma) step ...") mu_M, sigma_M = vt.maximization_mu_sigma( mu_M, sigma_M, q_Z, m_A, K, M, Sigma_A) # Drift L UtilsC.maximization_L( Y, m_A, m_H, L, P, XX.astype(np.int32), J, D, M, Ndrift, N) PL = np.dot(P, L) y_tilde = Y - PL # Beta if estimateBeta: logger.info("estimating beta") for m in xrange(0, M): Beta[m] = UtilsC.maximization_beta(beta, q_Z[m, :, :].astype(np.float64), Z_tilde[m, :, :].astype( np.float64), J, K, neighboursIndexes.astype(np.int32), gamma, maxNeighbours, MaxItGrad, gradientStep) logger.info("End estimating beta") logger.info(Beta) # Sigma noise logger.info("M sigma noise step ...") UtilsC.maximization_sigma_noise( Gamma, PL, sigma_epsilone, Sigma_H, Y, m_A, m_H, Sigma_A, XX.astype(np.int32), J, D, M, N) t02 = time.time() cTime += [t02 - t1] t2 = time.time() ########################################################################## # PLOTS and SNR computation if PLOT and 0: font = {'size': 15} matplotlib.rc('font', **font) savefig('./HRF_Iter_CompMod.png') hold(False) figure(2) plot(cAH[1:-1], 'lightblue') hold(True) plot(cFE[1:-1], 'm') hold(False) legend(('CAH', 'CFE')) grid(True) savefig('./Crit_CompMod.png') figure(3) plot(FreeEnergyArray) grid(True) savefig('./FreeEnergy_CompMod.png') figure(4) for m in xrange(M): plot(SUM_q_Z_array[m]) hold(True) hold(False) savefig('./Sum_q_Z_Iter_CompMod.png') figure(5) for m in xrange(M): plot(mu1_array[m]) hold(True) hold(False) savefig('./mu1_Iter_CompMod.png') figure(6) plot(h_norm_array) savefig('./HRF_Norm_CompMod.png') Data_save = xndarray(h_norm_array, ['Iteration']) Data_save.save('./HRF_Norm_Comp.nii') CompTime = t2 - t1 cTimeMean = CompTime / ni """ Norm = np.linalg.norm(m_H) m_H /= Norm Sigma_H /= Norm**2 sigmaH /= Norm**2 m_A *= Norm Sigma_A *= Norm**2 mu_M *= Norm sigma_M *= Norm**2 sigma_M = np.sqrt(np.sqrt(sigma_M)) """ logger.info("Nb iterations to reach criterion: %d", ni) logger.info("Computational time = %s min %s s", str( np.int(CompTime // 60)), str(np.int(CompTime % 60))) logger.info('mu_M: %f', mu_M) logger.info('sigma_M: %f', sigma_M) logger.info("sigma_H = %s", str(sigmaH)) logger.info("Beta = %s", str(Beta)) StimulusInducedSignal = vt.computeFit(m_H, m_A, X, J, N) SNR = 20 * \ np.log( np.linalg.norm(Y) / np.linalg.norm(Y - StimulusInducedSignal - PL)) SNR /= np.log(10.) logger.info('SNR comp = %f', SNR) return ni, m_A, m_H, q_Z, sigma_epsilone, mu_M, sigma_M, Beta, L, PL, CONTRAST, CONTRASTVAR, cA[2:], cH[2:], cZ[2:], cAH[2:], cTime[2:], cTimeMean, Sigma_A, StimulusInducedSignal
def __init__(self, m, K): # Variables: self.var = dict() self.var['X'] = cvxpy.Variable((m.n_x, K)) self.var['U'] = cvxpy.Variable((m.n_u, K)) self.var['sigma'] = cvxpy.Variable(nonneg=True) self.var['nu'] = cvxpy.Variable((m.n_x, K - 1)) self.var['delta_norm'] = cvxpy.Variable(nonneg=True) self.var['sigma_norm'] = cvxpy.Variable(nonneg=True) # Parameters: self.par = dict() self.par['A_bar'] = cvxpy.Parameter((m.n_x * m.n_x, K - 1)) self.par['B_bar'] = cvxpy.Parameter((m.n_x * m.n_u, K - 1)) self.par['C_bar'] = cvxpy.Parameter((m.n_x * m.n_u, K - 1)) self.par['S_bar'] = cvxpy.Parameter((m.n_x, K - 1)) self.par['z_bar'] = cvxpy.Parameter((m.n_x, K - 1)) self.par['X_last'] = cvxpy.Parameter((m.n_x, K)) self.par['U_last'] = cvxpy.Parameter((m.n_u, K)) self.par['sigma_last'] = cvxpy.Parameter(nonneg=True) self.par['weight_sigma'] = cvxpy.Parameter(nonneg=True) self.par['weight_delta'] = cvxpy.Parameter(nonneg=True) self.par['weight_delta_sigma'] = cvxpy.Parameter(nonneg=True) self.par['weight_nu'] = cvxpy.Parameter(nonneg=True) # Constraints: constraints = [] # Model: constraints += m.get_constraints(self.var['X'], self.var['U'], self.par['X_last'], self.par['U_last']) # Dynamics: # x_t+1 = A_*x_t+B_*U_t+C_*U_T+1*S_*sigma+zbar+nu constraints += [ self.var['X'][:, k + 1] == cvxpy.reshape(self.par['A_bar'][:, k], (m.n_x, m.n_x)) * self.var['X'][:, k] + cvxpy.reshape(self.par['B_bar'][:, k], (m.n_x, m.n_u)) * self.var['U'][:, k] + cvxpy.reshape(self.par['C_bar'][:, k], (m.n_x, m.n_u)) * self.var['U'][:, k + 1] + self.par['S_bar'][:, k] * self.var['sigma'] + self.par['z_bar'][:, k] + self.var['nu'][:, k] for k in range(K - 1) ] # Trust regions: dx = cvxpy.sum(cvxpy.square(self.var['X'] - self.par['X_last']), axis=0) du = cvxpy.sum(cvxpy.square(self.var['U'] - self.par['U_last']), axis=0) ds = self.var['sigma'] - self.par['sigma_last'] constraints += [cvxpy.norm(dx + du, 1) <= self.var['delta_norm']] constraints += [cvxpy.norm(ds, 'inf') <= self.var['sigma_norm']] # Flight time positive: constraints += [self.var['sigma'] >= 0.1] # Objective: sc_objective = cvxpy.Minimize( self.par['weight_sigma'] * self.var['sigma'] + self.par['weight_nu'] * cvxpy.norm(self.var['nu'], 'inf') + self.par['weight_delta'] * self.var['delta_norm'] + self.par['weight_delta_sigma'] * self.var['sigma_norm']) objective = sc_objective self.prob = cvxpy.Problem(objective, constraints)
def test_intro(self): """Test examples from cvxpy.org introduction. """ import numpy # Problem data. m = 30 n = 20 numpy.random.seed(1) A = numpy.random.randn(m, n) b = numpy.random.randn(m, 1) # Construct the problem. x = Variable(n) objective = Minimize(sum_squares(A*x - b)) constraints = [0 <= x, x <= 1] prob = Problem(objective, constraints) # The optimal objective is returned by p.solve(). result = prob.solve() # The optimal value for x is stored in x.value. print(x.value) # The optimal Lagrange multiplier for a constraint # is stored in constraint.dual_value. print(constraints[0].dual_value) ######################################## # Create two scalar variables. x = Variable() y = Variable() # Create two constraints. constraints = [x + y == 1, x - y >= 1] # Form objective. obj = Minimize(square(x - y)) # Form and solve problem. prob = Problem(obj, constraints) prob.solve() # Returns the optimal value. print("status:", prob.status) print("optimal value", prob.value) print("optimal var", x.value, y.value) ######################################## import cvxpy as cvx # Create two scalar variables. x = cvx.Variable() y = cvx.Variable() # Create two constraints. constraints = [x + y == 1, x - y >= 1] # Form objective. obj = cvx.Minimize(cvx.square(x - y)) # Form and solve problem. prob = cvx.Problem(obj, constraints) prob.solve() # Returns the optimal value. print("status:", prob.status) print("optimal value", prob.value) print("optimal var", x.value, y.value) self.assertEqual(prob.status, OPTIMAL) self.assertAlmostEqual(prob.value, 1.0) self.assertAlmostEqual(x.value, 1.0) self.assertAlmostEqual(y.value, 0) ######################################## # Replace the objective. prob.objective = Maximize(x + y) print("optimal value", prob.solve()) self.assertAlmostEqual(prob.value, 1.0) # Replace the constraint (x + y == 1). prob.constraints[0] = (x + y <= 3) print("optimal value", prob.solve()) self.assertAlmostEqual(prob.value, 3.0) ######################################## x = Variable() # An infeasible problem. prob = Problem(Minimize(x), [x >= 1, x <= 0]) prob.solve() print("status:", prob.status) print("optimal value", prob.value) self.assertEquals(prob.status, INFEASIBLE) self.assertAlmostEqual(prob.value, np.inf) # An unbounded problem. prob = Problem(Minimize(x)) prob.solve() print("status:", prob.status) print("optimal value", prob.value) self.assertEquals(prob.status, UNBOUNDED) self.assertAlmostEqual(prob.value, -np.inf) ######################################## # A scalar variable. a = Variable() # Column vector variable of length 5. x = Variable(5) # Matrix variable with 4 rows and 7 columns. A = Variable(4, 7) ######################################## import numpy # Problem data. m = 10 n = 5 numpy.random.seed(1) A = numpy.random.randn(m, n) b = numpy.random.randn(m, 1) # Construct the problem. x = Variable(n) objective = Minimize(sum_entries(square(A*x - b))) constraints = [0 <= x, x <= 1] prob = Problem(objective, constraints) print("Optimal value", prob.solve()) print("Optimal var") print(x.value) # A numpy matrix. self.assertAlmostEqual(prob.value, 4.14133859146) ######################################## # Positive scalar parameter. m = Parameter(sign="positive") # Column vector parameter with unknown sign (by default). c = Parameter(5) # Matrix parameter with negative entries. G = Parameter(4, 7, sign="negative") # Assigns a constant value to G. G.value = -numpy.ones((4, 7)) ######################################## # Create parameter, then assign value. rho = Parameter(sign="positive") rho.value = 2 # Initialize parameter with a value. rho = Parameter(sign="positive", value=2) ######################################## import numpy # Problem data. n = 15 m = 10 numpy.random.seed(1) A = numpy.random.randn(n, m) b = numpy.random.randn(n, 1) # gamma must be positive due to DCP rules. gamma = Parameter(sign="positive") # Construct the problem. x = Variable(m) error = sum_squares(A*x - b) obj = Minimize(error + gamma*norm(x, 1)) prob = Problem(obj) # Construct a trade-off curve of ||Ax-b||^2 vs. ||x||_1 sq_penalty = [] l1_penalty = [] x_values = [] gamma_vals = numpy.logspace(-4, 6) for val in gamma_vals: gamma.value = val prob.solve() # Use expr.value to get the numerical value of # an expression in the problem. sq_penalty.append(error.value) l1_penalty.append(norm(x, 1).value) x_values.append(x.value) ######################################## import numpy X = Variable(5, 4) A = numpy.ones((3, 5)) # Use expr.size to get the dimensions. print("dimensions of X:", X.size) print("dimensions of sum_entries(X):", sum_entries(X).size) print("dimensions of A*X:", (A*X).size) # ValueError raised for invalid dimensions. try: A + X except ValueError as e: print(e)
def graph_reg(beta, edges, weight): # Graph regularization (squared). return weight @ cp.square( cp.pnorm(beta[edges[:, 0], :] - beta[edges[:, 1], :], axis=1, keepdims=True))