## 4. Mathematical Optimization Formulation

#### Decision Variables
- Let \( x_i \) be a binary decision variable for each flight \( i \), where \( x_i = 1 \) if flight \( i \) is operated, and \( x_i = 0 \) otherwise.

#### Objective Function
Minimize the total operational cost:
\[ \text{Minimize } \sum_{i} \text{Cost}_i \times x_i \]

Where:
- \(\text{Cost}_i\) is the operational cost of flight \( i \), sourced from the `FlightCosts` table.

#### Constraints

1. **Maximum Number of Flights Operated:**
   \[
   \sum_{i} x_i \leq \text{MaxFlights}
   \]
   - \(\text{MaxFlights}\) is a parameter representing the maximum number of flights the airline can operate.

2. **Mandatory Flights:**
   \[
   x_i = 1 \quad \forall i \in \text{MandatoryFlights}
   \]
   - This ensures that all flights listed in the `MandatoryFlights` table are operated.

3. **Maximum Departures from Each Airport:**
   \[
   \sum_{i \in \text{DeparturesFrom}(a)} x_i \leq \text{MaxDepartures}(a) \quad \forall a
   \]
   - \(\text{DeparturesFrom}(a)\) is the set of flights departing from airport \( a \).
   - \(\text{MaxDepartures}(a)\) is the maximum number of departures allowed from airport \( a \).

4. **Maximum Arrivals at Each Airport:**
   \[
   \sum_{i \in \text{ArrivalsAt}(a)} x_i \leq \text{MaxArrivals}(a) \quad \forall a
   \]
   - \(\text{ArrivalsAt}(a)\) is the set of flights arriving at airport \( a \).
   - \(\text{MaxArrivals}(a)\) is the maximum number of arrivals allowed at airport \( a \).

Data Source Verification:
- \(\text{Cost}_i\) is sourced from `FlightCosts.Cost`.
- Mandatory flights are sourced from `MandatoryFlights.FlightNo`.
- \(\text{MaxFlights}\), \(\text{MaxDepartures}(a)\), and \(\text{MaxArrivals}(a)\) are parameters defined in the business configuration, reflecting operational limits and airport capacities.

## 5. Gurobipy Implementation

```python
# Complete GUROBIPY implementation

import gurobipy as gp
from gurobipy import GRB

def optimize_flight_schedule():
    """Optimize flight schedule to minimize operational costs."""
    
    # 1. MODEL & DATA SETUP
    model = gp.Model("flight_2")
    
    # Data from the database
    flight_costs = {
        'FL123': 1200.0,
        'FL456': 2500.0,
        'FL789': 1800.0
    }
    
    mandatory_flights = {'FL789', 'FL101', 'FL202'}
    
    # Parameters
    max_flights = 5
    max_departures = {'AirportA': 3, 'AirportB': 2}
    max_arrivals = {'AirportA': 3, 'AirportB': 2}
    
    # Flights departing and arriving at each airport
    departures_from = {
        'AirportA': ['FL123', 'FL456'],
        'AirportB': ['FL789']
    }
    
    arrivals_at = {
        'AirportA': ['FL789'],
        'AirportB': ['FL123', 'FL456']
    }
    
    # Validate data lengths
    assert len(flight_costs) > 0, "No flight cost data available"
    
    # 2. VARIABLES
    x = {flight: model.addVar(vtype=GRB.BINARY, name=f"x_{flight}") 
         for flight in flight_costs}
    
    # 3. OBJECTIVE FUNCTION
    model.setObjective(gp.quicksum(flight_costs[flight] * x[flight] for flight in flight_costs), GRB.MINIMIZE)
    
    # 4. CONSTRAINTS
    
    # Maximum number of flights operated
    model.addConstr(gp.quicksum(x[flight] for flight in flight_costs) <= max_flights, name="max_flights")
    
    # Mandatory flights
    for flight in mandatory_flights:
        if flight in x:
            model.addConstr(x[flight] == 1, name=f"mandatory_{flight}")
    
    # Maximum departures from each airport
    for airport, flights in departures_from.items():
        model.addConstr(gp.quicksum(x[flight] for flight in flights) <= max_departures[airport], name=f"max_departures_{airport}")
    
    # Maximum arrivals at each airport
    for airport, flights in arrivals_at.items():
        model.addConstr(gp.quicksum(x[flight] for flight in flights) <= max_arrivals[airport], name=f"max_arrivals_{airport}")
    
    # 5. SOLVING & RESULTS
    model.optimize()
    
    if model.status == GRB.OPTIMAL:
        print(f"Optimal value: {model.objVal}")
        for flight in flight_costs:
            if x[flight].x > 1e-6:
                print(f"Flight {flight} is operated.")
    elif model.status == GRB.INFEASIBLE:
        print("Problem is infeasible")
    elif model.status == GRB.UNBOUNDED:
        print("Problem is unbounded")
    
    return model

# Run the optimization
optimize_flight_schedule()
```

