I am trying to test the accuracy of a few optimization algorithms on the traveling salesman problem.
I wanted to create a system where I always knew what the optimal solution is. My logic was that I would create a bunch of random points on a unit circle and thus would always know the shortest path because it would just be the order of the points on the circle. And how would I find the order? Well, just iterate through each point and find its closest neighbor. It turns out that it works most of the time, but sometimes... It doesn't!
Are there any suggestions for an algorithm that would find the optimal solution of random points on a unit circle 100% of the time? I like being able to just randomly create points on the circle.
You can compute a 100% accurate solution using a convex hull algorithm. This solution will be exact as long as the optimal TSP path is convex, which is the case for a simple circle. The monotone chain algorithm is very interesting because it is both very fast and trivial to understand (not to mention that the implementation is also provided by Wikipedia in many languages).
Jérôme Richard's answer works for this case and for all cases involving points on the boundary of a convex polygon, but there is an even simpler algorithm that also works for all these cases: For each point, just find the angle that a line through that point and the circle centre makes with a horizontal line through the circle centre, and sort the points by that.
If the origin (0, 0) is inside your circle, and your language has the atan2() function (most do -- it's a standard trig function), you can just apply atan2() to each point and sort them by that.
Related
Hello,
in my 2d software i have two inputs available:
an array of XY points
[(x,y),(1,1),(2,2),(2,3),(-1,3),...]
and another matrix representing the closed 2D bezier curve handles
[((x,y),(x,y),(x,y)),
((-1,-1),(1,1),(1,2)),
((1,1),(2,2),(2,3)),
...]
How can i check if a point is inside or outside the given curve using python ? using preferably numpy maybe
I don't know how the theory of Bezier curves, so if your second list of points is a kind of compressed way to represent a Bezier curve, first try to sample some points of the curve with the precision you want.
So you have n points of your curve, and then you can apply a simple PIP algorithm : https://en.wikipedia.org/wiki/Point_in_polygon
I can explain in details later if you want to know how to do it programmatically.
I cant write code right here, because I need the entire program to understand properly, however I may provide two approaches how to do that.
The hardest way is to approximate each Bézier curve by a polyline. And then, according to the wiki you can use two techniques:
Ray casting algorithm: the shorthand of the algorithm: You put a ray, which starting from a point and goes through the entire polygon to an another point. Some lines lies inside a polygon, some outside. And then you check to which line belongs a specific point Looks like this:
Winding number algorithm: A little bit about winding numbers. So if a winding number is non-zero, the point lies inside the polygon
The huge drawback of this approach is that the accuracy depends on how close you approximated a curve to a polyline.
The second way is to use a bitmap. For example, you set your points to the white then render the area under the curve to the black and see if your points remain white. This method is more accurate and the fastest one, because you can use the GPU for the render.
And some links related to the first a approach:
https://pomax.github.io/bezierinfo/#intersections
http://web.mit.edu/hyperbook/Patrikalakis-Maekawa-Cho/node80.html
I'm looking for some solutions that, given a set S of circles with 2D-center points and radii, returns a minimal sub-set M in S that covers entirely a specific circle with 2d-center point and radius. This last circle is not in S.
I've chosen circles, but it doesn't matter if we change them to squares, hexagons, etc.
You have two distinct problems: you need to turn the geometric problem into a combinatoric problem, and then you need to solve the combinatoric problem. For the latter, you are looking at a minimum set cover problem, and there should be plenty of literature on that. Personally I like Knuth's Dancing Links approach to enumerate all solutions of a set cover, but I guess for a single minimal solution you can do better. A CPLEX formulation (to match your tag) would use a binary variable for each row, and a ≥1 constraint for each column.
So now about turning geometry into combinatorics. All the lines of all your circles divide the plane into a bunch of areas. The areas are delimited by lines. Of particular relevance are the points where two or more circles meet. The exact shape of the line between these points is less relevant, and you might imagine pulling those arcs straight to come up with a more classical planar graph representation. So compute all the pair-wise intersections between all your circles. Order all intersections of a single circle by angle and connect them with graph edges in that order. Do so for all circles. Then you can do a kind of bucket fill to determine for each circle which graph faces are within and which are outside.
Now you have your matrix for the set cover: every graph face which is inside the big circle is a column you need to cover. Every circle is a row and covers some of these faces, and you know which.
I am trying to find the smallest circles enclosing points using a hierarchical search (in a tree). I searched a lot and I seem to only find smallest enclosing circle (singular) algorithms online. This is for a university class so I am asking for possible solutions and ideas more than actual code.
My problem is that I have a formula that involves two constants and the radius of a circle to compute its cost and I need to minimise the total cost. This means that for a set of points (x,y), I could find one circle enclosing all points, or multiples circles, each enclosing a part of the points, depending on the cost of each circle.
As an example, if the formulae is 1+2*radius**2, my answer will surely have multiple small circles. All points must be in a circle at the end.
My goal is to use a graph search algorithm like a*, branch and bound or breadth first and build a tree using a state and its possible actions.
I am currently trying to write my possible actions as adding a circle, removing a circle and change a circle's radius. To limit compute time, I decided to only try those actions on positions that are between two points or between two sets of points (where the center of my circles could be). But this algorithm seems to be far from optimal. If you have any ideas, it would really help me.
Thanks anyway for your help.
If the question is unclear, please tell me.
I'm going to focus on finding optimal solutions. You have a lot more options if you're open to approximate solutions, and I'm sure there will be other answers.
I would approach this problem by formulating it as an integer program. Abstractly, the program looks like
variable x_C: 1 if circle C is chosen; 0 if circle C is not chosen
minimize sum_C cost(C) * x_C
subject to
for all points p, sum_{C containing p} x_C >= 1
for all circles C, x_C in {0, 1}.
Now, there are of course infinitely many circles, but assuming that one circle that contains strictly more area than another costs more, there are O(n^3) circles that can reasonably be chosen, where n is the number of points. These are the degenerate circles covering exactly one point; the circles with two points forming a diameter; and the circles that pass through three points. You'll write code to expand the abstract integer program into a concrete one in a format accepted by an integer program solver (e.g., GLPK) and then run the solver.
The size of the integer program is O(n^4), which is prohibitively expensive for your larger instances. To get the cost down, you'll want to do column generation. This is where you'll need to figure out your solver's programmatic interface. You'll be looking for an option that, when solving the linear relaxation of the integer program, calls your code back with the current price of each point and expects an optional circle whose cost is less than the sum of the prices of the points that it encloses.
The naive algorithm to generate columns is still O(n^4), but if you switch to a sweep algorithm, the cost will be O(n^3 log n). Given a pair of points, imagine all of the circles passing by those points. All of the circle centers lie on the perpendicular bisector. For each other point, there is an interval of centers for which the circle encloses this point. Compute all of these event points, sort them, and then process the events in order, updating the current total price of the enclosed points as you go. (Since the circles are closed, process arrivals before departures.)
If you want to push this even further, look into branch and price. The high-level branching variables would be the decision to cover two points with the same circle or not.
I have a set of points in a plane where each point has an associated altitude. I'm thinking of using the scipy.spatial library to compute the Delaunay triangulation of the point set and then use the result to interpolate for the points in between.
The library implements a nice function that, given a point, finds the triangle it lies in. This would be particularly useful when calculating the depth map from the mesh. I assume though (please do correct me if I'm wrong) that the search function searches from the same starting point every time it is called. Since the points I will be looking for will tend to lie either on the triangle the previous one lied on or on an adjacent one, I figure that's unneccessary, but can't seem to find a way to optimize the search, other than to implement it myself.
Is there a way to set the initial triangle for the search, or to optimize the depth map calculation otherwise?
You can try point in location test, especially Kirkpatrick algorithm/data structure. Basically you subdivide the mesh in both axis and re-triangulate it. A better and simpler solution is to give each triangle a color and draw a bitmap then check the color of the bitmap with the point.
I'd like to make a convex hull of a set of 2D points (in python). I've found several examples which have helped, but I have an extra feature I'd like that I haven't been able to implement. What I want to do is create the convex hull but allow it to pick up interior points if they are sufficiently "close" to the boundary. See the picture below -> if theta < x degrees, then that interior point gets added to the hull.
Obvious this can make things a bit more complex, as I've found out from my thoughts and tests. For example, if an interior point gets added, then it could potentially allow another further interior point to be added.
Speed is not really a concern here as the number of points I'll be working with will be relatively small. I'd rather have a more robust algorithm then a quick one.
I'm wondering if anyone knows of any such example or could point me in the right direction of where to start. Thanks.
Concave hull might be what you're looking for, though it doesn't use an angle as far as I know. The algorithm that the LOCAL project uses seems to use k nearest neighbours.
You could first compute the convex hull, and then ruin round the edges of that seeing if any of the edges should be broken to include an interior point.
The notion you are looking for may be alpha-shape. Tuning alpha, you admit more or less points in your concave hull. Look at Edelsbrunner algorithm for alpha-shape finding.