Coverage for moptipyapps / spoc / spoc_4 / challenge_1 / beginner / permutation_encoding.py: 30%

27 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-03 04:37 +0000

1"""The permutation-based encoding.""" 

2 

3 

4import numba # type: ignore 

5import numpy as np 

6from moptipy.api.encoding import Encoding 

7 

8from moptipyapps.spoc.spoc_4.challenge_1.beginner.base_obj import ( 

9 BaseObjectWithArrays, 

10) 

11 

12 

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

14def _decode(x: np.ndarray, y: np.ndarray, data: np.ndarray, 

15 earth: np.ndarray, lunar: np.ndarray, dest: np.ndarray) -> None: 

16 """ 

17 Decode the permutation to a selection. 

18 

19 :param x: the candidate solution 

20 :param y: the candidate solution 

21 :param data: the orbit data 

22 :param earth: the earth orbits 

23 :param lunar: the lunar orbits 

24 :param dest: the destination orbits 

25 """ 

26 earth.fill(0) 

27 lunar.fill(0) 

28 dest.fill(0) 

29 y.fill(0) 

30 for index in x: 

31 ue = data[index, 0] 

32 if earth[ue]: 

33 continue 

34 earth[ue] = 1 

35 ul = data[index, 1] 

36 if lunar[ul]: 

37 continue 

38 lunar[ul] = 1 

39 ud = data[index, 2] 

40 if dest[ud]: 

41 continue 

42 dest[ud] = 1 

43 y[index] = True 

44 

45 

46class PermutationEncoding(BaseObjectWithArrays, Encoding): 

47 """The permutation-based decoding function of the beginner problem.""" 

48 

49 def decode(self, x, y) -> None: 

50 """ 

51 Decode a permutation to a selection. 

52 

53 :param x: the permutation 

54 :param y: the selection 

55 """ 

56 _decode(x, y, self.instance, self.earth, self.lunar, self.dest)