domingo, 23 de octubre de 2011

End.

I started this blog many years ago, and i posted new notes while i studied my engineer degree.
Now, it is time to close.
Best regards.

martes, 11 de enero de 2011

Nested loop optimized with Java Iterators

Few days ago, i saw a piece of a geoprocessing algorithm code. This algorithm get a vector of points, each point with two spatial coordinates: (x,y), and calculate the euclidean distance between of each point with the whole points of the vector. The result is a beatifull matrix of distance between each points.

The problem was the follow: this algorithm use "Iterators" on the vector, as follows:

Iterator i1=layer.iterator();
while(i1.hasNext()){
i1.next();
Iterator i2=layer.iterator();
while(i2.hasNext()){
i2.next();
// calculate Euclidean distance
}
}

but it has a problem in optimization terms, because the distance between point #1 and point #2 is the same that between point #2 and point #1, i.e.,an optimized algorithm only need compute the superior diagonal of the matrix, beacuse the matrix of distances is simetric.

In above sample, with optimized algorithm, we need 3 iterations on the vector and symmetrize the matrix; in the other hand without optimization we need 9 iterations: n^2.

If we assume that the objects in the vector are unique, the best solution in order of do not lose the semantic of Iterators is the use the follows code:

Iterator i1 = l.iterator();
while (i1.hasNext()) {
Integer ii1 = (Integer) i1.next();
Iterator i2 = l.subList(l.indexOf(ii1),l.size()).iterator();
while (i2.hasNext()){
i2.next();
}
}

If we use two index in order to follow the loops, we can use the follows code:

int i=0;
Iterator i1 = l.iterator();
while (i1.hasNext()) {
c=0:
Integer ii1 = (Integer) i1.next();
Iterator i2 = l.subList(i,l.size()).iterator();
while (i2.hasNext()){
i2.next();
long distance = ....
matrix[i][j] = distance;
matrix[j][i] = distance;
c++;
}
i++;
}

In this way we can reduce the computing time in n/2.

viernes, 29 de octubre de 2010

Smart pointers. shared_ptr and normal pointer

Shared pointer can make our code more secure. shared_pointer is a kind of pointer that allow to others pointer share his container. And when no pointer points to a container, the garbage collector erase the container thus ensuring the reliable of the program in order to no consume the heap of the process.

If we use the next code, the above behaviour.

The follow code show the common error, creating new pointers and not erase the container.


If we compile and execute this two codes, we obtain the next statistical result:

martes, 5 de octubre de 2010

snGraph. Optimal software to manage scale-free networks

snGraph package provides a flexible and efficient tool for manage graphs representing scale-free network. Can be integrated into others informatic systems. It can be read easily from databases, for example using Hibernate, and build graphs using models. It can serve of bridge between data and software in order to make analisis, for example we can use Ucinet. Also permite design and implement new custom algorithms.

lunes, 14 de junio de 2010

Caminar los sueños

La rutina es un papel de lija que desgasta las ilusiones. Demasiadas veces lo cotidiano nos conduce a la monótona repetición de conductas, conversaciones y escaramucillas sin vuelo que transforman las hojas de nuestro calendario en un libro sin texto. Por el contrario, las ilusiones conseguidas son aquellas que quedan impresas para siempre en el libro del mejor recuerdo, esas épocas en las que tomamos conciencia de que el auténtico nivel de vida no lo da ni depende del dinero, sino de la felicidad, ese sentimiento que surge cuando lo soñado y lo vivido transcurren paralelos como los raíles del ferrocarril.

Por eso siempre hay que llevar una doble vida: la despierta y la soñada.

La vida despierta es ese obligado aterrizaje en el suelo duro que nos conduce a través de caminos proyectados por intereses ajenos, cuanto más masivos mas semáforos, radares, velocidades limitadas y direcciones prohibidas.
La vida soñada es la que nos impulsa a salir de lo establecido y nos anima a idear, imaginar,......elevarnos para buscar nuestros propios horizontes.
En una vida completa, soñar y caminar son vasos comunicantes, porque el ave no puede estar siempre volando, pero alzarse le permite divisar, entender, y porque no, ambicionar otros panoramas.

En el suelo reposa lo conocido y cotidiano; en el vuelo despega el sueño y la sana ambición.
La felicidad es caminar los sueños.

Ángela Becerra, ADN, 9 Junio 2010.
abecerra@adn.es

viernes, 30 de abril de 2010

Parallel algorithms. Java threads. Using multiple cores.

Today, computar machines has a multiples cores into his processors. The main problem is that the software engineers do not design the software accord this situation because they ignore this new technology or simply because they doesn´t want design parallel algorithms. This problem imply that the multiple core machines are underused.

I propose a simple problem: Adding an integer array of ten million positions with a due core machine.

  1. Only one process
  2. Four threads. A piece of vector by thread
  3. Two threads. A piece of vector by thread
The results:



Java Readers and Writer. Mutual exclusion.

Think about this situation, you have a buffer and into the bufer has one data. We have several elements in this situation: One Writer, that provide tha data into the buffer, and many Readers, thtat need the data into the buffer; and one restriction, only one persona can read or write into the buffer at the same time.

In order to resolve this problem we can use a Monitor. A monitor is a Class that provide a mutual exclusion in this methods. To write this in Java, we can introduce the keyword synchronized in the method. For ejample, in Buffer.java:




We can see complete execution over Main class



Output:

run:
Reader 0 is waiting to get data (total buffer=0)
Reader 9 is waiting to get data (total buffer=0)
Reader 8 is waiting to get data (total buffer=0)
Reader 7 is waiting to get data (total buffer=0)
Reader 3 is waiting to get data (total buffer=0)
Reader 6 is waiting to get data (total buffer=0)
Reader 2 is waiting to get data (total buffer=0)
Reader 1 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 0 unlock
Reader 0 has read a data (buff=0)
........reader 9 unlock
Reader 9 is waiting to get data (total buffer=0)
Reader 4 is waiting to get data (total buffer=0)
Reader 5 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 8 unlock
Reader 8 has read a data (buff=0)
........reader 7 unlock
Reader 7 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 3 unlock
Reader 3 has read a data (buff=0)
........reader 6 unlock
Reader 6 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 2 unlock
Reader 2 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
Reader 28 is waiting to get data (total buffer=0)
Reader 29 is waiting to get data (total buffer=0)
Reader 20 is waiting to get data (total buffer=0)
Reader 11 is waiting to get data (total buffer=0)
Reader 13 is waiting to get data (total buffer=0)
Reader 16 is waiting to get data (total buffer=0)
Reader 27 is waiting to get data (total buffer=0)
Reader 10 is waiting to get data (total buffer=0)
Reader 18 is waiting to get data (total buffer=0)
Reader 24 is waiting to get data (total buffer=0)
Reader 23 is waiting to get data (total buffer=0)
Reader 17 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 9 unlock
Reader 9 has read a data (buff=0)
........reader 4 unlock
Reader 4 is waiting to get data (total buffer=0)
Reader 21 is waiting to get data (total buffer=0)
Reader 22 is waiting to get data (total buffer=0)
Reader 19 is waiting to get data (total buffer=0)
Reader 12 is waiting to get data (total buffer=0)
Reader 25 is waiting to get data (total buffer=0)
Reader 26 is waiting to get data (total buffer=0)
Reader 14 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 5 unlock
Reader 5 has read a data (buff=0)
........reader 7 unlock
Reader 7 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 6 unlock
Reader 6 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
Reader 15 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 28 unlock
Reader 28 has read a data (buff=0)
........reader 29 unlock
Reader 29 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 20 unlock
Reader 20 has read a data (buff=0)
........reader 11 unlock
Reader 11 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 13 unlock
Reader 13 has read a data (buff=0)
........reader 16 unlock
Reader 16 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 27 unlock
Reader 27 has read a data (buff=0)
........reader 10 unlock
Reader 10 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 18 unlock
Reader 18 has read a data (buff=0)
........reader 24 unlock
Reader 24 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 23 unlock
Reader 23 has read a data (buff=0)
........reader 17 unlock
Reader 17 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 4 unlock
Reader 4 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 22 unlock
Reader 22 has read a data (buff=0)
........reader 19 unlock
Reader 19 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 12 unlock
Reader 12 has read a data (buff=0)
........reader 25 unlock
Reader 25 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 26 unlock
Reader 26 has read a data (buff=0)
........reader 14 unlock
Reader 14 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 7 unlock
Reader 7 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 15 unlock
Reader 15 has read a data (buff=0)
........reader 29 unlock
Reader 29 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 11 unlock
Reader 11 has read a data (buff=0)
........reader 16 unlock
Reader 16 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 10 unlock
Reader 10 has read a data (buff=0)
........reader 24 unlock
Reader 24 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 17 unlock
Reader 17 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 19 unlock
Reader 19 has read a data (buff=0)
........reader 25 unlock
Reader 25 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 14 unlock
Reader 14 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 29 unlock
Reader 29 has read a data (buff=0)
........reader 16 unlock
Reader 16 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 24 unlock
Reader 24 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 25 unlock
Reader 25 has read a data (buff=0)
........reader 1 unlock
Reader 1 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 16 unlock
Reader 16 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 1 unlock
Reader 1 has read a data (buff=0)
........reader 21 unlock
Reader 21 is waiting to get data (total buffer=0)
The writer is writing data (buffer:1)
........reader 21 unlock
Reader 21 has read a data (buff=0)
The writer is writing data (buffer:1)
The writer is writing data (buffer:2)
The writer is writing data (buffer:3)
The writer is writing data (buffer:4)
The writer is waiting to write data. The buffer is full

jueves, 29 de abril de 2010

Java Process and Threads. TCP Java Server.

