Why allowing diagonal movement would make the A* and Manhattan Distance inadmissible? - artificial-intelligence

I'm slightly confused about diagonal movement in a grid using A* and the Manhattan distance metric. Can someone explain why using diagonal movement makes it inadmissible? Wouldn't going in diagonal movement find a better optimal solution as in take less steps to get to goal state than up down left right or am I missing something?

Much as beaker's comment denotes, Manhattan Distance will over estimate the distance between a state and the states diagonally accessible to it. By definition, a heuristic that over estimates distances is not admissible.
Now, why exactly is this so?
Lets assume your Manhattan Distance procedure looks something like this:
function manhattan_dist(state):
y_dist = abs(state.y - goal.y)
x_dist = abs(state.x - goal.x)
return (y_dist + x_dist)
Now, consider the case of applying that procedure to the state of (1,1), and assume the goal is at (3,3). This will return the value of 4, which over estimates the actual distance which is 2. Therefore, Manhattan Distance in this situation will not work as an admissible heuristic.
On game boards that allow for diagonal movement Chebyshev Distance is typically used instead. Why?
Consider this new procedure:
function chebyshev dist(state):
y_dist = abs(state.y - goal.y)
x_dist = abs(state.x - goal.x)
return max(y_dist, x_dist)
Returning to the previous example of (1,1) and (3,3), this procedure will return the value of 2, which is indeed not an overestimation of the actual distance.

While this topic is older I would like to add a different solution that uses the actual fastest free path to the goal if diagonal movement is allowed.
function heuristic(state):
delta_x = abs(state.x - goal.x)
delta_y = abs(state.y - goal.y)
return min(delta_x, delta_y) * sqrt(2) + abs(delta_x - delta_y)
This method returns a heuristic that moves the maximum amount diagonally and the remainder in a straight way to the goal and presents the largest possible heuristic that does not over-estimate the movement costs to the goal.

Related

Using each element of a vector in a series of calculations

I am trying to write MATLAB code that uses a set of variables in a vector in a calculation. I am trying to run the same formula with each value in the vector and then store each result in a new vector.
The goal is to calculate and plot the cost of constructing a water tank based on various radius sizes. In the calculations I have a cylindrical tank and a hemispherical top. The exact value for the volume of the tank is 500m^3. The cost of the tank is $400/m^2 surface area for the hemispherical top and $300/m^2 surface area for the cylindrical body. I know I need to use element wise operators, however I am getting strange, unrealistic results which leads me to believe I am using these incorrectly.
rTank = 2:0.5:10;
h = ((250./(pi.*rTank(:)))-((rTank(:).^2)./3));
cost = ((2*pi*400.*(rTank(:).^2))+(2*pi*h(:).*300.*rTank(:)));
plot(rTank,cost)
I am expecting a curve of all positive values between radii 2m and 10m, with positive values for cost. For some reason I am getting negative values for results, and according to the resulting plot, the cost of the water tank is free when the radius is 8m, which makes no sense.
Filter out h<0
rTank = 2:0.5:10;
h = 250./(pi*rTank)-1/3*rTank.^2;
good_h=h(h>0);
good_rTank=rTank(h>0);
cost = 2*pi*400*good_rTank.^2 + 2*pi*good_h*300.*good_rTank;
plot(good_rTank, cost)

Informedness of distance metrics

How would you consider Manhattan distance and Chebyshev in terms of which is more informed and admissable in searching the shortest path in a grid from a to b, where only horizontal and vertical movements are allowed ?
The Manhattan distance is the sum of the distances on the two separate axes, Manhattan = |x_a - x_b| + |y_a - y_b|, whereas the Chebyshev distance is only the maximum of those two: Chebyshev = max(|x_a - x_b|, |y_a - y_b|). So, the Manhattan distance is always at least as big as the Chebyshev distance, typically bigger.
In the case where diagonal movement on a grid is not allowed, both of the distances are admissible (neither of them ever overestimates the true distance).
Given that both distance metrics are always equal to or smaller than the true distance, and the Manhattan distance is always equal to or greater than the Chebyshev distance, the Manhattan distance will always be at least as ''close to the truth''. In other words, the Manhattan distance will be more informative in this specific case.
Note that if diagonal movement is allowed, or if you're not talking about a grid, then the situation can be different.

How to efficiently evaluate or approximate a road Clothoid?

I'm facing the problem of computing values of a clothoid in C in real-time.
First I tried using the Matlab coder to obtain auto-generated C code for the quadgk-integrator for the Fresnel formulas. This essentially works great in my test scnearios. The only issue is that it runs incredibly slow (in Matlab as well as the auto-generated code).
Another option was interpolating a data-table of the unit clothoid connecting the sample points via straight lines (linear interpolation). I gave up after I found out that for only small changes in curvature (tiny steps along the clothoid) the results were obviously degrading to lines. What a surprise...
I know that circles may be plotted using a different formula but low changes in curvature are often encountered in real-world-scenarios and 30k sampling points in between the headings 0° and 360° didn't provide enough angular resolution for my problems.
Then I tried a Taylor approximation around the R = inf point hoping that there would be significant curvatures everywhere I wanted them to be. I soon realized I couldn't use more than 4 terms (power of 15) as the polynom otherwise quickly becomes unstable (probably due to numerical inaccuracies in double precision fp-computation). Thus obviously accuracy quickly degrades for large t values. And by "large t values" I'm talking about every point on the clothoid that represents a curve of more than 90° w.r.t. the zero curvature point.
For instance when evaluating a road that goes from R=150m to R=125m while making a 90° turn I'm way outside the region of valid approximation. Instead I'm in the range of 204.5° - 294.5° whereas my Taylor limit would be at around 90° of the unit clothoid.
I'm kinda done randomly trying out things now. I mean I could just try to spend time on the dozens of papers one finds on that topic. Or I could try to improve or combine some of the methods described above. Maybe there even exists an integrate function in Matlab that is compatible with the Coder and fast enough.
This problem is so fundamental it feels to me I shouldn't have that much trouble solving it. any suggetions?
about the 4 terms in Taylor series - you should be able to use much more. total theta of 2pi is certainly doable, with doubles.
you're probably calculating each term in isolation, according to the full formula, calculating full factorial and power values. that is the reason for losing precision extremely fast.
instead, calculate the terms progressively, the next one from the previous one. Find the formula for the ratio of the next term over the previous one in the series, and use it.
For increased precision, do not calculate in theta by rather in the distance, s (to not lose the precision on scaling).
your example is an extremely flat clothoid. if I made no mistake, it goes from (25/22) pi =~ 204.545° to (36/22) pi =~ 294.545° (why not include these details in your question?). Nevertheless it should be OK. Even 2 pi = 360°, the full circle (and twice that), should pose no problem.
given: r = 150 -> 125, 90 degrees turn :
r s = A^2 = 150 s = 125 (s+x)
=> 1+(x/s) = 150/125 = 1 + 25/125 x/s = 1/5
theta = s^2/2A^2 = s^2 / (300 s) = s / 300 ; = (pi/2) * (25/11) = 204.545°
theta2 = (s+x)^2/(300 s) = (6/5)^2 s / 300 ; = (pi/2) * (36/11) = 294.545°
theta2 - theta = ( 36/25 - 1 ) s / 300 == pi/2
=> s = 300 * (pi/2) * (25/11) = 1070.99749554 x = s/5 = 214.1994991
A^2 = 150 s = 150 * 300 * (pi/2) * (25/11)
a = sqrt (2 A^2) = 300 sqrt ( (pi/2) * (25/11) ) = 566.83264608
The reference point is at r = Infinity, where theta = 0.
we have x = a INT[u=0..(s/a)] cos(u^2) d(u) where a = sqrt(2 r s) and theta = (s/a)^2. write out the Taylor series for cos, and integrate it, term-by-term, to get your Taylor approximation for x as function of distance, s, along the curve, from the 0-point. that's all.
next you have to decide with what density to calculate your points along the clothoid. you can find it from a desired tolerance value above the chord, for your minimal radius of 125. these points will thus define the approximation of the curve by line segments, drawn between the consecutive points.
I am doing my thesis in the same area right now.
My approach is the following.
at each point on your clothoid, calculate the following (change in heading / distance traveled along your clothoid), by this formula you can calculate the curvature at each point by this simple equation.
you are going to plot each curvature value, your x-axis will be the distance along the clothoid, the y axis will be the curvature. By plotting this and applying very easy linear regression algorithm (search for Peuker algorithm implementation in your language of choice)
you can easily identify where are the curve sections with value of zero (Line has no curvature), or linearly increasing or decreasing (Euler spiral CCW/CW), or constant value != 0 (arc has constant curvature across all points on it).
I hope this will help you a little bit.
You can find my code on github. I implemented some algorithms for such problems like Peuker Algorithm.

How to calculate distance between 2 points in a 2D matrix

