Demo entry 5474289

a

   

Submitted by anonymous on Jun 27, 2016 at 03:13
Language: Python. Code size: 1.5 kB.

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 26 2016

@author: Gabriel Sabença Gusmão
"""
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import linprog

n = 100 # coefficients
m = 1000 # data points

# Data generation, base matrices
A0 = np.random.rand(m,n) # 1000-by-100 random, A matrix
b0 = np.random.rand(m,1) # 1000-by-1 random, b array

# L_1 minimization
A_1 = np.concatenate((np.concatenate((A0,-A0),0),np.concatenate((-np.identity(m),-np.identity(m)),0)),1)
c_1 = np.concatenate((np.zeros([n,1]),np.ones([m,1])),0)
bounds_1 = np.concatenate((np.repeat([[None,None]],n,0),np.repeat([[0,None]],m,0)),0)
b_1 = np.concatenate((b0,-b0),0)

# L_` minimization
A_inf = np.concatenate((np.concatenate((A0,-A0),0),-np.ones([2*m,1])),1)
c_inf = np.concatenate((np.zeros([n,1]),np.ones([1,1])),0)
bounds_inf = np.concatenate((np.repeat([[None,None]],n,0),np.repeat([[0,None]],1,0)),0)
b_inf = np.concatenate((b0,-b0),0)

# solve L_1 minimization
sol_1 = linprog(c=c_1.reshape(np.shape(c_1)[0]).tolist(),A_ub=A_1,b_ub=b_1,bounds=bounds_1,options={'maxiter':1e6,'disp':True,'tol':1e-6})

# solve L_inf minimization
sol_inf = linprog(c=c_inf.reshape(np.shape(c_inf)[0]).tolist(),A_ub=A_inf,b_ub=b_inf,bounds=bounds_inf,options={'maxiter':1e6,'disp':True,'tol':1e-6})

# L_1 outputs
x_1 = sol_1.x[0:n]
mu_1 = sol_1.x[n:(n+m+1)]
z_1 = sol_1.x[(n+m+1):]

# L_inf outputs
x_inf = sol_inf.x[0:n]
mu_inf = sol_inf.x[n:n+2]
z_inf = sol_inf.x[n+2+1:]

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).