Skip to content

CadetCareerProblem – Optimization Models & Meta-Heuristics Methods

solve_vft_pyomo_model(p_dict={}, printing=None)

Solve the VFT model using Pyomo, an optimization modeling library.

This method is responsible for solving the Value Focussed Thinking (VFT) model using Pyomo. The VFT model is a specific type of optimization model. It conducts the necessary preparation, builds the model, solves it, and handles the resulting solution. The goal is to find optimal solutions for the VFT problem.

Args: p_dict (dict): A dictionary of parameters used for the model. It allows for customization of the model's input parameters. printing (bool, None): A flag to control whether to print information during the model-solving process. If set to True, the method will print progress and debugging information. If set to False, it will suppress printing. If None, the method will use the default printing setting from the class instance.

Returns: solution: The solution of the VFT model, which contains the optimal values for the decision variables and other relevant information.

Notes: - Before using this method, it's important to ensure that the class instance contains valid and appropriate data. - This method uses external functions for building and solving the Pyomo model, and the specifics of those functions are located in the 'afccp.solutions.optimization' module.

Source code in afccp/main.py
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
def solve_vft_pyomo_model(self, p_dict={}, printing=None):
    """
    Solve the VFT model using Pyomo, an optimization modeling library.

    This method is responsible for solving the Value Focussed Thinking (VFT) model using Pyomo. The VFT model is a specific
    type of optimization model. It conducts the necessary preparation, builds the model, solves it, and handles the
    resulting solution. The goal is to find optimal solutions for the VFT problem.

    Args:
        p_dict (dict): A dictionary of parameters used for the model. It allows for customization of the model's
            input parameters.
        printing (bool, None): A flag to control whether to print information during the model-solving process. If set to
            True, the method will print progress and debugging information. If set to False, it will suppress printing.
            If None, the method will use the default printing setting from the class instance.

    Returns:
        solution: The solution of the VFT model, which contains the optimal values for the decision variables and other
            relevant information.

    Notes:
    - Before using this method, it's important to ensure that the class instance contains valid and appropriate data.
    - This method uses external functions for building and solving the Pyomo model, and the specifics of those functions
      are located in the 'afccp.solutions.optimization' module.
    """
    self._error_checking("Pyomo Model")
    if printing is None:
        printing = self.printing

    # Reset instance model parameters
    self._reset_functional_parameters(p_dict)

    # Build the model and then solve it
    model, q = afccp.solutions.optimization.vft_model_build(self, printing=printing)
    solution = afccp.solutions.optimization.solve_pyomo_model(self, model, "VFT", q=q, printing=printing)

    # Determine what to do with the solution
    self._solution_handling(solution)

    # Return the solution
    return solution

solve_original_pyomo_model(p_dict={}, printing=None)

Solve the original AFPC model using pyomo

Source code in afccp/main.py
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
def solve_original_pyomo_model(self, p_dict={}, printing=None):
    """
    Solve the original AFPC model using pyomo
    """
    self._error_checking("Pyomo Model")
    if printing is None:
        printing = self.printing

    # One little "switch" to get the original model objective function
    p_dict['assignment_model_obj'] = "Original Utility"

    # Reset instance model parameters
    self._reset_functional_parameters(p_dict)

    # Build the model and then solve it
    model = afccp.solutions.optimization.assignment_model_build(self, printing=printing)
    solution = afccp.solutions.optimization.solve_pyomo_model(self, model, "Original", printing=printing)

    # Determine what to do with the solution
    self._solution_handling(solution)

    # Return the solution
    return solution

solve_guo_pyomo_model(p_dict={}, printing=None)

Solve the "generalized assignment problem" model with the new global utility matrix constructed from the AFSC and Cadet Utility matrices. This is the "GUO" model.

Source code in afccp/main.py
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
def solve_guo_pyomo_model(self, p_dict={}, printing=None):
    """
    Solve the "generalized assignment problem" model with the new global utility matrix constructed
    from the AFSC and Cadet Utility matrices. This is the "GUO" model.
    """
    # One little "switch" to get the new assignment model objective function
    p_dict['assignment_model_obj'] = "Global Utility"

    # Reset instance model parameters
    self._reset_functional_parameters(p_dict)

    # Error handling
    self._error_checking("Pyomo Model")
    if printing is None:
        printing = self.printing

    # Determine solution method
    if self.mdl_p['solution_method'] is None:
        solution_method = 'GUO'
    else:
        solution_method = self.mdl_p['solution_method']

    # Build the model and then solve it
    model = afccp.solutions.optimization.assignment_model_build(self, printing=printing)
    solution = afccp.solutions.optimization.solve_pyomo_model(self, model, solution_method, printing=printing)

    # Determine what to do with the solution
    self._solution_handling(solution)

    # Return the solution
    return solution

solve_gp_pyomo_model(p_dict={}, printing=None)

Solve the Goal Programming Model (Created by Lt. Reynolds)

Source code in afccp/main.py
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
def solve_gp_pyomo_model(self, p_dict={}, printing=None):
    """
    Solve the Goal Programming Model (Created by Lt. Reynolds)
    """
    self._error_checking("Pyomo Model")
    if printing is None:
        printing = self.printing

    # Reset instance model parameters
    self._reset_functional_parameters(p_dict)

    # Convert VFT parameters to Goal Programming ("gp") parameters
    if self.gp_parameters is None:
        self.vft_to_gp_parameters(self.mdl_p)

    # Build the model and then solve it
    model = afccp.solutions.optimization.gp_model_build(self, printing=printing)
    solution = afccp.solutions.optimization.solve_pyomo_model(self, model, "GP", printing=printing)

    # Determine what to do with the solution
    self._solution_handling(solution)

    # Return the solution
    return solution

solve_vft_main_methodology(p_dict={}, printing=None)

This is the main method to solve the problem instance. We first determine an initial population of solutions. We then evolve the solutions further using the GA.

Source code in afccp/main.py
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
def solve_vft_main_methodology(self, p_dict={}, printing=None):
    """
    This is the main method to solve the problem instance. We first determine an initial population of solutions.
    We then evolve the solutions further using the GA.
    """
    self._error_checking("Pyomo Model")
    if printing is None:
        printing = self.printing

    # Reset instance model parameters
    self._reset_functional_parameters(p_dict)

    # Determine population for the genetic algorithm
    if self.mdl_p['population_generation_model'] == 'Assignment':
        self.mdl_p["initial_solutions"] = \
            afccp.solutions.sensitivity.populate_initial_ga_solutions_from_assignment_model(self, printing)
    else:
        self.mdl_p["initial_solutions"] = \
            afccp.solutions.sensitivity.populate_initial_ga_solutions_from_vft_model(self, printing)

    # Add additional solutions if necessary
    if self.mdl_p["solution_names"] is not None:

        # In case the user specifies "Solution" instead of ["Solution"]
        if type(self.mdl_p["solution_names"]) == str:
            self.mdl_p["solution_names"] = [self.mdl_p["solution_names"]]

        # Add additional solutions
        for solution_name in self.mdl_p["solution_names"]:
            solution = self.solutions[solution_name]
            self.mdl_p["initial_solutions"] = np.vstack((self.mdl_p["initial_solutions"], solution))

    self.mdl_p["initialize"] = True  # Force the initialize parameter to be true

    if printing:
        now = datetime.datetime.now()
        print('Solving Genetic Algorithm for ' + str(self.mdl_p["ga_max_time"]) + ' seconds at ' +
              now.strftime('%H:%M:%S') + '...')
    self.vft_genetic_algorithm(self.mdl_p, printing=self.mdl_p["ga_printing"])
    if printing:
        print('Solution value of ' + str(round(self.solution['z'], 4)) + ' obtained.')

    # Return solution
    return self.solution

vft_genetic_algorithm(p_dict={}, printing=None)

This is the genetic algorithm. The hyper-parameters to the algorithm can be tuned, and this is meant to be solved in conjunction with the pyomo model solution. Use that as the initial solution, and then we evolve from there

Source code in afccp/main.py
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
def vft_genetic_algorithm(self, p_dict={}, printing=None):
    """
    This is the genetic algorithm. The hyper-parameters to the algorithm can be tuned, and this is meant to be
    solved in conjunction with the pyomo model solution. Use that as the initial solution, and then we evolve
    from there
    """
    self._error_checking("Value Parameters")
    if printing is None:
        printing = self.printing

    # Reset instance model parameters
    self._reset_functional_parameters(p_dict)

    # Dictionary of failed constraints across all solutions (phased out since we're using APM as initial population)
    con_fail_dict = None

    # Get a starting population of solutions if applicable!
    if self.mdl_p["initialize"]:

        if self.mdl_p["initial_solutions"] is None:

            if self.solutions is None:
                raise ValueError("Error. No solutions in dictionary.")

            else:
                if self.mdl_p["solution_names"] is None:

                    # Get list of initial solutions
                    initial_solutions = np.array(
                        [self.solutions[solution_name]['j_array'] for solution_name in self.solutions])
                    solution_names = list(self.solutions.keys())

                else:

                    # If we just pass "Solution" instead of ["Solution"]
                    if type(self.mdl_p["solution_names"]) == str:
                        self.mdl_p["solution_names"] = [self.mdl_p["solution_names"]]

                    # Get list of initial solutions
                    initial_solutions = np.array(
                        [self.solutions[solution_name]['j_array'] for solution_name in self.mdl_p["solution_names"]])
                    solution_names = self.mdl_p["solution_names"]

                if printing:
                    print("Running Genetic Algorithm with initial solutions:", solution_names)

        else:

            # Get list of initial solutions
            initial_solutions = self.mdl_p["initial_solutions"]
            if printing:
                print("Running Genetic Algorithm with", len(initial_solutions), "initial solutions...")

    else:

        if printing:
            print("Running Genetic Algorithm with no initial solutions (not advised!)...")
        initial_solutions = None

    # Generate the solution
    solution, time_eval_df = afccp.solutions.algorithms.vft_genetic_algorithm(
        self, initial_solutions, con_fail_dict, printing=printing)

    # Determine what to do with the solution
    self._solution_handling(solution)

    # Return the final solution and maybe the time evaluation dataframe if needed
    if self.mdl_p["time_eval"]:
        return time_eval_df, solution
    else:
        return solution

genetic_matching_algorithm(p_dict={}, printing=None)

This method solves the problem instance using "Genetic Matching Algorithm"

Source code in afccp/main.py
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
def genetic_matching_algorithm(self, p_dict={}, printing=None):
    """
    This method solves the problem instance using "Genetic Matching Algorithm"
    """
    if printing is None:
        printing = self.printing

    # Reset instance model parameters
    self._reset_functional_parameters(p_dict)

    # Force solution iteration collection to be turned off
    self.mdl_p['collect_solution_iterations'] = False

    # Get the capacities
    capacities = afccp.solutions.algorithms.genetic_matching_algorithm(self, printing=printing)

    # Update capacities in parameters (quota_max or quota_min)
    self.parameters[self.mdl_p['capacity_parameter']] = capacities

    # Run the matching algorithm with these capacities
    solution = afccp.solutions.algorithms.classic_hr(self, printing=printing)

    # Determine what to do with the solution
    self._solution_handling(solution)

    return solution