clc
clear
[m] = policyall();
maxstock = 2;
[nn,cc]=size(m);
for p=1:nn
    clear prob proba totala totala1 totala2 x y
    i=1;
maxa=m(p,1);
maxb=m(p,2);
maxc=m(p,3);
maxd=m(p,4);
maxe=m(p,5);
mina=maxa-3;
minb=maxb-3;
minc=maxc-3;
mind=maxd-3;
mine=maxe-3;
%ËÒ Transition Probability = P
for a= mina:maxa
    for b= minb:maxb
        for c= minc:maxc
            for d= mind:maxd
                for e= mine:maxe
                    aa=maxa-a;
                    switch aa
                        case 3
                            aa=0.3;
                        case 2
                            aa=0.3;
                        case 1
                            aa=0.3;
                        case 0
                            aa=0.1;
                    end
                    bb=maxb-b;
                    switch bb
                        case 3
                            bb=0.4;
                        case 2
                            bb=0.3;
                        case 1
                            bb=0.2;
                        case 0
                            bb=0.1;
                    end
                    cc=maxc-c;
                    switch cc
                        case 3
                            cc=0.2;
                        case 2
                            cc=0.3;
                        case 1
                            cc=0.3;
                        case 0
                            cc=0.2;
                    end
                    dd=maxd-d;
                    switch dd
                        case 3
                            dd=0.1;
                        case 2
                            dd=0.5;
                        case 1
                            dd=0.2;
                        case 0
                            dd=0.2;
                    end
                    ee=maxe-e;
                    switch ee
                        case 3
                            ee=0.2;
                        case 2
                            ee=0.2;
                        case 1
                            ee=0.4;
                        case 0
                            ee=0.2;
                    end
                    proba(i)=ee*dd*cc*bb*aa;      
                    i=i+1;
                end
            end
        end
    end
end
prob=proba';
%end calculation Transiion probabiliy

i = 0;
j = 0;

%¡ÅØèÁ For ´Ñ§¡ÅèÒÇ¹Õéà»ç¹ Transition µé¹¤Òº
for za= mina:maxa
for zb= minb:maxb
for zc= minc:maxc
for zd= mind:maxd
for ze= mine:maxe
    i=i+1;
    j=0;
    if za<= 0 
        ordera =0;
    else
        ordera = za;
    end
    if zb<= 0 
        orderb =0;
    else
        orderb = zb;
    end
    if zc<= 0 
        orderc =0;
    else
        orderc = zc;
    end
    if zd<= 0 
        orderd =0;
    else
        orderd = zd;
    end
    if ze<= 0 
        ordere =0;
    else
        ordere = ze;
    end
    
%¤èÒãªé¨èÒÂã¹¡ÒÃ«×éÍÊÔ¹¤éÒ A , B , C , D , E (Transition µé¹¤Òº(n))    
    ca=(maxa-ordera)*2;
    cb=(maxb-orderb)*3;
    cc=(maxc-orderc)*2;
    cd=(maxd-orderd)*4;
    ce=(maxe-ordere)*1;  
    
%¡ÅØèÁ For ´Ñ§¡ÅèÒÇ¹Õéà»ç¹ Transition »ÅÒÂ¤Òº    
for a= mina:maxa
    for b= minb:maxb
        for c= minc:maxc
            for d=mind:maxd
                for e=mine:maxe
                    j=j+1;
                    
%¤èÒãªé¨èÒÂ Holding Cost ËÃ×Í Lost Sale ¢Í§ÊÔ¹¤éÒ A , B , C , D , E (Transition »ÅÒÂ¤Òº(n+1)) 
    if a< 0 
        %Lost Sale
        invena =2.5*abs(a);
    elseif a>=0
        %Holding Cost
        invena =a*0.2;
    end
    if b< 0 
        invenb =3.75*abs(b);
    elseif b>=0
        invenb =b*0.3;
    end
    if c< 0 
        invenc =2.6*abs(c);
    elseif c>=0
        invenc =c*0.2;
    end
    if d< 0 
        invend =5*abs(d);
    elseif d>=0
        invend =d*0.4;
    end
    if e< 0 
        invene =1.25*abs(e);
    elseif e>=0
        invene =e*0.1;
    end
    
%¤èÒãªé¨èÒÂÃÇÁ·Ñé§«×éÍÊÔ¹¤éÒ áÅÐ Holding Cost ËÃ×Í Lost Sale ·Ø¡ª¹Ô´
    totala(i,j) = ca+invena+cb+invenb+cc+invenc+cd+invend+ce+invene;
                end
            end
        end
    end
end          
end
end
end
end
end
totala1=totala(:,1:512);
totala2=totala(:,513:1024);
%end Calculate cost

for i=2:512
    prob(:,i)=prob(:,1);
end
x=totala1.*prob;
y=totala2.*prob;
xx=sum(x);
yy=sum(y);
format bank
Mincost=sum(xx')+sum(yy');
%plot(proba);
fprintf('A=%f B=%f C=%f D=%f E=%f Cost=%f \n',maxa,maxb,maxc,maxd,maxe,Mincost); 

%à¡çº¼Å¡ÒÃ¤Ó¹Ç³µé¹·Ø¹ã¹ Policy áµèÅèÐáºº
policy(p,1)=maxa;
policy(p,2)=maxb;
policy(p,3)=maxc;
policy(p,4)=maxd;
policy(p,5)=maxe;
policy(p,6)=Mincost;
end
