-
Notifications
You must be signed in to change notification settings - Fork 33
Chromosome Mapping Between Python and OpenCL
As we known, Python and OpenCL use different memory address. The mapping between Python and OpenCL seems a mystery of OpenCLGA.
Briefly speaking, kernel code, OpenCL, uses the index to represent a gene in a chromosome.
Let's see the TSP examples:
A TSP chromosome is comprised of the cities. In the Taiwan Travel example, we use an array of dicts to denote the cities. The cities is an argument of SimpleGene, which we call it elements of a Gene. The elements
term means we will use cities as the candidates for populating chromosome/gene. The current implementation, we use the index of elements in the kernel code to denote an element of the elements
. Let's see:
city1 = {}
city2 = {}
city3 = {}
...
city350 = {}
cities = [city1, city2, ..., city350]
The cities in python will be translated into OpenCL:
int cities[] = [0, 1, 2, ..., 349];
But in this example, we need the coordinate of a city to calculate the distance for fitness. There are two ways to finish that:
- Use code generation tricks to generate coordination information to fitness function. We had tried that before, that can be found at here.
- Use fitness arguments. We can find an example, here.
In the kernel code, or fitness function, implementation, we use the gene as the index to get the coordinate, like:
float taiwan_calc_fitness(global __ShufflerChromosome* chromosome,
int chromosome_size,
global float* pointsX,
global float* pointsY)
{
float dist = 0.0;
for (int i = 0; i < chromosome_size - 1; i++) {
dist += calc_spherical_distance(pointsX[chromosome->genes[i + 1]],
pointsY[chromosome->genes[i + 1]],
pointsX[chromosome->genes[i]],
pointsY[chromosome->genes[i]]);
}
return dist + calc_spherical_distance(pointsX[chromosome->genes[0]],
pointsY[chromosome->genes[0]],
pointsX[chromosome->genes[chromosome_size - 1]],
pointsY[chromosome->genes[chromosome_size - 1]]);
}