From: Anh Vo <anhvofrcaus@gmail.com>
Subject: Re: CONSTRAINT ERROR: erroneous memory access
Date: Sun, 7 Jun 2020 08:53:59 -0700 (PDT)
Date: 2020-06-07T08:53:59-07:00 [thread overview]
Message-ID: <7ba8ec59-de9c-4d8a-96ad-1d5c85d3995bo@googlegroups.com> (raw)
In-Reply-To: <d0ff83ad-a216-4fd6-b0ff-ef51a7c27d48o@googlegroups.com>
On Saturday, June 6, 2020 at 4:40:04 PM UTC-7, jcu...@gmail.com wrote:
> It has been a few years since I have written Ada; I used and taught Ada 95 back when I was working for a defense contractor. But, now that I'm retired, I wanted to get up to speed with Ada 2012, so I decided to implement a main program to see how the Shortest_Paths generic package works (taken from A.18.31, pages 574-576). But, when I read in the data and call the Shortest_Path function, it returns with CONSTRAINT ERROR: erroneous memory access. I've tried multiple times, but can't seem to figure out what I'm doing (or NOT doing) wrong.
>
> Here's the main program:
>
> with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
> with Ada.Float_Text_IO; use Ada.Float_Text_IO;
> with Ada.Text_IO; use Ada.Text_IO;
> with Shortest_Paths;
> with Ada.Command_Line; use Ada.Command_Line;
> with DirectedEdge; use DirectedEdge;
> with Ada.Containers; use Ada.Containers;
> with Ada.Exceptions;
>
> procedure Main_Test is
>
> Input_File : Ada.Text_IO.File_Type;
>
> Vertices : Integer; -- Number of nodes
> Edges : Integer; -- Number of paths
>
> Tail : Integer; -- Node From
> Head : Integer; -- Node To
> Weight : Float; -- Path Weight/Distance/Cost
>
> -- Instantiate Shortest Paths package with 0..Integer'Last subtype
> package SP is new Shortest_Paths(Node => Natural);
>
> -- Use Edge'Read to read the Edge components into Item
>
> -- Display directed edge components
> procedure Display(Edge : in SP.Edge) is
> begin
> Put(Edge.From, Width=>1); Put("->");
> Put(Edge.To, Width=>1); Put(" ");
> Put(Float(Edge.Length), Fore => 0, Aft => 2, Exp => 0); Put(" ");
> end Display;
>
> -- Display directed edge components at cursor
> -- Replace List'Write with Display
> procedure Display(Cursor: in SP.Adjacency_Lists.Cursor)
> is
> Edge : SP.Edge := SP.Adjacency_Lists.Element(Cursor);
> begin
> Display(Edge); -- Let other procedure do all the work
> end Display;
>
> begin
>
> -- Open input file using arg 1
> Open (File => Input_File,
> Mode => In_File,
> Name => Argument(1)); -- ../tinyEWD.txt
>
> Set_Input(Input_File); -- Redirect input
> New_Line;
> Put("Processing '"); Put(Argument(1)); Put_Line("'");
>
> -- Read number of nodes (vertices)
> Get(Vertices); New_Line;
> Put("Vertices: ");Put(Vertices, width=>2);New_Line;
>
> -- Read number of paths (edges)
> Get(Edges);
> Put("Edges: ");Put(Edges, Width=>2);New_Line(2);
>
> declare
>
> -- Constrain Vertex to zero-based subrange
> subtype Vertex is Natural range 0..Vertices-1;
>
> -- Adj is DLL of Adjacency Lists for each Vertex
> Adj : array (Vertex) of SP.Adjacency_Lists.List;
>
> -- Edge is a record of Tail, Head, and Weight components
> Edge : SP.Edge;
>
> begin
>
> Put_Line("Creating Adjacency Lists"); New_Line;
>
> -- For each node, create empty list of adjacent nodes
> Create_Empty_Adjacency_Lists: for Node in Vertex loop
>
> -- Initialize each adjacency list to empty
> Adj(Node) := SP.Adjacency_Lists.Empty_List;
>
> end loop Create_Empty_Adjacency_Lists;
>
> -- Display and append new edge to adjacency list for node
> -- Constrain Edge index to natural subrange
> Append_New_Edge: for E in 0..Edges-1 loop
>
> Put("Edge: ");Put(E, Width=>2);Put(" ");
>
> -- Get edge components from data file
> Get(Tail); -- Tail
> Get(Head); -- Head
> Get(Weight); -- Distance/Weight
>
> -- Set edge components
> Edge.From := Tail;
> Edge.To := Head;
> Edge.Length := SP.Distance(Weight);
>
> -- Display Edge
> Display(Edge);
> Put(" Appended to edge ");Put(Tail,1);
> New_Line;
>
> -- Append new edge to From adjacency list
> -- Indicating path to another node
> Adj(Edge.From).Append(Edge);
>
> end loop Append_New_Edge;
> New_Line;
>
> Close(Input_File);
> Set_Input(Standard_Input); -- Reset input source
>
> Put_Line("Node Adjacency Lists");
>
> -- Display contents of each adjacency list
> Display_Adjacency_Lists: for Node in Vertex loop
>
> Put("Adj[");Put(Node, Width=>1);Put("] ");
>
> declare
> Edges : Ada.Containers.Count_Type;
> begin
>
> -- How many edges are in this node?
> Edges := SP.Adjacency_Lists.Length(Adj(Node));
> Put(Integer(Edges), Width => 1);
> if (Edges > 1) then
> Put(" Edges: ");
> else
> Put(" Edge: ");
> end if;
>
> -- Iterate over all nodes in this adjacency list
> -- and Display each edge for each node
> SP.Adjacency_Lists.Iterate(Adj(Node), Process => Display'Access);
>
> end;
> New_Line;
>
> end loop Display_Adjacency_Lists;
> New_Line;
>
> -- Create Edge-Weighted Graph of Node and Adjacency List
> declare
> EWG : SP.Graphs.Vector; -- Edge Weighted Graphs
> Path : SP.Paths.List; -- Shortest Path
> begin
>
> Put_Line("Creating Edge-Weighted Graphs.");
> EWG := SP.Graphs.Empty_Vector;
> Put_Line("EWG Empty_Vector created.");
>
> Put("Initializing Shortest Path to empty list...");
> Path := SP.Paths.Empty_List;
> Put_Line("done.");
>
> for Vertex in 0..Vertices-1 loop
> Put("Vertex: ");Put(Vertex, Width => 1);
> EWG.Append(Adj(Vertex));
> Put_Line(" appended to EWG.");
> end loop;
> New_Line;
>
> Put("EWG.Length = "); Put(Integer(EWG.Length), Width => 1);New_Line;
>
> Put_Line("Finding shortest path from node 0 to node 6");
> declare
> use Ada.Exceptions;
> begin
> -- Compute Shortest Path
> Path := SP.Shortest_Path
> (EWG, Source => 0, Target => 6);
> exception
> when Error: others =>
> New_Line;
> Put_Line(Exception_Name(Error));
> Put_Line(Exception_Message(Error));
> New_Line;
> Return;
> end;
>
> Put("The Shortest Path from Node 0 to Node 6 ");
> Put("contains "); Put(Integer(Path.Length)); Put(" entries.");
> -- Display path
> end;
>
>
> end;
>
> end Main_Test;
>
> And here's the test data (taken from Algorithms, by Sedwick):
>
> 8
> 15
> 4 5 0.35
> 5 4 0.35
> 4 7 0.37
> 5 7 0.28
> 7 5 0.28
> 5 1 0.32
> 0 4 0.38
> 0 2 0.26
> 7 3 0.39
> 1 3 0.29
> 2 7 0.34
> 6 2 0.40
> 3 6 0.52
> 6 0 0.58
> 6 4 0.93
It is hard to comment without Ada units Shortest_Path and DirectedEdge posted. In other word, your code does not compile.
Anh Vo
next prev parent reply other threads:[~2020-06-07 15:53 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-06 23:40 CONSTRAINT ERROR: erroneous memory access jcupak
2020-06-07 8:00 ` Egil H H
2020-06-09 0:39 ` CONSTRAINT ERROR: erroneous memory access - SOLVED John Cupak
2020-06-09 10:07 ` AdaMagica
2020-06-07 8:51 ` CONSTRAINT ERROR: erroneous memory access Jeffrey R. Carter
2020-06-07 10:20 ` Simon Wright
2020-06-07 14:47 ` Jeffrey R. Carter
2020-06-07 15:53 ` Anh Vo [this message]
2020-06-07 16:17 ` Egil H H
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox