parser.add_argument('-n', type=int, help='Number of templates in dictionary') parser.add_argument( '-c', '--codegen', help='Codegen type to use (python, matlab, or C; default python)', default='python') args = parser.parse_args() m, n = (args.m, args.n) print "Running CBP example...." # TODO: takeaways from this example: "diag" constructor? p = QCML(debug=True) p.parse(""" dimensions m n variable c(n) variable u(n) variable v(n) parameter noise positive parameter lambda(n) parameter data(m) parameter dictc(m,n) parameter dictu(m,n) parameter dictv(m,n) parameter radii(n,n) # diagonal matrix parameter rctheta(n,n) # diagonal matrix minimize noise*norm(data - (dictc*c + dictu*u + dictv*v)) + lambda'*c subject to
# a QCML model is specified by strings # the parser parses each model line by line and builds an internal # representation of an SOCP s = """ dimensions m n variable a(n) variable b parameter X(m,n) # positive samples parameter Y(m,n) # negative samples parameter gamma positive minimize (norm(a) + gamma*sum(pos(1 - X*a + b) + pos(1 + Y*a - b))) """ print s raw_input("press ENTER to parse....") p = QCML(debug=True) p.parse(s) raw_input("press ENTER to canonicalize....") p.canonicalize() raw_input("press ENTER to generate code....") p.dims = {'n': n, 'm': m} p.codegen("python") raw_input("press ENTER to solve with ECOS....") socp_data = p.prob2socp(params=locals()) import ecos sol = ecos.solve(**socp_data)
m = 40 n = 2 A = np.zeros((m, n)) b = np.zeros(m) for i in range(m): a = np.random.randn(n) a = a / np.linalg.norm(a) bi = 1 + np.random.rand(1) A[i, :] = a b[i] = bi q = QCML() q.parse(''' dimensions m n variables x(n) r parameters A(m,n) b(m) maximize r A*x + r <= b ''') q.canonicalize() q.dims = {'m': m, 'n': n} q.codegen("python") socp_data = q.prob2socp(locals()) # stuffed variable size n = socp_data['G'].shape[1]
import numpy as np from qcml import QCML import ecos m = 10 n = 20 A = np.random.randn(m, n) b = np.random.randn(m) q = QCML() q.parse(''' dimensions m n variable x(n) parameters A(m,n) b(m) minimize norm1(x) A*x == b ''') q.canonicalize() q.dims = {'m': m, 'n': n} q.codegen('python') socp_vars = q.prob2socp(locals()) # convert to CSR for fast row slicing to distribute problem socp_vars['A'] = socp_vars['A'].tocsr() socp_vars['G'] = socp_vars['G'].tocsr() # the size of the stuffed x or v n = socp_vars['A'].shape[1]
s_right = np.sign(diff_right) diff_right = np.abs(diff_right) diff_right = s_right * np.log(diff_right + 1) s_right = np.abs(diff_right) s_right = 1.0 / (s_right + 1) #s_down is the scaling we want to associate with each difference. Large differences #in the original image are given small weight in the objective s_down = np.diag(s_down) # <codecell> from qcml import QCML import cvxopt p = QCML() rows = (m - 1) * n cols = m * n s = ''' dimension cols dimension rows variable y(cols) parameter top(rows,cols) parameter bottom(rows,cols) parameter left(rows,cols) parameter right(rows,cols) parameter diff_down(rows) parameter diff_right(rows)
# a QCML model is specified by strings # the parser parses each model line by line and builds an internal # representation of an SOCP s = """ dimensions m n pb sb variable r(n,1) parameters A(m,n) Ap(pb,n) As(sb,n) Lp(pb) Up(pb) minimize max(abs(As*r)) Ap*r >= Lp Ap*r <= Up A*r >= 0 """ print s raw_input("press ENTER to parse....") p = QCML(debug=True) p.parse(s) raw_input("press ENTER to canonicalize....") p.canonicalize() raw_input("press ENTER to generate python code....") p.dims = {'m': m, 'n': n, 'pb': pb, 'sb': sb} p.codegen("python") raw_input("press ENTER to solve with ECOS....") socp_data = p.prob2socp(params=locals()) import ecos sol = ecos.solve(**socp_data)
if __name__ == '__main__': test_problems = [("""variable x(3) minimize norm(x) x >= 0""", "norm"), ("""variable x(3) minimize quad_over_lin(x,1) x >= 0""", "quad_over_lin"), ("""variable x(3) minimize square(norm(x)) x >= 0""", "sq_norm"), ("""variable x(3) minimize sum(square(x)) x >= 0""", "sum_sq"), ("""variable x minimize inv_pos(x)""", "inv_pos")] p = QCML(debug=True) def solve(s): print s[1] p.parse(s[0]) p.canonicalize() p.codegen("C") p.save(s[1]) # socp_data = p.prob2socp(params=locals()) # import ecos # sol = ecos.solve(**socp_data) # return sol print map(solve, test_problems)
print "Creating lasso problem." # a QCML model is specified by strings # the parser parses each model line by line and builds an internal # representation of an SOCP s = """ dimensions m n variable x(n) parameters A(m,n) b(m) parameter gamma positive minimize (square(norm(A*x - b)) + gamma*norm1(x)) """ print s raw_input("press ENTER to parse....") p = QCML(debug=True) p.parse(s) raw_input("press ENTER to canonicalize....") p.canonicalize() raw_input("press ENTER to solve the problem....") res = p.solve() raw_input("press ENTER to generate C code and save it....") p.codegen("C") p.save("lasso") raw_input("press ENTER to write the test C program....") c_template = """ #include <stdio.h>
s_right = np.sign(diff_right) diff_right = np.abs(diff_right) diff_right = s_right*np.log(diff_right+1) s_right = np.abs(diff_right) s_right = 1.0/(s_right+1) #s_down is the scaling we want to associate with each difference. Large differences #in the original image are given small weight in the objective s_down = np.diag(s_down) # <codecell> from qcml import QCML import cvxopt p = QCML(debug=True) rows = (m-1)*n cols = m*n s = ''' dimension cols dimension rows variable y(cols) parameter top(rows,cols) parameter bottom(rows,cols) parameter left(rows,cols) parameter right(rows,cols) parameter diff_down(rows) parameter diff_right(rows)
from qcml import QCML import argparse parser = argparse.ArgumentParser(description="generate code for testing") parser.add_argument('-m', default=1,type=int,dest='m', help="problem dimension") parser.add_argument('-n', default=1,type=int,dest='n', help="size of variable") parser.add_argument('-q', default=None,type=int,dest='q', help="cone size") parser.add_argument('--cvx', help="solve the problem using CVX", action="store_true") group = parser.add_mutually_exclusive_group() group.add_argument("--svm", help="solve the SVM problem", action="store_true") group.add_argument("--portfolio", help="solve the portfolio problem", action="store_true") args = parser.parse_args() p = QCML(debug=True) m = args.m n = args.n q = args.q if args.svm: p.parse(""" dimensions m n variable a(n) variable b parameter X(m,n) # positive samples parameter Y(m,n) # negative samples parameter gamma positive minimize (norm(a) + gamma*sum(pos(1 - X*a + b) + pos(1 + Y*a - b)))
m = 100 # number of examples X = np.random.randn(m, n) - 1 Y = np.random.randn(m, n) + 1 gamma = 1 s = """ dimensions m n variable a(n) variable b parameter X(m,n) # positive samples parameter Y(m,n) # negative samples parameter gamma positive minimize (norm(a) + gamma*sum(pos(1 - X*a + b) + pos(1 + Y*a - b))) """ p = QCML() p.parse(s) p.canonicalize() p.dims = {'n': n, 'm': m} p.codegen("python") socp_vars = p.prob2socp(locals()) # convert to CSR for fast row slicing to distribute problem if socp_vars['A'] is not None: socp_vars['A'] = socp_vars['A'].tocsr() socp_vars['G'] = socp_vars['G'].tocsr() # the size of the stuffed x or v n = socp_vars['G'].shape[1]
#!/usr/bin/env python from qcml import QCML import argparse parser = argparse.ArgumentParser( description="Loads an external problem description to parse.") parser.add_argument('-f', dest='file', help="file to parse", required=True) # TODO: allow generation of code when dimensions are abstract # group = parser.add_mutually_exclusive_group(required=True) # group.add_argument("--ansi-c", help="generate ansi-C code; prints to command line", action="store_true") # group.add_argument("--python", help="generate python code; prints to command line", action="store_true") args = parser.parse_args() print "Reading", args.file with open(args.file, "r") as f: prob = f.read() print prob raw_input("press ENTER to parse....") p = QCML(debug=True) p.parse(prob) raw_input("press ENTER to canonicalize....") p.canonicalize()
if __name__ == '__main__': test_problems = [ (""" variable x(3) minimize norm(x) x >= 0""", "norm"), (""" variable x(3) minimize quad_over_lin(x,1) x >= 0""", "quad_over_lin"), (""" variable x(3) minimize square(norm(x)) x >= 0""", "sq_norm"), (""" variable x(3) minimize sum(square(x)) x >= 0""", "sum_sq") ] p = QCML() def solve(s): p.parse(s[0]) p.canonicalize() p.codegen("C") p.save(s[1]) # socp_data = p.prob2socp(params=locals()) # import ecos # sol = ecos.solve(**socp_data) # return sol print map(solve, test_problems)
#!/usr/bin/env python from qcml import QCML import numpy as np from numpy.random import randn import cProfile, pstats if __name__ == "__main__": n = 2 # number of features m = 100 # number of examples X = randn(m, n) - 1 Y = randn(m, n) + 1 gamma = 1 p = QCML(debug=True) p.parse(""" dimensions m n variable a(n) variable b parameter X(m,n) # positive samples parameter Y(m,n) # negative samples parameter Z(m,n) parameter W(m,n) parameter gamma positive parameter c(n) # variables x(n) z # # minimize huber(sum(x)) - sqrt(z) # x + z == 5 # x + z == 5 # # x(:,i+1) == A(:,:,i)*x(:,i) + B*u(:,i) for i = 1,...,T # # sum_{ij in E}