def echange_position_essai(chemin, a, b, x, inversion, negligeable=1e-05,
    distance=None):
    """
    Echange la place des villes ka et kb pour les placer entre les villes *i* et *i+1*,
    si inversion est True, on inverse également le chemin inséré, si inversion est False,
    on ne l'inverse pas,
    si cela améliore, déplace les villes et retourne True, sinon, retourne False.
    """
    nb = len(chemin)
    xa = (x + 1) % nb
    ka = (a - 1 + nb) % nb
    kb = (b + 1) % nb
    if not inversion:
        if x == ka:
            return False
        if x == kb:
            return False
        if xa == ka:
            return False
        if b < a:
            if a <= x <= b + nb:
                return False
        elif a <= x <= b:
            return False
        if b < a:
            if a <= x + nb <= b + nb:
                return False
        elif a <= x + nb <= b:
            return False
        d_x_xa = distance(chemin[x], chemin[xa])
        d_ka_a = distance(chemin[ka], chemin[a])
        d_b_kb = distance(chemin[b], chemin[kb])
        d_ka_kb = distance(chemin[ka], chemin[kb])
        d_x_a = distance(chemin[x], chemin[a])
        d_b_xa = distance(chemin[b], chemin[xa])
        d = d_ka_kb + d_x_a + d_b_xa - d_x_xa - d_ka_a - d_b_kb
        if d >= -negligeable:
            return False
        ech = []
        bp = b
        if bp < a:
            bp = b + nb
        for i in range(a, bp + 1):
            ech.append(chemin[i % nb])
        diff = bp - a + 1
        xp = x
        if xp < b:
            xp += nb
        for l in range(b + 1, xp + 1):
            ll = l % nb
            bp = (a + l - b - 1) % nb
            chemin[bp] = chemin[ll]
        for l in range(0, len(ech)):
            chemin[(x + l - diff + 1 + nb) % nb] = ech[l]
        return True
    else:
        if x == ka:
            return False
        if x == kb:
            return False
        if xa == ka:
            return False
        if b < a:
            if a <= x <= b + nb:
                return False
        elif a <= x <= b:
            return False
        if b < a:
            if a <= x + nb <= b + nb:
                return False
        elif a <= x + nb <= b:
            return False
        d_x_xa = distance(chemin[x], chemin[xa])
        d_ka_a = distance(chemin[ka], chemin[a])
        d_b_kb = distance(chemin[b], chemin[kb])
        d_ka_kb = distance(chemin[ka], chemin[kb])
        d_x_b = distance(chemin[x], chemin[b])
        d_a_xa = distance(chemin[a], chemin[xa])
        d = d_ka_kb + d_x_b + d_a_xa - d_x_xa - d_ka_a - d_b_kb
        if d >= -negligeable:
            return False
        ech = []
        bp = b
        if bp < a:
            bp = b + nb
        for i in range(a, bp + 1):
            ech.append(chemin[i % nb])
        ech.reverse()
        diff = bp - a + 1
        xp = x
        if xp < b:
            xp += nb
        for l in range(b + 1, xp + 1):
            ll = l % nb
            bp = (a + l - b - 1) % nb
            chemin[bp] = chemin[ll]
        for l in range(0, len(ech)):
            chemin[(x + l - diff + 1 + nb) % nb] = ech[l]
        return True
