Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to create a map to insert elements based on event

48 views
Skip to first unread message

Shivu

unread,
Nov 17, 2016, 12:50:32 PM11/17/16
to
Hello All,

I need a help to create a map of vehicle ID, pedestrian ID and its corresponding distance from vehicle and then to find the minimum distance for a particular vehicle. Something like the following:

VehID PedID Distance
0 0 X0
0 1 X1
0 2 X2
1 0 Y0
1 1 Y1
1 2 Y2 and so on..

I get each of these entries in different time based on events, like:
Event #1 at time t1 (Pedestrian_ID:0 have distance of X0 from Vehicle_ID:0)
Event #2 at time t2 (Pedestrian_ID:1 have distance of X1 from Vehicle_ID:0)
and so on....

I want to create a map so that it can store each line of entry and keep appending next entries according to next events. Finally at the last event I should get the full map with all the entries
Then I need to find:
The pedestrian which has the minimum distance from Vehicle_ID:0
The pedestrian which has the minimum distance from Vehicle_ID:1 and so on..

I have started with this code as below:

std::map <int vehicle_ID std::map<<int pedestrian_ID, double distance>> my_Map;

But I am not able to find, how to add subsequent entries based on events generated at different time and to store them.

Thanks

Ben Bacarisse

unread,
Nov 17, 2016, 4:07:03 PM11/17/16
to
Shivu <rout....@gmail.com> writes:

> I need a help to create a map of vehicle ID, pedestrian ID and its
> corresponding distance from vehicle and then to find the minimum
> distance for a particular vehicle. Something like the following:
>
> VehID PedID Distance
> 0 0 X0
> 0 1 X1
> 0 2 X2
> 1 0 Y0
> 1 1 Y1
> 1 2 Y2 and so on..
>
> I get each of these entries in different time based on events, like:
> Event #1 at time t1 (Pedestrian_ID:0 have distance of X0 from Vehicle_ID:0)
> Event #2 at time t2 (Pedestrian_ID:1 have distance of X1 from Vehicle_ID:0)
> and so on....
>
> I want to create a map

A map looks like overkill since the IDs appear to be small consecutive
integers. You might consider a vector of vectors.

> so that it can store each line of entry and
> keep appending next entries according to next events.

There no evidence that you need to store all the events. Is there more
to this problem than you have specified?

> Finally at the
> last event I should get the full map with all the entries
> Then I need to find:
> The pedestrian which has the minimum distance from Vehicle_ID:0
> The pedestrian which has the minimum distance from Vehicle_ID:1 and so on..
>
> I have started with this code as below:
>
> std::map <int vehicle_ID std::map<<int pedestrian_ID, double distance>> my_Map;
>
> But I am not able to find, how to add subsequent entries based on
> events generated at different time and to store them.

As it stand you can't. But as it stands you don't need to. You only
only need to record a new distance for some vehicle/pedestrian pair if
it is smaller than the last recorded distance. You can do that, I
presume?

--
Ben.

Shivu

unread,
Nov 17, 2016, 5:09:22 PM11/17/16
to
Thanks Ben. I am actually new to C++. The distance is a double data type and the Vehicle and Pedestrian ID are integers. Is it possible with vectors?
Let me explain the scenario more:

Event#1 at time t1: Pedestrian_ID:0 receives a message from Vehicle_ID:0 (with Vehicle's Position from GPS Coordinates)
Then pedestrian:0 calculates its distance from vehicle:0 based on its own position (available from its GPS coordinates)

Similarly,

Event#2 at time t2: Pedestrian_ID:1 receives a message from Vehicle_ID:0
Then pedestrian:1 calculates its distance from vehicle:0

Event#3 at time t3: Pedestrian_ID:2 receives a message from Vehicle_ID:0
Then pedestrian:2 calculates its distance from vehicle:0

Event#4 at time t4: Pedestrian_ID:0 receives a message from Vehicle_ID:1
Then pedestrian:0 calculates its distance from vehicle:1

Event#5 at time t5: Pedestrian_ID:1 receives a message from Vehicle_ID:1
Then pedestrian:1 calculates its distance from vehicle:1

Event#6 at time t6: Pedestrian_ID:2 receives a message from Vehicle_ID:1
Then pedestrian:2 calculates its distance from vehicle:1

I have to find which pedestrian has the minimum distance from Vehicle:0
and which pedestrian has the minimum distance from Vehicle:1
Then I need to send(broadcast) this information to all the pedestrians.

So I need a way to store the information at each event and increment it at next event and finally based on the information, find the minimum distance for each vehicle.

Thanks

Ben Bacarisse

unread,
Nov 17, 2016, 8:41:45 PM11/17/16
to
Yes.

> Let me explain the scenario more:
>
> Event#1 at time t1: Pedestrian_ID:0 receives a message from
> Vehicle_ID:0 (with Vehicle's Position from GPS Coordinates)
> Then pedestrian:0 calculates its distance from vehicle:0 based on its
> own position (available from its GPS coordinates)
>
> Similarly,
>
> Event#2 at time t2: Pedestrian_ID:1 receives a message from Vehicle_ID:0
> Then pedestrian:1 calculates its distance from vehicle:0
>
> Event#3 at time t3: Pedestrian_ID:2 receives a message from Vehicle_ID:0
> Then pedestrian:2 calculates its distance from vehicle:0
>
> Event#4 at time t4: Pedestrian_ID:0 receives a message from Vehicle_ID:1
> Then pedestrian:0 calculates its distance from vehicle:1
>
> Event#5 at time t5: Pedestrian_ID:1 receives a message from Vehicle_ID:1
> Then pedestrian:1 calculates its distance from vehicle:1
>
> Event#6 at time t6: Pedestrian_ID:2 receives a message from Vehicle_ID:1
> Then pedestrian:2 calculates its distance from vehicle:1

The talk of time and events is confusing to me. To me, pedestrians and
vehicles are moving objects so this data may be changing over time.

But if time is not a factor, you have a very simple problem: an n_p by
n_v matrix of distances. The times (and events) at which this data is
known are not really significant, although I still don't know if you
will always have full data -- all the vehicle/pedestrian distances.

> I have to find which pedestrian has the minimum distance from Vehicle:0
> and which pedestrian has the minimum distance from Vehicle:1

You need to think about a tie as well. The minimum distance may be the
same for two or more pedestrians or vehicles. It's obvious what you'll
need to do but don't forget about it.

> Then I need to send(broadcast) this information to all the
> pedestrians.

That will need clarifying. In particular, you probably don't want to
send all the data to all the pedestrians.

> So I need a way to store the information at each event and increment
> it at next event and finally based on the information, find the
> minimum distance for each vehicle.

I'm having trouble understanding what the problem is. You say you are
new to C++, but how would you do it in whatever language you know best?
What part of C++ is the barrier to translating that code into C++?

Depending on my understanding of some of the details above, all you are
asking is how to record a distance d between vehicle v and pedestrian
p. That's not much more complex than

distance[v][p] = d;

--
Ben.

Shivu

unread,
Nov 18, 2016, 3:00:25 PM11/18/16
to
Thank you Ben. The problem I am facing is, to get the data for all the pedestrians at single point of time. I am receiving them at different time. The vehicles and pedestrians change their positions but its after a certain amount of time, lets say 5 seconds. I want to do this calculation within that time. This procedure would repeat every 5 seconds when they change their positions.
So I need a container to hold the data for each pedestrian at each event and could able to append the next entries to the container for the next pedestrians in the next events.

Thanks

Louis Krupp

unread,
Nov 18, 2016, 4:05:44 PM11/18/16
to
On Fri, 18 Nov 2016 12:00:12 -0800 (PST), Shivu <rout....@gmail.com>
wrote:
Do you really need to store all of that data?

To see if I understand what you're trying to do, let's walk through
this using pedestrians and vehicles numbered starting at 1. I know
that array indices starting at zero are natural for us programmers,
but if you're going to interface with normal people, it's better to
start at 1.

For each vehicle, store the ID of the closest pedestrian and the
distance to that pedestrian. You could use an array (call it va), and
you could initialize each element to pedestrian zero and a very large
distance:

va[i].p = 0;
va[i].d = 100000000;

for each vehicle ID i.

When you get a report that pedestrian p is distance d from vehicle v,
see if va[v].d < d. If it is, set va[v].d to d and set va[v].p to p.
If va[v].d >= d, do nothing.

When you have to do your periodic report to the pedestrians, loop
through va and tell whoever needs to know that vehicle v is distance d
from pedestrian p.

As Ben said, you should think about what to do if two pedestrians are
the same distance from a vehicle and all other pedestrians are farther
away. I don't have a really good answer to that except to say that if
this is a real-world application in which vehicles will be driven to
pick up the closest pedestrian you'd want to make sure that
pedestrians with, say, higher ID numbers don't consistently get
preempted by people with lower IDs.

If you just pick the first pedestrian to report in case of a tie, or
if you pick the one with the most recent report, I think you should
add a comment to reflect the fact that's it's a conscious decision.

I have to admit that dealing with a tie isn't something I would have
thought of.

Louis

Shivu

unread,
Nov 18, 2016, 6:03:48 PM11/18/16
to
Thank a lot Louis, I am trying this way.
A small doubt though, distance is a double data type. I did not understand the following part:

va[i].p = 0;
va[i].d = 100000000;

Is va a 2-D array? How to initialize pedestrian id and distance as elements in that array?

Louis Krupp

unread,
Nov 18, 2016, 6:56:21 PM11/18/16
to
On Fri, 18 Nov 2016 15:03:39 -0800 (PST), Shivu <rout....@gmail.com>
va is a one-dimensional array of structures. The structure has two
elements, passenger ID p and distance from vehicle to passenger d.

This might be an impertinent question, but have you had experience
with arrays, especially arrays of structures?

Louis

Geoff

unread,
Nov 25, 2016, 5:02:49 PM11/25/16
to
On Thu, 17 Nov 2016 14:09:11 -0800 (PST), Shivu <rout....@gmail.com>
wrote:
The principle upon which you may need to rely is found in RADAR
technology called the "range gate".
0 new messages