Feed Right Docs

Architecture

End-to-end system architecture of FeedRight — from sensor inputs to feed actuation and adaptive retraining.

Component Overview

FeedRight is composed of five major subsystems that form a closed-loop control pipeline:

SubsystemModuleResponsibility
Video Analysisbackend/experimental_routes.pyYOLO-based fish detection, biomass estimation, and feeding-behaviour feature extraction
Environmentlib/dqn_sb3/env.pyWraps the 44-feature state space and reward logic as a Gymnasium environment
DQN Agentlib/dqn_sb3/agent.pyLoads a trained Stable-Baselines 3 model, selects actions, applies safety constraints
Trainerlib/dqn_sb3/train.pyTrains and evaluates the DQN model with checkpointing and TensorBoard logging
Adaptive Looplib/dqn_sb3/adaptive_retrain.py, experience_db.py, monitoring.pyStores real outcomes, retrains on field data, tracks performance drift

Data Flow Diagram

                   ┌─────────────┐
                   │  Underwater  │
                   │   Camera     │
                   └──────┬──────┘
                          │ video frames
              ┌───────────┴───────────┐
              ▼                       ▼
  ┌───────────────────┐   ┌───────────────────────┐
  │  YOLO Fish Model  │   │  YOLO Pellet Model    │
  │  (BiomassAnalyzer)│   │  (BehaviorAnalyzer)   │
  │  bbox → L, G, W   │   │  uneaten pellet count │
  │  per-fish weight   │   │  pellet sinking time  │
  └────────┬──────────┘   └──────────┬────────────┘
           │                         │
           │   ┌─────────────────┐   │
           │   │  Optical Flow   │   │
           │   │  (Farnebäck)    │   │
           │   │  motion, frenzy │   │
           │   │  surface ratio  │   │
           │   └───────┬─────────┘   │
           │           │             │
           ▼           ▼             ▼
┌──────────┐   ┌──────────────────────────┐   ┌──────────────┐
│ IoT      │──►│   State Constructor      │◄──│ Farm DB      │
│ Sensors  │   │   (44 normalised dims)   │   │ (feeding     │
│ (DO,temp │   │                          │   │  history,    │
│  wind…)  │   └────────────┬─────────────┘   │  cage meta)  │
└──────────┘                │                  └──────────────┘

                 ┌─────────────────────┐
                 │  CageFeedingAgent   │
                 │  ├─ DQN.predict()   │
                 │  └─ safety_override │
                 └─────────┬───────────┘
                           │ action (0–5)

                 ┌─────────────────────┐
                 │  Automatic Feeder   │
                 │  (0 / 0.5 / 1 / 2  │
                 │   / 3.5 / 5.0 kg)   │
                 └─────────┬───────────┘
                           │ outcome measured

                 ┌─────────────────────┐
                 │  ExperienceDatabase │
                 │  (SQLite)           │
                 │  ├─ experiences      │
                 │  ├─ model_perf      │
                 │  └─ retrain_hist    │
                 └─────────┬───────────┘
                           │ periodic

                 ┌─────────────────────────┐
                 │ AdaptiveRetrainingService│
                 │  prefill replay buffer  │
                 │  continue training      │
                 │  evaluate & deploy       │
                 └─────────────────────────┘

Video Analysis Pipeline

The video and biomass features for the 44-dimension state vector are derived from YOLO detection models and dense optical flow. The pipeline produces 11 features: 6 Biomass features (indices 13–18) from YOLO fish detection with allometric weight estimation, and 5 Video features (indices 19–23) from spatial motion analysis, temporal dynamics, and optional pellet detection.

For the full methodology, signal sources, and feature computation details, see the dedicated Video Analysis page.

Gymnasium Environment (FishFeedingEnv)

The environment conforms to the Gymnasium gym.Env interface:

  • Observation space: Box(low=0, high=1, shape=(44,), dtype=float32) — all features normalised to [0, 1].
  • Action space: Discrete(6) — mapped to concrete feed amounts via action_to_kg.
  • Transition model (_transition_state): Updates feeding history, hunger, motion intensity, and adds stochastic environmental noise.
  • Reward function (_calculate_reward): Multi-factor shaped reward detailed in the Reward System page.
  • Episode termination: After max_feeds_per_episode (default 6) feed actions have been executed.

CageFeedingAgent

CageFeedingAgent (lib/dqn_sb3/agent.py) wraps the trained DQN model for production use:

  1. Accepts either a dictionary of named features or a raw 44-dimensional array.
  2. Normalises and passes the state to DQN.predict(obs, deterministic=True).
  3. Maps the discrete action to kg via env.action_to_kg.
  4. Applies the Safety Constraint Layer.
  5. Returns a decision object with feed_amount, is_safe, confidence, raw_prediction, and action index.
  6. Optionally records the outcome to ExperienceDatabase for adaptive learning.

Each cage runs its own agent instance, identified by cage_id, enabling per-cage model specialisation in the future.

On this page