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

## 1. Problem Context and Goals

### Context  
A media company is focused on maximizing its total viewership share across multiple channels by strategically scheduling programs. The company must decide which programs to broadcast on which channels and at what times of day. This decision is guided by two key operational metrics: the share of viewership each program attracts on a specific channel and the overall rating of the channel itself. The goal is to ensure that the scheduling decisions are made in a way that maximizes the combined impact of these metrics.

The company operates under specific business rules:  
1. Each program can be broadcast at most once across all channels and time slots.  
2. Each channel has a maximum number of time slots available for broadcasting, currently set to 5.  
3. Each channel must broadcast a minimum number of local programs, currently set to 2, to comply with regulatory requirements and cater to local audiences.  

These rules ensure that the scheduling process remains balanced and adheres to both operational and regulatory constraints. The decision variables are binary, indicating whether a specific program is scheduled on a particular channel at a given time slot. The objective is to maximize the total viewership share, calculated as the sum of the products of program shares and channel ratings for all scheduled programs.

### Goals  
The primary goal of this optimization problem is to maximize the total viewership share across all channels. This is achieved by selecting the best combination of programs, channels, and time slots, weighted by the program's share of viewership on the channel and the channel's overall rating. Success is measured by the total viewership share generated from the scheduled programs, ensuring that the company's content reaches the largest possible audience while adhering to operational and regulatory constraints.

## 2. Constraints  

The optimization problem is subject to the following constraints:  
1. **Program Uniqueness**: Each program can be broadcast at most once across all channels and time slots. This ensures that no program is over-scheduled, maintaining variety in the programming lineup.  
2. **Channel Time Slot Limit**: Each channel can only use a limited number of time slots for broadcasting, currently capped at 5. This ensures that no channel is overburdened with too many programs, maintaining a balanced schedule.  
3. **Local Program Requirement**: Each channel must broadcast a minimum number of local programs, currently set to 2. This ensures compliance with regulatory requirements and caters to the preferences of local audiences.  

These constraints are designed to ensure that the scheduling decisions are both operationally feasible and aligned with the company's strategic and regulatory goals.

## 3. Available Data  

### Database Schema  
```sql
-- Iteration 3 Database Schema
-- Objective: Schema changes include adding tables for Max_Time_Slots and Min_Local_Programs to address missing optimization requirements. Configuration logic updates include scalar parameters for Max_Time_Slots and Min_Local_Programs.

CREATE TABLE time_slots (
  time_slot_id INTEGER,
  time_of_day STRING
);

CREATE TABLE program_origins (
  program_id INTEGER,
  origin STRING
);

CREATE TABLE broadcast_decisions (
  channel_id INTEGER,
  program_id INTEGER,
  time_slot_id INTEGER,
  x BOOLEAN
);

CREATE TABLE program_shares (
  channel_id INTEGER,
  program_id INTEGER,
  share_in_percent INTEGER
);

CREATE TABLE channel_ratings (
  channel_id INTEGER,
  rating_in_percent INTEGER
);
```

### Data Dictionary  
- **time_slots**: Contains available time slots for broadcasting programs.  
  - `time_slot_id`: Unique identifier for a time slot.  
  - `time_of_day`: Time of day for broadcasting (e.g., Morning, Afternoon, Evening).  

- **program_origins**: Indicates the origin of programs (e.g., local, international).  
  - `program_id`: Unique identifier for a program.  
  - `origin`: Origin of the program (e.g., Local, International).  

- **broadcast_decisions**: Represents binary decisions indicating whether a program is broadcast on a channel at a specific time.  
  - `channel_id`: Unique identifier for a channel.  
  - `program_id`: Unique identifier for a program.  
  - `time_slot_id`: Unique identifier for a time slot.  
  - `x`: Binary decision variable (0 or 1).  

- **program_shares**: Contains the share of viewership for a program on a channel.  
  - `channel_id`: Unique identifier for a channel.  
  - `program_id`: Unique identifier for a program.  
  - `share_in_percent`: Share of viewership for the program on the channel.  

- **channel_ratings**: Contains the rating of a channel.  
  - `channel_id`: Unique identifier for a channel.  
  - `rating_in_percent`: Rating of the channel.  


=== Schema ===
-- Iteration 3 Database Schema
-- Objective: Schema changes include adding tables for Max_Time_Slots and Min_Local_Programs to address missing optimization requirements. Configuration logic updates include scalar parameters for Max_Time_Slots and Min_Local_Programs.

CREATE TABLE time_slots (
  time_slot_id INTEGER,
  time_of_day STRING
);

