def param_setup(self, atom=None): np.random.seed(0) m = 5 n = 10 p = 7 A = cg.Parameter(m,n, name='A', value=np.random.randn(m,n)) B = cg.Parameter(n,p, name='B', value=np.random.randn(n,p)) C = cg.Parameter(m,p, name='C', value=np.random.randn(m,p)) D = cg.Parameter(p,n, name='D', value=np.random.randn(p,n)) P = cg.Parameter(n,n, name='P', value=np.random.randn(n,n)) r = cg.Parameter(1,n, name='r', value=np.random.randn(1,n)) c = cg.Parameter(n,1, name='c', value=np.random.randn(n,1)) params = [A, B, C, D, P, r, c] self.params = dict([(p.name(), p.value) for p in params]) if atom == 'abs': rhs = cg.abs(A) elif atom == 'add': rhs = A+A elif atom == 'diag_vec': rhs = cg.diag(c) elif atom == 'diag_mat': rhs = cg.diag(P) elif atom == 'hstack': rhs = cg.hstack(A, C) elif atom == 'index': rhs = A[0:4:2, 1:9:3] elif atom == 'max_entries': rhs = cg.max_entries(A) elif atom == 'mul': rhs = A*B elif atom == 'neg': rhs = -A elif atom == 'reshape': rhs = cg.reshape(A, n, m) elif atom == 'trace': rhs = cg.trace(P) elif atom == 'vstack': rhs = cg.vstack(A, D) x = cg.Variable(rhs.size[0], rhs.size[1], name='x') constr = [rhs == x] obj = 0 self.prob = cg.Problem(cg.Minimize(obj), constr) self.prob.solve() self.x_opt = x.value
def mpc_setup(self): np.random.seed(0) n = 5 m = 10 self.A_val = np.random.randn(m, n) self.b_val = np.random.randn(m, 1) A = cg.Parameter(m, n, name='A', value=self.A_val) b = cg.Parameter(m, 1, name='b', value=self.b_val) x = cg.Variable(n, name='x') objective = cg.norm(A * x - b) self.prob = cg.Problem(cg.Minimize(objective)) self.prob.solve() self.x_opt = x.value
def mpc_setup(self, objective='quad'): np.random.seed(0) n = 3 m = 1 T = 6 self.A_val = np.eye(n) + .2 * np.random.randn(n, n) self.B_val = 5 * np.random.randn(n, m) self.x0_val = 5 * np.random.randn(n, 1) A = cg.Parameter(n, n, name='A') B = cg.Parameter(n, m, name='B') x0 = cg.Parameter(n, 1, name='x0') A.value = self.A_val B.value = self.B_val x0.value = self.x0_val x = cg.Variable(n, T + 1, name='x') u = cg.Variable(m, T, name='u') obj = 0 constr = [] constr += [x[:, 0] == x0] for t in range(T): constr += [x[:, t + 1] == A * x[:, t] + B * u[:, t]] constr += [cg.norm(u[:, t], 'inf') <= 1] if objective == 'quad': obj += cg.sum_squares(x[:, t + 1]) + cg.sum_squares(u[:, t]) elif objective == '1norm': obj += cg.norm(x[:, t + 1], 1) + cg.norm(u[:, t], 1) elif objective == 'exp1norm': obj += cg.exp(cg.norm(x[:, t + 1], 1)) + cg.norm(u[:, t], 1) else: raise Exception self.prob = cg.Problem(cg.Minimize(obj), constr) self.prob.solve() self.x_opt = x.value
def _test_expr(self, expr, printing=False): # Get Callback param. prob = cg.Problem(cvxpy.Minimize(expr)) obj, constrs = expr.canonicalize() data = ECOS.get_problem_data(obj, constrs, prob._cached_data) true_obj_coeff = data[s.C] true_obj_offset = data[s.OFFSET] true_eq_coeff = data[s.A] true_eq_offset = data[s.B] true_leq_coeff = data[s.G] true_leq_offset = data[s.H] # Do code generation vars = prob.variables() params = prob.parameters() obj, constraints = prob.canonical_form code_generator = CodeGenerator(obj, constraints, vars, params) code_generator.codegen(target_dir) template_vars = code_generator.template_vars # Set up test harness. render(target_dir, template_vars, HARNESS_C, 'harness.c') render(target_dir, template_vars, CODEGEN_H, 'codegen.h') test_data = self._run_test(target_dir) test_obj_coeff = np.array(test_data['obj_coeff']) test_obj_offset = np.array(test_data['obj_offset']) test_eq_coeff = sp.csc_matrix((test_data['eq_nzval'], test_data['eq_rowidx'], test_data['eq_colptr']), shape = (test_data['eq_size0'], test_data['eq_size1'])) test_eq_offset = np.array(test_data['eq_offset']) test_leq_coeff = sp.csc_matrix((test_data['eq_nzval'], test_data['eq_rowidx'], test_data['eq_colptr']), shape = (test_data['eq_size0'], test_data['eq_size1'])) test_leq_offset = np.array(test_data['leq_offset']) if printing: print('\nTest objective coeff :\n', test_obj_coeff) print('\nTrue objective coeff :\n', true_obj_coeff) print('\nTest objective offset :\n', test_obj_offset) print('\nTrue objective offset :\n', true_obj_offset) print('\nTest equality offset :\n', test_eq_coeff) print('\nTrue equality offset :\n', true_eq_coeff) print('\nTest equality offset :\n', test_eq_offset) print('\nTrue equality offset :\n', true_eq_offset) print('\nTest inequality offset :\n', test_leq_coeff) print('\nTrue inequality offset :\n', true_leq_coeff) print('\nTest inequality offset :\n', test_leq_offset) print('\nTrue inequality offset :\n', true_leq_offset) self.assertAlmostEqualMatrices(true_obj_coeff, test_obj_coeff) self.assertAlmostEqualMatrices(true_obj_offset, test_obj_offset) self.assertAlmostEqualMatrices(true_eq_coeff, test_eq_coeff) self.assertAlmostEqualMatrices(true_eq_offset, test_eq_offset) self.assertAlmostEqualMatrices(true_leq_coeff, test_leq_coeff) self.assertAlmostEqualMatrices(true_leq_offset, test_leq_offset)
def test_optimal(self): x = cg.Variable() constr = [x >= 1] obj = x self.prob = cg.Problem(cg.Minimize(obj), constr) self._run_codegen_test(self.prob, MODULE, self.class_name, '_test_optimal')
def test_unbounded(self): x = cg.Variable() constr = [x <= 1] obj = x self.prob = cg.Problem(cg.Minimize(obj), constr) self._run_codegen_test(self.prob, MODULE, self.class_name, '_test_unbounded')
def test_infeas(self, atom=None): x = cg.Variable() constr = [x >= 1, x <= 0] obj = 0 self.prob = cg.Problem(cg.Minimize(obj), constr) self._run_codegen_test(self.prob, MODULE, self.class_name, '_test_infeas')