Exemplo n.º 1
0
    def tostdform(self):
        p = self.p

        ((ct, At, bt, Gt, ht), xt, d, optvars) = p.symbsolve('cAbGh')

        d = {}
        d['header'] = header()

        # Now is when we determine the signature.
        sig = 'lpstdform tostdform(\n' + ' ' * 8
        if getdims(p):
            sig += ', '.join(['int %s' % x for x in bylowerstr(getdims(p))])

        if getparams(p):
            sig += ',\n' + ' ' * 8
            sig += ', '.join(
                ['double *%s' % x for x in bylowerstr(getparams(p))])

        sig += ')'
        d['tostdform_sig'] = sig

        d['lsf_c'] = stuffmatrix(ct, 'lsf->c')
        d['lsf_A'] = stuffmatrix(At, 'lsf->A')
        d['lsf_b'] = stuffmatrix(bt, 'lsf->b')
        d['lsf_G'] = stuffmatrix(Gt, 'lsf->G')
        d['lsf_h'] = stuffmatrix(ht, 'lsf->h')

        sig = 'int convertback(outvar *vars,\n' + ' ' * 8
        if getdims(p):
            sig += ', '.join(['int %s' % x for x in bylowerstr(getdims(p))])
        sig += ',\n        double *xt)'

        d['convertback_sig'] = sig
        d['lsf_m'] = str(rows(At))
        d['lsf_n'] = str(cols(At))
        d['lsf_p'] = str(rows(Gt))

        s = ''
        i = 0
        rs = 0
        for x in optvars:
            if x in getoptvars(p):
                # jem: need to also deal with substitute back situations.
                s += '// Recover %s.\n' % str(x)
                name = 'vars[%d]' % i
                s += '%s->m = %s;\n' % (name, str(rows(x)))
                s += '%s->n = 1;\n' % name  # for the moment (jem).
                s += '%s->val = malloc((%s)*sizeof(double));\n' % (
                    name, str(cols(x)))
                s += 'for (row = 0; row < %s; row++)\n' % str(rows(x))
                s += '    %s->val[row] = xt[%s + row];\n\n' % (name, str(rs))
                i += 1

            rs = compactdims(rs + rows(x))

        d['varrecover'] = s

        writecode(self.outdir, self.skel, 'tostdform.h', d)
        writecode(self.outdir, self.skel, 'tostdform.c', d)
Exemplo n.º 2
0
    def tostdform(self):
        p = self.p

        ((ct, At, bt, Gt, ht), xt, d, optvars) = p.symbsolve('cAbGh')

        d = {}
        d['header'] = header()

        # Now is when we determine the signature.
        sig = 'lpstdform tostdform(\n' + ' '*8
        if getdims(p):
            sig += ', '.join(['int %s' % x for x in bylowerstr(getdims(p))])

        if getparams(p):
            sig += ',\n' + ' '*8
            sig += ', '.join(['double *%s' % x for x in bylowerstr(getparams(p))])

        sig += ')'
        d['tostdform_sig'] = sig

        d['lsf_c'] = stuffmatrix(ct, 'lsf->c')
        d['lsf_A'] = stuffmatrix(At, 'lsf->A')
        d['lsf_b'] = stuffmatrix(bt, 'lsf->b')
        d['lsf_G'] = stuffmatrix(Gt, 'lsf->G')
        d['lsf_h'] = stuffmatrix(ht, 'lsf->h')

        sig = 'int convertback(outvar *vars,\n' + ' '*8
        if getdims(p):
            sig += ', '.join(['int %s' % x for x in bylowerstr(getdims(p))])
        sig += ',\n        double *xt)'

        d['convertback_sig'] = sig
        d['lsf_m'] = str(rows(At))
        d['lsf_n'] = str(cols(At))
        d['lsf_p'] = str(rows(Gt))

        s = ''
        i = 0
        rs = 0
        for x in optvars:
            if x in getoptvars(p):
                # jem: need to also deal with substitute back situations.
                s += '// Recover %s.\n' % str(x)
                name = 'vars[%d]' % i;
                s += '%s->m = %s;\n' % (name, str(rows(x)))
                s += '%s->n = 1;\n' % name # for the moment (jem).
                s += '%s->val = malloc((%s)*sizeof(double));\n' % (name, str(cols(x)))
                s += 'for (row = 0; row < %s; row++)\n' % str(rows(x))
                s += '    %s->val[row] = xt[%s + row];\n\n' % (name, str(rs))
                i += 1

            rs = compactdims(rs + rows(x))

        d['varrecover'] = s

        writecode(self.outdir, self.skel, 'tostdform.h', d)
        writecode(self.outdir, self.skel, 'tostdform.c', d)
Exemplo n.º 3
0
    def testfa(self, n):
        p = self.p

        d = {}
        d['header'] = header()

        d['n'] = n

        d['params'] = joinlist(bylowerstr(getparams(p)), True)
        d['optvars'] = joinlist(bylowerstr(getoptvars(p)), True)

        s = joinlist(bylowerstr(getparams(p)), True)
        d['fa_args'] = s

        writecode(self.outdir, self.skel, 'testfa.c', d)
Exemplo n.º 4
0
    def testfa(self, n):
        p = self.p

        d = {}
        d['header'] = header()

        d['n'] = n

        d['params'] = joinlist(bylowerstr(getparams(p)), True)
        d['optvars'] = joinlist(bylowerstr(getoptvars(p)), True)

        s = joinlist(bylowerstr(getparams(p)), True)
        d['fa_args'] = s

        writecode(self.outdir, self.skel, 'testfa.c', d)
Exemplo n.º 5
0
    def testwithdata(self, dispvars=False):
        p = self.p

        d = {}
        d['header'] = header()

        d['dims'] = joinlist(bylowerstr(getdims(p)), True)
        d['params'] = joinlist(bylowerstr(getparams(p)), True)
        d['optvars'] = joinlist(bylowerstr(getoptvars(p)), True)
        d['numvars'] = str(len(getoptvars(p)))

        # jem: making the assumption that optvars order will be sorted like
        # this.
        s = ''
        i = 0
        for x in bylowerstr(getoptvars(p)):
            s += 'outvar %s;\n%s = vars[%d];\n' % (str(x), str(x), i)
            i += 1
        d['namevars'] = s

        s = joinlist(bylowerstr(getdims(p)) + bylowerstr(getparams(p)), True)
        d['tsf_args'] = s

        # jemjemjem.
        #if dispvars:
        #    d['tsf_args'] = s
        #// Display the variables.
        #pm(x->val, x->m, x->n);
        #pm(y->val, y->m, y->n);

        writecode(self.outdir, self.skel, 'testwithdata.c', d)
Exemplo n.º 6
0
    def testwithdata(self, dispvars=False):
        p = self.p

        d = {}
        d['header'] = header()

        d['dims'] = joinlist(bylowerstr(getdims(p)), True)
        d['params'] = joinlist(bylowerstr(getparams(p)), True)
        d['optvars'] = joinlist(bylowerstr(getoptvars(p)), True)
        d['numvars'] = str(len(getoptvars(p)))

        # jem: making the assumption that optvars order will be sorted like
        # this.
        s = ''
        i = 0
        for x in bylowerstr(getoptvars(p)):
            s += 'outvar %s;\n%s = vars[%d];\n' % (str(x), str(x), i)
            i += 1
        d['namevars'] = s

        s = joinlist(bylowerstr(getdims(p)) + bylowerstr(getparams(p)), True)
        d['tsf_args'] = s

        # jemjemjem.
        #if dispvars:
        #    d['tsf_args'] = s
        #// Display the variables.
        #pm(x->val, x->m, x->n);
        #pm(y->val, y->m, y->n);

        writecode(self.outdir, self.skel, 'testwithdata.c', d)
Exemplo n.º 7
0
    def testba(self, n):
        p = self.p

        dt = {}
        dt['header'] = header()

        dt['n'] = n

        dt['params'] = joinlist(bylowerstr(getparams(p)), True)
        dt['optvars'] = joinlist(bylowerstr(getoptvars(p)), True)

        print 'Creating method signature.'
        if getparams(self.p):
            dt['fb_sig_params'] = ', '.join(['double *%s' % x for x in \
                                     bylowerstr(getparams(self.p))])
            dt['fb_sig_params'] += ','
        else:
            dt['fb_sig_params'] = ''

        dt['fb_args'] = joinlist(bylowerstr(getparams(p)), True)

        writecode(self.outdir, self.skel, 'testba.c', dt)
Exemplo n.º 8
0
    def testba(self, n):
        p = self.p

        dt = {}
        dt['header'] = header()

        dt['n'] = n

        dt['params'] = joinlist(bylowerstr(getparams(p)), True)
        dt['optvars'] = joinlist(bylowerstr(getoptvars(p)), True)

        print 'Creating method signature.'
        if getparams(self.p):
            dt['fb_sig_params'] = ', '.join(['double *%s' % x for x in \
                                     bylowerstr(getparams(self.p))])
            dt['fb_sig_params'] += ','
        else:
            dt['fb_sig_params'] = ''

        dt['fb_args'] = joinlist(bylowerstr(getparams(p)), True)

        writecode(self.outdir, self.skel, 'testba.c', dt)
Exemplo n.º 9
0
    def test2p(self, As, bs, cs, m, n, p):
        dt = {}
        dt['header'] = header()
        dt['m'] = value(m)
        dt['n'] = value(n)
        dt['p'] = value(p)

        dt['params'] = joinlist(bylowerstr(getparams(self.p)), True)
        dt['optvars'] = joinlist(bylowerstr(getoptvars(self.p)), True)

        print 'Generating code for two phase fast barrier method:'

        print 'Creating method signature.'
        if getparams(self.p):
            dt['fb_sig_params'] = ', '.join(['double *%s' % x for x in \
                                     bylowerstr(getparams(self.p))])
            dt['fb_sig_params'] += ','
        else:
            dt['fb_sig_params'] = ''

        A = nzentries(As)
        b = nzentries(bs)
        print 'Generating code for initial feasible point.'
        s = ''
        # CVXMOD_WL_t1.
        t1 = optvar('CM_t1', m, 1)
        for i in range(m):
            t = -b[i]
            for j in range(n):
                if (i, j) in A:
                    t = t + A[i, j]
            s += 'CM_t1[%d] = %s;\n' % (i, exprtoC(t))
        dt['CM_t1'] = s
        dt['fb_args'] = joinlist(bylowerstr(getparams(self.p)), True)

        writecode(self.outdir, self.skel, 'test2p.c', dt)
Exemplo n.º 10
0
    def test2p(self, As, bs, cs, m, n, p):
        dt = {}
        dt['header'] = header()
        dt['m'] = value(m)
        dt['n'] = value(n)
        dt['p'] = value(p)

        dt['params'] = joinlist(bylowerstr(getparams(self.p)), True)
        dt['optvars'] = joinlist(bylowerstr(getoptvars(self.p)), True)

        print 'Generating code for two phase fast barrier method:'

        print 'Creating method signature.'
        if getparams(self.p):
            dt['fb_sig_params'] = ', '.join(['double *%s' % x for x in \
                                     bylowerstr(getparams(self.p))])
            dt['fb_sig_params'] += ','
        else:
            dt['fb_sig_params'] = ''

        A = nzentries(As)
        b = nzentries(bs)
        print 'Generating code for initial feasible point.'
        s = ''
        # CVXMOD_WL_t1.
        t1 = optvar('CM_t1', m, 1)
        for i in range(m):
            t = -b[i]
            for j in range(n):
                if (i,j) in A:
                    t = t + A[i,j]
            s += 'CM_t1[%d] = %s;\n' % (i, exprtoC(t))
        dt['CM_t1'] = s
        dt['fb_args'] = joinlist(bylowerstr(getparams(self.p)), True)

        writecode(self.outdir, self.skel, 'test2p.c', dt)
Exemplo n.º 11
0
    def fastbarr(self, As, bs, cs, m, n, p, suffix=''):
        dt = {}
        dt['header'] = header()
        dt['m'] = value(m)
        dt['n'] = value(n)
        dt['p'] = value(p)

        dt['suffix'] = suffix

        print 'Generating code for fast barrier method:'

        print 'Framing objective.'
        cs = value(cs)
        s = ''
        for i in range(n):
            s += 'CM_c[%d] = %s;\n' % (i, exprtoC(cs[i]))
        dt['CM_c'] = s

        print 'Creating method signature.'
        if getparams(self.p):
            dt['params'] = ', '.join(['double *%s' % x for x in \
                                      bylowerstr(getparams(self.p))])
            dt['params'] += ','
        else:
            dt['params'] = ''

        print 'Inspecting problem structure.'
        A = nzentries(As)
        AT = nzentries(tp(As))

        # Create a gh optvar simply to make it easy to convert the multiplication.
        gh = optvar('CM_gh', n, 1)

        # Now multiply and solve.
        print 'Generating reduced Newton system coefficient matrix.'
        (ASATnum, s, ss, p) = self.multstuff(A, AT, m, n)
        dt['CM_ASAT'] = s
        dt['CM_ASAT_size'] = len(ss)

        # Reverse the permutation. Ran into a weird bug when trying a fancier
        # scheme of inverting the permutation.
        pinv = [
            None,
        ] * len(p)
        for i in range(len(p)):
            pinv[p[i]] = i

        print 'Generating code for b = A*gh.'
        s = ''
        # Take note of the (inverse) permutation p.
        for i in range(m):
            d = 0
            for j in range(n):
                if (p[i], j) in A:
                    d = d - A[p[i], j] * gh[j]
            s += 'CM_b[%d] = %s;\n' % (i, exprtoC(d))
        dt['CM_b'] = s

        print 'Generating Cholesky factorization and solution code:'
        (d, (L, ssL, KL)) = self.cholsolve(ASATnum, ss)

        dt.update(d)

        print 'Generating code for dx.'
        s = ''
        w = optvar('CM_w', m, 1)
        for i in range(n):
            d = 0
            for j in range(m):
                if (i, j) in AT:
                    # Take note of the (inverse) permutation p.
                    d = d + AT[i, j] * w[pinv[j]]
            s += 'CM_dx[%d] = -CM_h[%d]*(%s + CM_g[%d]);\n' % (i, i,
                                                               exprtoC(d), i)
        dt['CM_dx'] = s

        writecode(self.outdir,
                  self.skel,
                  'fastbarr.c',
                  dt,
                  outname='fastbarr%s.c' % suffix)
Exemplo n.º 12
0
    def fastbarr(self, As, bs, cs, m, n, p, suffix=''):
        dt = {}
        dt['header'] = header()
        dt['m'] = value(m)
        dt['n'] = value(n)
        dt['p'] = value(p)

        dt['suffix'] = suffix

        print 'Generating code for fast barrier method:'

        print 'Framing objective.'
        cs = value(cs)
        s = ''
        for i in range(n):
            s += 'CM_c[%d] = %s;\n' % (i, exprtoC(cs[i]))
        dt['CM_c'] = s

        print 'Creating method signature.'
        if getparams(self.p):
            dt['params'] = ', '.join(['double *%s' % x for x in \
                                      bylowerstr(getparams(self.p))])
            dt['params'] += ','
        else:
            dt['params'] = ''

        print 'Inspecting problem structure.'
        A = nzentries(As)
        AT = nzentries(tp(As))

        # Create a gh optvar simply to make it easy to convert the multiplication.
        gh = optvar('CM_gh', n, 1)
        
        # Now multiply and solve.
        print 'Generating reduced Newton system coefficient matrix.'
        (ASATnum, s, ss, p) = self.multstuff(A, AT, m, n)
        dt['CM_ASAT'] = s
        dt['CM_ASAT_size'] = len(ss)

        # Reverse the permutation. Ran into a weird bug when trying a fancier
        # scheme of inverting the permutation.
        pinv = [None,]*len(p)
        for i in range(len(p)):
            pinv[p[i]] = i

        print 'Generating code for b = A*gh.'
        s = ''
        # Take note of the (inverse) permutation p.
        for i in range(m):
            d = 0
            for j in range(n):
                if (p[i], j) in A:
                    d = d - A[p[i],j]*gh[j]
            s += 'CM_b[%d] = %s;\n' % (i, exprtoC(d))
        dt['CM_b'] = s


        print 'Generating Cholesky factorization and solution code:'
        (d, (L, ssL, KL)) = self.cholsolve(ASATnum, ss)

        dt.update(d)

        print 'Generating code for dx.'
        s = ''
        w = optvar('CM_w', m, 1)
        for i in range(n):
            d = 0
            for j in range(m):
                if (i, j) in AT:
                    # Take note of the (inverse) permutation p.
                    d = d + AT[i,j]*w[pinv[j]]
            s += 'CM_dx[%d] = -CM_h[%d]*(%s + CM_g[%d]);\n' % (i, i, exprtoC(d), i)
        dt['CM_dx'] = s

        writecode(self.outdir, self.skel, 'fastbarr.c', dt,
                  outname='fastbarr%s.c' % suffix)