Least cost path - netlogo

I have a landscape with several habitats (i.e. polygons with different IDs). Each polygon of habitat is composed of several patches. In addition, each polygon of habitat has an associated cost. I would like to obtain the least cost path between the polygon that contains a turtle and all the polygons that are in buffer of 2 km around the polygon that contains a turtle.
In a first time, I think to use "weighted-distance-to" from NW extension. According to the example associated to this primitive, I should create a link between the polygon that contains a turtle and all the polygons that are in buffer of 2 km, then I should assign a weight value to the link. In the example, each link between two turtles is assigned to one weight value defined by user. In my case, as a link crosses different habitats, is it possible to calculate a weight value equal to cumulative costs along path towards one of polygons that are in buffer of 2 km ?
Thank you very much for your help.

Sound like you could create a cool variant of Dijkstra's shortest path algorithm.
http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
If you keep all generated paths in a TreeSet sorted on length, you pull the current shortest path, extend it with all possible polygons that have not been visited yet, and push these solutions in your TreeSet. If from a polygon you can only move to surrounding polygons at greater or equal costs than the shortest route you found so far you can drop that route. That way you expand only the shortest routes generating a breadth first search for the nearest turtle, while truncating possibilities that will never work.
good luck!

Related

Netlogo - selecting all turtles at a given distance from a special agent

I have to select and compute the nearest turtle from a fixed point, however i want the distance to be changeable, in the sense that i have a slider and i want to select the nearest turtle with that
given distance from that point.
I've tried many solution, also with the in-radius function, but it does not produce the desired output.
Can anyone help me? thanks in advance.
Enrico, I don't have enough reputation points to comment so I'll post this as an answer. Your question was two weeks ago and you may no longer need an answer.
If the discussion below correctly describes your problem, as well as the generic logic involved in it, you should have enough to be able to write the basic NetLogo model, or to get the basic control structures written and focus on the specific point where you can't figure out the NetLogo syntax to implement the desired functionality. Or perhaps you have code already in place and we just haven't seen it here.
At that point, the wonderful StackOverflow members should be able to help you answer specific questions in a shared context and understanding of what you're trying to do, what code you are using, and what happens when you try to run the model.
Is the following a correct restatement of your problem?
The Situation
The situation is a stage in a theater with multiple rows of seats. People come in one at a time and are always seated in the row nearest the stage that still has at least one empty seat. When that row is full, seating begins in the next row further away from the stage. When all rows are filled, the simulation stops. Within a row that has more than one empty seat, a seat is chosen that is maximizes some measure of collective distance from other seated people, including possibly people in other rows.
Question -- is the theater rectangular with linear rows of seats, or circular with concentric circular rows of seats around the stage?
Design Considerations
The collective-distance-metric calculation can be put into a separate routine and doesn't affect the rest of a basic NetLogo model setup.
Some possible metrics might be:
1) maximize the distance to the nearest seated person in the same row
2) maximize the distance to the nearest seated person in the entire theater
3) maximize the sum of distances to everyone seated in the same row
4) maximize the sum of distances to everyone seated in the entire theater
5) maximize the square root of the sum of squares of distance to everyone
* in the theater who smokes cigars.
Regardless of choice of metric, we will logically always do the same thing:
Pick initial seating arrangement and possibly initial seated persons
Pick the row to work on next
Loop
Pick which row to work on next, or exit if all rows are full
For each empty seat in that row
Find the agent-set of filled-seats that are relevant to the calculation
( say, filled-seats in the same row as the empty seat )
For that agent-set, compute the collective-distance-metric
Then pick the empty seat which generated the maximum collective-distance-metric
Sit there
update global statistics as desired
display statistics as desired
end-loop
NetLogo Implementation
Do what you can and come back with it here.
You might want to make people and seats two different breeds of turtles. You can layout all the seats in "setup" and then add people as you go.
Seats could own variables "row" and "occupied?" It's easy to layout circular rings of turtles using the "layout-circle" function. Maybe make unoccupied seats green circles of size 2, and change them to red when they become occupied.

Distance between two points with MapKit WITHOUT euclidean distance calculation

I have a game map that has been tiled over the world map of MapKit. I generate a path to take for the player. With this I find the 3 nearest nodes (in game cities) and select one at random then recurs this to find a 3rd node. I have some logic that means the chosen nodes at each stage aren't in any of the previous arrays to allow for a nice path and no "coming back on your self".
However, the issue I'm facing is I'm using CLLocation.distance(), this unfortunately uses an euclidean distance calculation due to the curvature of the earth. Is there any way to off set the curve as my current logic ends up in all paths slowly leaning towards the poles as the world map is just a flat image.
I've thought about translating CLLocation to a UIView between the first node and all possible second nodes, however this becomes massively intensive.
Any ideas on how to either offset the curve calulation or remove it all together?

DITMatlab: How to calculate hysteresis for experimental data set?

