2025-05-10 13:12:46 +02:00
package it.arnaldo.unibs.tamagolem ;
import java.util.* ;
public class WorldBuilder {
public static ElementGraph buildWorld ( ) {
Element [ ] allElements = Element . values ( ) ;
Random rand = new Random ( ) ;
// da cambiare il numero a 8
2025-05-12 19:12:33 +02:00
//int numElements = 3 + rand.nextInt(8);
2025-05-12 19:28:22 +02:00
// numero di elementi estratti a caso
int numElements = 5 ;
2025-05-10 13:12:46 +02:00
List < Element > selectedElements = new ArrayList < > ( Arrays . asList ( allElements ) ) ;
2025-05-12 19:28:22 +02:00
// elementi scelti
2025-05-10 13:12:46 +02:00
Collections . shuffle ( selectedElements ) ;
selectedElements = selectedElements . subList ( 0 , numElements ) ;
ElementGraph world = new ElementGraph ( ) ;
2025-05-12 19:28:22 +02:00
// mappa che contiene una mappa in modo che dato un elemento ti da una mappa con chiave un secondo elemento il cui valore è 1 se l'elemento infligge o -1 se l'elemento subisce
2025-05-12 19:12:33 +02:00
HashMap < Element , HashMap < Element , Integer > > linkDirections = new HashMap < > ( ) ;
2025-05-10 13:12:46 +02:00
for ( Element element : selectedElements ) {
2025-05-12 19:12:33 +02:00
HashMap < Element , Integer > links = new HashMap < > ( ) ;
Element followingElement = selectedElements . get ( ( selectedElements . indexOf ( element ) + 1 ) % selectedElements . size ( ) ) ;
for ( Element el : selectedElements ) {
if ( el = = followingElement ) {
links . put ( el , 1 ) ;
} else if ( el ! = element ) {
2025-05-12 19:16:28 +02:00
links . put ( el , 0 ) ;
2025-05-12 19:12:33 +02:00
}
2025-05-10 13:12:46 +02:00
}
2025-05-12 19:12:33 +02:00
linkDirections . put ( element , links ) ;
2025-05-10 13:12:46 +02:00
}
2025-05-12 19:12:33 +02:00
for ( Element element : selectedElements ) {
HashMap < Element , Integer > elementLinks = linkDirections . get ( element ) ;
for ( Element el : selectedElements ) {
if ( el = = element ) {
continue ;
2025-05-10 13:12:46 +02:00
}
2025-05-12 19:12:33 +02:00
2025-05-12 19:16:28 +02:00
// controllo anche prima per evitare che magari da un estrazione si vada a togliere
if ( elementLinks . get ( el ) = = 1 ) {
linkDirections . get ( el ) . put ( element , - 1 ) ;
}
if ( elementLinks . get ( el ) = = 0 ) {
if ( Math . random ( ) < 0 . 5 ) {
linkDirections . get ( el ) . put ( element , 1 ) ;
} else {
linkDirections . get ( el ) . put ( element , - 1 ) ;
}
}
if ( elementLinks . get ( el ) = = 1 ) {
linkDirections . get ( el ) . put ( element , - 1 ) ;
} else {
linkDirections . get ( el ) . put ( element , 1 ) ;
}
2025-05-10 13:12:46 +02:00
}
}
2025-05-12 19:12:33 +02:00
System . out . println ( linkDirections ) ;
2025-05-10 13:12:46 +02:00
2025-05-12 19:28:22 +02:00
// hashmap che dato una coppia di element (è un set cosi AB = BA) ti torna la posizione all0interno dell'array di valori passato al sistema lineare
2025-05-12 19:12:33 +02:00
HashMap < Set < Element > , Integer > pairIndex = new HashMap < > ( ) ;
2025-05-10 13:12:46 +02:00
2025-05-12 19:12:33 +02:00
int index = 0 ;
for ( int i = 0 ; i < selectedElements . size ( ) ; i + + ) {
for ( int j = i + 1 ; j < selectedElements . size ( ) ; j + + ) {
Set < Element > pair = Set . of ( selectedElements . get ( i ) , selectedElements . get ( j ) ) ;
2025-05-10 13:12:46 +02:00
2025-05-12 19:12:33 +02:00
pairIndex . put ( pair , index + + ) ;
}
}
2025-05-10 13:12:46 +02:00
2025-05-12 19:12:33 +02:00
System . out . println ( pairIndex ) ;
2025-05-10 13:12:46 +02:00
2025-05-12 19:28:22 +02:00
// matrice incognite
2025-05-12 19:12:33 +02:00
int [ ] [ ] A = new int [ numElements ] [ numElements * ( numElements - 1 ) / 2 ] ;
2025-05-12 19:28:22 +02:00
// matrice termini noti
2025-05-12 19:12:33 +02:00
int [ ] b = new int [ numElements ] ;
2025-05-10 13:12:46 +02:00
2025-05-12 19:28:22 +02:00
// popolo le 2 matrici
2025-05-12 19:12:33 +02:00
int i = 0 ;
for ( Element element : selectedElements ) {
int [ ] line = new int [ numElements * ( numElements - 1 ) / 2 ] ;
HashMap < Element , Integer > elementLinks = linkDirections . get ( element ) ;
for ( Element el : selectedElements ) {
if ( el = = element ) {
continue ;
} else {
int idx = getIndex ( element , el , pairIndex ) ;
line [ idx ] = elementLinks . get ( el ) ;
}
}
A [ i ] = line ;
b [ i ] = 0 ;
i + + ;
}
2025-05-10 13:12:46 +02:00
2025-05-12 19:28:22 +02:00
// creo e risolvo il sistema lineare associato
2025-05-12 19:12:33 +02:00
LinearSystem system = new LinearSystem ( numElements , A , b ) ;
2025-05-10 13:12:46 +02:00
2025-05-12 19:12:33 +02:00
int [ ] solution = system . resolve ( ) ;
System . out . println ( " Soluzione trovata: " ) ;
for ( int x : solution ) {
System . out . printf ( String . format ( " %d " , x ) ) ;
2025-05-10 13:12:46 +02:00
}
2025-05-12 19:12:33 +02:00
System . out . println ( ) ;
2025-05-10 13:12:46 +02:00
2025-05-12 19:28:22 +02:00
// popola il grafo in base ai risultati del sistema lineare
2025-05-12 19:12:33 +02:00
for ( Element element : selectedElements ) {
GraphElement graphElement = new GraphElement ( element ) ;
for ( Element el : selectedElements ) {
if ( el = = element ) {
continue ;
}
2025-05-12 19:16:28 +02:00
int direction = linkDirections . get ( element ) . get ( el ) ;
graphElement . addLink ( new ElementLink ( solution [ getIndex ( element , el , pairIndex ) ] * direction , el ) ) ;
2025-05-12 19:12:33 +02:00
}
world . addElement ( graphElement ) ;
}
2025-05-10 13:12:46 +02:00
world . printGraph ( ) ;
return world ;
}
2025-05-12 19:12:33 +02:00
private static int getIndex ( Element e1 , Element e2 , HashMap < Set < Element > , Integer > pairIndex ) {
return pairIndex . get ( Set . of ( e1 , e2 ) ) ;
}
2025-05-10 13:12:46 +02:00
}
2025-05-12 19:12:33 +02:00