lunes, 21 de mayo de 2012

OCTAVE

% Parameter M adjacency matrix where M_i,j represents the link from 'j' to 'i', such that for all 'j' sum(i, M_i,j) = 1
% Parameter d damping factor
% Parameter v_quadratic_error quadratic error for v
% Return v, a vector of ranks such that v_i is the i-th rank from [0, 1]
 
function [v] = rank(M, d, v_quadratic_error)

N = size(M, 2);  % N is equal to half the size of M
v = rand(N, 1);
v = v ./ norm(v, 2);
last_v = ones(N, 1) * inf;
M_hat = (d .* M) + (((1 - d) / N) .* ones(N, N));

    while(norm(v - last_v, 2) > v_quadratic_error)
        last_v = v;
        v = M_hat * v;
        v = v ./ norm(v, 2);
    end

endfunction



function [M]=GenerarM(size)
M=sparse(size,size);

for i=1:size
    for j=1:size
        if (i!=j)
            if (rand(1)>=0.75)
                M(i,j)=1;
            endif
        endif
    endfor
endfor 

MuestraM=full(M)

for j=1:size
    suma=0;
    for i=1:size
        suma=suma+M(i,j);        
    endfor
    for i=1:size
        M(i,j)=M(i,j)/suma;        
    endfor
endfor 



M=full(M);



endfunction


v=rank(GenerarM(15), 0.80, 0.001)

bar(v,'facecolor',"green")
title('Page Rank de las Paginas','Fontsize',48);
ylim([0,1])
xlim([0,16])  
xlabel('NUMERO DE PAGINA','Fontsize',32)
ylabel('PAGE RANK','Fontsize',32)


print -djpg "grafica.jpg"


lunes, 7 de mayo de 2012

R - Tarea


R is an open source programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians for developing statistical software and data analysis.
 Herramientas de cómputo - R
Archivo datos.dat
   Observation Gender Dosage Alertness
1            1      m      a         8
2            2      m      a        12
3            3      m      a        13
4            4      m      a        12
5            5      m      b         6
6            6      m      b         7
7            7      m      b        23
8            8      m      b        14
9            9      f      a        15
10          10      f      a        12
11          11      f      a        22
12          12      f      a        14
13          13      f      b        15
14          14      f      b        12
15          15      f      b        18
16          16      f      b        22
> file=datos.dat
> data.example=read.table(file,header=T)   
> data.example                             
> aov.ex2 = aov(Alertness~Gender*Dosage,data=data.example)
> summary(aov.ex2)                                
> print(model.tables(aov.ex2,"means"),digits=3)

lunes, 16 de abril de 2012

Python - Búsqueda Local Interrutas







#DATOS