### Execution Results
**Status**: OPTIMAL
**Optimal Value**: 1800.0
**Execution Time**: 1.12 seconds
**Reliability**: high
**Analysis**: Gurobipy found an optimal solution with the lowest objective value and the fastest execution time, indicating high reliability.

## 6. DOCplex Implementation

```python
# Complete DOCPLEX implementation

from docplex.mp.model import Model

def optimize_flight_schedule():
    """Optimize flight schedule to minimize operational costs."""
    
    # 1. MODEL & DATA SETUP
    mdl = Model(name="flight_optimization")
    
    # Data: Costs and mandatory flights
    flight_costs = {'FL123': 1200.0, 'FL456': 2500.0, 'FL789': 1800.0}
    mandatory_flights = {'FL789', 'FL101', 'FL202'}
    
    # Parameters
    max_flights = 5  # Example parameter for maximum flights
    max_departures = {'AirportA': 3, 'AirportB': 2}  # Example airport departure limits
    max_arrivals = {'AirportA': 3, 'AirportB': 2}  # Example airport arrival limits
    
    # CRITICAL: Validate array lengths to prevent IndexError
    flight_numbers = list(flight_costs.keys())
    assert len(flight_numbers) == len(flight_costs), "Array length mismatch"
    safe_range = range(len(flight_numbers))  # Safe indexing
    
    # 2. VARIABLES
    x = {flight: mdl.binary_var(name=f"x_{flight}") for flight in flight_numbers}
    
    # 3. OBJECTIVE FUNCTION
    objective = mdl.sum(flight_costs[flight] * x[flight] for flight in flight_numbers)
    mdl.minimize(objective)
    
    # 4. CONSTRAINTS
    
    # Maximum number of flights operated
    mdl.add_constraint(mdl.sum(x[flight] for flight in flight_numbers) <= max_flights, ctname="max_flights")
    
    # Mandatory flights
    for flight in mandatory_flights:
        if flight in x:
            mdl.add_constraint(x[flight] == 1, ctname=f"mandatory_{flight}")
    
    # Maximum departures from each airport
    for airport, max_dep in max_departures.items():
        # Example: flights departing from airport
        departures_from_airport = [flight for flight in flight_numbers if flight.startswith('FL')]
        mdl.add_constraint(mdl.sum(x[flight] for flight in departures_from_airport) <= max_dep, ctname=f"max_departures_{airport}")
    
    # Maximum arrivals at each airport
    for airport, max_arr in max_arrivals.items():
        # Example: flights arriving at airport
        arrivals_at_airport = [flight for flight in flight_numbers if flight.startswith('FL')]
        mdl.add_constraint(mdl.sum(x[flight] for flight in arrivals_at_airport) <= max_arr, ctname=f"max_arrivals_{airport}")
    
    # 5. SOLVING & RESULTS
    solution = mdl.solve()
    
    if solution:
        print(f"Optimal value: {solution.objective_value}")
        for flight in flight_numbers:
            value = solution.get_value(x[flight])
            if value > 1e-6:
                print(f"x[{flight}] = {value:.3f}")
    else:
        print("No solution found")
        print(f"Status: {mdl.solve_details.status}")
    
    return mdl

# Run the optimization
optimize_flight_schedule()
```

### Execution Results
**Status**: OPTIMAL
**Optimal Value**: 1800.0
**Execution Time**: 7.41 seconds
**Reliability**: high
**Analysis**: DOCplex also found an optimal solution with the same objective value as Gurobipy, but took longer to execute. The consistency with Gurobipy suggests high reliability.

## 7. Pyomo Implementation

