moptipy.operators.signed_permutations package

Search operators for signed permutations with repetitions.

These operators can be used in conjunction with the space signed_permutations.

  • Module op0_shuffle_and_flip provides a nullary operator sampling an entirely random signed permutation.

  • Module op1_swap_2_or_flip provides a unary operator which either swaps exactly two different elements of a permutation or flips the sign of one element.

Submodules

moptipy.operators.signed_permutations.op0_shuffle_and_flip module

A nullary operator shuffling a signed permutation and flipping signs.

This operator first copies the canonical permutation to the destination string. It then applies the shuffle procedure of the random number generator, which probably internally applies a Fisher-Yates Shuffle. The result is a random permutation. Then, it goes over the permutation once more and the signs of the elements randomly.

class moptipy.operators.signed_permutations.op0_shuffle_and_flip.Op0ShuffleAndFlip(space)[source]

Bases: Op0

Shuffle permutations randomly and flip signs randomly.

op0(random, dest)[source]

Copy the base string to dest and shuffle it and flip signs randomly.

Parameters:
  • random (Generator) – the random number generator

  • dest (ndarray) – the signed permutation that should be filled with a random sequence of the base permutation with potentially flipped signs.

Return type:

None

moptipy.operators.signed_permutations.op1_swap_2_or_flip module

An operator swapping two elements in a permutation or flipping a sign.

This operator is for signed_permutations. A similar operator which only swaps elements (for permutations) is defined in op1_swap2.

class moptipy.operators.signed_permutations.op1_swap_2_or_flip.Op1Swap2OrFlip[source]

Bases: Op1

A search operation that swaps two (different) elements or flips a sign.

In other words, it performs exactly one swap on a permutation or a sign flip. It spans a neighborhood of a rather limited size but is easy and fast.

moptipy.operators.signed_permutations.op1_swap_2_or_flip.swap_2_or_flip(random, dest, x)[source]

Copy x into dest and swap two different values or flip a sign.

Parameters:
  • random (Generator) – the random number generator

  • dest (ndarray) – the array to receive the modified copy of x

  • x (ndarray) – the existing point in the search space

>>> rand = np.random.default_rng(10)
>>> xx = np.array(range(10), int)
>>> out = np.empty(len(xx), int)
>>> swap_2_or_flip(rand, out, xx)
>>> print(out)
[0 1 7 3 4 5 6 2 8 9]
>>> swap_2_or_flip(rand, out, xx)
>>> print(out)
[0 1 8 3 4 5 6 7 2 9]
>>> swap_2_or_flip(rand, out, xx)
>>> print(out)
[ 0  1  2  3  4 -5  6  7  8  9]
>>> swap_2_or_flip(rand, out, xx)
>>> print(out)
[0 8 2 3 4 5 6 7 1 9]
>>> swap_2_or_flip(rand, out, xx)
>>> print(out)
[ 0 -1  2  3  4  5  6  7  8  9]
:rtype: :sphinx_autodoc_typehints_type:`\:py\:obj\:\`None\``
>>> swap_2_or_flip(rand, out, xx)
>>> print(out)
[ 0  1  2  3  4  5 -6  7  8  9]