=== Problem Context ===
# Complete Optimization Problem and Solution: student_1

## 1. Problem Context and Goals

### Context  
A school district is tasked with assigning students to classrooms in a way that balances the workload among teachers while ensuring that no classroom exceeds its capacity. The primary decision involves determining whether a student is assigned to a specific classroom, represented as a binary choice. The operational goal is to minimize the maximum number of students in any classroom, ensuring that the distribution of students is as even as possible across all classrooms.  

The business configuration includes a predefined maximum number of students allowed in any classroom, which is used to guide the optimization process. Additionally, a business logic formula calculates the maximum number of students in any classroom by summing the assignments for each classroom and identifying the highest value. This calculation directly supports the optimization objective of minimizing the maximum number of students in any classroom.  

The problem is designed to ensure that each student is assigned to exactly one classroom and that the total number of students in each classroom does not exceed its capacity. These requirements are expressed as linear relationships, avoiding any nonlinear scenarios such as variable products or divisions.  

### Goals  
The optimization goal is to minimize the maximum number of students in any classroom. Success is measured by achieving the smallest possible value for this maximum, ensuring that classroom assignments are balanced and within capacity limits. This goal aligns with the operational parameters and business configuration, which provide the necessary data and constraints to guide the optimization process.  

## 2. Constraints  

The problem is subject to the following constraints:  
1. **Student Assignment Constraint**: Each student must be assigned to exactly one classroom. This ensures that all students are placed and no student is left unassigned.  
2. **Classroom Capacity Constraint**: The total number of students assigned to each classroom must not exceed its predefined capacity. This ensures that classroom sizes remain within acceptable limits.  
3. **Maximum Students Constraint**: The total number of students assigned to any classroom must be less than or equal to the maximum number of students allowed in any classroom. This directly supports the optimization objective of minimizing the maximum number of students in any classroom.  

These constraints are expressed in linear terms, ensuring that the optimization problem remains straightforward and avoids any nonlinear complexities.  

## 3. Available Data  

### Database Schema  
```sql
-- Iteration 2 Database Schema
-- Objective: Added a new table for maximum students per classroom and updated business configuration logic to include a formula for calculating the maximum number of students in any classroom.

CREATE TABLE classroom_capacities (
  classroom_id INTEGER,
  capacity INTEGER
);

CREATE TABLE student_assignments (
  student_id INTEGER,
  classroom_id INTEGER,
  assignment BOOLEAN
);

CREATE TABLE max_students_per_classroom (
  classroom_id INTEGER,
  max_students INTEGER
);
```

### Data Dictionary  
- **classroom_capacities**:  
  - **Purpose**: Stores the maximum number of students allowed in each classroom.  
  - **Columns**:  
    - `classroom_id`: Unique identifier for each classroom.  
    - `capacity`: The maximum number of students allowed in the classroom.  

- **student_assignments**:  
  - **Purpose**: Tracks the assignment of students to classrooms.  
  - **Columns**:  
    - `student_id`: Unique identifier for each student.  
    - `classroom_id`: Unique identifier for each classroom.  
    - `assignment`: Indicates whether the student is assigned to the classroom (True or False).  

- **max_students_per_classroom**:  
  - **Purpose**: Represents the maximum number of students in any classroom.  
  - **Columns**:  
    - `classroom_id`: Unique identifier for each classroom.  
    - `max_students`: The maximum number of students in the classroom.  


=== Schema ===
-- Iteration 2 Database Schema
-- Objective: Added a new table for maximum students per classroom and updated business configuration logic to include a formula for calculating the maximum number of students in any classroom.

CREATE TABLE classroom_capacities (
  classroom_id INTEGER,
  capacity INTEGER
);

CREATE TABLE student_assignments (
  student_id INTEGER,
  classroom_id INTEGER,
  assignment BOOLEAN
);

CREATE TABLE max_students_per_classroom (
  classroom_id INTEGER,
  max_students INTEGER
);



=== LLM Response ===
```sql
-- Query Description: Retrieve the current number of students assigned to each classroom.
-- This is important for understanding the current distribution of students across classrooms and identifying which classrooms are over or under capacity.
SELECT 
    sa.classroom_id, 
    COUNT(sa.student_id) AS current_students
FROM 
    student_assignments sa
WHERE 
    sa.assignment = TRUE
GROUP BY 
    sa.classroom_id;

-- Query Description: Retrieve the capacity of each classroom.
-- This is crucial for ensuring that the number of students assigned to each classroom does not exceed its capacity.
SELECT 
    cc.classroom_id, 
    cc.capacity
FROM 
    classroom_capacities cc;

-- Query Description: Retrieve the maximum number of students allowed in any classroom.
-- This is essential for the optimization objective of minimizing the maximum number of students in any classroom.
SELECT 
    mspc.classroom_id, 
    mspc.max_students
FROM 
    max_students_per_classroom mspc;

-- Query Description: Retrieve the list of students who are not yet assigned to any classroom.
-- This helps in identifying students who need to be assigned, ensuring that the student assignment constraint is met.
SELECT 
    sa.student_id
FROM 
    student_assignments sa
WHERE 
    sa.assignment = FALSE;

-- Query Description: Retrieve the total number of students assigned to each classroom along with its capacity.
-- This provides a comprehensive view of how many students are in each classroom relative to its capacity, which is useful for balancing the workload.
SELECT 
    sa.classroom_id, 
    COUNT(sa.student_id) AS current_students, 
    cc.capacity
FROM 
    student_assignments sa
JOIN 
    classroom_capacities cc ON sa.classroom_id = cc.classroom_id
WHERE 
    sa.assignment = TRUE
GROUP BY 
    sa.classroom_id, cc.capacity;

-- Query Description: Retrieve the maximum number of students in any classroom based on current assignments.
-- This is directly related to the optimization goal of minimizing the maximum number of students in any classroom.
SELECT 
    MAX(current_students) AS max_students_in_any_classroom
FROM (
    SELECT 
        sa.classroom_id, 
        COUNT(sa.student_id) AS current_students
    FROM 
        student_assignments sa
    WHERE 
        sa.assignment = TRUE
    GROUP BY 
        sa.classroom_id
) AS classroom_counts;

-- Query Description: Retrieve the list of classrooms that are currently over capacity.
-- This is important for identifying classrooms that need to have students reassigned to meet the capacity constraint.
SELECT 
    sa.classroom_id, 
    COUNT(sa.student_id) AS current_students, 
    cc.capacity
FROM 
    student_assignments sa
JOIN 
    classroom_capacities cc ON sa.classroom_id = cc.classroom_id
WHERE 
    sa.assignment = TRUE
GROUP BY 
    sa.classroom_id, cc.capacity
HAVING 
    COUNT(sa.student_id) > cc.capacity;

-- Query Description: Retrieve the list of classrooms that are under capacity.
-- This helps in identifying classrooms that can accommodate more students, aiding in the redistribution process.
SELECT 
    sa.classroom_id, 
    COUNT(sa.student_id) AS current_students, 
    cc.capacity
FROM 
    student_assignments sa
JOIN 
    classroom_capacities cc ON sa.classroom_id = cc.classroom_id
WHERE 
    sa.assignment = TRUE
GROUP BY 
    sa.classroom_id, cc.capacity
HAVING 
    COUNT(sa.student_id) < cc.capacity;
```

These queries are designed to provide the necessary data for the optimization problem, including current student assignments, classroom capacities, and maximum student limits. They help in identifying over and under-capacity classrooms, unassigned students, and the current maximum number of students in any classroom, all of which are crucial for making informed decisions to balance the workload among teachers and ensure that no classroom exceeds its capacity.
