Hi
Thanks to every one for your answers and I want to apologize for no thank you before. The true is I’m a beginner in programming processing with C++ and I want to do an application using this.
Well, I´ve wrote my code as quarks said it but something goes wrong :
NullPointerException, in this line cities [from].x = (int)(getRand(MAX_DISTANCE));, I assume that is related with the x pointer.
This is a piece of the code:
int MAX_CITIES = 15;
float MAX_DISTANCE = 100.0;
float MAX_TOUR = (MAX_CITIES * MAX_DISTANCE);
int MAX_ANTS = 2*MAX_CITIES;
float ALPHAA = 0.5;
float BETA = 0.6;
float RHO = 0.9; /* Intensity / Evaporation */
int QVAL = 100;
int MAX_TOURS = 500;
int MAX_TIME = (MAX_TOURS * MAX_CITIES);
float INIT_PHEROMONE = (1.0 / MAX_CITIES);
float RAND_MAX = 100.0;
// Clases
CityType[] cities = new CityType[MAX_CITIES];
//AntType[] ants = new AntType[MAX_ANTS];
public class CityType{
public int x;
public int y;
}
/*public class AntType{
public int curCity;
public int nextCity;
public short[] tabu = new short[MAX_CITIES];
public int pathIndex;
public short[] path = new short[MAX_CITIES];
public double tourLength;
}*/
// End, Class
/* From To */
float[][] distance = new float[MAX_CITIES][MAX_CITIES];
/* From To */
float[][] pheromone = new float[MAX_CITIES][MAX_CITIES];
double best=(double)MAX_TOUR;
int bestIndex;
void Init(){
int from= 0;
int to = 0;
int ant = 0;
int xd = 0;
int yd = 0;
//Create the cities and their locations
for (from = 0; from<MAX_CITIES; from++){
//Randomly place cities around the grid
cities[from].x = (int)(getRand(MAX_DISTANCE)); // in this line I have the error
cities[from].y = (int)(getRand(MAX_DISTANCE));
for (to = 0; to<MAX_CITIES; to++){
distance[from][to] = 0.0;
pheromone[from][to] = INIT_PHEROMONE;
}
}
// Compute the distances for each of the cities on the map
for ( from = 0 ; from < MAX_CITIES ; from++) {
for ( to = 0 ; to < MAX_CITIES ; to++) {
if ((to != from) && (distance[from][to] == 0.0)) {
xd = abs(cities[from].x - cities[to].x);
yd = abs(cities[from].y - cities[to].y);
distance[from][to] = sqrt( (xd * xd) + (yd * yd) );
distance[to][from] = distance[from][to];
}
}
}
// Initialize the ants
to = 0;
for ( ant = 0 ; ant < MAX_ANTS ; ant++ ){
// Distribute the ants to each of the cities uniformly
if (to == MAX_CITIES) to = 0;
ants[ant].curCity = to++;
for ( from = 0 ; from < MAX_CITIES ; from++ ) {
ants[ant].tabu[from] = 0;
ants[ant].path[from] = -1;
}
ants[ant].pathIndex = 1;
ants[ant].path[0] = (short)(ants[ant].curCity);
ants[ant].nextCity = -1;
ants[ant].tourLength = 0.0;
// Load the ant's current city into taboo
ants[ant].tabu[ants[ant].curCity] = 1;
}
}
float getSRand(){
return (random(RAND_MAX) / RAND_MAX);
}
float getRand(float x){
float Ran = x*random(RAND_MAX)/(RAND_MAX+1.0);
return Ran; //(int)(x*random(RAND_MAX)/(RAND_MAX+1.0));
}