%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  NAME :
%      bookproduction.pl
%
% VERSION :
%     0
%
% AUTHOR : Matthieu Petit and Arnaud Gotlieb
%
% FUNCTION :
%      Model of the book production problem
%      
% REMARKS : any problem, contact petit@(without advertissement)irisa.fr
%
% NOTE TO THE USER :  need ctr_proba_v0.pl to work
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-----------
% Description
%-----------
% Our books production planning constraint model is adapted from the classical $M$ quarter 
% production planning problem of [Walsh02]. In our model, the demand of books is modelized by the
% PCO choose(Demand,[100,101,102,103,104,105]-[W_{100},W_ {101},W_{102},W_ {103},W_{104},W_{105}],[],Opt)
% where the weights W_i denote the unknown probability distribution. So, we solve the problem with an
% unknown probability distribution but we don't solve the original stochastic constraint system
% aiming at satisfying the clientele eighty percent of the time. 
%-----------


%%%%%%%%%%%%%%%%%%%%%%%
% Book Production
%%%%%%%%%%%%%%%%%%%%%%%

book_production(C,N,Distribution,M,Opt) :-     	  
        init_env(Env),
        domain(Distribution,1,10),
        exec(C),
        Prod in 1..N,
        construct_domain(N,Domain),
        choose(Demand,Domain-Distribution,[],Opt,Env),
        Prod#>=Demand,
        Surplus #= Prod-Demand,
        Cost #= Surplus+Cost_Rest,        
        M1 is M-1,
        book_production_rec(N,Domain-Distribution,M1,Production_Rest,Surplus,Cost_Rest,Opt,Env),
        lists:append([Prod|Production_Rest],Distribution,Labeling_List),
        labeling([minimize(Cost)],Labeling_List).
        
book_production_rec(_N,_Domain-_Distribution,0,[],_Surplus,0,_Opt,_Env).

book_production_rec(N,Domain-Distribution,M,[Prod|Production_Rest],Surplus,Cost,Opt,Env) :-
        Prod in 1..N,
	choose(Demand,Domain-Distribution,[],Opt,Env),
        Prod#>=Demand-Surplus,
        Surplus2 #= Prod-Demand+Surplus,
        Cost #= Prod-Demand+Surplus+Cost_Rest,
        M1 is M-1,
        book_production_rec(N,Domain-Distribution,M1,Production_Rest,Surplus2,Cost_Rest,Opt,Env).


construct_domain(0,[]) :-
        !.

construct_domain(N,Res) :-
        !,
        N1 is N-1,
        construct_domain(N1,List),
        lists:append(List,[N],Res).

ctr_req([domain([W1,W2,W3,W4,W5,W6],1,10),2*W1#=W6,2*W2#=W5,2*W3#=W4),[W1,W2,W3,W4,W5,W6]).

       

%----------
% Requests
%----------
% Demand Size: 6
% Quarter: 2
% Filtering algortihm: on
%  | ?- ctr_req(Ctrs,Distribution),book_production(Ctrs,6,Distribution,2,[domain_bound]).
%----------