I am both new to this website and new to C. I need a program to find the average 'jumps' it takes from all points.
The idea is this: Find "jump" distance from 1 to 2, 1 to 3, 1 to 4 ... 1 to 9, or find 2 to 1, 2 to 3, 2 to 4 2 to 5 etc.
Doing them on the first row is simple, just (2-1) or (3-1) and you get the correct number. But if I want to find the distance between 1 and 4 or 1 to 8 then I have absolutely no idea.
The dimensions of the matrix should potentially be changeable. But I just want help with a 3x3 matrix.
Anyone could show me how to find it?
Jump means vertical or horizontal move from one point to another. from 1 to 2 = 1, from 1 to 9 = 4 (shortest path only)
The definition of "distance" on this kind of problems is always tricky.
Imagine that the points are marks on a field, and you can freely walk all over it. Then, you could take any path from one point to the other. The shortest route then would be a straight line; its length would be the length of the vector that joins the points, which happens to be the difference vector among two points' positions. This length can be computed with the help of Pythagora's theorem: dist = sqrt((x2-x1)^2 + (y2-y1)^2). This is known as the Euclidian distance between the points.
Now imagine that you are in a city, and each point is a building. You can't walk over a building, so the only options are to go either up/down or left/right. Then, the shortest distance is given by the sum of the components of the difference vector; which is the mathematical way of saying that "go down 2 blocks and then one block to the left" means walking 3 blocks' distance: dist = abs(x2-x1) + abs(y2-y1). This is known as the Manhattan distance between the points.
In your problem, however, it looks like the only possible move is to jump to an adjacent point, in a single step, diagonals allowed. Then the problem gets a bit trickier, because the path is very irregular. You need some Graph Theory here, very useful when modeling problems with linked elements, or "nodes". Each point would be a node, connected to their neighbors, and the problem would be to find the shortest path to another given point. If jumps had different weights (for instance, is jumping in diagonal was harder), an easy way to solve this is would be with the Dijkstra's Algorithm; more details on implementation at Wikipedia.
If the cost is always the same, then the problem is reduced to counting the number of jumps in a Breadth-First Search of the destination point from the source.
Let's define the 'jump' distance : "the number of hops required to reach from Point A [Ax,Ay] to Point B [Bx,By]."
Now there can be two ways in which the hops are allowed :
Horizontally/VerticallyIn this case, you can go up/down or left/right. As you have to travel X axis and Y axis independently, your ans is:jumpDistance = abs(Bx - Ax) + abs(By - Ay);
Horizontally/Vertically and also Diagonally
In this case, you can go up/down or left/right and diagonally as well. How it differs from Case 1 is that now you have the ability to change your X axis and Y axis together at the cost of only one jump . Your answer now is:jumpDistance = Max(abs(Bx - Ax),abs(By - Ay));
What is the definition of "jump-distance" ?
If you mean how many jumps a man needs to go from square M to N, if he can only jumps vertically and horizontally, one possibility can:
dist = abs(x2 - x1) + abs(y2 - y1);
For example jump-distance between 1 and 9 is: |3-1|+|3-1| = 4
There are two ways to calculate jump distance.
1) when only horizontal and vertical movements are allowed, in that case all you need to do is form a rectangle in between the two points and calculate the length of two adjacent side. Like if you want to move from 1 to 9 then first move from 1 to 3 and then move from 3 to 9. (Convert it to code)
2) when movements in all eight directions are allowed, things get tricky. Like if you want to move from 1 to 6 suppose. What you'll need to do is you'll have to more from 1 to 5. And then from 5 to 6. The way of doing it in code is to find the maximum in between the difference in x and y coordinates. In this example, in x coordinate, difference is 2 (3-1) and in y coordinate, difference is 1 (2-1). So the maximum of this is 2. So here's the answer. (Convert to code)

Checking Triangle Similarity in C

The problem set asks me to create two triangles, defining them using points, and then checking if they're similar.
I did first part: created a struct point and a struct triangle, as the profesor told us to. To solve the problem of checking similarity, I thought I could use the points to define vectors, and them use the law of cosines to calculate its angles, together with some if sentences to check if the triangles are similar.
Which codes could help me achieve that? I could not find anything that I'd be able to turn into a partial solution.
What you said does the trick!
For the first triangle, take some measures, like as you said: an angle (or its cosine - easy to calculate with a dot product) on any vertex and the lengths of the sides next to it.
For another triangle, use if-conditions to see if the angle (or its cosine) is the same, and if the ratios of the lengths are also the same. You'd have to do this check from all 3 vertices in this way (if at least one fits, then the triangles are similar).
A faster way would be to always start with (for instnace) the vertex with the smallest angle, then you'd need to only compare once.
Now go code it! :-)
You are given coordinates of all three points of each triangle. Let us consider two triangles T1 A(a1,a2) B(b1,b2) C(c1, c2), T2 P(p1,p2) Q(q1,q2) R(r1,r2).
a = length of opposite side of vertex A
b = length of opposite side of vertex B
c = length of opposite side of vertex C
similarly p,q,r of triangle T2
So, for the two triangles to be similar, it has to follow the following conditions
1. AB = PQ; BC = QR; CA = RP
(We don't need their directions, So I am considering only magnitudes)
2. angle (A) = angle(B) i.e angle(BAC) = angle(QPR);
angle(B) = angle(Q) i.e angle(CBA) = angle (RQP) and
angle(C) = angle(R).
Now, you got to use coordinate geometry/ spherical geometry here.
COS (A) = ( b^2 + c^2 - a^2 )/2bc
COS (B) = ( c^2 + a^2 - b^2 )/2ac
COS (C) = (a^2 + b^2 - c^2)/2ab
Note:: As cosine is periodic with 2*pi, please make sure that you have exact angle. So, why don't you think of using inverse cosine functions where you get principle angles.(I am not sure of them, as how they work. please do check)
(Similarly for P,Q,R of triangle T2).
Actually there is another rule by which its easy to do.
law: a/sin(A) = b/sin(B) = c/sin(C).
I think you have to go through Spherical Geometry
I hope this helps you to do the program.
How to do the program:
Actually, its fine if you want to use structures. Create a structure with fields of 3 sides and 3 angles. Thus you need to take two variables under structure type and compare those quantities mentioned above.
If they satisfy, they are similar triangles.
I hope this helps you.

Resources