:- lib(ic).
:- lib(branch_and_bound).
:- local struct(signal(o, t, d, w)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
signal_packing(SignalList, FrameList) :-
csp_per_ecu(SignalList, FrameList, EmptyFrameList, T_List, D_List, W_List, FP_List, SP_List, K_List, Prod),
Obj #= sum(K_List),
bb_min(optimal_search(FrameList, K_List),
Obj,
bb_options{strategy : restart}),
writeln(FrameList),
writeln(EmptyFrameList),
write(" T: "), writeln(T_List),
write(" D: "), writeln(D_List),
write(" W: "), writeln(W_List),
write(" FP: "), writeln(FP_List),
write(" SP: "), writeln(SP_List),
write(" K: "), writeln(K_List),
write("Prod: "), writeln(Prod),
writeln("---------------------------------------------------").
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
optimal_search(FrameList, K_List) :-
flatten_array(FrameList, FrameListVars),
search(FrameListVars, 0, first_fail, indomain, complete, []),
search(K_List, 0, first_fail, indomain, complete, []).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
csp_per_ecu(SignalList, FrameList, EmptyFrameList, T_List, D_List, W_List, FP_List, SP_List, K_List, Prod) :-
INF is 99999,
TAU is 1 * 60 * 60 * 1000, % ms
RHO is 1 - 10 ^ (-5),
BER is 10 ^ (-3),
FC is 5, % ms
SLOT is 2048, % bits
length(SignalList, NrOfSignals),
dim(FrameList, [NrOfSignals, NrOfSignals]),
FrameList #:: [0 .. 1],
% ROWS => FRAMES ARE EMPTY or NOT
(count(I, 1, NrOfSignals), foreach(EmptyFrame, EmptyFrameList), param(NrOfSignals, FrameList) do
Row is subscript(FrameList, [I, 1 .. NrOfSignals]),
EmptyFrame #= (sum(Row) > 0)
),
% COLUMNS => ONE SIGNAL in ONE FRAME
(count(J, 1, NrOfSignals), param(NrOfSignals, FrameList) do
Col is subscript(FrameList, [1 .. NrOfSignals, J]),
sum(Col) #= 1
),
% (T, D, W) of a frame
(count(I, 1, NrOfSignals),
foreach(EmptyFrame, EmptyFrameList),
foreach(T, T_List),
foreach(D, D_List),
foreach(W, W_List),
foreach(FP, FP_List),
foreach(SP, SP_List),
foreach(K, K_List), param(INF, TAU, RHO, BER, SLOT, NrOfSignals, SignalList, FrameList) do
% (T, _, W) of a frame
(count(J, 1, NrOfSignals),
foreach(Signal, SignalList),
foreach(T_El, T_Row),
foreach(W_El, W_Row), param(INF, I, FrameList) do
Bool #= subscript(FrameList, [I, J]),
Signal = signal(_, T_Signal, _, W_Signal),
T_El #= (1 - Bool) * INF + T_Signal,
W_El #= Bool * W_Signal
),
T #= min(T_Row),
W #= sum(W_Row),
W #=< SLOT,
K #>= 0,
K #=< EmptyFrame * INF,
FP $= (1 - (1 - BER) ^ W) * EmptyFrame,
SP $= (1 - FP ^ (1 + K)) ^ (EmptyFrame * (TAU / T)),
SP $> RHO,
SP $=< 1,
% (_, D, _) of a frame
(count(J, 1, NrOfSignals),
foreach(Signal, SignalList),
foreach(D_El, D_Row), param(INF, I, FrameList, T) do
Bool #= subscript(FrameList, [I, J]),
Signal = signal(_, T_Signal, D_Signal, _),
Tmp_D #= Bool * D_Signal,
Tmp_T #= Bool * T_Signal,
compute_deadline(INF, Bool, Tmp_D, Tmp_T, T, D_El)
%suspend(D_El #= (1 - Bool) * INF + Tmp_D - T + gcd(T, Tmp_T), 0, [T, Tmp_T]->inst)
),
D #= min(D_Row),
D #> 0
),
Prod $= ic_prod(SP_List),
Prod $>= RHO,
Prod $=< 1.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
compute_deadline(INF, Bool, Tmp_D, Tmp_T, T, D_El) :-
var(Tmp_T) -> suspend(compute_deadline(INF, Bool, Tmp_D, Tmp_T, T, D_El), 0, Tmp_T->inst)
; var(T) -> suspend(compute_deadline(INF, Bool, Tmp_D, Tmp_T, T, D_El), 0, T->inst)
; D_El #= (1 - Bool) * INF + Tmp_D - T + gcd(T, Tmp_T).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- mode prod(+, -).
ic_prod(List, Rez) :-
ic_prod(List, 1, Rez).
ic_prod([], Rez, Rez).
ic_prod([H | T], Prod, Rez) :-
Prod1 $= H * Prod,
ic_prod(T, Prod1, Rez).