:- 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),
optimal_search(FrameList, K_List),
%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, FrameVars),
search(FrameVars, 0, input_order, 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(I, FrameList) do
Bool #= subscript(FrameList, [I, J]),
Signal = signal(_, T_Signal, _, W_Signal),
T_El $= T_Signal / Bool,
W_El #= Bool * W_Signal
),
T $= ic_min(T_Row),
W #= sum(W_Row),
W #=< SLOT,
K #>= 0,
K #=< INF * EmptyFrame,
FP $= (1 - (1 - BER) ^ W) * EmptyFrame, %fail_prob(EmptyFrame, BER, W),
SP $= (1 - FP ^ (1 + K)) ^ (EmptyFrame * (TAU / T)), %succ_prob(INF, FP, K, TAU, T),
SP $> RHO,
SP $=< 1
% (, D, _) of a frame
%(foreach(Signal, SignalList),
% foreach(D_El, D_Row), param(EmptyFrame, T) do
%
% Signal = signal(_, T_Signal, D_Signal, _),
% D_El #= EmptyFrame * (D_Signal - T + ic_gcd(T, T_Signal))
% ),
%D #= ic_min(D_Row),
%D #> 0
),
Prod $= ic_prod(SP_List),
Prod $>= RHO,
Prod $=< 1.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- mode ic_min(+, -).
ic_min(List, Min) :-
ic_min(List, +1.0Inf, Min).
ic_min([], Min, Min).
ic_min([H | T], Var, Min) :-
H $< Var,
ic_min(T, H, Min).
ic_min([H | T], Var, Min) :-
H $>= Var,
ic_min(T, Var, Min).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- mode ic_gcd(+, +, -).
ic_gcd(X, 0, X).
ic_gcd(X, Y, Z) :-
XY #= ic_mod(X, Y),
ic_gcd(Y, XY, Z).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- mode ic_mod(+, +, -).
ic_mod(X, Y, Z) :-
X #< Y,
Z #= X.
ic_mod(X, Y, Z) :-
X #>= Y,
D #= X - Y,
ic_mod(D, Y, Z).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- 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).