moptipy.operators.intspace package

Search operators defined on integer spaces.

Submodules

moptipy.operators.intspace.op0_random module

A nullary operator filling an integer list with random values.

>>> from moptipy.utils.nputils import rand_generator
>>> gen = rand_generator(12)
>>> space = IntSpace(4, -2, 2)
>>> op = Op0Random(space)
>>> xx = space.create()
>>> op.op0(gen, xx)
>>> xx
array([-1,  2,  2,  1], dtype=int8)
>>> op.op0(gen, xx)
>>> xx
array([-2, -2, -1, -1], dtype=int8)
>>> op.op0(gen, xx)
>>> xx
array([ 1,  0, -2,  2], dtype=int8)
>>> space = IntSpace(5, 12000, 20000)
>>> op = Op0Random(space)
>>> xx = space.create()
>>> op.op0(gen, xx)
>>> xx
array([15207, 19574, 18014, 12515, 14406], dtype=int16)
>>> op.op0(gen, xx)
>>> xx
array([16080, 13596, 13434, 14148, 16655], dtype=int16)
class moptipy.operators.intspace.op0_random.Op0Random(space)[source]

Bases: Op0

Fill a bit string with random values.

op0(random, dest)[source]

Fill the string dest with random values.

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

  • dest (ndarray) – the bit string to fill. Afterward, its contents will be random.

Return type:

None

moptipy.operators.intspace.op1_mnormal module

A multi-normal distribution.

>>> from moptipy.utils.nputils import rand_generator
>>> gen = rand_generator(12)
>>> from moptipy.operators.intspace.op0_random import Op0Random
>>> space = IntSpace(4, -2, 2)
>>> op0 = Op0Random(space)
>>> x1 = space.create()
>>> op0.op0(gen, x1)
>>> x1
array([-1,  2,  2,  1], dtype=int8)
>>> op1 = Op1MNormal(space, 1, True, 1.5)
>>> op1.initialize()
>>> x2 = space.create()
>>> op1.op1(gen, x2, x1)
>>> x2
array([-1,  0,  0,  1], dtype=int8)
>>> op1.op1(gen, x2, x1)
>>> x2
array([-1,  2,  1, -2], dtype=int8)
>>> op1.op1(gen, x2, x1)
>>> x2
array([-1,  2,  2,  0], dtype=int8)
>>> space = IntSpace(12, 0, 20)
>>> op0 = Op0Random(space)
>>> x1 = space.create()
>>> op0.op0(gen, x1)
>>> x1
array([ 6,  1, 18, 11, 13, 11,  2,  3, 13,  7, 10, 14], dtype=int8)
>>> op1 = Op1MNormal(space, 1, True, 1.5)
>>> op1.initialize()
>>> x2 = space.create()
>>> op1.op1(gen, x2, x1)
>>> x2
array([ 6,  1, 18, 11, 13, 11,  2,  3, 12,  7,  9, 13], dtype=int8)
>>> op1.op1(gen, x2, x1)
>>> x2
array([ 6,  1, 20, 11, 13, 11,  2,  3, 13,  7, 10, 14], dtype=int8)
>>> op1.op1(gen, x2, x1)
>>> x2
array([ 3,  1, 18, 11, 13, 11,  2,  3, 13,  7,  9, 14], dtype=int8)
>>> str(op1)
'normB1_1d5'
class moptipy.operators.intspace.op1_mnormal.Op1MNormal(space, m=1, at_least_1=True, sd=1.0)[source]

Bases: Op1

Randomly choose a number of ints to change with normal distribution.

initialize()[source]

Initialize this operator.

Return type:

None

log_parameters_to(logger)[source]

Log the parameters of this operator to the given logger.

Parameters:

logger (KeyValueLogSection) – the logger for the parameters

Return type:

None

op1(random, dest, x)[source]

Copy x into dest and change each value with probability m/n.

This method will first copy x to dest. Then it will change each value in dest with probability m/n, where n is the length of dest. Regardless of the probability, at least one value will always be changed if self.at_least_1 is True.

If a value is changed, we will put a normal distribution around it and sample it from that. Of course, we only accept values within the limits of the integer space and that are different from the original value.

Parameters:
  • self – the self pointer

  • random (Generator) – the random number generator

  • dest (ndarray) – the destination array to receive the new point

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

Return type:

None

moptipy.operators.intspace.op2_uniform module

The bit-string based uniform crossover operator used for intspaces.

class moptipy.operators.intspace.op2_uniform.Op2Uniform[source]

Bases: Op2

The uniform crossover operator for integer spaces is the same as for bitstrings.