:- lib(ic).
:- lib(listut).
:- lib(util).
:- lib(eplex).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main :-
ST is 14.5, % micro seconds
L_MS is 12 / 1000, % micro seconds
N_MS is 50,
DYN is L_MS * N_MS, % micro seconds
FC is ST + DYN, % micro seconds
T_List = [24, 13, 25, 36, 12, 24, 20, 18, 40, 4, 25, 50, 55, 64, 25, 16, 8, 16, 30, 6],
W_List = [6, 12, 12, 12, 8, 14, 12, 14, 26, 15, 5, 6, 14, 24, 6, 15, 16, 6, 4, 15],
%R_List = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
%R_List = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
R_List = [1, 2, 4, 2, 2, 4, 8, 4, 2, 4, 8, 8, 8, 8, 8, 8, 2, 4, 8, 4],
F_List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
%gen_exp(T_List, W_List, F_List),
write("T: "), writeln(T_List),
write("W: "), writeln(W_List),
write("F: "), writeln(F_List),
% all lists needs to have the same length
length(T_List, N),
length(W_List, N),
length(R_List, N),
length(F_List, N),
write(" N: "), writeln(N),
% Message Under Analysis
T_Mua is 14, % ms
W_Mua is 12, % minislots
R_Mua is 2,
F_Mua is max(F_List) + 1, % priority
Min_Load is N_MS + 2 - (F_Mua + W_Mua),
Max_Load is N_MS,
write("Min_Load: "), writeln(Min_Load),
write("Max_Load: "), writeln(Max_Load),
bag_create(Bag),
bag_erase(Bag),
time(optimal_solution(0, Bag, R_Mua, N, FC, T_List, W_List, R_List, F_List, Max_Load, Min_Load)),
get_bound(Bag, Bound_1),
write("Optimal Bound: "), writeln(Bound_1).
%bag_erase(Bag),
%time(optimal_solution(1, Bag, R_Mua, N, FC, T_List, W_List, R_List, F_List, Max_Load, Min_Load)),
%get_bound(Bag, Bound_2),
%write(" GM Bound: "), writeln(Bound_2).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% OPTIMAL in CLP %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
optimal_solution(Flag, Bag, R_Mua, N, FC, T_List, W_List, R_List, F_List, Max_Load, Min_Load) :-
(for(L, 3, 3, R_Mua), param(Flag, Bag, R_Mua, N, FC, T_List, W_List, R_List, F_List, Max_Load, Min_Load) do
cycle_types(N, L, R_List, SlotMxMatrix),
optimal_constraints(Flag, R_Mua, L, N, FC, T_List, W_List, F_List, Max_Load, Min_Load, SlotMxMatrix, BoolMatrix, LoadMatrix),
(for(Cycle, 1, L, R_Mua), param(N, BoolMatrix) do
Row is subscript(BoolMatrix, [Cycle, 1 .. N]),
search(Row, 0, input_order, indomain, complete, [])
),
% Print the results
(for(Cycle, 1, L, 1), param(N, BoolMatrix, LoadMatrix) do
write("\t"),
Row is subscript(LoadMatrix, [Cycle, 1 .. N]),
Load is sum(Row), write(Load), write(" :: "),
(count(Message, 1, N), param(Cycle, BoolMatrix) do
El is subscript(BoolMatrix, [Cycle, Message]),
write(El), write(" ")
),
writeln("")
),
writeln("\t---------------------------------------------\n"),
%write("Solution L = "), writeln(L),
bag_enter(Bag, L)
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cycle_types(N, L, R_List, SlotMxMatrix) :-
dim(SlotMxMatrix, [L, N]),
(count(Cycle, 1, L), param(N, R_List, SlotMxMatrix) do
(count(Message, 1, N),
foreach(R, R_List), param(Cycle, SlotMxMatrix) do
SlotMx is subscript(SlotMxMatrix, [Cycle, Message]),
Tmp is Cycle - 1,
mod(Tmp, R, Result),
assignValue(Result, SlotMx),
write(SlotMx), write(" ")
),
writeln("")
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
assignValue(Result, SlotMx) :-
Result = 0,
SlotMx is 1.
assignValue(Result, SlotMx) :-
Result \= 0,
SlotMx is 0.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
optimal_constraints(Flag, R_Mua, L, N, FC, T_List, W_List, F_List, Max_Load, Min_Load, SlotMxMatrix, BoolMatrix, LoadMatrix) :-
% Generate the Optimization Variables Matrix
dim(BoolMatrix, [L, N]),
(for(Cycle, 1, L, 1), param(Flag, N, SlotMxMatrix, BoolMatrix) do
(count(Message, 1, N), param(Flag, Cycle, SlotMxMatrix, BoolMatrix) do
Slot is subscript(SlotMxMatrix, [Cycle, Message]),
Bool is subscript(BoolMatrix, [Cycle, Message]),
Bool #:: [0 .. Slot]
)
),
%(for(Cycle, 1, L, 1), param(N, BoolMatrix) do
% Row is subscript(BoolMatrix, [Cycle, 1 .. N]),
% writeln(Row)
% ),
% Constraints regarding the total number of instances per message over a period of time
(for(Cycle, 1, L, 1), param(FC, T_List, N, BoolMatrix) do
write("\t"), write(" :: "),
(count(Message, 1, N),
foreach(T, T_List), param(FC, Cycle, BoolMatrix) do
Tmp_1 is ceiling(Cycle * FC / T),
Tmp_2 is fix(Tmp_1),
MaxNrOfInstances is Tmp_2, %min(Cycle, Tmp_2),
Col is subscript(BoolMatrix, [1 .. Cycle, Message]),
sum(Col) #=< MaxNrOfInstances,
write(MaxNrOfInstances), write(" ")
),
writeln("")
),
writeln("\t---------------------------------------------"),
% Generate the Load Matrix
dim(LoadMatrix, [L, N]),
(for(Cycle, 1, L, 1), param(W_List, N, BoolMatrix, LoadMatrix) do
(count(Message, 1, N),
foreach(W, W_List), param(Cycle, BoolMatrix, LoadMatrix) do
LoadMatrix[Cycle, Message] #= BoolMatrix[Cycle, Message] * (W - 1)
)
),
% Constraints regarding the load of the cycles
load_of_cycles(R_Mua, L, N, W_List, F_List, Max_Load, Min_Load, N, LoadMatrix).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load_of_cycles(R_Mua, L, N, W_List, F_List, Max_Load, Min_Load, N, LoadMatrix) :-
(for(Cycle, 1, L, R_Mua), param(W_List, F_List, Max_Load, Min_Load, N, LoadMatrix) do
(count(Message, 1, N),
foreach(F, F_List),
foreach(W, W_List),
param(Max_Load, Cycle, LoadMatrix) do
Row is subscript(LoadMatrix, [Cycle, 1 .. Message - 1]),
F + W - 1 + sum(Row) #=< Max_Load
),
Row is subscript(LoadMatrix, [Cycle, 1 .. N]),
sum(Row) #>= Min_Load
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Aditional Code %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
get_bound(Bag, Bound) :-
bag_retrieve(Bag, Bag_List),
length(Bag_List, Len),
nth1(Len, Bag_List, Bound).