Find turtles that are not connected to others in netlogo - hyperlink

I have a network with near of 4700 turtles, however not all of them are connected, and that is ok..... but I need to find the ones that are not connected to others (through links) and kill them, how can I do it?
I imagine something like:
""ask turtles with [no-links] [die]""
But... it doesn't work because no-links reports nothing!!... I dont know if it is the correct way (changing the with condition) or I must follow another step.
PD: all the turtles and their links are set up in a nw context.

If you take a look into the netlogo dictionairy, you will see that no-links reports an empty link agentset.
There are two reporters you can use for getting the number of links for each turtle: link-neighbors or my-links:
ask turtles with [not any? my-links] [die]
or
ask turtles with [not any? link-neighbors] [die]

Related

Best Grasshopper plugin to analyse floor plans

I'm trying to figure out the best way to analyse a grasshopper/rhino floor plan. I am trying to create a room map to determine how many doors it takes to reach an exit in a residential building. The inputs are the room curves, names and doors.
I have tried to use space syntax or SYNTACTIC, but some of the components are missing. Alot of the plugins I have been looking at are good at creating floor plans but not analysing them.
Your help would be greaty appreciated :)
You could create some sort of spine that goes through the rooms that passes only through doors, and do some path finding across the topology counting how many "hops" you need to reach the exit.
So one way to get the topology is to create a data structure (a tuple, keyValuePair) that holds the curve (room) and a point (the door), now loop each room to each other and see if the point/door of each of the rooms is closer than some threshold, if it is, store the relationship as a graph (in the abstract sense you don't really need to make lines out of it, but if you plan to use other plugins for path-finding, this can be useful), then run some path-finding (Dijkstra's, A*, etc...) to find the shortest distance.
As for SYNTACTIC: If copying the GHA after unblocking from the installation path to the special components folder (or pointing the folder from _GrasshopperDeveloperSettings) doesn't work, tick the Memory load *.GHA assemblies using COFF byte arrays option of the _GrasshopperDeveloperSettings.
*Note that SYNTACTIC won't give you any automatic topology.
If you need some pseudo-code just write a comment and I'd be happy to help.

Killing a random directed link in both direction in netlogo

Greeting,
I am very new to netlogo, and is very interested in GIS and network feature in netlogo.
Currently, I have constructed a directed graph network.
I would like to implement a function to kill a directed link path in both direction.
I have found way to kill a link in both direction if you know which are the linkage of the 2 node to kill off. Below is the code for that (by Seth Tisue in one of the question thread) :
ask a [ ask link-with b [ die ] ]
I would like to kill of a random linkage in both direction. I assume I would need to use "ask one-of links [ die ]" and another part of the code to kill of the corresponding other direction of the linkage.
I guess my question would be how the mechanism of "one-of" work so that I would be able to store this random link first, and then kill off both direction using the first code above.
As a side line, I am also puzzled by how netlogo work with variable which is quite unlike normal C++ program(my fundamental is with c++ coding). I do quite a lot of importing for my network and GIS shapefile, and currently I am still not using netlogo engine as my computational engine(a waste for me).
How do you store a simple "x=1+1" into a global variable in netlogo?
Really sorry if this question is too simple for most.
Just as a general stackoverflow convention, if you have two questions, please ask them as separate questions. This is to make stackoverflow a useful resource for programmers, so that the title and question and answer are both together and easily found.
First, how do you even know there is a link in both directions? Typically directed graphs have many pairs of nodes where there is A to B or B to A but not both. The code below tests the other direction and kills both.
I have included your example of setting variable x to the value of 1 + 1 in the setup.
I am convinced there is a better way of doing this as NetLogo code is usually less ugly, but I can't find it. The following code is a complete runnable example that creates a bunch of nodes and links and then kills a link (including the reciprocal if it exists) whenever the kill-link procedure is run.
globals [x tonode fromnode]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor]
repeat 100
[ ask one-of turtles
[ create-link-to one-of other turtles ; will not be created if already link
]
]
set x 1 + 1
end
to kill-link
ask one-of links
[ set fromnode end1
set tonode end2
die
]
ask tonode
[ if out-link-neighbor? fromnode
[ ask out-link-to fromnode [die]
]
]
end
Global variable should be declared at the beginning of program:
globals [
my-global-link; this will be my global variable
my-another-one-link; another one
]
To "store" random link use it like:
to do-something
set my-global-link one-of links
end
For local variables (inside a procedure) just use let do declare it first:
to do-something
let tmp-global-link one-of links
end

Detecting cycle in a singly linked list

Detecting cycles in a single linked list is a well known problem. I know that this question has been asked a zillion times all over the internet. The reason why I am asking it again is I thought of a solution which I did not encounter at other places. (I admit I haven't searched that deeply either).
My solution is:
Given a linked list and pointer to some node, break the link between node and node->next();
Then start at node->next() and traverse till either you hit an end (which means there was no loop) or till you reach at node which means there was a loop.
Is there anything wrong/good about above solution ?
Note: Do join the link back once you are done.
That will work to detect complete cycles (i.e., cycles with a period of the whole list), e.g.:
A -> B -> C -> D -> A
But what if we have a cycle somewhere else in the list?
e.g.,
A -> B -> C -> D -> E -> C
I can't see that your algorithm will detect the cycle in this case.
Keep in mind that to detect the first case, we need not even break the link. We could just traverse the list and keep comparing the next link for each node with the head element to see if we'd started back at the start yet (or hit the end).
I guess the most trivial approach (not necessarily the best, but one that everybody should know how to implement in Java in a few lines of code) is to build a Hash Set of the nodes, start adding them until you find one that you already saw before. Takes extra memory though.
If you can mark nodes, start marking them until you find one you marked before (the hash map is essentially an external marker).
And check the usual graph theory books...
You are not allowed to break a link, even if you join it back at the end. What if other programs read the list at the same timeĀ ?
The algorithm must not damage the list while working on it.

Find all paths between two nodes

Using a gremlin script and neo4j I try to find all paths between two nodes, descending at most 10 levels down. But all I get as response from the REST API is a
java.lang.ArrayIndexOutOfBoundsException: -1
Here is the script:
x = g.v(2)
y = g.v(6)
x.both.loop(10){!it.object.equals(y)}.paths
I looked through the documentation, but couldnt find anything relevant for this usecase.
In Gremlin the argument to loop is the number of steps back that you wish to go and the closure is evaluated to determine when to break out of the loop. In this case, because you have loop(10) it's going to go back way too far to a point where the pipeline is not defined. With the respect to the closure, you'll need to check not only if the object is the one in question, in which case you should stop, but also whether or not you've done 10 loops already.
What you really want it something like this:
x.both.loop(1){!it.object.equals(y) && it.loops < 10}.paths
However, I should add that if there is a cycle in the graph, this will gladly traverse the cycle over and over and result in far too many paths. You can apply some clever filter and sideEffect to avoid visiting nodes multiple times.
For more information see the Loop Pattern Page on the Gremlin Wiki.

Best way to detect and store path combinations for analysing purpose later

I am searching for ideas/examples on how to store path patterns from users - with the goal of analysing their behaviours and optimizing on "most used path" when we can detect them somehow.
Eg. which action do they do after what, so that we later on can check to see if certain actions are done over and over again - therefore developing a shortcut or assembling some of the actions into a combined multiaction.
My first guess would be some sort of "simple log", perhaps stored in some SQL-manner, where we can keep each action as an index and then just record everything.
Problem is that the path/action might be dynamically changed - even while logging - so we need to be able to take care of this fact too, when looking for patterns later.
Would you log everthing "bigtime" first and then POST-process every bit of details after some time or do you have great experience with other tactics?
My worry is that this is going to take up space, BIG TIME while logging 1000 users each day for a month or more.
Hope this makes sense and I am curious to see if anyone can provide sample code, pseudocode or perhaps links to something usefull.
Our tools will be C#, SQL-database, XML and .NET 3.5 - clients could also get .NET 4.0 if needed.
Patterns examples as we expect them
...
User #1001: A-B-A-A-A-B-C-E-F-G-H-A-A-A-C-B-A
User #1002: B-A-A-B-C-E-F
User #1003: F-B-B-A-E-C-A-A-A
User #1002: C-E-F
...
etc. no real way to know what they do next nor how many they will use, how often they will do it.
A secondary goal, if possible, if we later on add a new "action" called G (just sample to illustrate, there will be hundreds of actions) how could we detect these new behaviours influence on the previous patterns.
To explain it better, my thought here would be some way to detect "patterns within patterns", sort of like how compressions work, so that "repeative patterns" are spottet. We dont know how long these patterns might be, nor how often they might come. How do we break this down into "small bits and pieces" - whats the best approach you think?
I am not sure what you mean by path, but, if you gave every action in a path a unique symbol, you could reduce the problem to longest common substring or subsequence.
Or have a map of paths to the number of times that action occurred. Every time a certain path happens, increment the count for that path. Then sort to find the most common.
Pseudo idea/implementation so far
Log ever users action into a list/series of actions, bulk kinda style (textfiles/SQL - what ever, just store the whole thing for post-processing)
start counting every "1 action", "2 actions", "3 actions" up til a certain amount (lets say 30 levels)
sort them all, by giving values of importants to some of the actions (might be those producing end results)
A usefull result perhaps?
If we count all [A], [A-A], [A-B], [A-C], [A-A-A], [A-A-B] etc. its going to make a LONG and fine list of which actions are used in row frequently, and thats in the right direction, because if some of these results gets too high, we might need a shorter path. Problem is then, whats too few actions to be optimized and whats the longest needed actionlist to search for? My guess is that we need to do this counting first, then examine the numbers.
Problem is that this would be part of an analyzing tool we are developing and we dont have data until implementation, so we dont know what to look for before its actually done. hmm... wondering if there really IS an answer to this one.

Resources