This post try it to explain how to manage Process, Threads and concurrence.

The main goal is explain how is a thread, and how is a process.
We can explain very well this topics with follow ilustration of Tanenbaum book :


The ilustration show in a) case three process with one thread into each one, in case b) we can see ONE process with three threads into the unique process.
The main diference between Process and Thread is that the thread is more ligth than a Process in order of the Threads works in the details of thw Process`s work and it can share the Thread stack with the others Threads.

A very good example of Process and Threads can be a Server and a Client.The server will be a Process wich accepts requests of a clients. Whe it accept one request, will create a Thread to give service to a client request.
We need three JavaClass: Server.java, Process.java and Cliente.java.

Server class create a s = new ServerSocket(9000); and wait to accept conecctions. When a conecction from a Cliente is coming, the server instance one Process with Procesador proc = new Procesador(cont,s.accept()); proc.start();

In this moment, Server is waiting for a new incoming conecction, and his Thread Process is working with the cliente conecction. The Processador read, operate and request the conecction cliente: b = new BufferedReader( new InputStreamReader ( sc.getInputStream() ) ); , p = new PrintStream ( sc.getOutputStream() );

Now, only need the Cliente operations: it´s simple, only create a socket s = new Socket(host,port); and write the request to a server.
We can see the result.




One iteration between Server and two clients:

One cliente close the conecction, but the oher cliente is coneccted and work in the server.



The client #3 executed two operations in the server and close conecction. Really close the Process Thread.

martes, 30 de marzo de 2010

¿Existe el tiempo?

La idea de que el tiempo es un modo de decir que una cosa sigue a otra como resultado de esta otra, parece que es la clave de la verdadera naturaleza del tiempo.

viernes, 16 de octubre de 2009

Java Graph Package

This Java Class provide an interface to work with graphs (Direct an Undirect raphs). You can get output format to generate "bmp" graphs with Graphviz.



Step sample:


1. Import class


2. Code



3. Get results




4. Generate graph with output String Graphviz




jueves, 27 de agosto de 2009

Floyd algorithm

The problem: We have a Graph, no direct and valued, also, we have a vector with N stages. For example, one stage is between nodes 2 and 5, other stage is between nodes 5 and 1 , ect ...
We need the minimun path between first stage and last stage through the middle stages in order.

The solution: We use, Floyd algorithm.

The graph:


The stages :



The output:


miércoles, 22 de abril de 2009

I propose new algorithm problem about rated digraphs

The goal: We should get the minimun value way betwen two nodes, with follows constrains:

  1. * The total path value is the sum of the edges in the A->B direction plus double value of B->A direction (if exists) .
  2. The graph it´s representated by a adjacency matrix .

A graph example :



One possible solution is: 1->2->3->4 with total path value* 10 (1->2 [4] 2->3 [5] 3->4 [10])

Download pascal code
(You need stack estructure [easy to computer engineer ;)])

viernes, 10 de abril de 2009

Java Random Double in Range Generation

Estos días he estado pensando sobre como generar números aleatorios en Java dentro de un rango, pues bien, aquí esta mi solución y su análisis:



Así, analizando los valores límite, observamos:

max(0.0)+min(1-(0.0)) => min
max(0.5)+min(1-(0.5)) => (max+min)/2
max(1.0)+min(1-(1.0)) => max

De una manera gráfica, observamos como los valores están siempre encerrados dentro de los límites:


Un análisis matemático de ajuste.

Aquí un histograma con 100000 valores generados aleatoriamente, se puede observar como sigue una distribución uniforme.

Con un rango en la escala negativa.

martes, 28 de octubre de 2008

Algorítmica en php - The Knight's Tour

El juego de The Knight's Tour

En un post anterior explicaba el algoritmo minimax (como introducción a la poda alfa-beta). Ahora veremos un poco de algorítmica, mas concretamente el backtracking. Podría decirse (a groso modo) que sería una busqueda minimax o poda alfa-beta pero sin heurística, además, el backtracking nos asegura que siempre encontrará la solución (si la hay) recorriendo así todos los estados posibles.

En wikipedia podemos obtener las reglas y una explicación de su complejidad, excelentes.

Pongo la implementación en PHP como una primera aproximación para la resolución del problema (La implementación la he realizado en php y orientada a objetos para que sea mas facil comprender el proceso de backtracking, evidentemente, no persigo eficiencia).


Backtraking




El software dispone de modo traza para ver los pasos de "vuelta atrás"



Algunos tiempos de ejecución en mi ordenador:

  • Tablero 4x4 => 0.19897794723511 seg.
  • Tablero 5x5 => 39.455229997635 seg.
  • Tablero 6x6 => 364.00200605392 seg.
  • Tablero 7x7 => 39521.259037971 segundos











  • Tablero 8x8 => aún ejecutándose :)
Se observa (sin analizar matemáticamente la complejidad del algoritmo) su complejidad claramente exponencial.