------------------------------------------------------------------------------ ------------------------------------------------------------------------------ -- Calling PAES for the Task Clustering problem -- To compile : make alone "TARGET=call_paes" -- To execute : -- If we want to generate a task set : -- ./paes_d -g -- If we want to execute paes with a task set given in an xml file : -- ./paes_d -i -- Example : ./paes_d 4 10 5000 -i initial_tasks_set.xmlv3 -- ./paes_d 4 10 5000 -g 0.9 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ with Text_IO; use Text_IO; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with unbounded_strings; use unbounded_strings; use unbounded_strings.strings_table_package; use unbounded_strings.unbounded_string_list_package; with unbounded_strings; use unbounded_strings; with Parameters; use Parameters; with Parameters.extended; use Parameters.extended; use Parameters.Framework_Parameters_Table_Package; with Systems; use Systems; with Framework; use Framework; 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 Ada.Directories; use Ada.Directories; with Ada.text_IO; use Ada.text_IO; with Ada.Command_Line; use Ada.Command_Line; with GNAT.Command_Line; use GNAT.Command_Line; with GNAT.OS_Lib; use GNAT.OS_Lib; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with unbounded_strings; use unbounded_strings; with Ada.Strings; use Ada.Strings; with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO; with Ada.Directories; use Ada.Directories; with Ada.Calendar; use Ada.Calendar; with Ada.Calendar.Formatting; use Ada.Calendar.Formatting; with Ada.Text_IO; use Ada.Text_IO; with Paes_For_Clustering; with Paes; use Paes; with unbounded_strings; use unbounded_strings; with Tasks; use Tasks; with Task_Set; use Task_Set; with Task_Clustering_Rules; use Task_Clustering_Rules; with feasibility_test.feasibility_interval; use feasibility_test.feasibility_interval; 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 Paes_For_Clustering; use Paes_For_Clustering; with Hypervolume_computation; use Hypervolume_computation; with scheduler_Interface; use scheduler_Interface; with framework_config; use framework_config; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure call_paes is procedure init is new Paes.init (Paes_For_Clustering.init_for_clustering); procedure mutate is new Paes.mutate (Paes_For_Clustering.mutate_for_clustering); procedure evaluate is new Paes.evaluate (Paes_For_Clustering.evaluate_for_clustering); package Fix_IO is new Ada.Text_IO.Fixed_IO(DAY_DURATION); use Fix_IO; result : integer; My_tasks : Tasks_set; Total_cpu_utilization : float := 0.00; Dir : unbounded_string; Dir2 : unbounded_string; Data,Data2,Data3 : Unbounded_String; F : Ada.Text_IO.File_Type; dir1 : unbounded_string_list; A_capacity : natural; A_period : natural; A_deadline : natural; H : integer; FileStream : stream; command : unbounded_String; F1,F2,F3 : Ada.Text_IO.File_Type; line : unbounded_String; Buffer : unbounded_String; scheduler_Name : unbounded_String; obj2_Name : unbounded_String; initial_task_set_file_name : unbounded_string; hyperperiod_candidate_task_set : integer; Max_hyperperiod : integer; Processor_Utilization : integer; actual_cpu_utilization : float := 0.00; A_utilization : float; package Flt_IO is new Ada.Text_IO.Float_IO(FLOAT); use Flt_IO; Year,Month,Day : INTEGER; Start,Ends,Duration : DAY_DURATION; Time_And_Date : TIME; procedure Usage is begin New_Line; Put_Line ("paes_d is a program which from an initial schedulable task set, generate with PAES the front pareto: a set of schedulable task sets."); New_Line; Put_Line("Check Cheddar home page for details : http://beru.univ-brest.fr/~singhoff/cheddar "); New_Line; New_Line; Put_Line ("Usage : ./paes_d [switch] "); Put_Line (" switch can be :"); Put_Line (" -h get this help"); Put_Line (" -n Nb_functions the number of functions"); Put_Line (" -sched scheduler_Name the scheduler can be RM or EDF "); Put_Line (" -obj2 obj2_Name the second objective can be sum_lax or Min_lax "); Put_Line (" -iter iterations the number of PAES iterations "); Put_Line (" -i file_name the XML file which contains the initial task set i.e. each" & " function is assigned to a task "); Put_Line (" -U Total_cpu_utilization Total processor utilization "); New_Line; end Usage; begin Call_Framework.initialize (False); -- Get arguments -- loop case GNAT.Command_Line.Getopt ("h n: sched: obj2: iter: i: u:") is when ASCII.NUL => exit; when 'n' => genes := Integer'Value(GNAT.Command_Line.Parameter); Put_Line ("Number of functions = " & genes'img); when 's' => if Full_Switch = "sched" then scheduler_Name := To_Unbounded_String (GNAT.Command_Line.Parameter); Put_Line ("The scheduler is " & To_string(scheduler_Name)); if scheduler_Name = "RM" then The_Scheduler := Rate_Monotonic_Protocol; Task_priority := 1; Sched_policy := SCHED_FIFO; elsif scheduler_Name = "EDF" then The_Scheduler := Earliest_Deadline_First_Protocol; Task_priority := 0; Sched_policy := SCHED_OTHERS; else Usage; OS_Exit (0); end if; end if; when 'o' => if Full_Switch = "obj2" then obj2_Name := To_Unbounded_String (GNAT.Command_Line.Parameter); if obj2_Name = "sum_lax" or obj2_Name = "min_lax" then Second_fitness_function := obj2_Name; Put_Line ("The second fitness function is " & To_string(obj2_Name)); else Usage; OS_Exit (0); end if; end if; when 'i' => if Full_Switch = "i" then initial_task_set_file_name := To_Unbounded_String (GNAT.Command_Line.Parameter); Put_Line ("The initial_task_set_file_name is " & To_string(initial_task_set_file_name)); elsif Full_Switch = "iter" then iterations := Integer'Value(GNAT.Command_Line.Parameter); Put_Line ("The number of iterations = " & iterations'img); end if; when 'u' => Total_cpu_utilization := (Float'Value(GNAT.Command_Line.Parameter)/100.0); Processor_Utilization := Integer (Total_cpu_utilization * 100.0); Put ("The Total_cpu_utilization = "); Put (Total_cpu_utilization, 8, 8, 0); New_Line; when 'h' => Usage; OS_Exit (0); when others => Usage; OS_Exit (0); end case; end loop; -- Customize PAES parameters according our problem : depth := 4; minmax := 0; -- minimization problem objectives := 2; -- two ojectives archive := 200; -- the archive size is fixed to 200 -- initialization of the current solution c and -- the system "Sys" to invoke Cheddar. -- and the random float generator "G" used -- in the mutation procedure init; Create(F3,Ada.Text_IO.Out_File,"Time_of_execution.txt"); if (Length (initial_task_set_file_name) /= 0) then -- we use the task set described in the xml file given in argument initialize(Sys); Read_From_Xml_File (Sys, dir1, initial_task_set_file_name); My_tasks := Sys.Tasks; Build_xml_model (My_tasks, "candidate_solution.xmlv3"); H := Scheduling_Period (Sys.Tasks, to_unbounded_string("processor1")); Hyperperiod_of_Initial_Taskset := H; -- Check the schedulability of the given task set 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(F1, Ada.Text_IO.In_File,"Output.txt"); line := To_Unbounded_String(get_line(F1)); line := Unbounded_Slice(line,1,3); if line = "sch" then Initial_tasks_set := My_tasks; Put_Line ("The initial tasks set is schedulable"); line := To_Unbounded_String(get_line(F1)); 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(F1)); 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(F1)); 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); else Put_Line ("The initial tasks set is not schedulable!"); return; end if; Close(F1); elsif (Total_cpu_utilization /= 0.00) then Put_Line ("generate a task set of " & genes'img & " tasks with a total processor utilization" & Total_cpu_utilization'img ); Time_And_Date := Clock; Split(Time_And_Date, Year, Month, Day, Start); -- We use the given processor utilization to generate a task set Generate_initial_schedulable_task_set (My_tasks, genes, Total_cpu_utilization); Time_And_Date := Clock; Split(Time_And_Date, Year, Month, Day, Ends); Duration:= Ends - Start; Put("Task_set_generation_time " & ASCII.HT & ASCII.HT & ": "); Put(Duration, 8, 8, 0); Put_Line(""); Append (Data3, "Task_set_generation_time " & ":"); Unbounded_IO.Put(F3, Data3); Ada.Strings.Unbounded.Delete(Data3, 1, Length(Data3)); Put(F3, Duration, 8, 8, 0); Delay(1.0); initial_task_set_file_name := To_Unbounded_String("initial_tasks_set.xmlv3"); -- Initialize the tasks of the system variable with the Initial_tasks_set Sys.Tasks := Initial_tasks_set; else Usage; OS_Exit (0); end if; build_xml_model (Initial_tasks_set, "initial_tasks_set.xmlv3"); Append (Data, " depth =" & depth'Img & " iterations =" & iterations'Img & ASCII.LF); actual_cpu_utilization := 0.00; for i in 1..genes loop A_capacity := Task_Set.Get (My_Tasks => Initial_tasks_set, Task_Name => Suppress_Space (To_Unbounded_String ("Task" & i'Img)), Param_Name => Capacity); A_period := Task_Set.Get (My_Tasks => Initial_tasks_set, Task_Name => Suppress_Space (To_Unbounded_String ("Task" & i'Img)), Param_Name => Period); A_deadline := Task_Set.Get (My_Tasks => Initial_tasks_set, Task_Name => Suppress_Space (To_Unbounded_String ("Task" & i'Img)), Param_Name => Deadline); Ada.text_IO.Put (" C" & i'Img & " = " & A_capacity'Img); Ada.text_IO.Put (" T" & i'Img & " = " & A_period'Img); Ada.text_IO.Put (" D" & i'Img & " = " & A_deadline'Img); New_Line; A_utilization := Float(A_capacity) / Float(A_period); actual_cpu_utilization := actual_cpu_utilization + A_utilization; end loop; Put ("The actual processor utilization is : "); Put (actual_cpu_utilization, 8, 8, 0); New_Line; H := Scheduling_Period (Initial_tasks_set, to_unbounded_string("processor1")); Hyperperiod_of_Initial_Taskset := H; Put_Line ("The hyperperiod of the initial task set = " & H'Img); Max_hyperperiod := Hyperperiod_of_Initial_Taskset; Create(F,Ada.Text_IO.Out_File,"execution_trace.txt"); print_genome(c); -- Uncomment to check genome is correct for l in 1..genes loop Append (Data, c.chrom(l)'Img & " "); end loop; Append (Data, ASCII.LF); evaluate(c); -- Initialization of the Anti-Ideal point to the initial solution Z1_anti_ideal := c.obj(1); Z2_anti_ideal := c.obj(2); Put_Line ("Z1_anti_ideal = " & Z1_anti_ideal'Img); Put_Line ("Z2_anti_ideal = " & Z2_anti_ideal'Img); print_eval(c); -- Uncomment to check objective values generated Append (Data, Integer(c.obj(1))'Img & " "); Append (Data, Integer(c.obj(2))'Img & " "); Append (Data, ASCII.LF); add_to_archive(c); update_grid(c); Put_Line(" arclength = " & arclength'Img); Append (Data, " arclength = " & arclength'Img & ASCII.LF); New_Line; Append (Data, ASCII.LF); Put_Line("The genetic material in the archive is now... "); Append (Data, "The genetic material in the archive is now... " & ASCII.LF); for l in 1..arclength loop print_eval(arc(l)); Append (Data, Integer(arc(l).obj(1))'Img & " "); Append (Data, Integer(arc(l).obj(2))'Img & " "); New_Line; Append (Data, ASCII.LF); end loop; New_Line; Append (Data, ASCII.LF); for q in 1..arclength loop Put_Line("---------------------------------------------------------------------------------------"); Append (Data, "---------------------------------------------------------------------------------------" & ASCII.LF); Put("Solution " & q'img & ": "); Append (Data, "Solution " & q'img & ": "); print_genome(arc(q)); for l in 1..genes loop Append (Data, arc(q).chrom(l)'Img & " "); end loop; Append (Data, ASCII.LF); Put_Line("Objectives of solution " & q'img & " : "); Append (Data, "Objectives of solution " & q'img & " : " & ASCII.LF & " | Number of preemptions = " & Integer(arc(q).obj(1))'Img & " | " & To_String(Second_fitness_function) & " = " & Integer(Hyperperiod_of_Initial_Taskset - Integer(arc(q).obj(2)))'Img & " " & ASCII.LF); Put_Line (" | Number of preemptions = " & Integer(arc(q).obj(1))'Img & " | " & To_String(Second_fitness_function) & " = " & Integer(Hyperperiod_of_Initial_Taskset - Integer(arc(q).obj(2)))'Img & " "); Put_Line("---------------------------------------------------------------------------------------"); Append (Data, "---------------------------------------------------------------------------------------" & ASCII.LF); end loop; New_Line; Append (Data, ASCII.LF); ------------------------- -- begin the main loop -- ------------------------- Time_And_Date := Clock; Split(Time_And_Date, Year, Month, Day, Start); for i in 1..iterations loop New_Line; Append (Data, ASCII.LF); Put_Line(" ------------------ "); Append (Data, " ------------------ " & ASCII.LF); Put_Line(" iteration = " & i'Img); Append (Data, " iteration = " & i'Img & ASCII.LF); Put_Line(" ------------------ "); Append (Data, " ------------------ " & ASCII.LF); New_Line; Append (Data, ASCII.LF); m := c; -- copy the current solution New_Line; Append (Data, ASCII.LF); Put(" Objectives of the current solution : "); Append (Data, " Objectives of the current solution : "); print_eval(c); Append (Data, Integer(c.obj(1))'Img & " "); Append (Data, Integer(c.obj(2))'Img & " "); Append (Data, ASCII.LF); New_Line; Append (Data, ASCII.LF); Put_Line(" The current solution : "); Append (Data, " The current solution : " & ASCII.LF); print_genome(c); for l in 1..genes loop Append (Data, c.chrom(l)'Img & " "); end loop; Append (Data, ASCII.LF); New_Line; Append (Data, ASCII.LF); mutate(m); Put_Line(" The mutated normalized solution of iteration " & i'Img & " : "); Append (Data, " The mutated normalized solution of iteration " & i'Img & " : " & ASCII.LF); print_genome(m); for l in 1..genes loop Append (Data, m.chrom(l)'Img & " "); end loop; Append (Data, ASCII.LF); evaluate(m); -- compute the hyperperiod of the candidate solution "m" hyperperiod_candidate_task_set := Scheduling_Period (Appling_clustering_rules(m), to_unbounded_string("processor1")); Put_Line ("The hyperperiod of the candidate task set is : " & hyperperiod_candidate_task_set'img); if Max_hyperperiod < hyperperiod_candidate_task_set then Max_hyperperiod := hyperperiod_candidate_task_set; end if; -- compare coordinates of ideal point with coordinates of the -- candidate solution if m.obj(1) > Z1_anti_ideal then Z1_anti_ideal := m.obj(1); end if; if m.obj(2) > Z2_anti_ideal then Z2_anti_ideal := m.obj(2); end if; Put_Line(" The Anti-Ideal point in the iteration " & i'Img & " is : "); Put_Line ("Z1_anti_ideal = " & Z1_anti_ideal'Img); Put_Line ("Z2_anti_ideal = " & Z2_anti_ideal'Img); Put(" Objectives of the mutated solution : "); Append (Data, " Objectives of the mutated solution : "); print_eval(m); Append (Data, Integer(m.obj(1))'Img & " "); Append (Data, Integer(m.obj(2))'Img & " "); Append (Data, ASCII.LF); New_Line; New_Line; Append (Data, ASCII.LF); Append (Data, ASCII.LF); Put(" Comparing "); Append (Data, " Comparing "); print_eval(c); Append (Data, Integer(c.obj(1))'Img & " "); Append (Data, Integer(c.obj(2))'Img & " "); Append (Data, ASCII.LF); Put("and "); Append (Data, "and "); print_eval(m); Append (Data, Integer(m.obj(1))'Img & " "); Append (Data, Integer(m.obj(2))'Img & " "); Append (Data, ASCII.LF); New_Line; New_Line; Append (Data, ASCII.LF); Append (Data, ASCII.LF); -- MINIMIZE MAXIMIZE if (minmax = 0) then result := compare_min(c.obj, m.obj, objectives); else result := compare_max(c.obj, m.obj, objectives); end if; if (result /= 1) then -- if mutant is not dominated by current (else discard it) if (result = -1) then -- if mutant dominates current Put_Line("m dominates c"); Append (Data, "m dominates c" & ASCII.LF); update_grid(m); -- calculate grid location of mutant solution and renormalize -- archive if necessary archive_soln(m); -- update the archive by removing all dominated individuals c := m; -- replace c with m elsif (result = 0) then -- if mutant and current are nondominated wrt each other result := compare_to_archive(m); if (result /= -1) then -- if mutant is not dominated by archive (else discard it) update_grid(m); archive_soln(m); if (grid_pop(m.grid_loc) <= grid_pop(c.grid_loc)) or (result = 1) then -- if mutant dominates the archive or -- is in less crowded grid loc than c -- then replace c with m c := m; end if; end if; end if; end if; New_Line; Append (Data, ASCII.LF); Put_Line("The genetic material in the archive is now... "); Append (Data, "The genetic material in the archive is now... " & ASCII.LF); for q in 1..arclength loop Put_Line("---------------------------------------------------------------------------------------"); Append (Data, "---------------------------------------------------------------------------------------" & ASCII.LF); Put("Solution " & q'img & ": "); Append (Data, "Solution " & q'img & ": "); print_genome(arc(q)); for l in 1..genes loop Append (Data, arc(q).chrom(l)'Img & " "); end loop; build_xml_model (Appling_clustering_rules(arc(q)), To_string(Suppress_Space (To_Unbounded_String ("solution" & q'Img & ".xmlv3")))); Append (Data, ASCII.LF); Put_Line("Objectives of solution " & q'img & " : "); Append (Data, "Objectives of solution " & q'img & " : " & ASCII.LF & " | Number of preemptions = " & Integer(arc(q).obj(1))'Img & " | " & To_String(Second_fitness_function) & " = " & Integer(Hyperperiod_of_Initial_Taskset - Integer(arc(q).obj(2)))'Img & " " & ASCII.LF); Put_Line (" | Number of preemptions = " & Integer(arc(q).obj(1))'Img & " | " & To_String(Second_fitness_function) & " = " & Integer(Hyperperiod_of_Initial_Taskset - Integer(arc(q).obj(2)))'Img & " "); Put_Line("---------------------------------------------------------------------------------------"); Append (Data, "----------------------------------------------------------------------------------" & ASCII.LF); end loop; New_Line; Append (Data, ASCII.LF); Unbounded_IO.Put_Line(F, Data); Ada.Strings.Unbounded.Delete(Data, 1, Length(Data)); end loop; New_Line; New_Line; Append (Data, ASCII.LF); Append (Data, ASCII.LF); Put_Line("====================================================================================================== "); Put_Line(" The final Archive length = " & arclength'Img); Put_Line(" The final Archive is now... "); Append (Data,"======================================================================================" & "================ " & ASCII.LF & " The final Archive length = " & arclength'Img & ASCII.LF & " The final Archive is now... " & ASCII.LF); for i in 1..arclength loop Append(Data2, arc(i).obj(1)'Img & " " & arc(i).obj(2)'Img & ASCII.LF); Put_Line("---------------------------------------------------------------------------------------"); Append (Data, "---------------------------------------------------------------------------------------" & ASCII.LF & "Solution " & i'img & ": "); Put("Solution " & i'img & ": "); print_genome(arc(i)); for l in 1..genes loop Append (Data, arc(i).chrom(l)'Img & " "); end loop; Append (Data, ASCII.LF); build_xml_model (Appling_clustering_rules(arc(i)), To_string(Suppress_Space (To_Unbounded_String ("solution" & i'Img & ".xmlv3")))); Put_Line ("Objectives of solution " & i'img & " : "); Put_Line (" || Number of preemptions = " & Integer(arc(i).obj(1))'Img & " | " & To_String(Second_fitness_function) & " = " & Integer(Hyperperiod_of_Initial_Taskset - Integer(arc(i).obj(2)))'Img & " || "); Append (Data, "Objectives of solution " & i'img & " : " & ASCII.LF & " | Number of preemptions = " & Integer(arc(i).obj(1))'Img & " | " & To_String(Second_fitness_function) & " = " & Integer(Hyperperiod_of_Initial_Taskset - Integer(arc(i).obj(2)))'Img & " " & ASCII.LF); Put_Line("---------------------------------------------------------------------------------------"); Append (Data, "------------------------------------------------" & "---------------------------------------" & ASCII.LF); end loop; Put_Line("====================================================" & "================================================== "); Append (Data, "=======================================================" & "=============================================== " & ASCII.LF); Unbounded_IO.Put_Line(F, Data); Close(F); Create(F2,Ada.Text_IO.Out_File,"front.dat"); -- & To_String(Suppress_Space(To_Unbounded_String(Image(Date => Time_And_Date)))) & ".dat"); Unbounded_IO.Put_Line(F2, Data2); Close(F2); Put_Line ("The hyperperiod of initial solution: " & Hyperperiod_of_Initial_Taskset'img); Put_Line ("The maximum hyperperiod of candidate solution: " & Max_hyperperiod'img); Time_And_Date := Clock; Split(Time_And_Date, Year, Month, Day, Ends); Duration:= Ends - Start; Put("Paes_computation_time" & ASCII.HT & ":"); Put(Duration, 8, 8, 0); Put_Line(""); Append (Data3, ASCII.LF & "Paes_computation_time " & ":"); Unbounded_IO.Put(F3, Data3); Ada.Strings.Unbounded.Delete(Data3, 1, Length(Data3)); Put(F3, Duration, 8, 8, 0); Close(F3); end call_paes;