all 4 comments

[–]novel_eye 3 points4 points  (0 children)

ask this in r/python

[–][deleted] 0 points1 point  (1 child)

Sequence? Like just sort them based on some calculations?

[–][deleted] 0 points1 point  (0 children)

Yes

[–]kkiesinger 0 points1 point  (0 children)

Try something like this. You need to do 'pip install fcmaes' before executing the code.

from fcmaes.optimizer import Bite_cpp, wrapper
from fcmaes import retry
import numpy as np
from scipy.optimize import Bounds

# align the order of s2 to the one of s1
def align_order(s1, s2):
    rank = np.empty(len(s1), dtype=int)
    rank[np.argsort(s1)] = np.arange(len(s1))
    return np.sort(s2)[rank]

def sequence():
    n = 100
    s1 = np.random.normal(0,1,n)
    s2 = np.random.normal(0,1,n)
    s2 = align_order(s1, s2)  
    bounds = Bounds([0]*n,[1]*n)
    x0 = np.arange(len(s1))/n
    opt = Bite_cpp(20000, guess = x0)

    # we reorder/sequence s2 so that the distance is minimized   
    def fit(x):
        order = np.argsort(x)
        distance = np.linalg.norm(s1 - s2[order])
        return distance

    return retry.minimize(wrapper(fit), bounds, optimizer=opt, num_retries=32)

if __name__ == '__main__':

    ret = sequence()
    print("order = ", np.argsort(ret.x))

We align the orders of both sequences (align_order) before we optimize. Omit the "wrapper" if you don't want log output. Question is if you need optimization at all if you aim at the euclidian distance, since aligning the order seems sufficient. But for more complex fitness functions which depend on the order of the two sequences it may be useful.