CREATE TABLE program_origins (
  program_id INTEGER,
  origin STRING
);

CREATE TABLE broadcast_decisions (
  channel_id INTEGER,
  program_id INTEGER,
  time_slot_id INTEGER,
  x BOOLEAN
);

CREATE TABLE program_shares (
  channel_id INTEGER,
  program_id INTEGER,
  share_in_percent INTEGER
);

CREATE TABLE channel_ratings (
  channel_id INTEGER,
  rating_in_percent INTEGER
);



=== LLM Response ===
```sql
-- Query Description: Retrieve the program shares and channel ratings to calculate the objective function coefficients.
-- This query joins the program_shares and channel_ratings tables to get the combined impact of program shares and channel ratings.
-- The result will be used to calculate the total viewership share for each program-channel combination.
SELECT 
    ps.channel_id,
    ps.program_id,
    ps.share_in_percent,
    cr.rating_in_percent,
    (ps.share_in_percent * cr.rating_in_percent) AS viewership_impact
FROM 
    program_shares ps
JOIN 
    channel_ratings cr ON ps.channel_id = cr.channel_id;

-- Query Description: Retrieve the list of programs and their origins to ensure compliance with the local program requirement.
-- This query fetches the origin of each program from the program_origins table.
-- The result will be used to ensure that each channel broadcasts the minimum number of local programs.
SELECT 
    program_id,
    origin
FROM 
    program_origins;

-- Query Description: Retrieve the current broadcast decisions to enforce the program uniqueness constraint.
-- This query fetches the current broadcast decisions from the broadcast_decisions table.
-- The result will be used to ensure that each program is broadcast at most once across all channels and time slots.
SELECT 
    channel_id,
    program_id,
    time_slot_id,
    x
FROM 
    broadcast_decisions;

-- Query Description: Retrieve the available time slots for broadcasting.
-- This query fetches all available time slots from the time_slots table.
-- The result will be used to schedule programs within the available time slots.
SELECT 
    time_slot_id,
    time_of_day
FROM 
    time_slots;

-- Query Description: Retrieve the count of programs scheduled per channel to enforce the channel time slot limit.
-- This query aggregates the broadcast decisions to count the number of programs scheduled per channel.
-- The result will be used to ensure that no channel exceeds the maximum number of time slots (currently capped at 5).
SELECT 
    channel_id,
    COUNT(program_id) AS scheduled_programs
FROM 
    broadcast_decisions
WHERE 
    x = 1
GROUP BY 
    channel_id;

-- Query Description: Retrieve the count of local programs scheduled per channel to enforce the local program requirement.
-- This query joins the broadcast_decisions and program_origins tables to count the number of local programs scheduled per channel.
-- The result will be used to ensure that each channel broadcasts the minimum number of local programs (currently set to 2).
SELECT 
    bd.channel_id,
    COUNT(bd.program_id) AS local_programs_scheduled
FROM 
    broadcast_decisions bd
JOIN 
    program_origins po ON bd.program_id = po.program_id
WHERE 
    bd.x = 1 AND po.origin = 'Local'
GROUP BY 
    bd.channel_id;

-- Query Description: Retrieve the total viewership share for each channel to evaluate the current schedule.
-- This query aggregates the viewership impact for each channel based on the current broadcast decisions.
-- The result will be used to evaluate the current schedule and identify areas for improvement.
SELECT 
    bd.channel_id,
    SUM(ps.share_in_percent * cr.rating_in_percent) AS total_viewership_share
FROM 
    broadcast_decisions bd
JOIN 
    program_shares ps ON bd.channel_id = ps.channel_id AND bd.program_id = ps.program_id
JOIN 
    channel_ratings cr ON bd.channel_id = cr.channel_id
WHERE 
    bd.x = 1
GROUP BY 
    bd.channel_id;

-- Query Description: Retrieve the list of programs that are not yet scheduled to identify potential candidates for scheduling.
-- This query identifies programs that have not been scheduled on any channel or time slot.
-- The result will be used to consider additional programs for scheduling to maximize viewership share.
SELECT 
    p.program_id
FROM 
    program_origins p
LEFT JOIN 
    broadcast_decisions bd ON p.program_id = bd.program_id
WHERE 
    bd.x IS NULL OR bd.x = 0;
```

These queries are designed to retrieve the necessary data for the optimization problem, including objective function coefficients, constraint parameters, and relevant aggregated data. Each query serves a specific purpose in ensuring that the scheduling decisions are made in a way that maximizes total viewership share while adhering to the given constraints.