I got an experimental data set that looks more or less like this.
I need to determine how big the hysteresis loop is, aka if I look at two points with the same capacity (Y axis), whats the maximum distance between said points (X axis).
The issue is, data points arent located on the same Y value, aka I cant just find max X and min X for every Y and subtract them - that'd be too easy :^)
I figured I can use convex hull (convhull) to calculate the outer envelope of the set, but then I realised, it will only work for the convex part, not the concaved part, but I guess I can divide my data set into smaller subsets and find a sum of them... or something.
And then, assuming I have the data set thats only the outer outline of the data set, I need to calculate distances between left and right border (as shown here), but then again, thats just data set of X and Y, and Id need to find the point where green line crosses outer rim
So here are the questions:
Is there a matlab procedure that calculates the outer outline of data set, that works with the concaved part - kinda like convhull, but better?
Assuming I have the outline data set, is there an easy way to calculate secant line of data set, like shown on second picture??
Thanks for any advice, hope I made what I have in mind clear enough - english isnt my first language
Benji
EDIT 1: Or perhaps there is an easier (?) way to determine, which points form biggest outline? Like... group points into (duh) groups, lets say, those near 20%, 30%, 40%... and then pick two randomly (or brute force pick all possible pairs), one for top boundary, other for bot boundary, and then calculate area of polygon formed this way? Then, select set of points resulting in polygon with biggest area?
EDIT 2: Ooor I could group them like I thought I would before, and then work on only two groups at a time. Find convex hull for two groups, then for two next groups, and when Im done with all the groups, Id only need to find points common to all the group, and find a global hull :D Yeah, that might work :D

How do I optimize point-to-circle matching?

I have a table that contains a bunch of Earth coordinates (latitude/longitude) and associated radii. I also have a table containing a bunch of points that I want to match with those circles, and vice versa. Both are dynamic; that is, a new circle or a new point can be added or deleted at any time. When either is added, I want to be able to match the new circle or point with all applicable points or circles, respectively.
I currently have a PostgreSQL module containing a C function to find the distance between two points on earth given their coordinates, and it seems to work. The problem is scalability. In order for it to do its thing, the function currently has to scan the whole table and do some trigonometric calculations against each row. Both tables are indexed by latitude and longitude, but the function can't use them. It has to do its thing before we know whether the two things match. New information may be posted as often as several times a second, and checking every point every time is starting to become quite unwieldy.
I've looked at PostgreSQL's geometric types, but they seem more suited to rectangular coordinates than to points on a sphere.
How can I arrange/optimize/filter/precalculate this data to make the matching faster and lighten the load?
You haven't mentioned PostGIS - why have you ruled that out as a possibility?
http://postgis.refractions.net/documentation/manual-2.0/PostGIS_Special_Functions_Index.html#PostGIS_GeographyFunctions
Thinking out loud a bit here... you have a point (lat/long) and a radius, and you want to find all extisting point-radii combinations that may overlap? (or some thing like that...)
Seems you might be able to store a few more bits of information Along with those numbers that could help you rule out others that are nowhere close during your query... This might avoid a lot of trig operations.
Example, with point x,y and radius r, you could easily calculate a range a feasible lat/long (squarish area) that could be used to help rule it out if needless calculations against another point.
You could then store the max and min lat and long along with that point in the database. Then, before running your trig on every row, you could Filter your results to eliminate points obviously out of bounds.
If I undestand you correctly then my first idea would be to cache some data and eliminate most of the checking.
Like imagine your circle is actually a box and it has 4 sides
you could store the base coordinates of those lines much like you have lines (a mesh) on a real map. So you store east, west, north, south edge of each circle
If you get your coordinate and its outside of that box you can be sure it won't be inside the circle either since the box is bigger than the circle.
If it isn't then you have to check like you do now. But I guess you can eliminate most of the steps already.

Calculate nearest point of KML polygon for iPhone app

I have a series of nature reserves that need to be plotted, as polygon overlays, on a map using the coordinates contained within KML data. I’ve found a tutorial on the Apple website for displaying KML overlays on map instances.
The problem is that the reserves vary in size greatly - from a small pond right up to several hundred kilometers in size. As a result I can’t use the coordinates of the center point to find the nearest reserves. Instead I need to calculate the nearest point of the reserves polygon to find the nearest one. With the data in KML - how would I go about trying to achieve this?
I've only managed to find one other person ask this and no one had replied :(
Well, there are a couple different solutions depending on your needs. The higher the accuracy required, the more work required. I like Phil's meanRadius parameter idea. That would give you a rough idea of which polygon is closest and would be pretty easy to calculate. This idea works best if the polygons are "circlish". If the polygon are very irregular in shape, this idea loses it's accuracy.
From a math standpoint, here is what you want to do. Loop through all points of all polygons. Calculate the distance from those points to your current coordinate. Then just keep track of which one is closest. There is one final wrinkle. Imagine a two points making a line segment that is very long. You are located one meter away from the midpoint of the line. Well, the distance to these two points is very large, while, in fact you are very close to the polygon. You will need to calculate the distance from your coordinate to every possible line segment which you can do in a variety of manners which are outlined here:
http://www.worsleyschool.net/science/files/linepoint/distance.html
Finally, you need to ask yourself, am I in any polygons? If you're 10 meters away from a point on a polygon, but are, in fact, inside the polygon, obviously, you need to consider that. The best way to do that is to use a ray casting algorithm:
http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm