Sea un tablero definido como una matriz de nodos con conectividad 8:
6390 nodos => 50158 vecinos => 25079 aristas
Los ejemplos son del tipo:
Probabilidad de que esté la arista
Densidad real de aristas
Vecinos que hay(V)
P DR V
86,57% 80,76% 40508
43,14% 30,51% 15302
15,99% 9,60% 4814
74,72% 65,69% 32948
...
Siempre la densidad real es menor que la probabilidad de generación (que debería dar la densidad, no?)
// probability = rand(); antes de llamar a este metodo
void createRandomGraph(Board* board, long probability) {
int i, j;
// Connectivity 4
if(board->getConnectivity() == 4) {
// For every node
for(i=0; i<board->getHeight(); i++) {
for(j=0; j<board->getWidth(); j++) {
// If the sum is even
if(!((i+j) & 1)) {
// Check cross
if(rand() < probability) board->deleteConnection(i,j, north);
if(rand() < probability) board->deleteConnection(i,j, south);
if(rand() < probability) board->deleteConnection(i,j, east);
if(rand() < probability) board->deleteConnection(i,j, west);
}
}
}
}
// Connectivity 8
else {
// For every node
for(i=0; i<board->getHeight(); i++) {
for(j=0; j<board->getWidth(); j++) {
// If the sum is even
if(!((i+j) & 1)) {
// Check diagonals only
if(rand() < probability) board->deleteConnection(i,j, northeast);
if(rand() < probability) board->deleteConnection(i,j, southeast);
if(rand() < probability) board->deleteConnection(i,j, northwest);
if(rand() < probability) board->deleteConnection(i,j, southwest);
}
// If its odd
else {
// Check cross
if(rand() < probability) board->deleteConnection(i,j, north);
if(rand() < probability) board->deleteConnection(i,j, south);
if(rand() < probability) board->deleteConnection(i,j, east);
if(rand() < probability) board->deleteConnection(i,j, west);
if(rand() < probability) board->deleteConnection(i,j, northeast);
if(rand() < probability) board->deleteConnection(i,j, southeast);
if(rand() < probability) board->deleteConnection(i,j, northwest);
if(rand() < probability) board->deleteConnection(i,j, southwest);
}
}
}
}
...
}
float calculateDensity(Board* board) {
int i,j;
long num_neighbours=0, max_neighbours;
// Calculate density
max_neighbours = board->getConnectivity() * board->getWidth() * board->getHeight();
// substract not allowed neigbours (border)
if(board->getConnectivity() == 4)
max_neighbours = max_neighbours - board->getWidth()*2 - board->getHeight()*2;
else
max_neighbours = max_neighbours - board->getWidth()*6 - board->getHeight()*6 + 4;
// count real nodes
for( i=0; i<board->getHeight(); i++) {
for( j=0; j<board->getWidth(); j++) {
num_neighbours += board->getNodeAt(i,j)->getNeighboursNumber();
}
}
// et voilá
return (((float)num_neighbours/max_neighbours)*100);
}
...
p = rand();
createRandomGraph(board, p);
real = calculateDensity(board);
probable = 100-(((float)p/RAND_MAX)*100);
imprimir real y probable
...
max_neighbours = board->getConnectivity() * board->getWidth() * board->getHeight();
// If the sum is even
if(!((i+j) & 1)) {....
max_neighbours = board->getConnectivity() * board->getWidth() * board->getHeight()/2;
QUOTE (gdl) | ||
No entiendo tu código al 100%, pero parece que lo que haces es borrar conexiones. ¿No deberían ser los if del tipo if(rand() > probability)..? [/quote] Uhmmmmm, podría ser. Podría ser. Lo pruebo y comento.
|