I am trying to make a program to calculate when train comes for a to b.
I have time when train leaves and the time it takes to travel to distance from a to b.
I need help a algorithm to find if that train bypasses 24 hours.
Like I got these times: Train leaves at 20:55, train ride time - 11:40.
The result should be 8:35, but how could I get it?
program troleibusai;
var xxx:integer ;
f,g:text ;
a:real;
Begin
Assign(F,'train_times');
Reset(F);
Assign(G,'results.txt');
Rewrite(G);
Read(F,left_hour);
Read(F,left_minute);
Read(F,ride_hour);
Read(F,ride_minute);
Heres the code.
Have a look at the between functions in unit dateutils,
e.g. http://www.freepascal.org/docs-html/rtl/dateutils/minutesbetween.html
Calculate left_hour * 60 + left_minute + ride_hour * 60 + ride_minute
and get it div 60 and mod 60
Related
I use OMNeT++-4.6, sumo-0.22.0 and Veins-4a2.
I am interested to calculate the speed of the vehicle when a message is received. I used getSpeed() function to do it. But the problem is that when I calculated manually the speed basing on the time and the distance (using the formula s = d / t), the value is different.
For example, at t= 55.104470531278 s and the distance d= 29.0477 m, the speed obtained by calling the function getSpeed() is s= 3.34862 m/s = 10.8 km/h.
On the other hand the one calculated manually is s= 0.52713 m/s = 1.9 km/h.
I need help to understand why the value obtained by using getSpeed() is different please.
getSpeed() returns the current speed of the vehicle (to be precise the one in the last simulation step which is by default 1s) while your calculation gives the average speed over the last ~55s (assuming your simulation started at time 0).
I run a mapreduce job on a hadoop cluster. The job's running time I saw in browser at master:8088 and master:19888 (job history server web UI) are shown below:
master:8088
master:19888
I have two questions:
Why are the elapsed times from two pictures different?
Why sometimes the Average Reduce Time is a negative number?
It looks like the Average Reduce Time is based on the times the previous tasks (shuffle/merge) took to finish and not necessarily the amount of time the reduce actually took to run.
Looking at this source code you can see the relevant calculations occurring around line 300.
if (attempt.getState() == TaskAttemptState.SUCCEEDED) {
numReduces++;
avgShuffleTime += (attempt.getShuffleFinishTime() - attempt.getLaunchTime());
avgMergeTime += attempt.getSortFinishTime() - attempt.getShuffleFinishTime();
avgReduceTime += (attempt.getFinishTime() - attempt.getSortFinishTime());
}
Followed by:
if (numReduces > 0) {
avgReduceTime = avgReduceTime / numReduces;
avgShuffleTime = avgShuffleTime / numReduces;
avgMergeTime = avgMergeTime / numReduces;
}
Looking at your numbers, they seem to be generally in-line with this approach to calculating the run times (everything converted to seconds):
Total Pre-reduce time = Map Run Time + Ave Shuffle + Ave Merge
143 = 43 + 83 + 17
Ave Reduce Time = Elapsed Time - Total Pre-reduce
-10 = 133 - 143
So looking at how long the Map, Shuffle and Merge took compared with the Elapsed we end up with a negative number close to your -8.
This is a partial answer, only for question 1!
I see a difference in "Submitted" and "Started" of 8 seconds in the second picture, while the time "Started" in the first picture is equal to the "Submitted" time of the second. I guess this covers the 8-second difference that you see as "Elapsed" time.
I am very curious for the second question as well, but it may not be a coincidence that it is also 8 seconds.
I am programming in java and I have come across a problem I could use some help with. Basically I need the user to enter how many times they expect a certain event to happen in a certain amount of times. The event takes a certain amount of time to complete as well. With all that said I need to use a random number generator to decide whether or not the event should happen based on the expected value.
Here's an example. Say the event takes 2 seconds to complete. The user says they want 100 seconds total and they expect the event to happen 25 times. Right now this is what I have. Units is the units of time and expectedLanding is how many times they would like the event to take place.
double isLandingProb = units/expectedLanding;
double isLanding = isLandingProb * random.nextDouble();
if(isLanding >= isLandingProb/2){
//do event here
}
This solution isn't working, and I'm having trouble thinking of something that would work.
Try this:
double isLandingProb = someProbability;
double isLanding = random.nextDouble();
if(isLanding <= isLandingProb){
//do event here
}
For example, if your probability is .25 (1 out of 4), and nextDouble returns a random number between 0 and 1, then your nextDouble needs to be less than (or equal to) .25 to achieve a landing.
Given an event that takes x seconds to run, but you want it to run on average once every y seconds, then it needs to execute with probability x/y. Then the expectation of the number of seconds the event is running over y seconds is x = one event.
int totalSeconds;
int totalTimes;
double eventTime;
double secondsPerEvent = 1.0d * totalSeconds / totalTimes;
if( eventTime > secondsPerEvent ) throw new Exception("Impossible to satisfy");
double eventProbability = eventTime / secondsPerEvent;
if( eventProbability < random.nextDouble() )
// do event
I have a problem when I am trying to decide if its possible to transfer from one train to another. Conditions are, that arrival (A1) time of the first train must be at least 5 min. before departure of the second train (D2). AND you cannot wait for more than 180 minutes, since you have arrived to the station, for second train to arrive (A2) (You can wait in the second train to departure arbitrarily)?
Time you have to eneter is in format: HH:MM
I did compare those times after I have converted them to minutes elapsed since midnight.
The problem is, that if you want to compare times before midnight with time after midnight, you has to change "if condition" in this cases: A1 and D2 is after midnight but A2 is before midnight, A1 and A2 are before midnight and D2 is after midnight, A1 is before and A2 and D2 is after, A1 and A2 are before midnight (but A2 is sooner) and D2 is after midnight.
In all of those cases you would have to have different condition. How to solve this?
PS: I think I should use different time format (not minutes since midnight), but how?
Thank you!
I suggest you use 24 hour time. That way if the second time is lower then you know that it rolled over to the next day.
If the second time is lower then all you would have to do is:
A1 - D1 = (D)ifference
A1 - D = time before D1 leaves (so the time between A1 and D1)
(can someone check my math? I think that is right.)
Don't judge the values based on minutes elapsed since midnight. Judge them based on their actual values. Most any programming language you would deal with would have date/time classes/functions that you can use to do simple date subtraction. If you update your question to indicate what language you are using, I am sure you will be able to get practical code examples.
As a generic UNIX/LINUX practice, for example, you might convert the value to UNIX timestamps and just subtract to get the time difference in seconds.
Can you convert to a DateTime and then do DateDiff?
Pseudocode (syntactically wrong):
If DateDiff(minutes, A1, D2) > 5 Then
If DateDiff(minutes, A1, A2) < 180 Then
Go ahead and allow the transfer
else
Look for another train (too long of a wait)
end if 'DateDiff(minutes, A1, A2) < 180
Look for another train (not enough time to transfer)
end if 'DateDiff(minutes, A1, D2) > 5
This is what I am working on for a similar problem with Excel:
( D3 + IF( (C3-D3)>0,5 ; 1 ; 0 ) ) > ( 0,010417 + C3 + IF( (D3-C3)>0,5 ; 1 ; 0 ) )
00:00 = 0 in Excel, 24:00 = 1
I want to know if D3 is more than 15 minutes after C3.
The above formula accomplishes that without having to muck about with dates.
Note the formula depends on C3 and D3 not being more than 12 hours apart, which is always true in my usage scenario so it's no issue.
Laymans terms:
you compare Time1 to (time2 + 15min) but in case there's a midnight switch you check whether the times are more than 12 hours apart and if so add 24h to either Time1 or Time2.
I haven't actually gotten it working yet but that I believe has more to do with Excel being a piece of turd.
In case I overlooked something though be sure to let me know!
I have an app that accepts integers at a variable rate every .25 to 2 seconds.
I'd like to output the data in a smoothed format for 3, 5 or 7 seconds depending on user input.
If the data always came in at the same rate, let's say every .25 seconds, then this would be easy. The variable rate is what confuses me.
Data might come in like this:
Time - Data
0.25 - 100
0.50 - 102
1.00 - 110
1.25 - 108
2.25 - 107
2.50 - 102
ect...
I'd like to display a 3 second rolling average every .25 seconds on my display.
The simplest form of doing this is to put each item into an array with a time stamp.
array.push([0.25, 100])
array.push([0.50, 102])
array.push([1.00, 110])
array.push([1.25, 108])
ect...
Then every .25 seconds I would read through the array, back to front, until I got to a time that was less than now() - rollingAverageTime. I would sum that and display it. I would then .Shift() the beginning of the array.
That seems not very efficient though. I was wondering if someone had a better way to do this.
Why don't you save the timestamp of the starting value and then accumulate the values and the number of samples until you get a timestamp that is >= startingTime + rollingAverageTime and then divide the accumulator by the number of samples taken?
EDIT:
If you want to preserve the number of samples, you can do this way:
Take the accumulator, and for each input value sum it and store the value and the timestamp in a shift register; at every cycle, you have to compare the latest sample's timestamp with the oldest timestamp in the shift register plus the smoothing time; if it's equal or more, subtract the oldest saved value from the accumulator, delete that entry from the shift register and output the accumulator, divided by the smoothing time. If you iterate you obtain a rolling average with (i think) the least amount of computation for each cycle:
a sum (to increment the accumulator)
a sum and a subtraction (to compare the timestamp)
a subtraction (from the accumulator)
a division (to calculate the average, done in a smart way can be a shift right)
For a total of about 4 algebric sums and a division (or shift)
EDIT:
For taking into account the time from the last sample as a weighting factor, you can divide the value for the ratio between this time and the averaging time, and you obtain an already weighted average, without having to divide the accumulator.
I added this part because it doesn't add computational load, so you can implement quite easy if you want to.
The answer from clabacchio has the basics right, but perhaps you need a bit more sophisticated answer.
Calculating the average:
0.25 - 100
0.50 - 102
1.00 - 110
In the above subset of the data what is the answer you want? You could use the mean of these numbers or you could do it in a weighted fashion. You could convert the data into:
0.50 - 0.25 = 0.25 ---- (100+102)/2 = 101
1.00 - 0.50 = 0.50 ---- (102+110)/2 = 106
Then you can take the weighted average of these values, weight being the time difference, and value being the average value.
The final answer = (0.25*101 + 0.5*106)/(0.25+0.5) = whatever the value is.
Now coming to "moving" averages:
You can either use previous k values or previous k seconds worth of data. In both cases you can keep two sums: weighted sum and sum of weights.
So... the worst case scenario is 4 readings per second over 7 seconds = 28 values in your array to process. That will be done in nanoseconds anyway, so not worth optimizing IMHO.