if alpha==0:
         print "**ERRO**"
         print "aplha=0, impossivel continuar o algorimto"
         break
     step_size=sigma/alpha
 error=(X*w_old)-Y    
 grad1=it.GetGradL(error,X)
 U=it.getU(w_old,step_size,grad1).todense()
 w_new=list(itertools.imap(soft,U.tolist(),[step_size*lamb for x in xrange(len(U.tolist()))]))
 w_new=np.matrix(w_new)
 w_new=w_new.transpose()
 dif=w_new-w_old
 dif=dif.transpose()*dif
 w_new=sparse.csr_matrix(w_new)
 error=(X*w_new)-Y
 y_new=it.get_func(error,w_new,lamb) #funcao de erro        
 count=0            
 if i!=0:
     while y_new>=y_old-sigma2*alpha*dif[0,0]:
         print "A diminuir step:",i
         step_size=step_size/10
         U=it.getU(w_old,step_size,grad1).todense()
         w_new=list(itertools.imap(soft,U.tolist(),[step_size*lamb for x in xrange(len(U.tolist()))]))
         w_new=np.matrix(w_new)
         w_new=w_new.transpose()
         error=(X*w_new)-Y
         dif=w_new-w_old
         dif=dif.transpose()*dif
         y_new=it.get_func(error,w_new,lamb) #funcao de erro
         count=count+1
         if count==40:
    y=[]
    #X,total=cria_dados.delcomun(X,total)

   

    ##Y=np.log2(Y)
    
    #dictionaryDev,totalDev,yDev=read_output("../le_ficheiro/dev.txt")
    #XDev,YDev=criaXY(dictionaryDev,total,yDev)
    
    #YDev=YDev/maxi
    #YDev=np.log10(YDev)
        
    W,erro_dev,lamb=calculaW(X,Y,Xteste,Yteste,Xdev,Ydev)
    error=(X*W-Y)
    print "OBJECTIVO:", it.get_func(error,W,lamb)
    '''
    print "___________________________________________________"
    print "___________________________________________________\n\n"
    print "lambda:",lamb
    print "erro do teste-----------------------\n", erro(f+"test.txt",W,total)
    #print "erro do treino-----------------------\n", erro("../le_ficheiro/train.txt",W,total)
    #print "erro do dev-----------------------\n", erro("../le_ficheiro/dev.txt",W,total)
    print "__________________________"
    print "W:"
    print W
    error=(X*W)-Y
    print "----------------------------------"
    print "funcao de custo:"
    print it.get_func(error,W,lamb)
    #print "----------------------------------"
Ytotal=pickle.load(file)

file=open("../dados_priberam/dados_W_random.pkl","rb")
W=pickle.load(file)



trainsize=int(Xtotal.shape[0]*0.8)
devsize=int(Xtotal.shape[0]*0.1)+trainsize
lamb=10
max_iter=10000
vec=sparse.csr_matrix([0 for i in xrange(Xtotal.shape[1])])
vec=vec.transpose()
#pdb.set_trace()
error=Xtotal[:trainsize,:]*W - Ytotal[:trainsize,:]
custo=it.get_func(error,W,lamb)
print custo
#w_estimado=grad.grad(Xtotal[:trainsize,:],Ytotal[0:trainsize,0],vec,Xtotal[devsize:,:],Ytotal[devsize:,0],Xtotal[trainsize:devsize,:],Ytotal[trainsize:devsize,0],custo,lamb)
#w_estimado,yy1,xx1=grad_old.grad(Xtotal,Ytotal,vec,Xtotal,Ytotal,Xtotal,Ytotal,lamb,max_iter,True)
#w_estimado,yy2,xx2=grad_old.grad(Xtotal,Ytotal,vec,Xtotal,Ytotal,Xtotal,Ytotal,lamb,max_iter,False)
#w_estimado,yy3,xx3=grad.grad(Xtotal,Ytotal,vec,Xtotal,Ytotal,Xtotal,Ytotal,max_iter)
w0=[]
w1=[]
w2=[]
w3=[]
w4=[]
lambs=[0.1,1,10,10e2,10e3,10e4,10e5,10e6,10e7,10e8,10e9,10e10,10e11]
xg=[np.log10(x) for x in lambs]
'''
for  lamb in lambs:
    w_estimado,yy3,xx3=grad.grad(Xtotal,Ytotal,vec,Xtotal,Ytotal,Xtotal,Ytotal,max_iter,lamb)
def lasso(X,Y,w0,total):
    import matplotlib.pyplot as plt   
    print "A fazer calculos..." 
    w_new=np.zeros(w0.shape)
    Y=sparse.csr_matrix(np.log2(Y.todense()))
    final=[0,0]

    #Use a fixed small step size 
    step_size = 0.00000002
    #max iterations
    max_iter = 1000
    yyy=[]
    xxx=[]
    it.soft=soft
    for lamb in [10**x for x in xrange(-12,-9)]:
    #for lamb in [0.000001]:
        w_old=w0
        yy=[]
        
        xx=[]
        
               
        print "calculos terminados, a fazer iterecoes"
        for i in xrange(max_iter):
            
            error=(X*w_old)-Y    
            grad1=it.GetGradL(error,X)
            U=it.getU(w_old,step_size,grad1).todense()
            w_new=list(itertools.imap(soft,U.tolist(),[step_size*lamb for x in xrange(len(U.tolist()))]))
            w_new=np.matrix(w_new)
            w_new=w_new.transpose()
            error=(X*w_new)-Y
            y_new=it.get_func(error,w_old,lamb) #funcao de erro
            y_old=y_new
            w_old = sparse.csr_matrix(w_new)
            yy.append(y_new)
            xx.append(i)
            
        print "exceeded maximum number of iterations, leaving"
        
        media=RRegression_beta.erro("../le_ficheiro/dev.txt",w_new,total)
        
        print lamb,media
        if final[0]>media or final[0]==0:
            final[0]=media
            final[1]=lamb
            graphFinal=yy
    
        
        
        zero=0.0
        for J in xrange(w_new.shape[0]):
            if w_new[J,0]==0:
                zero=zero+1.0
                #print "zero->",zero
                
        
        sp=(zero/w_new.shape[0])*100
        
        print "percentagem:",sp        
        yyy.append(sp)
        xxx.append(lamb)
    
    
    plt.figure(1)
    plt.subplot(211)
    plt.title("Funcao de custo")
    plt.plot(xx,graphFinal,"r")
    plt.subplot(212)
    plt.title("Percentagem de W com valor =0")
    import pylab    
    print yyy
    #pylab.ylim([0,100])
    plt.plot(yyy,"b",yyy,"ro")    
    plt.show()  
    return w_new,y_new