Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
9468 connectés 

  FORUM HardWare.fr
  Programmation
  Algo

  [Algo] Astar

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Algo] Astar

n°2545945
lady ada
Posté le 12-11-2026 à 00:18:40  profilanswer
 

Bonjour,
 
Mon implémentation avace Ada trouve une solution. Cependant la liste retournée est incomplète.
 
 

Code :
  1. procedure Astar (open : in Element_List;
  2.                close  : in out Element_List) is
  3.     
  4.     
  5.     
  6.      Openned : Node_List;    
  7.      Closed : Node_List;
  8.     
  9.      Openned_Curs : Node_Lists.Cursor;
  10.      Close_Curs : Element_Lists.Cursor;
  11.     
  12.      Done : Boolean := False;
  13.     
  14.      Initial_Node : Node_Record;
  15.      Goal : Element;
  16.      Node : Node_Record;
  17.     
  18.      begin
  19.      Goal := Element_Lists.Last_Element(close);
  20.      Close_Curs := Element_Lists.First(close);
  21.      Adjust(Initial_Node, Element_Lists.First_Element(open));    
  22.      Node_Lists.Append(Openned, Initial_Node);
  23.      Element_Lists.Delete_First(Close);          
  24.      Text_Io.Put_Line("initialized !" );
  25.     
  26.     Astar_Loop :
  27.     
  28.      while not Is_Empty(Openned) loop
  29.         
  30.         U_Sorting.Sort(Openned);
  31.         H_Sorting.Sort(Openned);
  32.         declare
  33.            Openned_Find_Curs : Node_Lists.Cursor :=
  34.          Node_Lists.First(Openned);
  35.            Best_Node : Node_Record;
  36.            F : Float := 0.0;
  37.         begin
  38.            Best_Node := Node_Lists.Element(Openned_Find_Curs);          
  39.            Node_Lists.Delete_First(Openned);          
  40.           
  41.  
  42.            declare
  43.           Best_Successors : El_Array := Successors(Best_Node.E);
  44.           
  45.            begin
  46.  
  47.           for Succ in Best_Successors ' range loop
  48.              declare
  49.             E : Element := Best_Successors(Succ);
  50.              begin
  51.             
  52.             
  53.             if Equal(E, Goal) then
  54.                Adjust(Node, E);
  55.               
  56.                Node_Lists.Append(Closed, Node);
  57.                --Adjust(Node, Goal);
  58.                Text_Io.Put_Line("Count : " & Integer'Image(Integer(Node_Lists.Length (Closed))));
  59.                --Node_Lists.Append(Closed, Node);
  60.  
  61.                Text_Io.Put_Line("Goal !" );
  62.                exit Astar_Loop;                      
  63.             else
  64.               
  65.                Adjust(Node, E);
  66.               
  67.                Node.Ucost :=  Best_Node.Ucost + heuristic(Node.E) + Best_Node.Hcost;
  68.                Text_Io.Put_Line("Ucost : " & float'Image(Node.ucost));
  69.                Node.Hcost := Best_Node.Hcost + Heuristic(Node.E) - Uniform(Node.E);
  70.                Text_Io.Put_Line("Hcost : " & float'Image(Node.Hcost));
  71.                F := Node.Hcost + Best_Node.ucost + Uniform(Node.E);
  72.               
  73.               
  74.               
  75.                Text_Io.Put_Line("F : " & Float'Image(F));
  76.               
  77.                if (Node.Hcost + (Node.Ucost) + Best_Node.ucost) < F then
  78.                   Node_Lists.Append(closed, node);
  79.               
  80.                   
  81.                   
  82.                   
  83.                end if;
  84.                if not Node_Lists.Is_Empty(openned) then
  85.                  Text_Io.Put_Line("Openned length : " & Integer'Image(Integer(Node_Lists.Length(openned))));
  86.                  declare
  87.                     Open_Curs : Node_Lists.Cursor := Node_Lists.First(Openned);
  88.                  begin
  89.                     
  90.                     declare
  91.                        Open_Node : Node_Record := Node_Lists.Element(Open_Curs);
  92.                     begin
  93.                       
  94.                        if Equal(Open_Node.E, Node.E)
  95.                       and (Open_Node.Ucost + Open_Node.Hcost) > F then
  96.                       Done := True;
  97.                        end if;
  98.                       
  99.                     end;
  100.                     
  101.                     if not Done then
  102.                        for Open in 1..Node_Lists.Length(openned) - 1 loop
  103.                       Open_Curs := Node_Lists.Next(Open_Curs);
  104.                       
  105.                       declare
  106.                          Open_Node : Node_Record := Node_Lists.Element(Open_Curs);
  107.                       begin
  108.                         
  109.                          if Equal(Open_Node.E, Node.E)
  110.                         and  (Open_Node.Ucost + Open_Node.Hcost) > F then
  111.                         Done := True;
  112.                          end if;
  113.                         
  114.                       end;
  115.                       exit when Open = Node_Lists.Length(Openned);
  116.                       
  117.                       
  118.                        end loop;
  119.                     end if;
  120.                     
  121.                     
  122.                  end;
  123.                   end if;
  124.                   if not Done then
  125.                  Node_Lists.Append(Openned, Node);
  126.  
  127.                 
  128.                   end if;
  129.                   Done := False;
  130.  
  131.                   
  132.  
  133.             end if;            
  134.              end;
  135.           end loop;
  136.           
  137.            end;
  138.         end;
  139.         
  140.         
  141.      end loop Astar_Loop;
  142.     
  143.      Openned_Finalization(close, closed);
  144.      --Element_Lists.Reverse_Elements(Close);
  145.     
  146.      end Astar;


 

1 2 0 4
 5 6 3 7
 9 10 11 8
 13 14 15 12
 1 2 3 4
 5 6 11 7
 9 10 15 8
 13 14 0 12
 1 2 3 4
 5 6 7 0
 9 10 11 8
 13 14 15 12
 1 2 3 4
 5 0 6 7
 9 10 11 8
 13 14 15 12
 1 0 3 4
 5 2 6 7
 9 10 11 8
 13 14 15 12
 1 2 3 4
 5 10 6 7
 9 14 11 8
 13 0 15 12
 1 2 3 4
 5 6 7 0
 9 10 11 8
 13 14 15 12
 1 2 3 4
 0 5 6 7
 9 10 11 8
 13 14 15 12
 1 2 3 4
 5 6 11 7
 9 10 15 8
 13 14 0 12
 1 2 4 0
 5 6 3 7
 9 10 11 8
 13 14 15 12
 1 0 2 4
 5 6 3 7
 9 10 11 8
 13 14 15 12
 1 2 3 0
 5 6 7 4
 9 10 11 8
 13 14 15 12
 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 0


 
Merci pour votre aide.


---------------
main is business suite for Ada projects. Web site : inconito.net
mood
Publicité
Posté le 12-11-2026 à 00:18:40  profilanswer
 

n°2545946
lady ada
Posté le 12-11-2026 à 01:42:37  profilanswer
 

J'en suis là  
 

Init...
 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 0
Random...
Step :  5
 1 2 3 4
 0 5 6 7
 9 10 11 8
 13 14 15 12
Init...
Astar...
initialized !
Ucost :  9.99998E+05
Hcost : -1.00000E+00
F : -1.00000E+06
Ucost :  9.99998E+05
Hcost : -1.00000E+00
F : -1.00000E+06
Openned length :  1
Ucost :  9.99998E+05
Hcost : -1.00000E+00
F : -1.00000E+06
Openned length :  2
Ucost :  9.99996E+05
Hcost : -1.00000E+00
F : -1.00000E+06
Openned length :  2
Count :  5
Goal !
Count :  5
List...
First...
 
 5
 0 2 3 4
 1 5 6 7
 9 10 11 8
 13 14 15 12
 1 2 3 4
 9 5 6 7
 13 10 11 8
 0 14 15 12
 1 2 3 4
 5 6 7 0
 9 10 11 8
 13 14 15 12
 1 2 3 0
 5 6 7 4
 9 10 11 8
 13 14 15 12
 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 0


 
Ucost et Hcost sont initialisés à 999_999.0 et je fais des soustractions
 

Code :
  1. --
  2.               Adjust(Node, E);
  3.               
  4.                Node.Ucost :=  (Best_Node.Ucost - uniform(Node.E)) + Best_Node.Hcost;
  5.                Text_Io.Put_Line("Ucost : " & float'Image(Node.ucost));
  6.                Node.Hcost := Best_Node.Hcost + Heuristic(Node.E) + Uniform(Node.E);
  7.                Text_Io.Put_Line("Hcost : " & float'Image(Node.Hcost));
  8.                F := Node.Hcost - Best_Node.ucost - Uniform(Node.E);
  9.               
  10.               
  11.               
  12.                Text_Io.Put_Line("F : " & Float'Image(F));
  13.               
  14.                if (Best_Node.Hcost - Node.ucost) + Node.hcost > F then
  15.                   Node_Lists.Append(closed, node);
  16.               
  17.                   
  18.                   
  19.                   
  20.  
  21.  
  22.                end if;


Message édité par lady ada le 12-11-2026 à 01:52:28

---------------
main is business suite for Ada projects. Web site : inconito.net
n°2545947
lady ada
Posté le 12-11-2026 à 05:28:43  profilanswer
 

Init...
 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 0
Random...
Step :  5
 1 2 3 4
 5 6 7 8
 9 0 10 11
 13 14 15 12
Init...
Astar...
initialized !
Ucost :  3.00000E+00
Hcost : -1.20000E+01
F :  1.00000E+01
Ucost :  3.00000E+00
Hcost : -1.20000E+01
F :  1.00000E+01
Openned length :  1
Ucost :  5.00000E+00
Hcost : -1.20000E+01
F :  1.40000E+01
Openned length :  2
Ucost :  6.00000E+00
Hcost : -1.20000E+01
F :  1.60000E+01
Openned length :  3
Ucost :  2.10000E+01
Hcost : -3.60000E+01
F :  5.40000E+01
Openned length :  3
Ucost :  2.10000E+01
Hcost : -3.60000E+01
F :  5.40000E+01
Openned length :  4
Count :  7
Goal !
Count :  7
List...
First...
 
 7
 1 2 3 4
 5 0 7 8
 9 6 10 11
 13 14 15 12
 1 2 3 4
 5 6 7 8
 0 9 10 11
 13 14 15 12
 1 2 3 4
 5 6 7 8
 9 14 10 11
 13 0 15 12
 1 2 3 4
 5 6 7 8
 9 10 11 0
 13 14 15 12
 1 2 3 4
 5 6 7 0
 9 10 11 8
 13 14 15 12
 1 2 3 4
 5 6 7 8
 9 10 0 11
 13 14 15 12
 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 0
 
Astar task halted.


Code :
  1. --
  2.           Adjust(Node, E);
  3.               
  4.                Node.Ucost :=  -((Best_Node.Hcost) + (Best_Node.ucost))  + Uniform(Node.E);
  5.                Text_Io.Put_Line("Ucost : " & float'Image(Node.ucost));
  6.                Node.Hcost := Heuristic(Node.E) + (Best_Node.Hcost - Node.Ucost);
  7.                Text_Io.Put_Line("Hcost : " & float'Image(Node.Hcost));
  8.                F :=  (-(Node.Hcost) + Node.Ucost + heuristic(Node.E));
  9.               
  10.               
  11.               
  12.                Text_Io.Put_Line("F : " & Float'Image(F));
  13.                if (Node.Hcost + Node.Ucost + Best_Node.Ucost) - Uniform((Best_Node.E)) > -F then
  14.                   Node_Lists.Append(closed, node);
  15.                end if;


 

Code :
  1. type Node_Record is tagged
  2.      record
  3.         E : Element;
  4.         Ucost : Float := 0.0;
  5.         Hcost  : Float := 0.0;
  6.      end record;


 

Code :
  1. function Token_Length(Usr : in Taquin_8x8) return Float is
  2.      --Count : Integer := 0;
  3.   begin
  4.      for Line in Usr.Matrix'Range(1) loop
  5.           for Col in  Usr.Matrix'Range(2) loop
  6.              if Usr.Matrix(Line, Col).Num = 0 then
  7.                 return -Float((Usr.Matrix'Last(1) - (Line) + (Usr.Matrix'Last(2) - (Col)))); --Float(Count);
  8.              end if;
  9.           end loop;
  10.      end loop;
  11.      return 1.0;
  12.   end Token_length;
  13.  
  14.  
  15.  
  16.  
  17.   function Heuristic (Usr : in Taquin_8x8) return Float is
  18.      
  19.      
  20.      
  21.      
  22.   begin
  23.      
  24.      return Token_Length(Usr) - 1.0;
  25.   end Heuristic;
  26.  
  27.   function Uniform (Usr : in Taquin_8x8) return Float is
  28.      --Count : Integer := 0;
  29.   begin
  30.      for Line in reverse Usr.Matrix'Range(1) loop
  31.           for Col in reverse Usr.Matrix'Range(2) loop
  32.              if Usr.Matrix(Line, Col).Num = 0 then
  33.                 return Float((Usr.Matrix'Last(1) + (Line) + (Usr.Matrix'Last(2) + (Col)))) + 1.0;-- Float(Count);
  34.              end if;
  35.           end loop;
  36.      end loop;
  37.      return  1.0;
  38.   end Uniform;


Message édité par lady ada le 12-11-2026 à 05:40:17

---------------
main is business suite for Ada projects. Web site : inconito.net

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Algo

  [Algo] Astar

 

Sujets relatifs
[Algo] Une idée pour mélanger le jeu du taquin ?une technique pour découvrir Astar à l'endroit ?
exercice Algo NSI en python : consigne pas claire.[Algo] Afficher la moyenne des multiple de 3 et la moyenne des nb pair
[Algo] P = NP ?petit algo, pour avis
Algo du SolitaireJeu et algo pour jouer et solver le jeu Absens
[C] Algo pour trouver un sens d'incrémentAide algo convertisseur hex/bin/dec (pseudo-code)
Plus de sujets relatifs à : [Algo] Astar


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)