distance=[
[0.00,16.40,13.34,9.21,10.29,12.20,6.32,6.40,17.72,6.08,12.04,4.00,10.44,3.16,7.21,9.84,9.43,14.14,10.44,13.6,14.31],
[16.40,0.00,13,8.94,19.41,6.00,13.89,10.00,18.78,12.65,25.05,19.10,16.00,19.41,17.46,25.49,21.58,23.00,25.49,30.00,26.30],
[13.34,13.00,0.00,6.4,7.21,14.31,7.07,10.00,8.94,15.00,15.00,13.03,20.61,16.12,18.78,18.02,22.47,26.41,23.08,24.51,27.51],
[9.21,8.94,6.40,0.00,11.00,8.24,5.00,4.47,9.84,8.94,16.12,10.81,14.42,12.36,13.60,17.02,17.26,20.61,19.23,22.36,22.36],
[10.29,19.41,7.21,11.00,0.00,19.10,7.61,13.60,4.47,17.00,8.54,10.29,22.47,14.42,19.84,13.00,22.02,26.87,20.61,20.02,26.62],
[12.2,6.00,14.31,8.24,19.10,0.00,12.04,6.32,17.11,7.21,22.80,15.65,10.00,14.86,11.70,21.95,15.81,17.00,20.24,25.45,20.39],
[6.32,13.89,7.07,5.00,7.61,12.04,0.00,6.08,5.09,9.43,11.18,6.32,15.00,9.05,12.8,12.2,15.65,20.00,16.03,18.02,20.61],
[6.40,10.00,10.00,4.47,13.60,6.32,6.08,0.00,11.00,4.47,16.49,9.43,10.00,9.43,9.21,15.81,13.03,16.15,15.81,20.00,18.11],
[17.72,18.78,8.94,9.84,4.47,17.11,5.09,11.00,0.00,13.60,6.40,5.83,18.78,10.00,15.81,9.21,17.69,22.67,16.15,16.03,22.2],
[6.08,12.65,15.00,8.94,17.00,7.21,9.43,4.47,13.60,0.00,18.00,10.04,5.65,8.06,5.00,15.81,9.05,11.70,13.03,18.43,14.00],
[12.04,25.05,15.00,16.12,8.54,22.80,11.18,16.49,6.40,18.00,0.00,8.06,22.36,11.70,18.68,5.83,19.23,24.59,15.55,12.65,22.80],
[4.00,19.10,13.03,10.81,10.29,15.65,6.32,9.43,5.83,10.04,8.06,0.00,14.31,4.24,10.77,6.40,12.04,17.2,12.80,11.70,16.40],
[10.44,16.00,20.61,14.42,22.47,10.00,15.00,10.00,18.78,6.65,22.36,14.31,0.00,11.00,4.12,19.02,7.07,7.00,13.03,19.69,10.77],
[3.16,19.41,16.12,12.36,14.42,14.86,9.05,9.43,10.00,8.06,11.70,4.24,11.00,0.00,7.07,8.06,7.81,13.03,7.00,10.63,12.20],
[7.21,17.46,18.78,13.60,19.84,11.70,12.80,9.21,15.81,5.00,18.68,10.77,4.12,7.07,0.00,15.00,4.12,7.21,9.21,15.65,9.00],
[9.84,25.49,18.02,17.02,13.00,21.95,12.20,15.81,9.21,15.81,5.83,6.40,19.02,8.06,15.00,0.00,14.56,19.92,10.00,7.07,17.49],
[9.43,21.58,22.47,17.26,22.02,15.81,15.65,13.03,17.69,9.05,19.23,12.04,7.07,7.81,4.12,14.56,0.00,5.38,6.32,13.34,5.09],
[14.14,23.00,26.41,20.61,26.87,17.00,20.00,16.15,22.67,11.70,24.59,17.20,7.00,13.03,7.21,19.92,5.38,0.00,11.00,18.02,5.00],
[10.44,25.49,23.08,19.23,20.61,20.24,16.03,15.81,16.15,13.03,15.55,12.80,13.03,7.00,9.21,10.00,6.32,11.00,0.00,7.07,7.00],
[13.60,30.00,24.51,22.36,20.02,25.45,18.02,20.00,16.03,18.43,12.65,11.70,19.69,10.63,15.65,7.07,13.34,18.02,7.07,0.00,14.14],
[14.31,26.30,27.51,22.36,26.62,20.39,20.61,18.11,22.20,14.00,22.80,16.40,10.77,12.2,9.00,17.49,5.09,5.00,7,14.14,0.00],
]

coordenadas=[[0,15,12],[1,2,2],[2,2,15],[3,6,10],[4,6,21],[5,8,2],[6,9,14],[7,10,8],[8,10,19],[9,14,6],[10,14,24],[11,15,16],[12,18,2],[13,18,13],[14,19,6],[15,19,21],[16,23,7],[17,25,2],[18,25,13],[19,26,20],[20,28,6]]
#DATOS

#FUNCIONES
def city(x,y,k):
res=750
r=15
xnorm=res/30
ynorm=res/28
x=x*xnorm
y=y*ynorm
canvas.create_oval(x-r, y-r, x+r, y+r, width = 0, fill = '#0000FF')
canvas.create_text(x-r-5,y-r-5,text=k,fill="black")


def depot(x,y):
res=750
r=20
xnorm=res/30
ynorm=res/28
x=x*xnorm
y=y*ynorm
canvas.create_oval(x-r, y-r, x+r, y+r, width = 0, fill = '#FF0000')
canvas.create_text(x,y-r-8,text="Centro",fill="black")

def flecharp(city1,city2):
res=750
r=10
xnorm=res/30
ynorm=res/28
x1=xnorm*coordenadas[city1][1]
y1=ynorm*coordenadas[city1][2]
x2=xnorm*coordenadas[city2][1]
y2=ynorm*coordenadas[city2][2]
canvas.create_line(x1, y1, x2, y2, width=4, arrow=LAST, dash=(2,2), fill='#FF0000')

def flechavp(city1,city2):
res=750
r=10
xnorm=res/30
ynorm=res/28
x1=xnorm*coordenadas[city1][1]
y1=ynorm*coordenadas[city1][2]
x2=xnorm*coordenadas[city2][1]
y2=ynorm*coordenadas[city2][2]
canvas.create_line(x1, y1, x2, y2, width=4, arrow=LAST, dash=(2,2), fill='#FFA500')


def flechan(city1,city2):
res=750
r=10
xnorm=res/30
ynorm=res/28
x1=xnorm*coordenadas[city1][1]
y1=ynorm*coordenadas[city1][2]
x2=xnorm*coordenadas[city2][1]
y2=ynorm*coordenadas[city2][2]
canvas.create_line(x1, y1, x2, y2, width=6, arrow=LAST, fill='#000000')

def flechaa(city1,city2):
res=750
r=10
xnorm=res/30
ynorm=res/28
x1=xnorm*coordenadas[city1][1]
y1=ynorm*coordenadas[city1][2]
x2=xnorm*coordenadas[city2][1]
y2=ynorm*coordenadas[city2][2]
canvas.create_line(x1, y1, x2, y2, width=6, arrow=LAST, fill='#800080')


def near(jaja):
k=0
distance_near_city=999999
while (k<=19):
exist=(k in ruta)
if(distance[jaja][k]0.00 and (not exist) ):
distance_near_city=distance[jaja][k]
best_near_city=k
k=k+1
return best_near_city


def graficar_rutaON(r):
k=0
while(k<=(len(r)-2)):
flechan(r[k],r[k+1])
k=k+1

def graficar_rutaOA(r):
k=0
while(k<=(len(r)-2)):
flechaa(r[k],r[k+1])
k=k+1

def graficar_rutaMRP(r):
k=0
while(k<=(len(r)-2)):
flecharp(r[k],r[k+1])
k=k+1

def graficar_rutaMVP(r):
k=0
while(k<=(len(r)-2)):
flechavp(r[k],r[k+1])
k=k+1

def distancia_ruta(ruta):
distancia=0
k=0
while (k<(len(ruta)-1)):
distancia=distancia+distance[ruta[k]][ruta[k+1]]
k += 1
return distancia

#FUNCIONES




#IMPRIMIR GRAFO
from Tkinter import *
from sys import argv

tk = Tk() # controlador de ventana
res=750
canvas = Canvas(tk, width = res, height = res, bg='#FFFFFF') # fondo de ventana

depot(15,12)
k=1
while (k<=20):
city(coordenadas[k][1],coordenadas[k][2],k)
k=k+1
#FIN IMPRIMIR GRAFO


#HEURISTICA INTERRUTAS

instancia=int(sys.argv[1])


if (instancia==1):
ruta1=[0,19,20,17,16,14,12,9,5,1,6,7,0]
ruta2=[0,3,2,4,8,10,15,18,13,11,0]

if (instancia==2):
ruta1=[0,8,4,2,1,5,7,9,12,14,17,0]
ruta2=[0,3,6,10,15,11,13,19,18,20,16,0]

if (instancia==3):
ruta1=[0,9,10,17,8,16,14,13,18,19,15,0]
ruta2=[0,11,12,20,4,6,3,2,1,5,7,0]






graficar_rutaON(ruta1)
graficar_rutaOA(ruta2)

print "Distancia Ruta 1", distancia_ruta(ruta1)
print "Distancia Ruta 2", distancia_ruta(ruta2)
orig_ruta1=distancia_ruta(ruta1)
orig_ruta2=distancia_ruta(ruta2)

print "Ruta 1 Original", ruta1
print "Ruta 2 Original", ruta2


#BUSQUEDA LOCAL
#PRUEBA INTERCAMBIANDO CADA UNO DE LAS CIUDADES EN LAS RUTAS PARA VER SI EXISTE UN INTERRUTEO CON DISTANCIA RECORRIDA MAS CORTA

j,k=1,1

optimoinicial=5
optimofinal=1

