def test_ecos_leastsquares(): x = cvx.Variable (name='x') F = cvx.Parameter(name='F') g = cvx.Parameter(name='g') objective = cvx.square(cvx.norm( F*x - g )) objective = cvx.Minimize(objective) problem = cvx.Problem(objective, []) canon = Canonicalize(problem, verbose=True) # Set values of parameters parameters = { 'F' : 42, 'g' : 42, } canon.assign_values(parameters) solution = cvx.solve(canon, verbose = True) if solution: print('Solution obj:', solution['info']['pcost']) print('Solution x:', solution['x'][0]) assert( np.isclose(solution['x'][0], 1.0) ) reset_symbols()
def test_ecos_polyhedradist(): import cvx_sym as cvx n = 2 # number of dimensions m = 3 # number of lines defining polyhedron 1 p = 3 # number of lines defining polyhedron 2 x1 = cvx.Variable ((n,1),name='x1') x2 = cvx.Variable ((n,1),name='x2') A1 = cvx.Parameter((m,n),name='A1') A2 = cvx.Parameter((p,n),name='A2') B1 = cvx.Parameter((m,1),name='B1') B2 = cvx.Parameter((p,1),name='B2') objective = cvx.square(cvx.norm(x1 - x2)) constraints = [ A1[i,:].T * x1 <= B1[i] for i in range(m) ] constraints += [ A2[i,:].T * x2 <= B2[i] for i in range(p) ] objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) canon = Canonicalize(problem, verbose=True) # Set values of parameters parameters = { 'A1' : np.array([[-1,1],[1,1],[0,-1]]), 'B1' : np.array([[3],[3],[0]]), 'A2' : np.array([[.5,-1],[0,1],[+1,0]]), 'B2' : np.array([[-3],[3],[5]]), } canon.assign_values(parameters) solution = cvx.solve(canon, verbose = True) if solution: print('Solution obj:', solution['info']['pcost']) # Gather the OG solution variables v_indices = [n for n, vn in enumerate(canon.vars.keys()) if 'x' in vn] x_solution = [solution['x'][i] for i in v_indices] print('Solution x:', x_solution) print('Solution vec:', solution['x']) # Solution is found at intersection of polyhedra assert( np.allclose(x_solution, [0,3, 0,3], atol=1e-4) ) # So therefore the objective should be near zero assert( np.allclose(solution['info']['pcost'], 0.0 ) ) reset_symbols()
def test_ecos_robustlp(): import cvx_sym as cvx n = 2 # number of dimensions m = 3 # number of elementwise elements x = cvx.Variable ((n,1),name='x') A = cvx.Parameter((m,n),name='A') B = cvx.Parameter((m,1),name='B') C = cvx.Parameter((n,1),name='C') P = cvx.Parameter((m,n),name='P') objective = C.T * x constraints = [A[i].T * x + cvx.norm(P[i].T * x) <= B[i] for i in range(m)] objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) canon = Canonicalize(problem, verbose=True) # Set values of parameters parameters = { 'A' : np.array([[1,1],[1,1],[1,1]]), 'B' : np.array([[3],[3],[3]]), 'C' : np.array([[.1],[.2]]), 'P' : np.array([[1,2],[3,4],[5,6]]) } canon.assign_values(parameters) solution = cvx.solve(canon, verbose = True) if solution: print('Solution obj:', solution['info']['pcost']) # Gather the OG solution variables v_indices = [n for n, vn in enumerate(canon.vars.keys()) if 'x' in vn] x_solution = [solution['x'][i] for i in v_indices] print('Solution x:', x_solution) print('Solution vec:', solution['x']) assert( np.allclose(x_solution, [3,-3] ) ) reset_symbols()
def test_ecos_chebyshevcenter(): import cvx_sym as cvx n = 2 m = 3 r = cvx.Variable((1,1),name='r') x = cvx.Variable((n,1),name='x') A = cvx.Parameter((m,n),name='A') B = cvx.Parameter((m,1),name='B') objective = -r constraints = [A[i,:].T * x + r * cvx.norm(A[i,:]) <= B[i] for i in range(m)] constraints += [r >= 0] objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) canon = Canonicalize(problem, verbose=True) # Set values of parameters parameters = { 'A' : np.array([[-1,1],[1,1],[0,-1]]), 'B' : np.array([[3],[3],[0]]), } canon.assign_values(parameters) solution = cvx.solve(canon, verbose = True) if solution: print('Solution obj:', solution['info']['pcost']) # Gather the OG solution variables v_indices = [n for n, vn in enumerate(canon.vars.keys()) if 'x' in vn] x_solution = [solution['x'][i] for i in v_indices] print('Solution x:', x_solution) print('Solution vec:', solution['x']) assert( np.allclose(x_solution, [0, 1.242641] ) ) reset_symbols()
def test_ecos_leastsquares_constr(): x = cvx.Variable ((3,1),name='x') F = cvx.Parameter((3,3),name='F') g = cvx.Parameter((3,1),name='g') U = cvx.Parameter((3,1),name='U') L = cvx.Parameter((3,1),name='L') constraints = [] objective = cvx.square(cvx.norm( F*x - g )) constraints += [ x <= U ] constraints += [ L <= x ] objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) canon = Canonicalize(problem, verbose=True) # Set values of parameters parameters = { 'F' : np.array([[1,2,3],[4,5,6],[7,8,9]]), 'g' : np.array([[1],[2],[3]]), 'U' : np.array([[42],[42],[42]]), 'L' : np.array([[1],[2],[3]]) } canon.assign_values(parameters) solution = cvx.solve(canon, verbose = True) if solution: print('Solution obj:', solution['info']['pcost']) # Gather the OG solution variables v_indices = [n for n, vn in enumerate(canon.vars.keys()) if 'x' in vn] x_solution = [solution['x'][i] for i in v_indices] print('Solution x:', x_solution) print('Solution vec:', solution['x']) assert( np.allclose(x_solution, [1,2,3] ) ) reset_symbols()
def test_ecos_trivial_norm1(): x = cvx.Variable((3,1),name='x') constraints = [x >= -1] constraints += [x <= +1] objective = cvx.norm(x, kind=1) objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) canon = Canonicalize(problem, verbose=True) canon.assign_values({}) solution = cvx.solve(canon, verbose = True) if solution: print('Solution obj:', solution['info']['pcost']) print('Solution x:', solution['x']) assert( np.allclose(solution['x'][0:3], [0.0, 0.0, 0.0]) ) reset_symbols()
def __init__(self): K = 50 self.Xv = cvx.Variable((K, 14), name='Xv') self.Uv = cvx.Variable((K, 14), name='Uv') self.nuv = cvx.Variable((14 * (K - 1)), name='nuv') self.delta = cvx.Variable(K, name='delta') self.sigmav = cvx.Variable(name='sigmav') self.delta_sv = cvx.Variable(name='delta_sv') self.x_init = cvx.Parameter((1, 14), name='x_init') self.x_final = cvx.Parameter((1, 14), name='x_final') # Indexes to expect config variables on m_dry = 0 tan_gamma_gs = 1 cos_theta_max = 2 cos_delta_max = 3 w_B_max = 4 T_min = 5 T_max = 6 self.config = cvx.Parameter((7), name='config') # Parameters: self.A_bar_parm = cvx.Parameter((K, 14 * 14), name='A_bar_parm') self.B_bar_parm = cvx.Parameter((K, 14 * 3), name='B_bar_parm') self.C_bar_parm = cvx.Parameter((K, 14 * 3), name='C_bar_parm') self.S_bar_parm = cvx.Parameter((K, 14), name='S_bar_parm') self.z_bar_parm = cvx.Parameter((K, 14), name='z_bar_parm') self.X_last_parm = cvx.Parameter((K, 14), name='X_last_parm') self.U_last_parm = cvx.Parameter((K, 14), name='U_last_parm') self.s_last_parm = cvx.Parameter(name='s_last_parm') self.w_delta_parm = cvx.Parameter(name='w_delta_parm') self.w_nu_parm = cvx.Parameter(name='w_nu_parm') self.w_delta_sigma_parm = cvx.Parameter(name='w_delta_sigma_parm') """ The start/end indexes of each variable in the vector V = XABCSz """ self.idx = [14] # end of x (14,1) self.idx += [self.idx[0] + (14 * 14)] # end of A (14,14) self.idx += [self.idx[1] + (14 * 3)] # end of B (14,3) self.idx += [self.idx[2] + (14 * 3)] # end of C (14,3) self.idx += [self.idx[3] + (14 * 1)] # end of S (14,1) self.idx += [self.idx[4] + (14 * 1)] # end of z (14,1) self.constraints = [] # ----------------------------------------------------- Dynamics: for k in range(K - 1): self.constraints += [ self.Xv[k + 1, :] == (cvx.reshape(self.A_bar_parm[k, :], (14, 14)) * self.Xv[k, :] + cvx.reshape(self.B_bar_parm[k, :], (14, 3)) * self.Uv[k, 0:3] + cvx.reshape(self.C_bar_parm[k, :], (14, 3)) * self.Uv[k + 1, 0:3] + self.S_bar_parm[k, :] * self.sigmav + self.z_bar_parm[k, :] + self.nuv[k * 14:(k + 1) * 14]) ] # ----------------------------------------------------- State constraints: self.constraints += [self.Xv[:, 0] >= self.config[m_dry]] for k in range(K): self.constraints += [ self.config[tan_gamma_gs] * cvx.norm(self.Xv[k, 2:4]) <= self.Xv[k, 1], self.config[cos_theta_max] <= 1 - 2 * cvx.sum_squares(self.Xv[k, 9:11]), cvx.norm(self.Xv[k, 11:14]) <= self.config[w_B_max] ] # ------------------------------------------------- Control constraints: #B_g = self.U_last_parm[k, 0:3] / cvx.norm(self.U_last_parm[k, 0:3]) self.constraints += [ # self.config[T_min] <= B_g * self.Uv[k, 0:3].T, self.Uv[k, 0] >= 0, cvx.norm(self.Uv[k, 0:3]) <= self.config[T_max], self.config[cos_delta_max] * cvx.norm(self.Uv[k, 0:3]) <= self.Uv[k, 0] ] # ------------------------------------------------- Trust regions: dx = self.Xv[k, :] - self.X_last_parm[k, :] du = self.Uv[k, :] - self.U_last_parm[k, :] self.constraints += [ cvx.sum_squares(dx) + cvx.sum_squares(du) <= self.delta[k] ] self.constraints += [ cvx.norm(self.sigmav - self.s_last_parm) <= self.delta_sv ] self.constraints += [self.sigmav >= 0] self.constraints += [ self.Xv[0, 0:7] == self.x_init[0, 0:7], # init all but attitude self.Xv[0, 11:14] == self.x_init[0, 11:14], self.Xv[K - 1, 1:14] == self.x_final[0, 1:14], # final all but mass self.Uv[K - 1, 1] == 0, self.Uv[K - 1, 2] == 0, ] objective = cvx.Minimize(self.sigmav # minimize flight time #-Xv[-1,0] # minimize fuel use # virtual control + self.w_nu_parm * cvx.norm(self.nuv, kind=1) # trust region on dynamics + self.w_delta_parm * cvx.norm(self.delta) # trust region on sigma + self.w_delta_sigma_parm * cvx.norm(self.delta_sv, kind=1)) self.problem = cvx.Problem(objective, self.constraints) gen = cvx.Generate( self.problem, name='Aeroland_core', folder='raw', verbose=2 # show each stage of the process )
import cvx_sym as cvx import numpy as np ''' Least squares optimization problem ''' x = cvx.Variable(name='x') F = cvx.Parameter(name='F') g = cvx.Parameter(name='g') constraints = [] objective = cvx.square(cvx.norm(F * x - g)) objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) generator = cvx.Generate(problem, name='least_squares_unconstr', folder='integrated_projects', verbose=True)
def full_scale_ecos_control(): """ Takes a long time to canonicalize, so don't run as routine test """ import cvx_sym as cvx n = 8 m = 2 T = 50 x = cvx.Variable((n, T+1), name='x') u = cvx.Variable((m, T), name='u') x_0 = cvx.Parameter((n,1), name='x_0') A = cvx.Parameter((n,n), name='A') B = cvx.Parameter((n,m), name='B') states = [] constraints = [ x[:,T] == 0 ] constraints += [ x[:,0] == x_0[:,0] ] for t in range(T): constraints += [ x[:,t+1] == A*x[:,t] + B*u[:,t] ] constraints += [ cvx.norm(u[:,t], kind = 'inf') <= 1 ] cost = cvx.sum_squares(x[:,t+1]) + cvx.sum_squares(u[:,t]) states.append( cost ) # sums problem objectives and concatenates constraints. objective = cvx.sum(states) objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) canon = Canonicalize(problem, verbose = 1 ) np.random.seed(1) alpha = 0.2 beta = 5 A_set = np.eye(n) + alpha*np.random.randn(n,n) B_set = np.random.randn(n,m) x_0_set = beta*np.random.randn(n,1) # Set values of parameters parameters = { 'A' : A_set, 'B' : B_set, 'x_0' : x_0_set, } canon.assign_values(parameters) solution = cvx.solve(canon, verbose = True) if solution: obj = solution['info']['pcost'] # Gather the OG solution variables v_indices = [n for n, vn in enumerate(canon.vars.keys()) if 'x' in vn] x_solution = [solution['x'][i] for i in v_indices] print('Solution obj:', obj) print('Solution x:', x_solution) print('Solution vec:', solution['x']) assert( np.isclose(obj, 64470.59019495451) ) reset_symbols()
import cvx_sym as cvx n = 2 # number of dimensions m = 3 # number of lines defining polyhedron 1 p = 3 # number of lines defining polyhedron 2 x1 = cvx.Variable((n, 1), name='x1') x2 = cvx.Variable((n, 1), name='x2') A1 = cvx.Parameter((m, n), name='A1') A2 = cvx.Parameter((p, n), name='A2') B1 = cvx.Parameter((m, 1), name='B1') B2 = cvx.Parameter((p, 1), name='B2') objective = cvx.square(cvx.norm(x1 - x2)) constraints = [A1[i].T * x1 <= B1[i] for i in range(m)] constraints += [A2[i].T * x2 <= B2[i] for i in range(p)] objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) generator = cvx.Generate(problem, name='polyhedradist', folder='integrated_projects', verbose=1)
x = cvx.Variable((n, T+1), name='x') u = cvx.Variable((m, T), name='u') x_0 = cvx.Parameter((n), name='x_0') A = cvx.Parameter((n,n), name='A') B = cvx.Parameter((n,m), name='B') states = [] constraints = [ x[:,T] == 0 ] constraints += [ x[:,0] == x_0[:,0] ] for t in range(T): constraints += [ x[:,t+1] == A*x[:,t] + B*u[:,t] ] constraints += [ cvx.norm(u[:,t], kind = 'inf') <= 1 ] cost = cvx.sum_squares(x[:,t+1]) + cvx.sum_squares(u[:,t]) states.append( cost ) # sums problem objectives and concatenates constraints. objective = cvx.sum(states) objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) problem = cvx.Problem(objective, constraints) generator = cvx.Generate(problem, name = 'control', folder = 'integrated_projects', verbose = 1)
import cvx_sym as cvx n = 2 # number of dimensions m = 3 # number of elementwise elements x = cvx.Variable((n, 1), name='x') A = cvx.Parameter((m, n), name='A') B = cvx.Parameter((m, 1), name='B') C = cvx.Parameter((n, 1), name='C') P = cvx.Parameter((m, n), name='P') objective = C.T * x constraints = [A[i].T * x + cvx.norm(P[i].T * x) <= B[i] for i in range(m)] objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) generator = cvx.Generate(problem, name='robustlp_socp', folder='integrated_projects', verbose=1)
import cvx_sym as cvx n = 2 m = 3 r = cvx.Variable((1, 1), name='r') x = cvx.Variable((n, 1), name='x') A = cvx.Parameter((m, n), name='A') B = cvx.Parameter((m, 1), name='B') objective = -r constraints = [A[i, :].T * x + r * cvx.norm(A[i, :]) <= B[i] for i in range(m)] constraints += [r >= 0] objective = cvx.Minimize(objective) problem = cvx.Problem(objective, constraints) generator = cvx.Generate(problem, name='chebyshevcenter', folder='integrated_projects', verbose=1)