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

## 1. Problem Context and Goals

### Context  
A bank is focused on optimizing its loan allocation strategy across its branches to maximize the total loan amount disbursed. The bank must ensure that its loan allocation adheres to several operational constraints to maintain financial stability and risk control. Specifically, the bank must respect the capacity of each branch in terms of the number of customers it can serve, the creditworthiness of each customer as reflected by their credit score, and the total loan exposure per customer.  

The bank has established specific operational parameters to guide this process:  
- **Maximum loan amount per customer**: No single customer can receive a loan exceeding $15,000.  
- **Maximum loan amount per credit score unit**: For every unit of a customer’s credit score, the loan amount cannot exceed $600. This ensures that customers with higher credit scores are eligible for larger loans.  
- **Maximum total loan amount per customer**: The cumulative loan amount for any single customer across all branches cannot exceed $60,000, preventing overexposure to any individual customer.  

The decision variable in this optimization is the loan amount to be disbursed, which is a continuous value. The bank aims to allocate these loan amounts in a way that maximizes the total disbursement while adhering to the above constraints.  

### Goals  
The primary goal of this optimization is to maximize the total loan amount disbursed across all branches. Success is measured by the sum of all loan amounts allocated, ensuring that the bank’s resources are utilized effectively while maintaining compliance with the established operational constraints.  

## 2. Constraints  

The optimization must respect the following constraints:  
1. **Branch customer capacity**: The total loan amount allocated to a branch cannot exceed the product of the number of customers in that branch and the maximum loan amount per customer. This ensures that each branch operates within its customer service capacity.  
2. **Customer credit score**: The loan amount allocated to a customer cannot exceed the product of their credit score and the maximum loan amount per credit score unit. This ensures that loan amounts are proportional to the customer’s creditworthiness.  
3. **Total loan exposure per customer**: The sum of all loan amounts allocated to a single customer across all branches cannot exceed the maximum total loan amount per customer. This prevents overexposure to any individual customer.  

These constraints are designed to ensure that the bank’s loan allocation strategy is both efficient and risk-controlled, aligning with its operational and financial goals.  

## 3. Available Data  

### Database Schema  
```sql
-- Iteration 1 Database Schema
-- Objective: Schema changes include adding missing parameters to business_configuration_logic.json and ensuring all tables meet the 3-row minimum rule. Configuration logic updates address scalar parameters and formulas identified as missing in the OR expert analysis.

CREATE TABLE loan (
  amount FLOAT
);

CREATE TABLE bank (
  no_of_customers INTEGER
);

CREATE TABLE customer (
  credit_score INTEGER
);
```

### Data Dictionary  
- **Loan Table**:  
  - **Purpose**: Stores details about the loans to be disbursed.  
  - **Optimization Role**: Contains the decision variable for the optimization model.  
  - **Columns**:  
    - **amount**: The loan amount to be disbursed. This is the primary decision variable in the optimization.  

- **Bank Table**:  
  - **Purpose**: Stores information about each branch’s customer capacity.  
  - **Optimization Role**: Provides data for the branch customer capacity constraint.  
  - **Columns**:  
    - **no_of_customers**: The number of customers in a branch, used to calculate the maximum loan allocation per branch.  

- **Customer Table**:  
  - **Purpose**: Stores customer-specific details, particularly their credit scores.  
  - **Optimization Role**: Provides data for the credit score-based loan allocation constraint.  
  - **Columns**:  
    - **credit_score**: The credit score of a customer, used to determine the maximum loan amount they are eligible for.  


=== Schema ===
-- Iteration 1 Database Schema
-- Objective: Schema changes include adding missing parameters to business_configuration_logic.json and ensuring all tables meet the 3-row minimum rule. Configuration logic updates address scalar parameters and formulas identified as missing in the OR expert analysis.

CREATE TABLE loan (
  amount FLOAT
);

CREATE TABLE bank (
  no_of_customers INTEGER
);

CREATE TABLE customer (
  credit_score INTEGER
);



=== LLM Response ===
```sql
-- Query Description: Retrieve the total number of customers per branch to calculate the maximum loan allocation per branch.
-- This is essential for the branch customer capacity constraint.
SELECT no_of_customers FROM bank;

-- Query Description: Retrieve the credit scores of all customers to determine the maximum loan amount each customer is eligible for.
-- This is crucial for the credit score-based loan allocation constraint.
SELECT credit_score FROM customer;

-- Query Description: Retrieve the current loan amounts to understand the existing loan exposure per customer.
-- This is important for the total loan exposure per customer constraint.
SELECT amount FROM loan;

-- Query Description: Calculate the maximum loan amount per customer based on their credit score.
-- This is derived from the credit score and the maximum loan amount per credit score unit ($600).
-- This helps in enforcing the credit score-based loan allocation constraint.
SELECT credit_score, credit_score * 600 AS max_loan_per_credit_score
FROM customer;

-- Query Description: Calculate the total loan exposure per customer across all branches.
-- This is essential for ensuring that the cumulative loan amount for any single customer does not exceed $60,000.
SELECT SUM(amount) AS total_loan_exposure
FROM loan
GROUP BY customer_id;  -- Assuming there is a customer_id column in the loan table

-- Query Description: Retrieve the maximum loan amount per customer ($15,000) and the maximum total loan exposure per customer ($60,000).
-- These are fixed constraints and need to be considered in the optimization.
SELECT 15000 AS max_loan_per_customer, 60000 AS max_total_loan_exposure;

-- Query Description: Calculate the maximum loan allocation per branch based on the number of customers and the maximum loan amount per customer.
-- This is essential for the branch customer capacity constraint.
SELECT no_of_customers, no_of_customers * 15000 AS max_loan_allocation_per_branch
FROM bank;

-- Query Description: Retrieve the current loan amounts along with the credit scores of the customers to assess compliance with the credit score-based loan allocation constraint.
-- This helps in ensuring that the loan amounts are proportional to the customers' creditworthiness.
SELECT l.amount, c.credit_score
FROM loan l
JOIN customer c ON l.customer_id = c.customer_id;  -- Assuming there is a customer_id column in both tables

-- Query Description: Retrieve the total loan amount disbursed across all branches.
-- This is the primary objective to maximize in the optimization problem.
SELECT SUM(amount) AS total_loan_disbursed
FROM loan;

-- Query Description: Retrieve the number of customers and their credit scores per branch to assess the overall creditworthiness and capacity of each branch.
-- This helps in making informed decisions on loan allocation.
SELECT b.no_of_customers, c.credit_score
FROM bank b
JOIN customer c ON b.branch_id = c.branch_id;  -- Assuming there is a branch_id column in both tables
```

These queries are designed to retrieve the necessary data for the optimization problem, including decision variables, objective function coefficients, and constraint parameters. They also consider aggregated data and relationships between tables to provide a comprehensive view of the data needed for the optimization.
