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

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.

Related

Grid based dungeon with random room sizes

I'm making a game, where levels are grid based.
At first, I have rectangle MxM cells. Then within this rectangle I have to put rooms. Room is another rectangle which is AxB cells, where 2 <= A, B <= 4. Besides, It's necessary to put N rooms in each row and N ones in each column. They should fill all the space, there can't be empty space between them. In other words, I have to feel rectangle with other rectangles the way that there will no be empty space between them and they will form a grid with N rows and N columns.
What I did:
I store information about rooms in form of their left-top corner, calculate it and then put rooms based on their and neighbor's corners. To do that:
Divide grid on rooms 3x3
In each of 3x3 rooms define area which is obligatory floor (2x2 square, let's call it red area)
In loop for each room count it's neighbor x and y corner position the way that it doesn't cross none of the obligatory floor ares. For that:
a. Get red area of current room and it's neighbors. Set corner somewhere between them, making sure the dimensions of the room are within range above.
b. Resolve collisions, when it's not possible to set random corner. For instance, if x position of room above isn't equal to our room, then we can't put horizontal wall between to rooms righter them in random y position, because in that case these rooms will overlap each other.
Some other stuff with converting information about corners to rooms themselves
So, what's the problem? My code with a lot of if-statements and crutches became so unreadable and huge that it almost impossible to test and find bugs. Approach I used seems to work but it's impossible to control the way it's working or not working.
Another issue is that I want to have more control on how it looks like. Grid must be interesting, which means that neighbor rooms are preferably not of the same size. There's an example (grid) of such a grid (with red areas that are gray there), which is not bad.
Is there some alternative to solve this? In other questions I saw a lot of similar solutions, but all of them doesn't assume that there's fixed amount of rows and columns.
Recommend me some articles I haven't managed to find, probably, literature devoted to this topic, or point the direction where to move and find a working solution.
A traditional method of generating grids containing rooms is to use Binary-Space-Partition trees.
One thing about that method is that it often produces grids that are less densely populated than your example. You might be able to modify some BSP example code and make the map more dense though.
Another possible approach would be to generate the rectangles first, (perhaps with a border along two edges for the gap) then try to pack them using a rectangle packing algorithm. This previous answer has several potential packing algorithms.

Matlab - Flag points using nearest neighbour search

I have the following problem and I am a bit clueless how to tackle it as my programming skills are very elementary ( I am an engineer, so please dont bite my head off).
Problem
I have a point cloud, the picture above displaying one level off it. Every point is a centroid off a block (x =5, y=1, z=5) and is specefied by carteisian coordinates.
The centroids further have two values: one called "access" and one "product". If the product value is positive and pays for the access to the point I want to include it in my outcome. The red marker in the picture represents a possible starting point.
Starting Idea
As a start I am trying to set up an algorithm, that starts at the red marker, runs through the blocks left and right (along the x-axis), checks until where it would be feasible to access (so sum "product" > sum "access") and then goes to the next point (in y direction from marker) and does the same until the end of the level.
Final Goal
My final goal is that I can Flag points as accessed and the algorithm connects profitable "products" (so products that would pay for their access) on the shortest way to the access point (by setting blocks/points on the way to accessed).
I know this is a very open question and I apologize for that. I am just lacking a good starting point programming wise. I was thinking of knnsearch, but I am not sure if this is the right way to go as the blocks have different sizes and i technically want the nearest neighbour in every direction but also only one per direction.
Another idea I had was using shortestpath or creating a travel salesman problem out of it, but I am not sure how to properly implement it.
If you have any ideas or you could offer any help I would very much appreciate it. If any more information is needed I gladly provide it.

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

Least cost path

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!

Finding users close to you while the coordinates of you and others is free to change

I have a database with the current coordinates of every online user. With a push of a button the user can update his/her coordinates to update his current location (which are then sent off to server). The app will allow you to set the radius of a circle (where the user is in the center) in which you can see the other users on a map. The users outside the circle are discarded.
What is the optimal way to find the users around you?
1) The easiest solution is to find the distance between you and every user and then see if it's less than the radius. This would place the sever under unnecessarily great load as comparison has to be made with every user in the world. In addition, how would one deal with changes in the locations?
2) An improved way would be to only calculate and compare the distance with other users who have similar latitude and longitude. Again in order to be efficient, if the radius is decreased the app should only target users with even closer coordinates. This is not as easy as it sounds. If one were to walk around the North Pole with, say, 10m radius then every step around the circumference would equal to a change of 9 degrees longitude. Every step along the equator would be marginal. Still, even being very rough and assuming there aren't many users visiting the Poles I could narrow it down to some extent.
Any ideas regarding finding users close-by and how to keep them up to date would be much appreciated! :)
Andres
Very good practice is to use GeoHash concept (http://geohash.org/) or GeoModel http://code.google.com/p/geomodel/ (better for BigTable like databases). Those are efficient ways of geospatial searches. I encourage you to read some of those at links I have provided, but in few words:
GeoHash translates lon and lat to unique hash string, than you can query database through those hashes. If points are closer to each other similar prefix will bi longer
GeoModel is similar to GegoHash with that difference that hashed are squares with set accuracy. If square is smaller the hash is longer.
Hope I have helped you. But decision, which you will pick, is yours :).
Lukasz
1) you would probably need a two step process here.
a) Assuming that all locations go into a database, you can do a compare at the sql level (very rough one) based on the lat & long, i.e. if you're looking for 100m distances you can safely disregard locations that differ by more than 0.01 degree in both directions. I don't think your North Pole users will mind ;)
Also, don't consider this unnecessary - better do it on the server than the iPhone.
b) you can then use, for the remaining entries, a comparison formula as outlined below.
2) you can find a way to calculate distances between two coordinates here http://snipplr.com/view/2531/calculate-the-distance-between-two-coordinates-latitude-longitude/
The best solution currently, in my opinion, is to wrap the whole earth in a matrix. Every cell will cover a small area and have a unique identifier. This information would be stored for every coordinate in the database and it allows me to quickly filter out irrelevant users (who are very far away). Then use Pythagoras to calculate the distance between all the other users and the client.