Ejemplo n.º 1
0
    def test_FISTA_cvx(self):
        if not cvx_not_installable:
            # Problem data.
            m = 30
            n = 20
            np.random.seed(1)
            Amat = np.random.randn(m, n)
            A = LinearOperatorMatrix(Amat)
            bmat = np.random.randn(m)
            bmat.shape = (bmat.shape[0], 1)

            # A = Identity()
            # Change n to equal to m.

            b = DataContainer(bmat)

            # Regularization parameter
            lam = 10
            opt = {'memopt': True}
            # Create object instances with the test data A and b.
            f = Norm2sq(A, b, c=0.5, memopt=True)
            g0 = ZeroFun()

            # Initial guess
            x_init = DataContainer(np.zeros((n, 1)))

            f.grad(x_init)

            # Run FISTA for least squares plus zero function.
            x_fista0, it0, timing0, criter0 = FISTA(x_init, f, g0, opt=opt)

            # Print solution and final objective/criterion value for comparison
            print(
                "FISTA least squares plus zero function solution and objective value:"
            )
            print(x_fista0.array)
            print(criter0[-1])

            # Compare to CVXPY

            # Construct the problem.
            x0 = Variable(n)
            objective0 = Minimize(0.5 * sum_squares(Amat * x0 - bmat.T[0]))
            prob0 = Problem(objective0)

            # The optimal objective is returned by prob.solve().
            result0 = prob0.solve(verbose=False, solver=SCS, eps=1e-9)

            # The optimal solution for x is stored in x.value and optimal objective value
            # is in result as well as in objective.value
            print(
                "CVXPY least squares plus zero function solution and objective value:"
            )
            print(x0.value)
            print(objective0.value)
            self.assertNumpyArrayAlmostEqual(numpy.squeeze(x_fista0.array),
                                             x0.value, 6)
        else:
            self.assertTrue(cvx_not_installable)
Ejemplo n.º 2
0
    def test_FBPD_Norm1_cvx(self):
        if not cvx_not_installable:
            opt = {'memopt': True}
            # Problem data.
            m = 30
            n = 20
            np.random.seed(1)
            Amat = np.random.randn(m, n)
            A = LinearOperatorMatrix(Amat)
            bmat = np.random.randn(m)
            bmat.shape = (bmat.shape[0], 1)

            # A = Identity()
            # Change n to equal to m.

            b = DataContainer(bmat)

            # Regularization parameter
            lam = 10
            opt = {'memopt': True}
            # Create object instances with the test data A and b.
            f = Norm2sq(A, b, c=0.5, memopt=True)
            g0 = ZeroFun()

            # Initial guess
            x_init = DataContainer(np.zeros((n, 1)))

            # Create 1-norm object instance
            g1 = Norm1(lam)

            # Compare to CVXPY

            # Construct the problem.
            x1 = Variable(n)
            objective1 = Minimize(0.5 * sum_squares(Amat * x1 - bmat.T[0]) +
                                  lam * norm(x1, 1))
            prob1 = Problem(objective1)

            # The optimal objective is returned by prob.solve().
            result1 = prob1.solve(verbose=False, solver=SCS, eps=1e-9)

            # The optimal solution for x is stored in x.value and optimal objective value
            # is in result as well as in objective.value
            print(
                "CVXPY least squares plus 1-norm solution and objective value:"
            )
            print(x1.value)
            print(objective1.value)

            # Now try another algorithm FBPD for same problem:
            x_fbpd1, itfbpd1, timingfbpd1, criterfbpd1 = FBPD(
                x_init, Identity(), None, f, g1)
            print(x_fbpd1)
            print(criterfbpd1[-1])

            self.assertNumpyArrayAlmostEqual(numpy.squeeze(x_fbpd1.array),
                                             x1.value, 6)
        else:
            self.assertTrue(cvx_not_installable)
Ejemplo n.º 3
0
# conda install -c cvxgrp cvxpy libgcc

# Whether to use or omit CVXPY
use_cvxpy = True
if use_cvxpy:
    from cvxpy import *

import numpy as np
import matplotlib.pyplot as plt

# Problem data.
m = 30
n = 20
np.random.seed(1)
Amat = np.random.randn(m, n)
A = LinearOperatorMatrix(Amat)
bmat = np.random.randn(m)
bmat.shape = (bmat.shape[0],1)

# A = Identity()
# Change n to equal to m.

b = DataContainer(bmat)

# Regularization parameter
lam = 10
opt = {'memopt':True}
# Create object instances with the test data A and b.
f = Norm2sq(A,b,c=0.5, memopt=True)
g0 = ZeroFun()
Ejemplo n.º 4
0
    def test_FISTA_Norm1_cvx(self):
        if not cvx_not_installable:
            try:
                opt = {'memopt': True}
                # Problem data.
                m = 30
                n = 20
                np.random.seed(1)
                Amat = np.random.randn(m, n)
                A = LinearOperatorMatrix(Amat)
                bmat = np.random.randn(m)
                bmat.shape = (bmat.shape[0], 1)

                # A = Identity()
                # Change n to equal to m.

                b = DataContainer(bmat)

                # Regularization parameter
                lam = 10
                opt = {'memopt': True}
                # Create object instances with the test data A and b.
                f = Norm2sq(A, b, c=0.5, memopt=True)
                g0 = ZeroFun()

                # Initial guess
                x_init = DataContainer(np.zeros((n, 1)))

                # Create 1-norm object instance
                g1 = Norm1(lam)

                g1(x_init)
                g1.prox(x_init, 0.02)

                # Combine with least squares and solve using generic FISTA implementation
                x_fista1, it1, timing1, criter1 = FISTA(x_init, f, g1, opt=opt)

                # Print for comparison
                print(
                    "FISTA least squares plus 1-norm solution and objective value:"
                )
                print(x_fista1.as_array().squeeze())
                print(criter1[-1])

                # Compare to CVXPY

                # Construct the problem.
                x1 = Variable(n)
                objective1 = Minimize(0.5 *
                                      sum_squares(Amat * x1 - bmat.T[0]) +
                                      lam * norm(x1, 1))
                prob1 = Problem(objective1)

                # The optimal objective is returned by prob.solve().
                result1 = prob1.solve(verbose=False, solver=SCS, eps=1e-9)

                # The optimal solution for x is stored in x.value and optimal objective value
                # is in result as well as in objective.value
                print(
                    "CVXPY least squares plus 1-norm solution and objective value:"
                )
                print(x1.value)
                print(objective1.value)

                self.assertNumpyArrayAlmostEqual(numpy.squeeze(x_fista1.array),
                                                 x1.value, 6)
            except SolverError as se:
                print(str(se))
                self.assertTrue(True)
        else:
            self.assertTrue(cvx_not_installable)