Skip to content

Commit aa8c9b7

Browse files
authored
Update IntelPython_GPU_numba-dpex_Genetic_Algorithm.py
1 parent 16ab829 commit aa8c9b7

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

AI-and-Analytics/Features-and-Functionality/IntelPython_GPU_numba-dpex_Genetic_Algorithm/IntelPython_GPU_numba-dpex_Genetic_Algorithm.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# ## Genetic algorithms
1919
#
2020
# Let's start with the question **What is a genetic algorithm?**. It is an algorithm, search heuristic inspired by the process of natural selection. It is usually applied to various optimization problems, NP-hard problems for which finding a solution by standard methods is very time and resource consuming. This algorithm makes it possible to obtain a satisfying high quality result based on biology-inspired operations, such as:
21-
#  
21+
#
2222
# * selection - is the process of selecting parents who mate and recombine to create offspring for the next generation. Parent selection is very crucial to the convergence rate of the GA as good parents drive individuals to better and fitter solutions.
2323
# * crossover - is a process similar to biological crossover. In this, more than one parent is selected and one or more offspring are produced using the genetic material of the parents.
2424
# * mutation - small random tweak in the chromosome, to get a new solution. It is used to maintain and introduce diversity in the genetic population and is usually applied with a low probability.
@@ -260,16 +260,17 @@ def next_generation(chromosomes, fitnesses):
260260
#
261261
# The only par that differs form the standard implementation is the evaluation function.
262262
#
263-
# The most important part is to specify the global index of the computation. This is the current index of the computed chromosomes. This serves as a loop function across all chromosomes.
263+
# The most important part is to specify the index of the computation. This is the current index of the computed chromosomes. This serves as a loop function across all chromosomes.
264264

265265
# In[ ]:
266266

267267

268268
import numba_dpex
269+
from numba_dpex import kernel_api
269270

270271
@numba_dpex.kernel
271-
def eval_genomes_sycl_kernel(chromosomes, fitnesses, chrom_length):
272-
pos = numba_dpex.get_global_id(0)
272+
def eval_genomes_sycl_kernel(item: kernel_api.Item, chromosomes, fitnesses, chrom_length):
273+
pos = item.get_id(0)
273274
num_loops = 3000
274275
for i in range(num_loops):
275276
fitnesses[pos] += chromosomes[pos*chrom_length + 1]
@@ -300,7 +301,8 @@ def eval_genomes_sycl_kernel(chromosomes, fitnesses, chrom_length):
300301
chromosomes_flat_dpctl = dpnp.asarray(chromosomes_flat, device="gpu")
301302
fitnesses_dpctl = dpnp.asarray(fitnesses, device="gpu")
302303

303-
eval_genomes_sycl_kernel[numba_dpex.Range(pop_size)](chromosomes_flat_dpctl, fitnesses_dpctl, chrom_size)
304+
exec_range = kernel_api.Range(pop_size)
305+
numba_dpex.call_kernel(eval_genomes_sycl_kernel, exec_range, chromosomes_flat_dpctl, fitnesses_dpctl, chrom_size)
304306
fitnesses = dpnp.asnumpy(fitnesses_dpctl)
305307
chromosomes = next_generation(chromosomes, fitnesses)
306308
fitnesses = np.zeros(pop_size, dtype=np.float32)
@@ -398,14 +400,14 @@ def eval_genomes_sycl_kernel(chromosomes, fitnesses, chrom_length):
398400
#
399401
# The evaluate created generation we are calculating the full distance of the given path (chromosome). In this example, the lower the fitness value is, the better the chromosome. That's different from the general GA that we implemented.
400402
#
401-
# As in this example we are also using numba-dpex, we are using a global index like before.
403+
# As in this example we are also using numba-dpex, we are using an index like before.
402404

403405
# In[ ]:
404406

405407

406408
@numba_dpex.kernel
407-
def eval_genomes_plain_TSP_SYCL(chromosomes, fitnesses, distances, pop_length):
408-
pos = numba_dpex.get_global_id(0)
409+
def eval_genomes_plain_TSP_SYCL(item: kernel_api.Item, chromosomes, fitnesses, distances, pop_length):
410+
pos = item.get_id(1)
409411
for j in range(pop_length-1):
410412
fitnesses[pos] += distances[int(chromosomes[pos, j]), int(chromosomes[pos, j+1])]
411413

@@ -526,7 +528,8 @@ def next_generation_TSP(chromosomes, fitnesses):
526528
chromosomes_flat_dpctl = dpnp.asarray(chromosomes, device="gpu")
527529
fitnesses_dpctl = dpnp.asarray(fitnesses.copy(), device="gpu")
528530

529-
eval_genomes_plain_TSP_SYCL[numba_dpex.Range(pop_size)](chromosomes_flat_dpctl, fitnesses_dpctl, distances_dpctl, pop_size)
531+
exec_range = kernel_api.Range(pop_size)
532+
numba_dpex.call_kernel(eval_genomes_plain_TSP_SYCL, exec_range, chromosomes_flat_dpctl, fitnesses_dpctl, distances_dpctl, pop_size)
530533
fitnesses = dpnp.asnumpy(fitnesses_dpctl)
531534
chromosomes = next_generation_TSP(chromosomes, fitnesses)
532535
fitnesses = np.zeros(pop_size, dtype=np.float32)
@@ -553,4 +556,3 @@ def next_generation_TSP(chromosomes, fitnesses):
553556

554557

555558
print("[CODE_SAMPLE_COMPLETED_SUCCESFULLY]")
556-

0 commit comments

Comments
 (0)