Coverage for moptipy / operators / bitstrings / op1_flip1.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-24 08:49 +0000

1"""A unary operator flipping exactly one bit.""" 

2from typing import Final 

3 

4import numpy as np 

5from numpy.random import Generator 

6 

7from moptipy.api.operators import Op1 

8 

9 

10class Op1Flip1(Op1): 

11 """A unary search operation that flips exactly one bit.""" 

12 

13 def op1(self, random: Generator, dest: np.ndarray, x: np.ndarray) -> None: 

14 """ 

15 Copy `x` into `dest` and flip exactly one bit. 

16 

17 :param self: the self pointer 

18 :param random: the random number generator 

19 :param dest: the destination array to receive the new point 

20 :param x: the existing point in the search space 

21 """ 

22 np.copyto(dest, x) # copy source to destination 

23 idx: Final = random.integers(len(x)) # get bit index 

24 dest[idx] = not dest[idx] # flip bit 

25 

26 def __str__(self) -> str: 

27 """ 

28 Get the name of this unary operator. 

29 

30 :return: "flip1" 

31 """ 

32 return "flip1"