Coverage for moptipyapps / spoc / spoc_4 / challenge_1 / beginner / experiment.py: 63%

38 statements  

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

1"""A small example experiment.""" 

2 

3import argparse 

4from typing import Final 

5 

6from moptipy.algorithms.so.rls import RLS 

7from moptipy.api.execution import Execution 

8from moptipy.api.experiment import run_experiment 

9from moptipy.api.improvement_logger import FileImprovementLoggerFactory 

10from moptipy.operators.permutations.op0_shuffle import Op0Shuffle 

11from moptipy.operators.permutations.op1_swap2 import Op1Swap2 

12from moptipy.operators.permutations.op1_swapn import Op1SwapN 

13from moptipy.spaces.bitstrings import BitStrings 

14from moptipy.spaces.permutations import Permutations 

15from moptipy.utils.help import moptipy_argparser 

16from pycommons.io.console import logger 

17from pycommons.io.path import Path 

18 

19from moptipyapps.spoc.spoc_4.challenge_1.beginner.instance import Instance 

20from moptipyapps.spoc.spoc_4.challenge_1.beginner.objective_no_penalty import ( 

21 BeginnerObjectiveNP, 

22) 

23from moptipyapps.spoc.spoc_4.challenge_1.beginner.permutation_encoding import ( 

24 PermutationEncoding, 

25) 

26from moptipyapps.spoc.submission import SubmissionSpace 

27 

28MAX_FES: Final[int] = 1_000 # Just a small test 

29 

30 

31def base_setup( 

32 instance: Instance, 

33 ilogger: FileImprovementLoggerFactory = FileImprovementLoggerFactory( 

34 max_files=10)) -> tuple[Permutations, Execution]: 

35 """ 

36 Create the basic setup. 

37 

38 :param instance: the instance to use 

39 :param title: a title 

40 :param desc: a description 

41 :param ilogger: the logger to use 

42 :return: the search space and the basic execution 

43 """ 

44 space: Final[Permutations] = Permutations.standard(instance.n) 

45 

46 return (space, Execution().set_max_fes(MAX_FES) 

47 .set_search_space(space) 

48 .set_encoding(PermutationEncoding(instance)) 

49 .set_objective(BeginnerObjectiveNP(instance)) 

50 .set_improvement_logger(ilogger) 

51 .set_solution_space(SubmissionSpace( 

52 space=BitStrings(instance.n), 

53 challenge_id="spoc-4-luna-tomato-logistics", 

54 problem_id=instance.name))) 

55 

56 

57def rls_n(instance: Instance) -> Execution: 

58 """ 

59 Create the RLS setup. 

60 

61 :param instance: the instance to use 

62 :return: the RLS 

63 """ 

64 space, execute = base_setup(instance=instance) 

65 return execute.set_algorithm(RLS( 

66 Op0Shuffle(space), Op1SwapN())) 

67 

68 

69def rls_1(instance: Instance) -> Execution: 

70 """ 

71 Create the RLS setup. 

72 

73 :param instance: the instance to use 

74 :return: the RLS 

75 """ 

76 space, execute = base_setup(instance=instance) 

77 return execute.set_algorithm(RLS( 

78 Op0Shuffle(space), Op1Swap2())) 

79 

80 

81def run(base_dir: str) -> None: 

82 """ 

83 Run the experiment. 

84 

85 :param base_dir: the base directory 

86 """ 

87 use_dir: Final[Path] = Path(base_dir) 

88 use_dir.ensure_dir_exists() 

89 logger(f"Writing to directory {use_dir!r}.") 

90 

91 run_experiment( 

92 base_dir=use_dir, 

93 instances=Instance.list_instances(), 

94 setups=[rls_n, rls_1], 

95 n_runs=(1, 2, 3, 4, 5, 6, 7, 8), 

96 perform_warmup=True, 

97 perform_pre_warmup=True) 

98 

99 

100# Run the experiment from the command line 

101if __name__ == "__main__": 

102 parser: Final[argparse.ArgumentParser] = moptipy_argparser( 

103 __file__, "SpOC 4: Challenge 1", "Run the 1st challenge of SpOC 4") 

104 parser.add_argument( 

105 "dest", help="the directory to store the experimental results under", 

106 type=Path, nargs="?", default="./results/") 

107 args: Final[argparse.Namespace] = parser.parse_args() 

108 run(args.dest)