Netlogo: How to ask something to each neighbor linked - hyperlink

I need some help. I have some turtles linked by links. Turtles have both a variable "x" and a variable "y". The latter can be true or false.
I would like to do the following: each turtle has to "inspect" the "y" variable of all the neighbors linked to it, one at a time. Then if "y" is true the (inspecting) turtle updates its "x" = x + 1, if "y" is false the (inspecting) turtle updates its "x" = x - 1.
Summaring, each turtles updates it "x" during each meeting. Thererore if a turtle has 3 links with "y" true, it "x" has to be x + 3.
Thanks

So the value of X is given by the number of linked turtles with true y - number of linked turtles with false y? If so, you want something like:
ask turtles
[ set X count link-neighbors with [Y?] - count link-neighbors with [not Y?] ]
Note that standard practice in NetLogo is to have a question mark at the end of the variable name for boolean variables and I have named the y variable accordingly.

Related

Need help explaining this formula provided to me

I recently posted on here to get help with a formula, here is the link...https://stackoverflow.com/questions/75068029/vlook-up-style-forumla-but-range-is-2-cells A user called rockinfreakshow was really awesome and provided a great solution for me. I'm not very experienced and don't understand what the formula at all but I'd love to be able to add more attributes to it. Is anyone able to help break it down for me ?
I havent tried anything here, it's totally out of my realm of understanding
=MAKEARRAY(COUNTA(B2:B),COUNTA(D1:O1),LAMBDA(r,c,IF(REGEXMATCH(LAMBDA(ax,bx,IFS(REGEXMATCH(ax,"Mixed")*REGEXMATCH(INDEX(C2:C,r),"Blend")*REGEXMATCH(INDEX(C2:C,r),"Filter"),"BLEND-"&bx&"|FILTER-"&bx,REGEXMATCH(ax,"Mixed")*NOT(REGEXMATCH(INDEX(C2:C,r),"Blend"))*REGEXMATCH(INDEX(C2:C,r),"Filter"),"ESP-"&bx&"|FILTER-"&bx,REGEXMATCH(ax,"Mixed")*NOT(REGEXMATCH(INDEX(C2:C,r),"Filter")),"BLEND-"&bx&"|ESP-"&bx,LEN(ax),SUBSTITUTE(ax&"-"&bx,"Espresso","ESP")))(regexextract(INDEX(B2:B,r),"([^\s]*?) Subscription"),IFNA(SWITCH(REGEXEXTRACT(INDEX(C2:C,r),"Small|Medium|Large"),"Small",250,"Medium",450,"Large",900),SWITCH(REGEXEXTRACT(INDEX(B2:B,r),"Medium|Large"),"Medium",225,"Large",450))),"(?i)"&INDEX(D1:O1,,c)),1,)))
see the WHY LAMBDA? part of this answer to understand the LAMBDA
the formula contains 2x LAMBDA and there are a total of 4 placeholders which translates to:
r - COUNTA(B2:B)
c - COUNTA(D1:O1)
ax - REGEXEXTRACT(INDEX(B2:B, r), "([^\s]*?) Subscription")
bx - IFNA(SWITCH(REGEXEXTRACT(INDEX(C2:C, r), "Small|Medium|Large"),
"Small", 250, "Medium", 450, "Large", 900),
SWITCH(REGEXEXTRACT(INDEX(B2:B, r), "Medium|Large"),
"Medium", 225, "Large", 450))
r counts how many items are in B column
c counts how many items are in row 1 of range D1:O1
ax extracts the word from B column that precedes the word Subscription
bx is a bit complex but essentially it extracts from C column word Small or Medium or Large and replaces it with 250, 450 or 900 respectively. then if C column does not contain one of those 3 words it checks for Medium or Large within B column and assigns 225 or 450 respectively
what we are left with is the core of the formula:
IFS( REGEXMATCH(ax, "Mixed")*
REGEXMATCH(INDEX(C2:C, r), "Blend")*
REGEXMATCH(INDEX(C2:C, r), "Filter"), "BLEND-"&bx&"|FILTER-"&bx,
___________________________________________________________________________
REGEXMATCH(ax, "Mixed")*
NOT(REGEXMATCH(INDEX(C2:C, r), "Blend"))*
REGEXMATCH(INDEX(C2:C, r), "Filter"), "ESP-"&bx&"|FILTER-"&bx,
___________________________________________________________________________
REGEXMATCH(ax, "Mixed")*
NOT(REGEXMATCH(INDEX(C2:C, r), "Filter")), "BLEND-"&bx&"|ESP-"&bx,
___________________________________________________________________________
LEN(ax), SUBSTITUTE(ax&"-"&bx, "Espresso", "ESP"))
for better visualization, the IFS formula contains only 4 elements. each of these 4 elements acts as a switch - if there is a match x we get output y. for example let's dissect the first element...
REGEXMATCH(ax, "Mixed")*
REGEXMATCH(INDEX(C2:C, r), "Blend")*
REGEXMATCH(INDEX(C2:C, r), "Filter"), "BLEND-"&bx&"|FILTER-"&bx
there are 3x REGEXMATCHes multiplied by each other. whenever there is such multiplication in array formulae it translates as AND logic gate (if there would be + it would mean OR logic gate) eg.:
1 * 1 = 1
1 * 0 = 0
0 * 1 = 0
0 * 0 = 0
REGEXMATCH outputs TRUE or FALSE so if we get 3x TRUE the whole argument is considered as TRUE (because 1 * 1 * 1 = 1) so we proceed to output our first switch
therefore if B column contains Mixed and C column contains Blend and C column contains Filter then we output Blend-000|Filter-000 where 000 stands for a specific number determined from bx placeholder/formula and also you can notice the | (which btw stands for OR logic within the regex) but in this case, it's just a unique symbol to join stuff for REGEXMATCH. which REGEXMATCH is this for you may ask? ...this one:
so the output of IFS formula is the input for most outer REGEXMATCH and we check if the IFS output matches something within D1:O1 range. IF yes then output 1 otherwise output nothing. shortened:
IF(REGEXMATCH(IFS(...), "(?i)"&INDEX(D1:O1,,c), 1, )
(?i) in regex means "case insensitive". it is there just for safety reasons because regex is by default case sensitive.
and we reached the MAKEARRAY formula that creates an array of numbers across the whole range with height r and width c where output is the result of IF eg. either 1 or empty cell

Difficulty in building a self-organized network in NetLogo

I'm new to netlogo and I'm trying to build a network where nodes have a random number that changes with each tick, and connect randomly to other nodes. Once the connection is established I need that: if the number between the two nodes is the same then the link crystallizes and changes color, otherwise it is deleted for that tick, the procedure is repeated until all nodes have the same random value.
Thanks in advance
Following the clarification in the comments...
The thing you mentioned can be done in a fairly simple way, so hopefully you can use the explanation and keywords below to browse the NetLogo Manual (and in particular the NetLogo Dictionary) and become fully familiar with what is going on.
First, let's setup a few things
globals [
; You'll probably want to declare these two variables directly
; in the Interface with a slider or something, but here I'll
; set them up from within the Code section.
number-of-turtles
level-of-randomness
]
turtles-own [
my-number
connected?
]
to setup
clear-all
reset-ticks
set-globals
create-turtles number-of-turtles [
setxy random-xcor random-ycor
set connected? false
]
end
to set-globals
set number-of-turtles 500
set level-of-randomness 1000
end
So far you have a number of turtles scattered in their environment, a couple of global variables that everyone can access, and a couple of variables belonging to each turtle to keep track of their state.
Based word for word on what you described, you could go on as:
to go.v1
; This first block of commands below is to get rid of the links that
; emerged in the previous iteration of 'go.v1', but that
; you don't want to keep because they link turtles with
; different 'my-number'. You will also need to include the
; stop condition that best suits you.
ask links with [color != green] [
die
]
; Insert here a stop condition.
ask turtles with [connected? = false] [
set my-number random level-of-randomness
]
; The 'let' command creates a local variable.
ask turtles [
let target one-of other turtles
create-link-with target
; The statement below containing "[who] ..." is how you need to
; call a link: 'who' is a built-in turtle-own variable reporting
; the ID number of the turtle, and to report a link you will need
; to give it the two IDs of the two turtles being connected.
if (my-number = [my-number] of target) [
ask link ([who] of self) ([who] of target) [
set color green
]
if (connected? = false) [
set connected? true
]
if ([connected?] of target = false) [
ask target [
set connected? true
]
]
]
]
tick
end
The code above does what you said: each turtle always creates the link with another turtle, then the condition is tested, and based on that condition (the result of which is stored as the link's color: only the good links become green) the link is kept or eliminated at the beginning of the following iteration of go.v1.
However, although you may have reasons to do it in the way above, you might just be happy with an alternative which requires less computation:
to go.v2
; Insert here a stop condition.
ask turtles with [connected? = false] [
set my-number random level-of-randomness
]
ask turtles [
let target one-of other turtles
if (my-number = [my-number] of target) [
create-link-with target [
set color green
ask both-ends [
if (connected? = false) [
set connected? true
]
]
]
]
]
tick
end
This way, turtles evaluate the potential companion before creating any link, and they only proceed to create it if my-number is the same.
That way, there is no need to create and then eliminate all the unwanted links (and the my-number condition had to be tested anyway even in go.v1).

Defining states, Q and R matrix in reinforcement learning

I am new to RL and I am referring couple of books and tutorials, yet I have a basic question and I hope to find that fundamental answer here.
the primary book referred: Sutton & Barto 2nd edition and a blog
Problem description (only Q learning approach): The agent has to reach from point A to point B and it is in a straight line, point B is static and only the initial position of Agent is always random.
-----------A(60,0)----------------------------------B(100,0)------------->
keeping it simple Agent always moves in the forward direction. B is always at X-axis position 100, which also a goal state and in first iteration A is at 60 X-axis position. So actions will be just "Go forward" and "Stop". Reward structure is to reward the agent 100 when A reaches point B and else just maintain 0, and when A crosses B it gets -500. So the goal for the Agent is to reach and stop at position B.
1)how many states would it require to go from point A to point B in this case? and how to define a Q and an R matrix for this?
2)How to add a new col and row if a new state is found?
Any help would be greatly appreciated.
Q_matrix implementation:
Q_matrix((find(List_Ego_pos_temp == current_state)) ,
possible_actions) = Q_matrix(find(List_Ego_pos_temp == current_state),possible_actions) + this.learning_rate * (Store_reward(this.Ego_pos_counter) + ...
this.discount * max(Q_matrix(find(List_Ego_pos_temp == List_Ego_pos_temp(find(current_state)+1))),possible_actions) - Q_matrix((find(List_Ego_pos_temp == current_state)) , possible_actions));
This implementation is in matlab.
List_Ego_pos_temp is a temporary list which store all the positions of the Agent.
Also, lets say there are ten states 1 to 10 and we also know that with what speed and distance the agent moves in each state to reach till state 10 and the agent always can move only sequentially which means agent can go from s1 to s2 to s3 to s4 till 10 not s1 to s4 or s10.
lets say at s8 is the goal state and Reward = 10, s10 is a terminal state and reward is -10, from s1 to s7 it receives reward of 0.
so will it be a right approach to calculate a Q table if the current state is considered as state1 and the next state is considered as state2 and in the next iteration current state as state2 and the next state as state3 and so on? will this calculate the Q table correctly as the next state is already fed and nothing is predicted?
Since you are defining the problem in this case, many of the variables are dependent on you.
You can define a minimum state (for e.g. 0) and a maximum state (for e.g. 150) and define each step as a state (so you could have 150 possible states). Then 100 will be your goal state. Then your action will be defined as +1 (move one step) and 0 (stop). Then the Q matrix will be a 150x2 matrix for all possible states and all actions. The reward will be scalar as you have defined.
You do not need to add new column and row, since you have the entire Q matrix defined.
Best of luck.

NETLOGO: Making network with exact number of links

I need to do the network when agents will be connected with links and i want it make so that there will be exact number (variable) of links going from each agent. Lets say for example that i want 3 links going from each agent to another. No more, no less. I was trying to use this code:
let num-links (links * number) / 2
while [count links < num-links ]
[
ask one-of turtles
[
let choice (min-one-of (other turtles with [not link-neighbor? myself])
[distance myself])
if choice != nobody [ create-link-with choice ]
]
]
Where "number" is the number of nodes and "links" is number of links i want to go from each agent- But this code unfortunately works so that "links" is really just an average degree of node. So if I want 3 links, i could get all agent (except for example two) with 3 links going from them, but one of them would have only 1 link and another 5 (average is 3 then). Is there some way How to do it.
And is there some way how to do it so that each "link" would be actually two directed links, one going from the node and one going to the node?
And one last question. I want to give this links a variable, but i need to do it so that sum of these variables from each agent is exactly 100 (as percents).
Any help? Thank you very much.
Here is how I create a fixed degree network for a small network (easy to understand)
to make-legal
create-turtles 100 [setxy random-xcor random-ycor]
let target-degree 5
while [ min [ count my-links ] of turtles < target-degree ]
[ ask links [die]
makeNW-Lattice target-degree
]
end
to makeNW-Lattice [DD]
ask turtles
[ let needed DD - count my-links
if needed > 0
[ let candidates other turtles with [ count my-links < DD ]
create-links-with n-of min (list needed count candidates) candidates
]
]
end
See NetLogo Efficient way to create fixed number of links for more efficient methods for larger networks.
Please ask separate questions for separate issues
UPDATE to ensure all nodes have required degree, in response to comment
Based on the following code, the basic generator does a legal network a little under 50% of the time. Therefore I simply threw the original code in a while loop and regenerated if not legal. This is not a good solution for larger networks, but is a reasonable hack.
to setup
let mins (list 0 0 0 0 0 0)
repeat 100
[ ask turtles [die]
ask links [die]
makeNW-lattice
let this-min min [ count my-links ] of turtles
set mins replace-item this-min mins (item this-min mins + 1)
]
print mins
end

How to add just one single link between two different breeds in Netlogo

I'd love some help creating links in Netlogo.
Essentially I want to add just one single link between two different breeds in Netlogo, with a defined length 'particle-length'. This link needs to be joined to the other breed that is next to it.
At the moment this code creates a volume exclusive set of turtles, with 3 breeds. One is randomly scattered, and the other two are inside a box. I think the initial arrangement of the two different breeds inside the box needs fixing as well so every purple is next to every green.
Ultimately I want 50 greens(Big) and 50 purples(Small) that are connected with one link (green to purple) with link length of particle-length 1.
Note water-number, Big-number and small-number are defined on a slider with value 50.
Netlogo Code:
breed [Bigs Big]
breed [Smalls Small]
breed [ waters water ]
to setup
clear-all
set particle-length 1.0
set-default-shape turtles "square"
;;To set up the bigs and smalls inside a central box
ask patches with [ abs (min-pxcor - pxcor) > 22
and abs (min-pxcor - pxcor) < 23 + floor (sqrt ( Big-number + Small-number ))
and abs (max-pycor - pycor) > 22
and abs (max-pycor - pycor) < 23 + floor (sqrt (Big-number + Small-number))]
[sprout-Bigs 1 [ set color green ] ]
;;Now make half of the Bigs into Smalls
ask n-of Big-number Bigs [ set breed Smalls]
ask Smalls [set color violet]
;;randomly makes volume exclusive Waters where there are no Bigs or Smalls
ask n-of water-number patches with [ not any? turtles-here]
[sprout-waters 1 [ set color blue ]]
ask Bigs with [any? Smalls-on neighbors4] [create-link-with one-of Smalls-on neighbors4]
reset-ticks
end
The code that is throwing the error is
ask n-of ( Big-number / 2 ) Bigs [create-link-with one-of Smalls-on neighbors4]
The simple reason is that for at least one of the bigs being asked there are no small on neighbors4`
Changing it to
ask n-of ( Big-number / 2 ) Bigs
[
If any? Smalls-on neighbors4
[create-link-with one-of Smalls-on neighbors4]
]
Will prevent the error but might not be the behavior you want. Alternatively
ask n-of ( Big-number / 2 ) Bigs with[any? Small-on neighbors4][create-link-with one-of Smalls-on neighbors4]

Resources