while(optimofinal optimoinicial=distancia_ruta(ruta1)+distancia_ruta(ruta2)
k,j=1,1
while(j<(len(ruta1)-1) ):
k=1
while(k<(len(ruta2)-1) ):
original=distancia_ruta(ruta1)+distancia_ruta(ruta2)
ruta1[j],ruta2[k]=ruta2[k],ruta1[j]
modified=distancia_ruta(ruta1)+distancia_ruta(ruta2)
print "Paso ",j," Paso ",k," Original=",original," Modified=",modified
if(modified O_ruta1=ruta1
O_ruta2=ruta2
print "Cambio"
else:
ruta1[j],ruta2[k]=ruta2[k],ruta1[j]

k += 1
j += 1
optimofinal=distancia_ruta(ruta1)+distancia_ruta(ruta2)



print "Ruta1: ", O_ruta1
print "Ruta2: ", O_ruta2

graficar_rutaMRP(ruta1)
graficar_rutaMVP(ruta2)

print "Distancia Nueva Ruta 1=", distancia_ruta(ruta1)," (cambio de ",orig_ruta1," a ",distancia_ruta(ruta1)," )"
print "Distancia Nueva Ruta 2=", distancia_ruta(ruta2)," (cambio de ",orig_ruta2," a ",distancia_ruta(ruta2)," )"



#BUSQUEDA LOCAL



#HEURISTICA INTERRUTAS




canvas.pack()
tk.mainloop()


martes, 13 de marzo de 2012

SED + GNUPLOT






Codigo de Bash

#!/bin/bash -f

sed 's/\.1.\{1,\}/\.1/g' < datos.browser| sed 's/\.2.\{1,\}/\.2/g'|
sed 's/\.3.\{1,\}/\.3/g'| sed 's/\.4.\{1,\}/\.4/g'|
sed 's/\.5.\{1,\}/\.5/g'| sed 's/\.6.\{1,\}/\.6/g'|
sed 's/\.7.\{1,\}/\.7/g'| sed 's/\.8.\{1,\}/\.8/g'|
sed 's/\.9.\{1,\}/\.9/g'| sed 's/\.0.\{1,\}/\.0/g' > datos-p.browser

cat datos-p.browser chartok.plot > chartok2.plot

gnuplot chartok2.plot

echo "Listo"





Codigo de GNUPLOT

reset

IE=35.8
Chrome=30.8
Firefox=20.5
Safari=6.5
Opera=4.3
NetScape=3.6

set term svg
set output 'chart.svg'

total=IE+Chrome+Firefox+Safari+Opera+NetScape

IEn=IE/total
Chromen=Chrome/total
Firefoxn=Firefox/total
Safarin=Safari/total
Operan=Opera/total
NetScapen=NetScape/total

b=0.4
s=0.2
B=0.5

set view 30, 20
set parametric
unset border
unset tics
unset key
set ticslevel 0
unset colorbox
set urange [0:1]
set vrange [0:1]
set xrange [-2:2]
set yrange [-2:2]
set zrange [0:3]
set multiplot

# First, we draw the 'box' around the plotting volume
set palette model RGB functions 0.9, 0.9,0.95
splot -2+4*u, -2+4*v, 0 w pm3d

set palette model RGB functions 0.8, 0.8, 0.85

set urange [IEn:1]
splot cos(u*2*pi)*v, sin(u*2*pi)*v, 0 w pm3d

set urange [0:IEn]
splot cos(IEn*pi)*b+cos(u*2*pi)*v, sin(IEn*pi)*b+sin(u*2*pi)*v, 0 w pm3d

set palette model RGB functions 1, 0, 0
set urange [0:IEn]
splot cos(IEn*pi)*b+cos(u*2*pi)*v, sin(IEn*pi)*b+sin(u*2*pi)*v, s w pm3d

set palette model RGB functions 0, 0.5, 0
set urange [IEn:IEn+Chromen]
splot cos(u*2*pi)*v, sin(u*2*pi)*v, s w pm3d

set palette model RGB functions 0, 0, 0.8
set urange [IEn+Chromen:IEn+Chromen+Firefoxn]
splot cos(u*2*pi)*v, sin(u*2*pi)*v, s w pm3d

set palette model RGB functions 0.8, 0, 0.8
set urange [IEn+Chromen+Firefoxn:IEn+Chromen+Firefoxn+Safarin]
splot cos(u*2*pi)*v, sin(u*2*pi)*v, s w pm3d

set palette model RGB functions 0.8, 0.8, 0
set urange [IEn+Chromen+Firefoxn+Safarin:IEn+Chromen+Firefoxn+Safarin+Operan]
splot cos(u*2*pi)*v, sin(u*2*pi)*v, s w pm3d

set palette model RGB functions 0, 0.8, 0.8
set urange [IEn+Chromen+Firefoxn+Safarin+Operan:IEn+Chromen+Firefoxn+Safarin+Operan+NetScapen]
set label 1 'IE' at cos(IEn*pi)*B+cos(IEn*pi), sin(IEn*pi)*B+sin(IEn*pi)
D = 2*IEn+Chromen
set label 2 'Chrome' at cos(D*pi)*B+cos(D*pi), sin(D*pi)*B+sin(D*pi)
D=D+Chromen+Firefoxn
set label 3 'Firefox' at cos(D*pi)*B+cos(D*pi), sin(D*pi)*B+sin(D*pi)
D=D+Firefoxn+Safarin
set label 4 'Safari' at cos(D*pi)*B+cos(D*pi), sin(D*pi)*B+sin(D*pi)
D=D+Safarin+Operan
set label 5 'Opera' at cos(D*pi)*B+cos(D*pi), sin(D*pi)*B+sin(D*pi)
D=D+Operan+NetScapen
set label 6 'NetScape' at cos(D*pi)*B+cos(D*pi), sin(D*pi)*B+sin(D*pi)
splot cos(u*2*pi)*v, sin(u*2*pi)*v, s w pm3d
unset multiplot








lunes, 13 de febrero de 2012

Tarea de AWK





¿Que es AWK?


AWK es un lenguaje de programación diseñado para procesar datos basados en texto, ya sean ficheros o flujos de datos. El nombre AWK deriva de los apellidos de los autores: Alfred Aho, Peter Weinberger, y Brian Kernighan. awk, cuando está escrito todo en minúsculas, hace referencia al programa de Unix o Plan 9 que interpreta programas escritos en el lenguaje de programación AWK.

AWK es ejemplo de un lenguaje de programación que usa ampliamente el tipo de datos de listas asociativas (es decir, listas indexadas por cadenas clave), y expresiones regulares. El poder, brevedad y limitaciones de los programas de AWK y los guiones de sed inspiraron a Larry Wall a escribir Perl. Debido a su densa notación, todos estos lenguajes son frecuentemente usados para escribir programas de una línea.



Presentación:










Codigo de BASH Llamando a AWK
echo "Escriba el Nombre del Archivo Fuente: ";
read file

chmod +x tornillos.awk


./tornillos.awk < $file > "Result_"$file

echo "Listo"



Codigo de AWK ejecutando instrucciones




#!/usr/bin/awk -f

BEGIN {
T1=0;
T2=0;
T3=0;
T4=0;
T5=0;
T6=0;

A_T1=0;
A_T2=0;
A_T3=0;
A_T4=0;
A_T5=0;
A_T6=0;
check=FALSE;

year=1900;
month=0;
}
{
c_year=substr($1, 7, 4);
c_month=substr($1, 4, 2);


if (c_month != month){

if(month != 0){
print "Mes:" month " |" T1 " |" T2 " |" T3 " |" T4 " |" T5 " |" T6;
}

month=c_month;
T1=0;
T2=0;
T3=0;
T4=0;
T5=0;
T6=0;
}

if (c_year != year){


A_B_TOTAL=A_T1+A_T2+A_T3+A_T4+A_T5+A_T6;




if(year != 1900){
print " ";
print "Tot(T):" year " |" A_T1 " |" A_T2 " |" A_T3 " |" A_T4 " |" A_T5 " |" A_T6;
print "Total de Gramos de Tornillos del Año " year " : " A_B_TOTAL;
}
print " "
print "--------------------------------------------------------------";
print "Año:" c_year " |T1 |T2 |T3 |T4 |T5 |T6";


year=c_year

A_T1=0
A_T2=0
A_T3=0
A_T4=0
A_T5=0
A_T6=0

}


T1=T1+$2
T2=T2+$3
T3=T3+$4
T4=T4+$5
T5=T5+$6
T6=T6+$7

A_T1=A_T1+$2
A_T2=A_T2+$3
A_T3=A_T3+$4
A_T4=A_T4+$5
A_T5=A_T5+$6
A_T6=A_T6+$7


}
END {

}

lunes, 30 de enero de 2012

TAREA DE BASH





Bueno este es el programa de BASH para la tarea

Es un algoritmo para Generar o "desencriptar" un codigo MD5 que es un metodo de encriptacion de un solo sentido , Genera el HASH MD5 para cualquier caracter pero este codigo solamente desencripta cadenas de NUMEROS por ahora ya que para decifrar uno con caracteres alganumericos se necesitarian muchas corridas



echo "JUGANDO CON MD5"
echo "Quiere Generar un Codigo o Desencriptarlo? 1-Generar 2-Desencriptar"
read dec

if [ $dec -eq 1 ]
then
echo "Ponga la Frase a encriptar"
read captura
echo "MD5 Generado: "
echo -n $captura|md5sum
echo "Listo"

elif [ $dec -eq 2 ]
then
echo "Ponga el codigo MD5 que desea Desencriptar"
read capturamd5

i=0

while [ $i -le 999999 ]
do

cadena=`echo -n $i|md5sum`

if [ "$cadena"="$capturamd5" ]
then
echo "La cadena original es:" $i
fi

i=$(($i+1))
done


fi










VERSION ONLINE CON PHP

DESCARGAR ARCHIVO md5.sh