Skip to content

Commit

Permalink
A concorde/agpl-cr-assigner-mtsp_concordefake.ads
Browse files Browse the repository at this point in the history
A    concorde/agpl-optimization-concordefake.ads
A    concorde/agpl-cr-assigner-mtsp_concordefake.adb
A    concorde/agpl-optimization-concordefake.adb
M    agpl-cr-assigner-greedy_exhaustive.adb
M    agpl-gdk/agpl-gdk-managed.adb
M    agpl-gdk/agpl-gdk-managed.ads
M    agpl-cr-assigner-greedy_exhaustive.ads
M    agpl-cr-tasks-insertions.adb
M    agpl-cr-tasks-insertions.ads
M    agpl-cr-assignment.adb
M    agpl-cr-assignment.ads
M    agpl-cr-plan_assigner-greedy1.ads
M    agpl-cr-assigner-greedy_minmax_exhaustive.ads
M    agpl-graphs-bellman_ford.adb
M    agpl-graphs-bellman_ford.ads
M    agpl-random.adb
M    agpl-random.ads
  • Loading branch information
mosteo committed Sep 15, 2006
1 parent fb36d26 commit 121bf64
Show file tree
Hide file tree
Showing 18 changed files with 1,465 additions and 79 deletions.
20 changes: 13 additions & 7 deletions agpl-cr-assigner-greedy_exhaustive.adb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
------------------------------------------------------------------------------

with Agpl.Chronos;
with Agpl.Cr.Agent.Handle;
with Agpl.Cr.Assignment;
with Agpl.Cr.Tasks.Insertions;
Expand Down Expand Up @@ -76,6 +77,7 @@ package body Agpl.Cr.Assigner.Greedy_Exhaustive is
raise Program_Error; -- Shouldn't be reached.
end Remove_From_Pending;

Timer : Chronos.Object;
begin
-- Set agents
declare
Expand All @@ -90,20 +92,24 @@ package body Agpl.Cr.Assigner.Greedy_Exhaustive is

