solutions.algorithms
¶
This module implements the core matching and optimization algorithms for the Air Force Cadet Career Problem (AFCCP). It contains both deterministic assignment methods (e.g., Hospitals/Residents, rated board procedures) and meta-heuristic approaches (e.g., genetic algorithms) to explore feasible and optimal cadetβAFSC assignments under a variety of policy and quota conditions.
Contents¶
- Matching Algorithms
classic_hr
: Implements the Hospitals/Residents deferred acceptance algorithm.rotc_rated_board_original
: Recreates the legacy ROTC Rated board assignment process.soc_rated_matching_algorithm
: Source-of-commission (SOC) specific HR matching with reserves and matched slots.allocate_ots_candidates_original_method
: Adds OTS candidates into the status quo one-market solution.
- Meta-Heuristic Algorithms
vft_genetic_algorithm
: Evolves candidate solutions using a genetic algorithm with Value-Focused Thinking objectives.genetic_matching_algorithm
: Tunes AFSC capacities for the HR algorithm to minimize blocking pairs.
Workflow¶
- Initialization
- Load cadet and AFSC preferences, quotas, and capacities from the
CadetCareerProblem
instance. - Establish dictionaries for cadet proposals, AFSC acceptances, and reserves.
- Load cadet and AFSC preferences, quotas, and capacities from the
- Assignment Algorithms
- Apply Hospitals/Residents (HR) or rated-board logic to produce a feasible cadet-to-AFSC assignment.
- Track iterations of proposals, matches, and rejections for debugging and visualization (e.g., BubbleCharts).
- Optimization Algorithms
- Use genetic search to evolve solutions, balancing AFSC quotas, cadet utilities, and blocking pair minimization.
- Support hybrid workflows where GA-produced capacities are re-validated through HR-style deferred acceptance.
See Also¶
afccp.solutions.handling
: Fitness functions, blocking pair evaluation, and solution utilities.afccp.data.preferences
: Helpers for determining SOC-specific rated AFSCs and cadet preference matrices.
classic_hr(instance, capacities=None, printing=True)
¶
Matches cadets and AFSCs across all rated, space, and NRL positions using the Hospitals/Residents algorithm.
Parameters: instance (CadetCareerProblem): The instance of the CadetCareerProblem class. capacities (numpy.ndarray or None): The capacities of AFSCs. If None, the capacities are taken from the instance parameters. Default is None. printing (bool): Whether to print status updates or not. Default is True.
Returns: dict: The solution dictionary containing the assigned AFSCs for each cadet.
This function implements the Hospitals/Residents algorithm to match cadets and AFSCs across all rated, space,
and NRL positions. It takes an instance of the CadetCareerProblem class as input and an optional parameter
capacities
to specify the capacities of AFSCs. If capacities
is None, the capacities are taken from the
instance parameters. By default, the function prints status updates during the matching process.
The algorithm initializes the necessary variables and dictionaries. It then proceeds with the Hospitals/Residents algorithm by having cadets propose to their top choices and AFSCs accept or reject cadets based on their preferences and capacities. The matching process continues until all cadets are matched or have exhausted their preferences. The function updates the matches and rejections for each AFSC and tracks the progress through iterations.
The function returns a solution dictionary containing the assigned AFSCs for each cadet.
Example usage: solution = classic_hr(instance, capacities=capacities, printing=True)
Source code in afccp/solutions/algorithms.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
rotc_rated_board_original(instance, printing=False)
¶
Assigns Rated AFSCs to ROTC cadets based on their preferences and the existing quotas for each AFSC using the current rated board algorithm.
Parameters: instance (CadetCareerProblem): The instance of the CadetCareerProblem class. printing (bool): Whether to print status updates or not. Default is False.
Returns: dict: The solution dictionary containing the assigned AFSCs for each cadet.
This function assigns Rated AFSCs to ROTC cadets based on their preferences and the existing quotas for each AFSC.
It follows the current rated board algorithm. The function takes an instance of the CadetCareerProblem class as
input and an optional parameter printing
to specify whether to print status updates. By default, printing
is
set to False. The function initializes the necessary variables and dictionaries for the algorithm. It then goes
through each phase of the rated board algorithm, considering cadets' order of merit and interest levels for each
AFSC. Cadets are assigned AFSCs based on availability and eligibility. The function updates the assigned AFSCs
for each cadet and tracks the number of matched cadets for each AFSC. Finally, it converts the assigned AFSCs into
a solution dictionary and returns it.
Example usage: solution = rotc_rated_board_original(instance, printing=True)
Source code in afccp/solutions/algorithms.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
soc_rated_matching_algorithm(instance, soc='usafa', printing=True)
¶
Matches or reserves cadets to their Rated AFSCs based on the Source of Commissioning (SOC) using the Hospitals/Residents algorithm.
Parameters: instance (CadetCareerProblem): The instance of the CadetCareerProblem class. soc (str): The SOC for which to perform the matching algorithm. Options are 'usafa' (United States Air Force Academy) or 'rotc' (Reserve Officer Training Corps). Default is 'usafa'. printing (bool): Whether to print status updates or not. Default is True.
Returns: tuple: A tuple containing three solution dictionaries: the overall solution, the reserves solution, and the matches solution.
This function implements the Hospitals/Residents algorithm to match or reserve cadets to their Rated AFSCs
based on the Source of Commissioning (SOC). It takes an instance of the CadetCareerProblem class as input and
an optional parameter soc
to specify the SOC for which the matching algorithm should be performed. The available
options for soc
are 'usafa' (United States Air Force Academy) and 'rotc' (Reserve Officer Training Corps). By
default, the SOC is set to 'usafa'. The function also takes an optional parameter printing
to control whether
status updates are printed during the matching process.
The algorithm initializes the necessary variables and dictionaries. It then proceeds with the Hospitals/Residents algorithm by having cadets propose to their top choices and AFSCs accept or reject cadets based on their preferences and capacities. The matching process continues until all cadets are matched or have exhausted their preferences. The function tracks the progress through iterations and collects information on both reserved and matched AFSCs.
The function returns a tuple containing three solution dictionaries: the overall solution, the reserves solution, and the matches solution. Each solution dictionary contains the assigned AFSCs for each cadet. The reserves solution only includes cadets with reserved slots, the matches solution only includes cadets with matched slots, and the overall solution includes both cadets with reserved and matched slots.
Example usage: solution, reserves, matches = soc_rated_matching_algorithm(instance, soc='usafa', printing=True)
Source code in afccp/solutions/algorithms.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
vft_genetic_algorithm(instance, initial_solutions=None, con_fail_dict=None, printing=False)
¶
Solves the optimization problem using a genetic algorithm.
Parameters: instance (CadetCareerProblem): An instance of the CadetCareerProblem class representing the optimization problem. initial_solutions (ndarray or None): An optional array of initial solutions in the population. If provided, it should be a numpy ndarray of shape (pop_size, N) where pop_size is the size of the population and N is the number of cadets. Default is None. con_fail_dict (dict or None): An optional dictionary containing information about constraints that failed for the initial solutions. It should be a dictionary where the keys are the indices of the initial solutions (0-based) and the values are lists of constraint indices that failed for that solution. Default is None. printing (bool): A flag indicating whether to print status updates during the genetic algorithm execution. Default is False.
Returns: tuple: A tuple containing the best solution and the time evaluation dataframe (if time evaluation is enabled).
This function implements a genetic algorithm to solve the optimization problem defined by the CadetCareerProblem instance. The genetic algorithm works by iteratively evolving a population of candidate solutions through selection, crossover, and mutation operations. The fitness of each solution is evaluated using the Value-Focused Thinking (VFT) objective function.
The genetic algorithm operates as follows: 1. Initialize the population: If initial_solutions are provided, they are used as the initial population. Otherwise, a random population is generated. 2. Evaluate the fitness of each solution in the population using the VFT objective function. 3. Sort the population based on the fitness scores in descending order. 4. Create the next generation of solutions: - The top two solutions (best fitness) from the current population are automatically included in the next generation. - For the remaining solutions, select two parents based on their fitness scores using rank selection. - Apply multi-point crossover to generate two offspring solutions from the selected parents. - Perform mutation on the offspring solutions to introduce small random changes. - Add the offspring solutions to the next generation. 5. Repeat steps 2-4 until the termination condition is met (e.g., maximum time limit).
The best solution found during the genetic algorithm execution is returned as the output. If time evaluation is enabled, a time evaluation dataframe is also returned, containing the objective values at different time points during the algorithm execution.
Example usage: solution, time_eval_df = vft_genetic_algorithm(instance, initial_solutions, con_fail_dict, printing=True)
Source code in afccp/solutions/algorithms.py
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 |
|
genetic_matching_algorithm(instance, printing=False)
¶
Genetic algorithm that determines optimal capacities to the classic deferred acceptance algorithm to minimize blocking pairs
Parameters: instance (CadetCareerProblem): An instance of the CadetCareerProblem class representing the optimization problem. printing (bool): A flag indicating whether to print additional information during the algorithm execution. Default is False.
Returns: ndarray: An array representing the optimal capacities determined by the genetic algorithm.
This function implements a genetic algorithm to determine the optimal capacities for the classic deferred acceptance algorithm. The goal is to minimize the number of blocking pairs in the matching process.
The genetic algorithm works as follows: 1. Initialize the population of capacities randomly. Each capacity is selected within the valid range for the corresponding AFSC. 2. Evaluate the fitness of each capacity configuration using the classic deferred acceptance algorithm with the given capacities. The fitness is determined by the number of blocking pairs in the resulting matching. 3. Sort the population based on fitness scores in descending order. 4. Create the next generation of capacities: - The two best capacities (lowest fitness) from the current population are automatically included in the next generation. - For the remaining capacities, select two parents based on their fitness scores using rank selection. - Apply multi-point crossover to generate two offspring capacities from the selected parents. - Perform mutation on the offspring capacities to introduce small random changes. - Add the offspring capacities to the next generation. 5. Repeat steps 2-4 until a termination condition is met (e.g., maximum time or number of generations).
The best capacity configuration found during the genetic algorithm execution is returned as the output.
Example usage: optimal_capacities = genetic_matching_algorithm(instance, printing=True)
Source code in afccp/solutions/algorithms.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 |
|