0

I am implementing an evolutionary algorithm where I have a numerical genetic encoding (0-n). Where each number from 0 to n represents a function. I have implemented a numpy version where it is possible to do the following. The actual implementation is a bit more complicated but this snippet captures the core functionality.

n = 3
max_ops = 10
# Generate randomly generated args and OPs 
for i in range(number_of_iterations):
    args = np.random.randint(min_val_arg, max_val_arg, size=(arg_count, arg_shape[0], arg_shape[1])
    gene_of_operations = np.random.randint(0,n,size=(max_ops))

    # A collection of OP encodings and operations. Doesn't need to be a dict.
    dict_of_n_OPs = {
    0:np.add,
    1:np.multiply,
    2:np.diff 
    }

    @njit
    def execute_genome(gene_of_operations, args, dict_of_n_OPs):
        result = 0
        for op, arg in zip(gene_of_operations,args) 
            result+= op(arg)
        return result

     ## executing the gene
     execute_genome(gene_of_operations, args, dict_of_n_OPs)
     print(results)

Now when adding the njit decorator expects a statically typed function. Where heterogenously typed collections such as my dict_of_n_OPs are not supported, I have tried rendering it as a numpy array, numba.typed.Dict, numba.typed.List. But discovered none supports heteregoenous types.

What would be a numba compliant approach that allows for executing different functions based on a numerical encoding such as '00201'. Where number 0 would execute function 0?

Is the only way an n line if else statement for n unique operations/functions?

Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32

0 Answers0