#MRPDet Mula et al (2006)
#ADAPTED TO GUSEK by Jorge Hernandez 2013

#Sets

set T;# Set of periods in the planning horizon(t=1...T)
set I;# Set of products (i=1...I)
set J;# Set the parent products in the bill of materials(j=1...J)..finished good
set R;# Set of resources (r=1...R)

#Params

param d {i in I, t in T}; #Market demand of the product i on period t
param a {i in I, j in J}; #Required quantity of i to produce an unit of the product j
param TS {i in I}; #Lead time of the product i
param INVT0 {i in I, t in T: t=0}; #Inventory of the product i on period 0
param Rd0 {i in I, t in T: t=0}; #Delayed demand of the product i on period 0
param RP {i in I, t in T}; #Programmed receptions of the product i on period t

#Technolpgic parameters

param AR {i in I, r in R}; # Required time of the resource r for unit of production of the product i
param CAP {r in R, t in T}; #Available capacity of the resource r in the period t

#objective function paramaters

param cp {i in I}; #Variable cost of production of a unit of the product i
param ci {i in I}; #Inventory cost of a unit of the product i
param crd {i in I}; #Delayed demand cost of a unit of the product i
param ctoc {r in R, t in T}; #Undertime hour cost of the resource r on period t
param ctex {r in R, t in T}; #Overtime hour cost of the resource r on period t

#Decision Variables
var P {i in I, t in T} >=1; #Quantity to produce of the product i on period t
var INVT {i in I, t in T} >=1; #Inventory of the product i at the end of period t
var Rd {i in I, t in T} >=1; #Delayed demand of the product i at the end of period t
var TOC {r in R, t in T} >=1; #Undertime hours of the resource r on period t
var TEX {r in R, t in T} >=1; #Overtime hours of the resource r on period t

#Objective funcion

minimize z: (sum{i in I, t in T} (cp[i]*P[i,t]+ci[i]*INVT[i,t]+crd[i]*Rd[i,t])) + (sum{r in R, t in T} (ctoc[r,t]*TOC[r,t]+ctex[r,t]*TEX[r,t]))

#Constraints
subject to C1 {i in I: i>=1, j in J: j>=1, t in T: t>=1}: (INVT[i,t-1]+P[i,t-TS[i]]+RP[i,t]-INVT[i,t]-Rd[i,t-1])-(sum{j in J} (a[i,j]*(P[j,t]+RP[j,t])))+(Rd[i,t])=d[i,t];
subject to C2 {i in I: i>=1, t in T: t>=1, r in R: r>=1}: (sum{i in I} (AR[i,r]*P[i,t]))+TOC[r,t]-TEX[r,t]=CAP[r,t]
subject to C3 {i in I: i>=1}: Rd[i,T]=0;

solve;

#Display
display P;
display INVT;
display Rd;
display TOC;
display TEX;

#data

data;

set T:= P1 P2 P3 P4 P5 P6 P7 P8 P9 P10; #ten periods
set I:= comp1; #one component
set J:= FG1; #one finished good
set R:= R1; #one resource available 


param d:P1 P2 P3 P4 P5 P6 P7 P8 P9 P10:= 
comp1 0 0 0 50 0 0 70 0 0 0; #demand

param a: FG1:=
comp1 2; #one FG1 requires two comp1

param TS: comp1: 2; #lead time is two periods

param INVT0: P1:=
comp1 80; #Initial inventory

param Rd0: P1:=
comp1 0; #Initial delay on-demand

param RP: P1 P2 P3 P4 P5 P6 P7 P8 P9 P10:= 
comp1 0 0 0 0 0 0 0 0 0 0; #no planned receptions at any period

param AR:R1:=
comp1 2; #time requiered to manufacture comp1


param CAP: P1 P2 P3 P4 P5 P6 P7 P8 P9 P10:= 
R1 50 50 50 50 50 50 50 50 50 50;

param cp: comp1:= 500; #comp1 cost

param ci: comp1:= 900; #comp1 inventory cost

param crd: comp1:= 999999; #comp1 delay cost

param ctocP1: P2 P3 P4 P5 P6 P7 P8 P9 P10:= 
R1 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000;

param ctex: P2 P3 P4 P5 P6 P7 P8 P9 P10:= 
R1 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000;

end;
