Loop through List(Integer)

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Loop through List(Integer)

Post by stefan » Mon Nov 21, 2016 2:55 pm

Hi,

I have tuples that represent bus stops including the departure times of that stop. The departure times are listed as [710, 810, 910] that represents 7:10, 8:10, 9:10. The reason for that is that I cannot use a timestamp because the time is the same for each weekday. I know that I can access these values e. g. via a MAP operator by using e. g. monday[0].

Example:

Code: Select all

'stop': 'Stopname',
'serviceLine' : '901',
'headingDirection': 'Station',
'monday': [710, 810, 910],
'tuesday': [710, 810, 910],
'wednesday': [710, 810, 910],
'thursday': [710, 810, 910],
'friday': [710, 810, 910],
'saturday': [910],
'sunday': [],
'travelTime' : 5,
'waitingTime': 0 (must be calculated),
'nextDeparture': (must be calculated),
'arrivalTime': (must be calculated),
'desiredDepartureTime': 700,
'desiredDepartureWeekday': 'monday',
'isPossible': true
Lets assume that an user wants to start at 7 am on monday. Now I want to get the value of the closest departure time in the attribute 'monday' in this case 710. Than I want to calculate the waiting time (7:10 - 7:00 = 10) and calculate the arrival time on the next bus stop (currentTime + waitingTime + travelTime = arrivalTime; 7:00 + 10 + e. g. 5 = 7:15). This value shall be stored in an attribute. I want to use this calculated time to for the next route segment (next tuple) via the StateMap operator. If there is no fitting value (see sunday), than a flag 'isPossible' should be set to false.

Does anybody have an idea or hints? The most difficult thing is to "loop" through the departure times and get the closest value. The rest should be possible by "basic calculations" in a map/statemap operator.

Thanks,
Stefan

User avatar
Michael Brand
Posts: 12
Joined: Tue Jul 29, 2014 9:56 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Michael Brand » Mon Nov 21, 2016 3:13 pm

I think, all is possible with "basic operations", but it might by not with good performance ;)
You could use the UNNEST operator to get single tuples for each departure time. On the single elements you could run a MAP operator to calculate the waiting time. Afterwards, an aggregation could calculate the minimum waiting time.
Greetings
Michael

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Mon Nov 21, 2016 4:10 pm

Hi Michael,

ok, I will check that. If it works or I am running into problems I will post again.
Is there a better way to do such calculations or should I "outsource" such calculations?

Thank you!
Stefan

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Mon Nov 21, 2016 4:41 pm

Hi,

ok, first question: Is it possible to set the attribute value in the UNNEST operator based on another attribute? In my case something like:

Code: Select all

Test := UNNEST(
	{
		attribute = desiredDepartureDay
	}, NodeRel
)
greetings,
Stefan

User avatar
Michael Brand
Posts: 12
Joined: Tue Jul 29, 2014 9:56 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Michael Brand » Mon Nov 21, 2016 5:18 pm

No, I don't think that's possible, because "attribute" requires a String. You can define a variable in Odysseus Script:

Code: Select all

#DEFINE desiredDepartureDay 'monday'

Test := UNNEST(
   {
      attribute = ${desiredDepartureDay}
   }, NodeRel
)
Maybe that helps?
Greetings
Michael

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Mon Nov 21, 2016 5:25 pm

Hmm, but I think I am not able to set this value based on a tuple attribute like:

#DEFINE desiredDepartureDay nodeRel.weekday

That should be replaced for each tuple and I dont think there is a possibility to do that like this. Hmm, ok, than I have to change my preprocessing that I just get the desiredWeekday.

Thanks,
Stefan

User avatar
Michael Brand
Posts: 12
Joined: Tue Jul 29, 2014 9:56 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Michael Brand » Mon Nov 21, 2016 5:46 pm

Maybe we can find another way, but not today. We'll discuss that issue.
Greetings
Michael

User avatar
Michael Brand
Posts: 12
Joined: Tue Jul 29, 2014 9:56 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Michael Brand » Mon Nov 21, 2016 6:26 pm

We discussed it and we have a solution, that may work.
First, the problem of using the value of an attribute as unnest-attribute is the schema. Odysseus calculates the output-schema at query-compile-time and that's difficult in such a scenario, because it depends on "desiredDepartureDay". So our suggestion aims at preprocessing:

1. Use an integer attribute for the desiredDepartureDay (0 for monday, 1 for tuesday etc.)
2. Use a MAP operator with toList to put your lists for each day into a list. The result should look like: departures:List<List<Integer>> = {{710, 810, 910}, {710, 810, 910}, ...}
3. Use a MAP operator to get the list with the index specified in desiredDepartureDay. Result: relevantDepartures = departures[desiredDepartureDay] = {710, 810, 910}
4. Use the unnest with attribute = 'relevantDepartures'
Greetings
Michael

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Mon Nov 21, 2016 8:30 pm

Great idea! I will test that.
But I run into some problems. I tried the way you described and I am sure that the problem was not fully clear:
I have several way segments and want to calculate the full time needed to drive this way (think of a journey where you have to use 2 or 3 different bus service lines).
The user defines a desired departure time - that indicates the first starting time for the full way. Now I have to get the closest departure time of stop1 (tuple 1) - i. e. the next possible departure time of stop 1. Than I have to calculate the users initial starting time + waiting time + travel time to know when the user will arrive at stop 2. This is for the second way segment (tuple 2) the "starting time". Based on this calculated starting time I can pick again the next possible departure time of bus stop 2 and calculate the values again etc.

In the way you described I get all waiting times based on the very first time (defined by the user) - I calculate the values at once. Instead I want to get the next possible departure time and min waiting time for the first tuple, calculate the arrival time for the next (second) tuple, and based on this time get the next possible departure time and min waiting in tuple 2.

Hopefully this can clarify the problem. Just let me know if something is not fully clear.

I am not sure if this is even possible with Odysseus operators. If it is possible, how? If not, please let me know.

Thanks,
Stefan

User avatar
Marco Grawunder
Posts: 272
Joined: Tue Jul 29, 2014 10:29 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Marco Grawunder » Tue Nov 22, 2016 8:42 am

Hi Stefan,

maybe this can be done with a StateMap-Operator. You can use in MAP the eif(condition, value1, value2) to check, if you use a last state as input or the initial search value from the user.

Can you give an example with a set of tuples, where you think, that would be the desired solution?


Greetings,

Marco

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest