Can we have different distance for single source and destination? - google-distancematrix-api

On Monday, August 8, 2022 20:37:30(IST) and Sunday, August 14, 2022 17:02:32(IST), when we called Google Distance Matrix API for following source and destination address combination
origin_addresses: 143 River St, Jaffrey, NH 03452, USA
destination_addresses: 75 Lowell Rd, Salem, NH 03079, USA
We got different Response as following:
August 8, 2022 20:37:30- We got 101 km
August 14, 2022 17:02:32(IST) - We got 85.9 km
We don't have any extra query parameters except source an destination
API URL= Google API URL
Can we know, Is it possible to have different distance for single source and destination?

The short answer is NO. That is, if you are using Distance Matrix API. The API will only return a single route, which is the best possible route.
But it is possible to have different distance for a single origin and destination by including the alternatives parameter and setting it to true using Directions API.
I commented on your question as to why it is recommended to use Directions API if you only have a single origin and a single destination. Or watch this Youtube Video for a quick explanation about Distance Matrix API and it's difference with Directions API.
With that said, I tried the address you used on the Directions API and included the parameter alternatives and set it to true and it showed an array of three alternate routes:
Results are as follows:
Route #1
distance: 101 km
duration: 1 hour 20 mins
Route #2
distance: 78.6 km
duration: 1 hour 22 mins
Route #3
distance: 85.9 km
duration: 1 hour 20 mins
This concludes that the results you got on August 8 was the Route #1 and the one from August 14 was the Route #3.
The most probable cause of this could be the almost the same duration. although duration in hours is the same, if you look at the value on the results, route #1 had a value: 4794 while route #3 had a value: 4793. And it could be interchanged because of traffic data and other factors, given that you had two different date and time on your requests(duration in traffic could be different on different days and different times of the day).
Here's what the docs says:
"The API returns the most efficient routes when calculating directions. Travel time is the primary factor optimized, but the API may also take into account other factors such as distance, number of turns and many more when deciding which route is the most efficient."
Here's my request if you need some reference: https://maps.googleapis.com/maps/api/directions/json?units=metric&origin=143%20River%20St,%20Jaffrey,%20NH%2003452,%20USA&alternatives=true&destination=75%20Lowell%20Rd,%20Salem,%20NH%2003079,%20USA&key=YOUR_API_KEY
Just use your own API key and it should work.
I hope this helps.

Related

amCharts 4: How to handle real gaps?

I have date based Data with real gaps in it, not only the value is missing, also the date is missing. In amCharts 3, graph.connect = false, the Date based chart produced gaps.
In amCharts 4 series.connect = false only works if at least one data point follows with only the date, but without the value.
Is it some how possible to produce gaps when the whole data point is missing?
The demos with gaps always have at least on data point for the gap.
To continue with David Liang's answer, you have to put connect=false and use autoGapCount. But be careful as it won't work with XYCharts!
Taken from amcharts:
The feature (available since version 4.2.4) responsible for that is called autoGapCount, and it works like this: if the distance between two adjacent data points is bigger than baseInterval * autoGapCount and connect = false the line breaks.
The default for this setting is 1.1 which means that if the distance is at least 10% greater than the granularity of data, we consider it a gap.
Looking at the sample data above, we have daily granularity: distance between each data point is one day (24 hours). Now, since April 22nd is missing the distance between 21st and 23rd becomes two days (48 hours) which satisfies "gap" criteria (24 hours * 1.1 = 26.4 hours). Boom - a gap.
This allows fine-grained control of what to consider a gap in the data. Say we want a line to break only if there's a distance of three days between data points: we set autoGapCount = 3. Now, if there's distance of one or two days between two data points, they'll be connected, even if connect = false

A Route Assignment Program Algorithm

What im trying to do, is create a program that will assign a route for a driving test. there will be three diffrent routes, linked together at certain points. Never should there be more than one student at a point of intersection.
Best way to solve this is to schedule these interection points by time.
This isnt my only problem, i will need routes to be equally distributed to examiners.
So route 1 will be given to examiner 1
route 2 - examiner 2
route 3- examiner 3...
The Real Baumann suggested this:
Calculate collision times from start.
Route 1 has 6 points. {A,B,C,D,E,F}
Route 2 has 5 points. {A,F,G,H,I}
Route 3 has 6 points. {A,H,K,L,M,N}
Possible Collisions at: {A,F,H}
So you need to calculate the following times:
Route 1: A->F, A->A
Route 2: A->F, A->H, A->A
Route 3: A->H, A->A
From here you can calculate time differences that create a collision.
If it takes you 20 minutes to go from route 1A to Route 1F and 5
minutes to get from Route 2A to Route 2F, then you know a collision
will occur if start an appointment on Route 2 exactly 15 minutes after
you began an appointment at Route 1.
Then you would have a set of non-working collisions:
Route 1 & 2 collide at: 15, 25, 40
Route 1 & 3 collide at: 25, 30
Route 2 & 3 collide at: 30, 40, 45
This i can understand to a point. But in terms of an algorithm i dont know where to start.
IF someone could help me with some pseudo code to work off, or something to make it clearer in my own mind. it would help a lot.
You should be able to calculate collision times from start.
Route 1 has 6 points. {A,B,C,D,E,F}
Route 2 has 5 points. {A,F,G,H,I}
Route 3 has 6 points. {A,H,K,L,M,N}
Possible Collisions at: {A,F,H}
So you need to calculate the following times:
Route 1: A->F, A->A
Route 2: A->F, A->H, A->A
Route 3: A->H, A->A
From here you can calculate time differences that create a collision.
If it takes you 20 minutes to go from route 1A to Route 1F and 5 minutes to get from Route 2A to Route 2F, then you know a collision will occur if start an appointment on Route 2 exactly 15 minutes after you began an appointment at Route 1.
Then you would have a set of non-working collisions:
Route 1 & 2 collide at: 15, 25, 40
Route 1 & 3 collide at: 25, 30
Route 2 & 3 collide at: 30, 40, 45
From here you should pretty easily be able to create your schedule without collisions.
I'm supposing you're not asking for tips on writing a super-duper algorithm that can resolve hundreds of paths with thousands of intersections simultaneously. It sounds like you need something simple and serviceable, so let's aim for that.
First off, let's simplify the problem. Looking at the map, what you're really saying is something like this: If a student starts route 1 at 8am, he'll be in intersection A sometime between 8:03 and 8:05, and then in intersection B sometime between 8:07 and 8:09.
To ensure that no other students are in the intersection, you can consider Intersection A "booked" from 8:03-8:05 by the first guy, and Intersection B "booked" similarly from 8:07-8:09.
Each intersection would have its own busy/free table.
Each time you schedule a route, you book the appropriate intersections during the time you believe the student will be in them.
When looking for the earliest available time for a route, you go through the routes and consider start time X "available" for that route if the of each intersections you'd pass through on the route are available at the time you'd pass through them.

How to handle recurring times?

First off, I marked this question as language agnostic, but I'm using PHP and MySQL. It shouldn't affect the question itself very much tho.
I'm creating an application which shows times of certain shows throughout the week. Every single show is recurring (on weekly basis) and there might be shows which are airing through 2 days - eg. starting on Sunday at 23:30, ending on Monday at 00:30. I'm storing start of the show (day of the week - Monday, Tuesday... - it's never exact date; time) and duration. There are never shows that would take more than 24 hours.
My problem is with validation if newly added shows aren't overlapping some old ones. Especially if it comes to Sunday-Monday shows.
How are such recurring events usually handled on both DB side and server side?
tl;dr version with stuff I considered
My first idea was to create some custom validation algorithm, but it seemed too cumbersome and complicated. Not that I'd whine about complicated hand-made solutions, but I'm interested if there isn't something more basic that I'm missing.
Other alternative that came to mind was to change table structure to use datetime (instead of "day of week" and "time"), and use a fake fixed date range to store the data. For example all Mondays would be set to 5th Jan 1970, Sundays would use 11th Jan 1970. There would be one exception to this rule - if there would be some show which starts on Sunday and ends on Monday, it would be stored as 12th Jan 1970. This solution would allow more flexible quering of the DB than the original one, and it would also simplify queries for shows which overlap between individual weeks (since we can do the comparison directly in the query). There are some disadvantages to this solution as well (for one, using fake dates might make it confusing).
Both solutions smell of wrong algorithms to me and would love to hear some opinions from more experienced fellow developers.
Sounds like you could just store the starting minute of each show as an integer number of minutes since the start of the week (10,080 possible values).
Then a show starting at minute $a with duration $dur_a will overlap $b if and only if
(10080 + $b - $a) % 10080 < $dur_a
For example consider a show starting at 11pm Sunday and another starting at 12.30am Monday. Here $a == 10020 and $dur_a == 120 and $b == 30. (10080 + $b - $a) % 10080 == 90. This is less than $dur_a and hence the shows overlap.
This problem could be simplified by converting the data into a format that is amenable to the calculations that are required. I recommend creating a type that represents the start times as the number of minutes from Sunday at midnight. Then simple integer range comparisons could be used to find overlapping shows.
The internal representation must, of course, be hidden and abstracted. You may, at some point, want to change the representation from minutes to seconds, for example.
I would opt for a custom validation algorithm:
For each show, compute all showing intervals [start1, end1], [start2, end2], ... [startN, endN], where N is the number of recurrence of the show.
For a new show, also compute these intervals.
Now check if any of these new intervals intersect any old intervals. This is the case if the start or the end of one interval is contained in the other.

Algorithm to find the best possible available times

Here is my scenario,
I run a Massage Place which offers various type of massages. Say 30 min Massage, 45 min massage, 1 hour massage, etc. I have 50 rooms, 100 employees and 30 pieces of equipment.When a customer books a massage appointment, the appointment requires 1 room, 1 employee and 1 piece of equipment to be available.
What is a good algorithm to find available resources for 10 guests for a given day
Resources:
Room – 50
Staff – 100
Equipment – 30
Business Hours : 9AM - 6PM
Staff Hours: 9AM- 6PM
No of guests: 10
Services
5 Guests- (1 hour massages)
3 Guests - (45mins massages)
2 Guests - (1 hour massage).
They are coming around the same time. Assume there are no other appointment on that day
What is the best way to get ::
Top 10 result - Fastest search which meets all conditions gets the top 10 result set. Top ten is defined by earliest available time. 9 – 11AM is best result set. 9 – 5pm is not that good.
Exhaustive search (Find all combinations) - all sets – Every possible combination
First available met (Only return the first match) – stop after one of the conditions have been met
I would appreciate your help.
Thanks
Nick
First, it seems the number of employees, rooms, and equipment are irrelevant. It seems like you only care about which of those is the lowest number. That is your inventory. So in your case, inventory = 30.
Next, it sounds like you can service all 10 people at the same time within the first hour of business. In fact, you can service 30 people at the same time.
So, no algorithm is necessary to figure that out, it's a static solution. If you take #Mario The Spoon's advice and weight the different duration massages with their corresponding profits, then you can start optimizing when you have more than 30 customers at a time.
Looks like you are trying to solve a problem for which there are quite specialized software applications. If your problem is small enough, you could try to do a brute force approach using some looping and backtracking, but as soon as the problem becomes too big, it will take too much time to iterate through all possibilities.
If the problem starts to get big, look for more specialized software. Things to look for are "constraint based optimization" and "constraint programming".
E.g. the ECLIPSe tool is an open-source constraint programming environment. You can find some examples on http://eclipseclp.org/examples/index.html. One nice example you can find there is the SEND+MORE=MONEY problem. In this problem you have the following equation:
S E N D
+ M O R E
-----------
= M O N E Y
Replace every letter by a digit so that the sum is correct.
This also illustrates that although you can solve this brute-force, there are more intelligent ways to solve this (see http://eclipseclp.org/examples/sendmore.pl.txt).
Just an idea to find a solution:
You might want to try to solve it with a constraint satisfaction problem (CSP) algorithm. That's what some people do if they have to solve timetable problems in general (e.g. room reservation at the University).
There are several tricks to improve CSP performance like forward checking, building a DAG and then do a topological sort and so on...
Just let me know, if you need more information about CSP :)

How to implement a real estate recommendation engine?

I am talking about something like movie/item recommendation, but it seems that real estate is more tricky. When visiting a web-site and doing some search for RE, the user should be presented with some suggestions. Let's separate the task in two tasks:
a) the user has still not entered any personal info - item based recommendation
b) the user has already entered his/hers details such as income, location, etc. - item/user based recommendation
The first thing that comes to my mind for task a) is to start modeling RE features, but using some ranges instead of exact values. For example:
Area in m2
40 - 50 we can mark it for "1"
50 - 70 is "2"
etc ...
Price:
20 - 30 thousands € will be marked as 1
30 - 40 will be 2
etc ...
Proximity to city center:
1 for the RE being within the city center
2 for Zone 2 or up to 2/3 kilometers from center
3 for Zone 3 or 7 kilometers from center
So having ranges lets us assign a vector to each RE property which will allows us to use: Euclidean distance, Pearson correlation and some nearest neighbor algorithms.
Please comment on my approach or suggest a new one.
If you already have a website with enough traffic, you can try a pure collaborative filtering approach, i.e people who viewed this property also viewed these other properties. You could use the Pearson correlation there for good results.
Similarity between 2 RE can be defined as
number of people who viewed both RE1 and RE2
sim = ---------------------------------------------
number of people who viewed either 1 or both
When a user is viewing property RE you can sort all other RE properties based on the similarity score with the property being shown and show the top few.
You could add some obvious filters on top of this like the location of the property, the price range etc.
You can also define the similarity as you have suggested and mix the results from both for good representation from new RE entries which do not have a high chance of getting in if a pure collaborative filtering algorithm is used.

Resources