-- Assign tasks:
while not Pending.Is_Empty loop
Log ("Pending:" & Pending.Length'Img, Always);
Log ("Pending:" & Pending.Length'Img & " (" & Timer.Image & ")",
Always);
Timer.Reset;

declare
New_Ass : Cr.Assignment.Object;
Id_Used : Htn.Tasks.Task_Id;
use Htn.Tasks;
begin
-- Insert best task in best agent:
Cr.Tasks.Insertions.Greedy (A,
Pending,
Costs,
This.Criterion,
New_Ass,
Id_Used);
Cr.Tasks.Insertions.Greedy
(A,
Pending,
Costs,
This.Criterion,
New_Ass,
Id_Used,
This.Randomize);
if Id_Used /= No_Task then
A := New_Ass;
Remove_From_Pending (Id_Used);
Expand Down
5 changes: 4 additions & 1 deletion agpl-cr-assigner-greedy_exhaustive.ads
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ package Agpl.Cr.Assigner.Greedy_Exhaustive is

-- O (T * A * T * T) ~ O (n^4)

pragma Preelaborate;
-- pragma Preelaborate;

type Object is new Assigner.Object with record
Randomize : Boolean := False;
Criterion : Assignment_Criteria := Criterion_Time_Critical;
end record;
-- Random is true, when two agents are tied in a step, the winner is chosen
-- at random.

function Assign
(This : in Object;
Expand Down
2 changes: 1 addition & 1 deletion agpl-cr-assigner-greedy_minmax_exhaustive.ads
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ package Agpl.Cr.Assigner.Greedy_Minmax_Exhaustive is

-- O (T * A * T * T) ~ O (n^4)

pragma Preelaborate;
-- pragma Preelaborate;

type Object (Keep_Order : Boolean) is new Assigner.Object with null record;
-- If Keep_Order, any tasks in an Agent passed to Agents will be kept in
Expand Down
57 changes: 57 additions & 0 deletions agpl-cr-assignment.adb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ with Agpl.Htn.Tasks.Utils;

package body Agpl.Cr.Assignment is

package Agent_Lists renames Cr.Agent.Containers.Lists;
package Task_Lists renames Htn.Tasks.Containers.Lists;
function To_String is new Conversions.To_Str (Costs);

---------
Expand Down Expand Up @@ -403,6 +405,61 @@ package body Agpl.Cr.Assignment is
Minsum => Get_Cummulative_Cost (This, C));
end Get_Cost;

-------------------------
-- Get_Cost_Until_Task --
-------------------------

function Get_Cost_Until_Task (This : in Object;
Job : in Agpl.Htn.Tasks.Task_Id;
Criterion : in Assignment_Criteria)
return Agpl.Cr.Costs
is
Agents : constant Agent_Lists.List := This.Get_Agents;
Minmax,
Minsum : Cr.Costs := 0.0;

-----------------
-- Check_Agent --
-----------------

procedure Check_Agent (I : Agent_Lists.Cursor) is
Tasks : constant Task_Lists.List := Agent_Lists.Element (I).Get_Tasks;
Acum : Cr.Costs := 0.0;
T : Task_Lists.Cursor := Tasks.First;
use Agent_Lists;
use Task_Lists;
use type Htn.Tasks.Task_Id;
begin
while Task_Lists.Has_Element (T) loop
if T /= Tasks.First then
Acum := Acum + Element (I).Get_Cost (Element (Previous (T)),
Element (T));
else
Acum := Acum + Element (I).Get_Cost (Element (T));
end if;

if Element (T).Get_Id = Job then
Minmax := Acum;
exit;
end if;
Task_Lists.Next (T);
end loop;
end Check_Agent;

procedure Check_Minsum (I : Agent_Lists.Cursor) is
use Agent_Lists;
begin
Minsum := Minsum + Cr.Costs'Min (Minmax, Element (I).Get_Plan_Cost);
end Check_Minsum;

begin
Agents.Iterate (Check_Agent'Access);
Agents.Iterate (Check_Minsum'Access);
return Cr.Evaluate (Criterion,
Minmax => Minmax,
Minsum => Minsum);
end Get_Cost_Until_Task;

------------------------
-- Invalid_Assignment --
------------------------
Expand Down
6 changes: 6 additions & 0 deletions agpl-cr-assignment.ads
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ package Agpl.Cr.Assignment is
Criterion : in Assignment_Criteria) return Costs;
-- Uses one of the two previous according to the Criterion

function Get_Cost_Until_Task (This : in Object;
Job : in Agpl.Htn.Tasks.Task_Id;
Criterion : in Assignment_Criteria)
return Agpl.Cr.Costs;
-- Says the cost incurred until finishing

function Invalid_Assignment return Object;
-- Returns an invalid assignment.

Expand Down
2 changes: 1 addition & 1 deletion agpl-cr-plan_assigner-greedy1.ads
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

package Agpl.Cr.Plan_Assigner.Greedy1 is

pragma Preelaborate;
-- pragma Preelaborate;

Log_Section : constant String := "agpl.cr.plan_assigner.greedy1";

Expand Down
40 changes: 29 additions & 11 deletions agpl-cr-tasks-insertions.adb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
with Agpl.Cr.Agent.Containers;
with Agpl.Cr.Agent.Utils; use Agpl.Cr.Agent.Utils;
with Agpl.Dynamic_Vector;
with Agpl.Htn.Tasks.Utils;
with Agpl.Random;
-- with Agpl.Text_Io; use Agpl.Text_Io;

package body Agpl.Cr.Tasks.Insertions is
Expand Down Expand Up @@ -298,12 +300,17 @@ package body Agpl.Cr.Tasks.Insertions is
Costs : in Cost_Matrix.Object;
Criterion : in Assignment_Criteria;
New_Ass : out Assignment.Object;
Success : out Boolean)
Success : out Boolean;
Random : in Boolean := False)
is
Agents : constant Agent.Containers.Lists.List := Ass.Get_Agents;

Best_Agent : Agent.Handle.Object;
Best_Cost : Cr.Costs := Infinite;
package Candidate_Vectors is
new Dynamic_Vector (Cr.Agent.Handle.Object);

Candids : Candidate_Vectors.Object (First => 1);

Best_Cost : Cr.Costs := Cr.Infinite;

procedure Check_Agent (I : in Cr.Agent.Containers.Lists.Cursor) is
New_Agent : Cr.Agent.Handle.Object;
Expand All @@ -330,9 +337,12 @@ package body Agpl.Cr.Tasks.Insertions is
Minmax => New_Total,
Minsum => New_Delta);

if New_Cost < Best_Cost then
Best_Cost := New_Cost;
Best_Agent := New_Agent;
if New_Cost < Cr.Infinite and then New_Cost = Best_Cost then
Candids.Append (New_Agent);
elsif New_Cost < Best_Cost then
Best_Cost := New_Cost;
Candids.Clear;
Candids.Append (New_Agent);
end if;
end if;
end Check_Agent;
Expand All @@ -341,10 +351,16 @@ package body Agpl.Cr.Tasks.Insertions is

Agent.Containers.Lists.Iterate (Agents, Check_Agent'Access);

if Best_Agent.Is_Valid then
if not Candids.Is_Empty then
Success := True;
New_Ass.Set_Agent (Best_Agent.Get);
-- Put_Line ("Assigned to " & Best_Agent.Get.Get_Name);
if Random then
New_Ass.Set_Agent
(Candids.Vector
(Agpl.Random.Get_Integer
(Candids.First, Candids.Last)).Get);
else
New_Ass.Set_Agent (Candids.Vector (Candids.First).Get);
end if;
else
Success := False;
end if;
Expand All @@ -359,7 +375,8 @@ package body Agpl.Cr.Tasks.Insertions is
Costs : in Cost_Matrix.Object;
Criterion : in Assignment_Criteria;
New_Ass : out Assignment.Object;
Inserted : out Htn.Tasks.Task_Id)
Inserted : out Htn.Tasks.Task_Id;
Random : in Boolean := False)
is
Pending : constant Htn.Tasks.Containers.Vectors.Vector :=
Htn.Tasks.Utils.To_Vector (Tasks);
Expand All @@ -381,7 +398,8 @@ package body Agpl.Cr.Tasks.Insertions is
Costs,
Criterion,
Temp_Ass,
Partial_Success);
Partial_Success,
Random);
if Partial_Success then
Temp_Cost := Temp_Ass.Get_Cost (Costs, Criterion);
if Temp_Cost < Best_Cost then
Expand Down
10 changes: 7 additions & 3 deletions agpl-cr-tasks-insertions.ads
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use Agpl;

package Agpl.Cr.Tasks.Insertions is

pragma Preelaborate;
-- pragma Preelaborate;

type Insertion_Procedures is access
procedure (A : in Agent.Object'Class;
Expand Down Expand Up @@ -101,19 +101,23 @@ package Agpl.Cr.Tasks.Insertions is
Costs : in Cost_Matrix.Object;
Criterion : in Assignment_Criteria;
New_Ass : out Assignment.Object;
Success : out Boolean);
Success : out Boolean;
Random : in Boolean := False);
-- Insert a task in the best place of the best agent of an assignment
-- The results are given in New_Ass, with Success true.
-- If random, a random agent is chosen on tie. If not, the first one wins

procedure Greedy (Ass : in Assignment.Object;
Tasks : in Htn.Tasks.Containers.Lists.List;
Costs : in Cost_Matrix.Object;
Criterion : in Assignment_Criteria;
New_Ass : out Assignment.Object;
Inserted : out Htn.Tasks.Task_Id);
Inserted : out Htn.Tasks.Task_Id;
Random : in Boolean := False);
-- Insert the best task of the list in the best agent.
-- Just *one* task is inserted.
-- Inserted can be No_Task if failure.
-- If random, a random agent is chosen on tie. If not, the first one wins

procedure Greedy_Tail (Agent : in Cr.Agent.Object'Class;
Tasks : in Htn.Tasks.Containers.Lists.List;
Expand Down

0 comments on commit 121bf64

Please sign in to comment.