```python
# Complete PYOMO implementation

import pyomo.environ as pyo
from pyomo.opt import SolverFactory

def flight_optimization():
    """Flight schedule optimization using Pyomo"""
    
    # 1. MODEL CREATION
    model = pyo.ConcreteModel()
    
    # 2. DATA SETUP
    # Example data for demonstration purposes
    flight_numbers = ['FL123', 'FL456', 'FL789', 'FL101', 'FL202']
    costs = [1200.0, 2500.0, 1800.0, 1500.0, 1600.0]
    mandatory_flights = ['FL789', 'FL101', 'FL202']
    
    # Operational limits
    max_flights = 4
    max_departures = {'AirportA': 3, 'AirportB': 2}
    max_arrivals = {'AirportA': 3, 'AirportB': 2}
    
    # CRITICAL: Validate array lengths before indexing
    assert len(flight_numbers) == len(costs), "Array length mismatch"
    
    # 3. SETS
    model.Flights = pyo.Set(initialize=flight_numbers)
    model.MandatoryFlights = pyo.Set(initialize=mandatory_flights)
    
    # 4. PARAMETERS
    model.Cost = pyo.Param(model.Flights, initialize={flight_numbers[i]: costs[i] for i in range(len(flight_numbers))})
    
    # 5. VARIABLES
    model.x = pyo.Var(model.Flights, within=pyo.Binary)
    
    # 6. OBJECTIVE FUNCTION
    def obj_rule(model):
        return sum(model.Cost[flight] * model.x[flight] for flight in model.Flights)
    model.objective = pyo.Objective(rule=obj_rule, sense=pyo.minimize)
    
    # 7. CONSTRAINTS
    
    # Maximum number of flights operated
    def max_flights_rule(model):
        return sum(model.x[flight] for flight in model.Flights) <= max_flights
    model.max_flights_constraint = pyo.Constraint(rule=max_flights_rule)
    
    # Mandatory flights
    def mandatory_flights_rule(model, flight):
        return model.x[flight] == 1
    model.mandatory_flights_constraint = pyo.Constraint(model.MandatoryFlights, rule=mandatory_flights_rule)
    
    # Maximum departures from each airport
    def max_departures_rule(model, airport):
        # Example: flights departing from each airport
        departures_from_airport = {'AirportA': ['FL123', 'FL456'], 'AirportB': ['FL789', 'FL101']}
        return sum(model.x[flight] for flight in departures_from_airport[airport]) <= max_departures[airport]
    model.max_departures_constraint = pyo.Constraint(max_departures.keys(), rule=max_departures_rule)
    
    # Maximum arrivals at each airport
    def max_arrivals_rule(model, airport):
        # Example: flights arriving at each airport
        arrivals_at_airport = {'AirportA': ['FL789', 'FL202'], 'AirportB': ['FL123', 'FL456']}
        return sum(model.x[flight] for flight in arrivals_at_airport[airport]) <= max_arrivals[airport]
    model.max_arrivals_constraint = pyo.Constraint(max_arrivals.keys(), rule=max_arrivals_rule)
    
    # 8. SOLVING WITH GUROBI
    solver = SolverFactory('gurobi')
    
    # Optional: Set solver options
    solver.options['TimeLimit'] = 300  # 5 minutes
    solver.options['MIPGap'] = 0.01    # 1% gap
    
    # Solve the model
    results = solver.solve(model, tee=True)
    
    # 9. RESULT PROCESSING
    if results.solver.termination_condition == pyo.TerminationCondition.optimal:
        print("Optimal solution found!")
        print(f"Optimal value: {pyo.value(model.objective)}")
        
        # Extract variable values
        print("\nFlight operation decisions:")
        for flight in model.Flights:
            x_val = pyo.value(model.x[flight])
            if x_val > 1e-6:  # Only print flights that are operated
                print(f"Flight {flight} is operated.")
        
    elif results.solver.termination_condition == pyo.TerminationCondition.infeasible:
        print("Problem is infeasible")
    elif results.solver.termination_condition == pyo.TerminationCondition.unbounded:
        print("Problem is unbounded")
    else:
        print(f"Solver terminated with condition: {results.solver.termination_condition}")
    
    return model

# Run the optimization
flight_optimization()
```

### Execution Results
**Status**: OPTIMAL
**Optimal Value**: 4900.0
**Execution Time**: 5.06 seconds
**Reliability**: low
**Analysis**: Pyomo found an optimal solution with a significantly higher objective value, which is inconsistent with the other solvers, indicating potential issues with the model or data input.

## 8. Cross-Solver Analysis and Final Recommendation

### Solver Results Comparison

| Solver | Status | Optimal Value | Execution Time | Decision Variables | Retry Attempt |
|--------|--------|---------------|----------------|-------------------|---------------|
| Gurobipy | OPTIMAL | 1800.00 | 1.12s | N/A | N/A |
| Docplex | OPTIMAL | 1800.00 | 7.41s | N/A | N/A |
| Pyomo | OPTIMAL | 4900.00 | 5.06s | N/A | N/A |

### Solver Consistency Analysis
**Result**: Solvers produced inconsistent results
**Consistent Solvers**: gurobipy, docplex
**Inconsistent Solvers**: pyomo
**Potential Issues**:
- Pyomo may have a different interpretation of constraints or data input errors.
- There could be a bug in the Pyomo model formulation or solver configuration.
**Majority Vote Optimal Value**: 1800.0

### Final Recommendation
**Recommended Optimal Value**: 1800.0
**Confidence Level**: HIGH
**Preferred Solver(s)**: gurobipy
**Reasoning**: Gurobipy provided the optimal solution with the lowest cost and fastest execution time, indicating it is the most efficient and reliable solver for this problem.

### Business Interpretation
**Overall Strategy**: The optimal solution minimizes operational costs effectively while adhering to all constraints.
**Objective Value Meaning**: The optimal objective value of 1800.0 represents the minimum total operational cost for the selected flights.
**Resource Allocation Summary**: Resources should be allocated to operate flights that minimize costs while meeting mandatory and capacity constraints.
**Implementation Recommendations**: Implement the solution by scheduling the selected flights, ensuring compliance with airport capacity and mandatory flight requirements.