Skip to content

Payal-mak/Food-Demand-and-Waste-Forecasting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advanced Food Supply Chain Forecasting System

Complete Implementation Guide

Project Overview

This project implements a Unified Multivariate and Dual-Objective Forecasting Model for Sustainable Food Supply Chains using External Signals. The system combines real-time data integration, advanced machine learning models, and sustainability metrics to provide accurate predictions for both food demand and waste.

Key Features

  • Dual-Objective Prediction: Simultaneously forecasts food demand (orders) and food waste (kg)
  • Real-time API Integration: Weather, fuel prices, economic indicators, and Google Trends
  • Advanced ML Models: Random Forest, Gradient Boosting, and Ensemble methods
  • Sustainability Metrics: CO2 impact calculations and waste reduction potential
  • Production-Ready Architecture: Database integration, model persistence, and API endpoints
  • Comprehensive Evaluation: Multiple metrics including R², MAE, RMSE, and MAPE

System Architecture

1. Data Collection Layer

- Real-time Weather Data (WeatherAPI)
- Fuel Price Data (Multiple sources)
- Economic Indicators (World Bank/Financial APIs)
- Google Trends (Consumer behavior)
- Holiday Information (Country-specific)

2. Data Processing Layer

- Feature Engineering (Temporal, lag, interaction features)
- Data Normalization (MinMaxScaler, StandardScaler)
- Sequence Creation (10-week lookback windows)
- Data Validation and Cleaning

3. Model Layer

- Random Forest Regressor
- Gradient Boosting Regressor
- LSTM-style Ensemble
- Multi-output Regression

4. Prediction Layer

- Real-time Prediction API
- Batch Prediction System
- Alert Generation
- Confidence Scoring

Installation and Setup

Prerequisites

pip install pandas>=1.5.0
pip install numpy>=1.20.0
pip install scikit-learn>=1.1.0
pip install requests>=2.28.0
pip install holidays>=0.16
pip install sqlite3  # Usually included with Python

Optional Dependencies (for enhanced features)

pip install tensorflow>=2.10.0  # For LSTM/Transformer models
pip install pytrends>=4.9.0     # For Google Trends
pip install optuna>=3.0.0       # For hyperparameter optimization
pip install flask>=2.0.0        # For API deployment

Database Setup

The system automatically creates a SQLite database with the following tables:

  • food_data: Historical demand and waste data
  • enhanced_food_data: Data with external features
  • predictions: Model predictions and actuals

Usage Guide

1. Initialize the System

from advanced_forecaster import AdvancedFoodSupplyChainForecaster

# Initialize forecaster
forecaster = AdvancedFoodSupplyChainForecaster({
    'weather_api_key': 'YOUR_API_KEY',
    'location': 'Ahmedabad,India',
    'lookback': 10,
    'prediction_horizon': 1
})

2. Prepare Dataset

# Prepare comprehensive dataset with external features
enhanced_data = forecaster.prepare_comprehensive_dataset(
    start_date='2022-01-01',
    end_date='2024-01-01'
)

# Create sequences for training
X, y = forecaster.create_sequences(enhanced_data, lookback=10)

# Split data
(X_train, y_train), (X_val, y_val), (X_test, y_test) = forecaster.split_data(X, y)

3. Train Models

from model_trainer import ModelTrainer

trainer = ModelTrainer(forecaster)

# Train multiple models
rf_result = trainer.train_random_forest(X_train, y_train, X_val, y_val)
gb_result = trainer.train_gradient_boosting(X_train, y_train, X_val, y_val)
lstm_result = trainer.train_lstm_model(X_train, y_train, X_val, y_val)

# Evaluate models
test_results = trainer.evaluate_all_models(X_test, y_test)

4. Real-time Predictions

from prediction_system import RealTimePredictionSystem

# Initialize prediction system
prediction_system = RealTimePredictionSystem(forecaster, trainer, best_model)

# Make real-time prediction
prediction = prediction_system.create_prediction_api_response()

# Batch predictions
batch_predictions = prediction_system.batch_predict(
    start_date=datetime.now(),
    end_date=datetime.now() + timedelta(weeks=4)
)

API Configuration

Weather API Setup

  1. Sign up at WeatherAPI
  2. Get your free API key
  3. Set the key in configuration:
config = {
    'weather_api_key': 'YOUR_WEATHER_API_KEY',
    'location': 'Your_City,Country'
}

Economic Data APIs

