mercredi 5 mai 2021

Efficient "destiny knot" partial inheritance function

In the Pokémon games (since Gen VI), Destiny Knot is an item that, while held, will cause a parent to pass on some of its stats to any children it may have.

I'm looking to create a function in python 3 that will create a copy of a list of numbers that is partially randomly generated, and partially inherited from a "parent". Here is my first implementation:

def destiny_knot(parent, prob):
    child = []
    for n in parent :
        if np.random.rand(1)<prob :
            child.append(n)
        else :
            child.append(np.random.normal())
    return child

Which works as intended when ran:

foo = range(10)
destiny_knot(foo, 0.5)

[3.7189086135642975, 1, 0.6303126955135048, -0.9230447017112412, 4, 5, 6, 0.8633075878923896, 0.4633879779484653, -1.5192557497361636]

However, it's quite obvious to me that this will take a long time when actually implemented for the uses I'm looking for: each of my lists will be thousands, if not hundreds of thousands, of elements, and I'll be aiming to generate thousands of these.

I was thinking about determining a number of elements that will be inherited, and generating N minus that many random numbers, but I would also need them to be inherited at the correct positions, which adds a layer of complexity to the whole problem.

What would be the most efficient way to do this?




Aucun commentaire:

Enregistrer un commentaire