Skip to main content
. 2025 Aug 27;14(17):3004. doi: 10.3390/foods14173004
Algorithm 3 Spoilage Prediction Using GNN and Actor–Critic Learning with Context-Aware Weights and Coordination
  •   1:

    Input: Cold-chain graph G=(V,E), node features Xv=[T,H,delay,fruittype], disruption scenarios D, spoilage labels y

  •   2:

    Initialize: Actor μθ, critic Qϕ, GNN encoder fGNN, replay buffer R

  •   3:

    Initialize: Shared intention buffer B; static reward coefficients α1,α2

  •   4:

    for each training episode do

  •   5:

        Simulate disrupted scenario from D

  •   6:

        Build graph G with node features X

  •   7:

        Compute graph embedding z=fGNN(G,X)

  •   8:

        Set initial state s0=z

  •   9:

        for each timestep t do

  • 10:

              Select action at=μθ(st)

▹ e.g., spoilage threshold or inspection trigger
  • 11:

              Append (st,at) to B

▹ Declare spoilage alert intention
  • 12:

              if conflicting inspections or redundant predictions detected in B then

  • 13:

                 Apply penalty ρ or adjust decision based on criticality

  • 14:

              end if

  • 15:

              Predict spoilage: y^t=sigmoid(Wat+b)

  • 16:

              Observe ground truth yt and compute classification loss Lpred

  • 17:

              Compute false negative (FN) rate as critical misprediction signal

  • 18:

              Extract context vector: ctx=[Lpred,FN,inspectioncost]

  • 19:
              Compute dynamic weights:
    ωj=αj·ctxjkαk·ctxk, j=1,2,3
  • 20:
              Compute reward:
    rt=(ω1·Lpred+ω2·FN+ω3·cost)ρ
  • 21:

              Observe next state st+1 from updated GNN encoding

  • 22:

              Store (st,at,rt,st+1) in R

  • 23:

              Sample mini-batch from R

  • 24:
              Update critic:
    Lcritic=r+γQϕ(s,μθ(s))Qϕ(s,a)2
  • 25:
              Update actor:
    θJaQϕ(s,a)·θμθ(s)
  • 26:

              Optionally fine-tune fGNN with prediction loss Lpred

  • 27:

              Clear B

  • 28:

           end for

  • 29:

    end for

  • 30:

    Output: Trained spoilage policy μ*(s) and GNN encoder fGNN*