For production use, integrate with:

  • World Bank API: Economic indicators
  • Financial Modeling Prep: Real-time economic data
  • Trading Economics: Comprehensive economic data

Fuel Price APIs

Available options:

  • Fuel Price APIs India: Real-time fuel prices
  • HERE Technologies: Global fuel price data
  • Local government APIs: Region-specific data

Model Performance

Based on our evaluation with 105 weeks of data:

Model Demand MAE Demand R² Waste MAE Waste R² Overall Score
Random Forest 20.9 -1.71 5.10 0.415 -0.650
LSTM Ensemble 20.9 -1.79 5.01 0.412 -0.689
Gradient Boosting 26.3 -3.22 6.75 0.116 -1.553

Note: Negative R² values indicate the model performs worse than a naive mean predictor, suggesting need for more data or feature engineering.

Sustainability Metrics

The system calculates:

  • CO2 Impact: 2.5 kg CO2 per kg food waste
  • Waste Reduction Potential: Difference between predicted and optimal waste
  • Cost Savings: Based on operational cost models
  • Environmental Benefits: Quantified sustainability improvements

Deployment Options

1. Local Development

python advanced_forecaster.py

2. Flask API Deployment

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    date = request.json.get('date')
    prediction = prediction_system.create_prediction_api_response(date)
    return jsonify(prediction)

if __name__ == '__main__':
    app.run(debug=True)

3. Cloud Deployment

Deploy to AWS, Google Cloud, or Azure using containerization:

FROM python:3.9-slim

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Data Requirements

Minimum Dataset Size

  • Training: At least 52 weeks (1 year) of historical data
  • Validation: 20-30% of training data
  • Features: 10-15 external features recommended

Data Quality Requirements

  • Completeness: <5% missing values
  • Consistency: Regular weekly intervals
  • Accuracy: Validated business data
  • Freshness: Real-time external signals

Performance Optimization

1. Feature Engineering

  • Add more temporal features (seasonality, trends)
  • Include interaction terms
  • Use domain-specific features (menu changes, promotions)

2. Model Improvements

  • Hyperparameter tuning with Optuna
  • Ensemble methods combining multiple algorithms
  • Deep learning models with TensorFlow

3. Data Enhancement

  • Increase historical data size
  • Add more external signals
  • Improve data quality and preprocessing

Monitoring and Maintenance

1. Model Performance Monitoring

# Track prediction accuracy over time
def monitor_model_performance():
    recent_predictions = get_recent_predictions()
    actual_values = get_actual_values()

    current_mae = calculate_mae(recent_predictions, actual_values)

    if current_mae > threshold:
        trigger_model_retraining()

2. Data Quality Monitoring

  • Monitor API response times and availability
  • Validate data ranges and distributions
  • Alert on anomalous values

3. Automated Retraining

  • Schedule monthly model retraining
  • A/B test new models before deployment
  • Maintain model versioning

Research and Publication

Key Contributions

  1. Novel Architecture: Dual-objective forecasting with real-time signals
  2. Comprehensive Integration: Multiple external data sources
  3. Sustainability Focus: Environmental impact quantification
  4. Production-Ready: Complete end-to-end system

Potential Publications

  • Journals: Nature Food, Journal of Cleaner Production, Food Policy
  • Conferences: ICML, NeurIPS, IEEE Big Data
  • Industry: Supply Chain Management Review

Next Steps

  1. Collect larger dataset (2-3 years)
  2. Implement Transformer architecture
  3. Add graph neural networks for supply chain modeling
  4. Conduct real-world pilot study
  5. Publish research findings

Troubleshooting

Common Issues

1. API Rate Limits

# Implement exponential backoff
import time

def api_call_with_retry(api_func, max_retries=3):
    for attempt in range(max_retries):
        try:
            return api_func()
        except RateLimitError:
            time.sleep(2 ** attempt)
    raise Exception("Max retries exceeded")

2. Missing Dependencies

# Install all requirements
pip install -r requirements.txt

# For specific issues
pip install --upgrade scikit-learn
pip install --upgrade pandas

3. Memory Issues

# Reduce batch size or sequence length
config['batch_size'] = 16
config['lookback'] = 5

Contact

For technical support or research collaboration:

About

The Advanced Food Supply Chain Forecasting System is a machine learning-powered application designed to build unified, dual-objective forecasting model for sustainable food supply chains. This simultaneously predicts food demand (orders) and food waste by leveraging real-time external signals such as weather data, fuel prices, economic indicators.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors