with Ada.text_IO; use Ada.text_IO; with Ada.Integer_text_IO; use Ada.Integer_text_IO; with Ada.Float_Text_IO; use Ada.Float_Text_IO; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; with Ada.Strings; use Ada.Strings; with Ada.Strings.Fixed; use Ada.Strings.Fixed; with Ada.Numerics.Float_Random ; use Ada.Numerics.Float_Random ; with unbounded_strings; use unbounded_strings; with Tasks; use Tasks; with Task_Set; use Task_Set; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Unbounded_Strings; use Unbounded_Strings; with convert_unbounded_strings; use unbounded_strings.unbounded_string_list_package; with feasibility_test.feasibility_interval; use feasibility_test.feasibility_interval; with Core_Units; use Core_Units; use Core_Units.Core_Units_Table_Package; with Processors.extended; use Processors.extended; with Scheduler_Interface; use Scheduler_Interface; with Address_Spaces; use Address_Spaces; with Address_Space_Set; use Address_Space_Set; with Processors; use Processors; with Processor_Set; use Processor_Set; with processor_interface; use processor_interface; with Call_Framework; use Call_Framework; with Call_Framework_Interface; use Call_Framework_Interface; use Call_Framework_Interface.Framework_Response_Package; use Call_Framework_Interface.Framework_Request_Package; with Call_Scheduling_Framework; use Call_Scheduling_Framework; with Pipe_Commands; use Pipe_Commands; with Ada.Text_IO; use Ada.Text_IO; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO; with Ada.Directories; use Ada.Directories; with GNAT.OS_Lib; use GNAT.OS_Lib; with paes_for_clustering; use paes_for_clustering; with Task_Clustering_Rules; use Task_Clustering_Rules; with integer_util; use integer_util; package body exact_front_computation is ------------------- -- enumerate_sol -- ------------------- procedure enumerate_sol is A_sol, A_sol_norm : solution; m : chrom_type; b : boolean; NB_consistent_solutions : Integer := 0; NB_consistent_and_sched_solutions : Integer := 0; New_Task_set : Tasks_Set; New_nb_tasks : integer; FileStream : stream; command : unbounded_String; F : Ada.Text_IO.File_Type; line : unbounded_String; Buffer : unbounded_String; iter : integer; begin iter := 1; for i in 1..genes loop A_sol.chrom(i) := 1; m(i) := 1; end loop; Put_Line(" ------------------ "); Put_Line(" iteration = " & iter'Img); Put_Line(" ------------------ "); New_Line; Put_Line ("The candidate solution is : "); print_genome(A_sol); New_Line; -- normalize the candidate solution A_sol_norm := A_sol; normalize(A_sol_norm); New_Line; Put_Line ("After normalization the candidate solution is : "); print_genome(A_sol_norm); New_Line; ---------------------------------------------------------------------------------------- -- After generating a mutate solution, we shoud verify : -- 1) If the new solution is consistent or not i.e two non-harmonic functions -- which are grouped alone in the same task. -- 2) If it was a consistent solution then we should verify its schedulability -- by calling the Cheddar tool to simulate the scheduling of the candidate task set -- Else, we must regenerate a new candidate solution ----------------------------------------------------------------------------------------- If Is_solution_consistent (A_sol_norm) then Put_Line (" The candidate solution is consistent and then we check the schedulability"); NB_consistent_solutions := NB_consistent_solutions + 1; -- check the schedulability New_Task_set := Appling_clustering_rules (A_sol_norm); New_nb_tasks := Number_of_tasks (A_sol_norm); Sys.Tasks := New_Task_set; Build_xml_model (New_Task_set, "candidate_solution.xmlv3"); command := To_Unbounded_String("/media/DATA/These_Rahma/CHEDDAR/trunk/src/call_cheddar_d " & Hyperperiod_of_Initial_Taskset'img & " candidate_solution.xmlv3"); FileStream := execute(To_string(command), read_file); loop begin Buffer := read_next(FileStream); exception when Pipe_Commands.End_of_file => exit; end; end loop; close(FileStream); Open(F, Ada.Text_IO.In_File,"Output.txt"); line := To_Unbounded_String(get_line(F)); line := Unbounded_Slice(line,1,3); if line = "sch" then put_line("**The candidate task set is schedulable**"); line := To_Unbounded_String(get_line(F)); line := Unbounded_Slice(line, index(line,"=") + 3, length(line)); Nb_preemption_of_the_candidate_sol := integer'Value(to_string(Unbounded_Slice(line,1,length(line)))); put_line("Nb_preemptions = " & Nb_preemption_of_the_candidate_sol'Img); line := To_Unbounded_String(get_line(F)); line := Unbounded_Slice(line, index(line,"=") + 3, length(line)); Sum_laxities_of_the_candidate_sol := integer'Value (to_string(Unbounded_Slice(line,1,length(line)))); put_line("sum_lax = " & Sum_laxities_of_the_candidate_sol'Img); line := To_Unbounded_String(get_line(F)); line := Unbounded_Slice(line, index(line,"=") + 3, length(line)); Min_laxities_of_the_candidate_sol := integer'Value (to_string(Unbounded_Slice(line,1,length(line)))); put_line("Min_lax = " & Min_laxities_of_the_candidate_sol'Img); evaluate_for_clustering (A_sol_norm); NB_consistent_and_sched_solutions := NB_consistent_and_sched_solutions + 1; All_consistent_valid_solutions.Append (A_sol_norm); --All_consistent_valid_solutions (NB_consistent_and_sched_solutions) := A_sol_norm; else put_line("**The candidate task set is NOT schedulable**"); end if; Close(F); else Put_Line (" The candidate solution is NOt consistent and then " & " we do not check the schedulabily"); end if; -- Generate the next candidate next(A_sol,m,genes,b); while b loop iter := iter + 1; Put_Line(" ------------------ "); Put_Line(" iteration = " & iter'Img); Put_Line(" ------------------ "); New_Line; Put_Line ("The candidate solution is : "); print_genome(A_sol); New_Line; -- normalize the candidate solution A_sol_norm := A_sol; normalize(A_sol_norm); New_Line; Put_Line ("After normalization the candidate solution is : "); print_genome(A_sol_norm); New_Line; ---------------------------------------------------------------------------------------- -- After generating a mutate solution, we shoud verify : -- 1) If the new solution is consistent or not i.e two non-harmonic functions -- which are grouped alone in the same task. -- 2) If it was a consistent solution then we should verify its schedulability -- by calling the Cheddar tool to simulate the scheduling of the candidate task set -- Else, we do not check the schedulability ----------------------------------------------------------------------------------------- If Is_solution_consistent (A_sol_norm) then Put_Line (" The candidate solution is consistent and then we check the schedulability"); NB_consistent_solutions := NB_consistent_solutions + 1; -- check the schedulability New_Task_set := Appling_clustering_rules (A_sol_norm); New_nb_tasks := Number_of_tasks (A_sol_norm); Sys.Tasks := New_Task_set; Build_xml_model (New_Task_set, "candidate_solution.xmlv3"); command := To_Unbounded_String("/media/DATA/These_Rahma/CHEDDAR/trunk/src/call_cheddar_d " & Hyperperiod_of_Initial_Taskset'img & " candidate_solution.xmlv3"); FileStream := execute(To_string(command), read_file); loop begin Buffer := read_next(FileStream); exception when Pipe_Commands.End_of_file => exit; end; end loop; close(FileStream); Open(F, Ada.Text_IO.In_File,"Output.txt"); line := To_Unbounded_String(get_line(F)); line := Unbounded_Slice(line,1,3); if line = "sch" then put_line("**The candidate task set is schedulable**"); line := To_Unbounded_String(get_line(F)); line := Unbounded_Slice(line, index(line,"=") + 3, length(line)); Nb_preemption_of_the_candidate_sol := integer'Value(to_string(Unbounded_Slice(line,1,length(line)))); put_line("Nb_preemptions = " & Nb_preemption_of_the_candidate_sol'Img); line := To_Unbounded_String(get_line(F)); line := Unbounded_Slice(line, index(line,"=") + 3, length(line)); Sum_laxities_of_the_candidate_sol := integer'Value (to_string(Unbounded_Slice(line,1,length(line)))); put_line("sum_lax = " & Sum_laxities_of_the_candidate_sol'Img); line := To_Unbounded_String(get_line(F)); line := Unbounded_Slice(line, index(line,"=") + 3, length(line)); Min_laxities_of_the_candidate_sol := integer'Value (to_string(Unbounded_Slice(line,1,length(line)))); put_line("Min_lax = " & Min_laxities_of_the_candidate_sol'Img); evaluate_for_clustering (A_sol_norm); NB_consistent_and_sched_solutions := NB_consistent_and_sched_solutions + 1; Put_line(" NB_consistent_and_sched_solutions = " & NB_consistent_and_sched_solutions'img); All_consistent_valid_solutions.Append (A_sol_norm); --All_consistent_valid_solutions (NB_consistent_and_sched_solutions) := A_sol_norm; else put_line("**The candidate task set is NOT schedulable**"); end if; Close(F); else Put_Line (" The candidate solution is NOt consistent and then " & " we do not check the schedulabily"); end if; next(A_sol,m,genes,b); end loop; nb_consistent := NB_consistent_solutions; nb_consistent_sched := NB_consistent_and_sched_solutions; --l := k - 1; end enumerate_sol; ---------- -- next -- ---------- procedure next (s : in out solution; m : in out chrom_Type; n : integer; b: out boolean) is i,max : integer; --s1 : solution; begin -- Update s: 1 1 1 1 -> 2 1 1 1 -> 1 2 1 1 -> 2 2 1 1 -> 3 2 1 1 -> 1 1 2 1 ... --Put(" -> ("); --for j in 1..n loop -- Put(s(j)'img); --New_Line; i := 1; s.chrom(i) := s.chrom(i) + 1; while ((i < n) and (s.chrom(i) > m(i) + 1)) loop s.chrom(i) := 1; i := i + 1; s.chrom(i) := s.chrom(i) + 1; end loop; -- If i is has reached n-1 th element, then the last unique partitiong -- has been found if (i = n) then b := false; else -- Because all the first i elements are now 1, s[i] (i + 1 th element) -- is the largest. So we update max by copying it to all the first i -- positions in m. max := s.chrom(i); if (m(i) > max) then max := m(i); end if; for j in 1 .. i-1 loop m(j) := max; end loop; b := true; end if; --s1 := s; --normalize(s1,n); --Put("s : "); --for j in 1..n loop -- Put(s1.chrom(j)'img); --end loop; --New_Line; --Put("m : "); --for j in 1..genes loop -- Put(m(j)'img); --end loop; --New_Line; end next; ---------------------------- -- Is_solution_consistent -- ---------------------------- function Is_solution_consistent (s : in solution) return boolean is Is_consistent : boolean; min_periods : Integer; nb_tasks : Integer; gcd_periods : Integer; i : Integer; nb_functions_in_task_i : Integer; periods_array : array (1..genes) of Integer; Initial_set : Tasks_Set := Initial_tasks_set; begin Is_consistent := true; nb_tasks := number_of_tasks(s); Put_Line ("nb_tasks = " & nb_tasks'img); i := 1; while (i<= nb_tasks) and Is_consistent loop nb_functions_in_task_i := 0; for j in 1 .. genes loop if (s.chrom(j) = i) then nb_functions_in_task_i := nb_functions_in_task_i + 1; periods_array (nb_functions_in_task_i) := Get(My_Tasks => Initial_set, Task_Name => Suppress_Space (To_Unbounded_String ("Task" & j'Img)), Param_Name => Period); end if; end loop; if (nb_functions_in_task_i = 2) then if ( periods_array(1) < periods_array(2) ) then min_periods := periods_array(1); else min_periods := periods_array(2); end if; gcd_periods := integer_util.gcd (periods_array(1) , periods_array(2)); if (min_periods /= gcd_periods) then Is_consistent := false; end if; elsif (nb_functions_in_task_i > 2) then min_periods := periods_array(1); gcd_periods := integer_util.gcd (periods_array(1) , periods_array(2)); for j in 2 .. nb_functions_in_task_i loop if (periods_array(j) < min_periods) then min_periods := periods_array(j); end if; if (j < nb_functions_in_task_i) then gcd_periods := integer_util.gcd (gcd_periods, periods_array(j+1)); end if; end loop; if (min_periods /= gcd_periods) then Is_consistent := false; end if; else Is_consistent := true; end if; i := i + 1; end loop; return Is_consistent; end Is_solution_consistent; end exact_front_computation;