Coverage for moptipy / operators / bitstrings / op2_uniform.py: 80%

15 statements  

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

1""" 

2A binary operator copying each bit from either source string. 

3 

4Uniform crossover copies the value of every bit with the same probability 

5from either of the two parent strings. This means that all bits where both 

6parent strings have the same value remain unchanged and are copied directly 

7to the offspring. The bits where the parent strings have different values 

8are effectively randomized. This is easy to see from the fact that, if both 

9parents have different values for one bit, then one of them must have the 

10bit set to `1` and the other one set to `0`. This means that the value in the 

11offspring is set to `0` with probability `0.5` and set to `1` with probability 

12`0.5`. This corresponds to drawing it uniformly at random, i.e., randomizing 

13it. 

14 

151. Gilbert Syswerda. Uniform Crossover in Genetic Algorithms. In J. David 

16 Schaffer, editor, *Proceedings of the 3rd International Conference on 

17 Genetic Algorithms* (ICGA'89), June 4-7, 1989, Fairfax, VA, USA, pages 2-9. 

18 San Francisco, CA, USA: Morgan Kaufmann Publishers Inc. 

19 ISBN: 1-55860-066-3. https://www.researchgate.net/publication/201976488. 

202. Hans-Georg Beyer and Hans-Paul Schwefel. Evolution Strategies - A 

21 Comprehensive Introduction. *Natural Computing: An International 

22 Journal* 1(1):3-52, March 2002, http://doi.org/10.1023/A:1015059928466. 

23 https://www.researchgate.net/publication/220132816. 

243. Hans-Georg Beyer. An Alternative Explanation for the Manner in which 

25 Genetic Algorithms Operate. *Biosystems* 41(1):1-15, January 1997, 

26 https://doi.org/10.1016/S0303-2647(96)01657-7. 

274. William M. Spears and Kenneth Alan De Jong. On the Virtues of Parameterized 

28 Uniform Crossover. In Richard K. Belew and Lashon Bernard Booker, editors, 

29 *Proceedings of the Fourth International Conference on Genetic Algorithms* 

30 (ICGA'91), July 13-16, 1991, San Diego, CA, USA, pages 230-236. 

31 San Francisco, CA, USA: Morgan Kaufmann Publishers Inc. 

32 ISBN: 1-55860-208-9. https://www.mli.gmu.edu/papers/91-95/91-18.pdf. 

33""" 

34 

35 

36import numba # type: ignore 

37import numpy as np 

38from numpy.random import Generator 

39 

40from moptipy.api.operators import Op2 

41 

42 

43@numba.njit(cache=True, inline="always", fastmath=True, boundscheck=False) 

44def uniform(random: Generator, dest: np.ndarray, x0: np.ndarray, 

45 x1: np.ndarray) -> None: 

46 """ 

47 Perform the actual work of the uniform crossover. 

48 

49 :param random: the random number generator 

50 :param dest: the destination array 

51 :param x0: the first source array 

52 :param x1: the second source array 

53 

54 >>> a = np.full(10, True) 

55 >>> b = np.full(len(a), False) 

56 >>> r = np.random.default_rng(10) 

57 >>> out = np.empty(len(a), bool) 

58 >>> uniform(r, out, a, b) 

59 >>> print(out) 

60 [ True True False False True True True False True True] 

61 >>> uniform(r, out, a, b) 

62 >>> print(out) 

63 [False False False True False True False False True True] 

64 >>> uniform(r, out, a, b) 

65 >>> print(out) 

66 [False True False False True True True True True True] 

67 >>> uniform(r, out, a, b) 

68 >>> print(out) 

69 [False True True False True True False False False True] 

70 """ 

71 for i in range(len(dest)): # pylint: disable=C0200 

72 v = random.integers(0, 2) # create boolean value 

73 # copy from x0 with p=0.5 and from x1 with p=0.5 

74 dest[i] = x1[i] if v == 0 else x0[i] 

75 

76 

77class Op2Uniform(Op2): 

78 """ 

79 A binary search operation that copies each bit from either source. 

80 

81 For each index `i` in the destination array `dest`, uniform 

82 crossover copies the value from the first source string `x0`with 

83 probability 0.5 and otherwise the value from the second source 

84 string `x1`. All bits that have the same value in `x0` and `x1` 

85 will retain this value in `dest`, all bits where `x0` and `x1` 

86 differ will effectively be randomized (be `0` with probability 0.5 

87 and `1` with probability 0.5). 

88 """ 

89 

90 def __init__(self): 

91 """Initialize the uniform crossover operator.""" 

92 super().__init__() 

93 self.op2 = uniform # type: ignore 

94 

95 def __str__(self) -> str: 

96 """ 

97 Get the name of this binary operator. 

98 

99 :return: "uniform" 

100 """ 

101 return "uniform"