How do I find find out probability of Y occurring depends on both X1 and X2 in Netlogo? Y , X1 and X2 are turtle variables - netlogo

I have specifically two questions:
How do I find out the probability of leadershipactivation depends on both leaderactivation and othersperception.
How to I find out that the probability of leaderactivation depends on motivation (condition motivation > 3.32), selfschemata (selfschemata >7) and neti (social factors, neti>0) at a particular time.
'''
globals [taskdemand ;; the taskdemand of the turtles
percent-LA;
percent-LP;
percent-LI;
probLA;
probLP;
probLI;
err;;
;percent-LIr
;percent-aLAnLp
;percent-nLALp
;percent-LALP
;percent-nLAnLP
]
turtles-own [
motivation ;;motivation to lead of each turtle
selfschemata ;; self-schemata of each turtle
status ;;status of each turtle
others;;agentset of agents within same group
;;teammates;; agentset of teammates
othersactivation;; othersactivation computes leaderactivation of each turtle within the group of turtle
allothers;;agentset of all other agents within the collective
neti;;neti computes socialfactors and taskdemand of each turtle
x;;x is prospective leader's perceived characteristics
y;;y is perceivers ILT for various dimensions
othersperception ;; leadershipperception of all other turtles in collective level
leaderactivation;;leader self-schema activation
leadershipperception;; leadershipperception of each turtle in relational level
leadershipactivation;; leadershipactivation in a collective level
leadershipactivationr;;
mygroup;; group of turtles
LA?;;
aLA?;;
aLP?;;
aLi?;;
aLir?
aLAnLp?
nLALp?
LALP?
nLAnLP?
]
to setup
clear-all; clears ticks, patches, plots
ask patches[ set pcolor white];;blank background
crt 100 [
setxy random-xcor random-ycor
set shape "person"
set motivation random-normal 3.32 0.75;;motivation to lead is normally distributed with mean 3.32 and standard deviation 0.75
set selfschemata (random 11) + 1 ;;sets selfschemata 1 to 11
set status random-normal 4 0.49 ;;status is normally distributed with mean 4 and standard deviation 0.49
set leaderactivation random 1 ;;sets leaderactivation values 0 to each turtle (self-schema activation, individual level)
set leadershipperception random 1 ;; sets leadershipperception values 0 to each turtle (relational level)
set neti random 1 ;;sets neti values 0 to each turtle, neti calculates the social factors
set othersactivation random 1 ;;sets each turtles, othersactivation (i.e., leaderactivation) value in a group to 0
set othersperception random 1;;sets othersperception values 0 to each turtle
set leadershipactivation random 1;; sets leadershipactivation values 0 to each turtle (collective level)
set leadershipactivationr random 1;;
set others no-turtles
set allothers no-turtles
if ( status > 4) OR (status = 4)[set color blue]
if ( status < 4)[set color red]
set x n-values 5 [random 1];; setting random values 0 or 1 for 5 dimensions of prospective leader's perceived characteristics
set y n-values 5[random 1];;perceiver's ILT for various dimensions
set mygroup -1
]
assign-by-size
set taskdemand [0.1 0.9]
set err random-normal 0 0.01;;setting error with mean 0 and standard deviation 0.01
reset-ticks
end
to assign-by-size
let unassigned turtles
let current 0
while [any? unassigned]
[
;; place a randomly chosen set of group-size turtles into the current
;; group. or, if there are less than group-size turtles left, place the
;; rest of the turtles in the current group.
ask n-of (min (list group-size (count unassigned))) unassigned
[ set mygroup current ]
;; consider the next group.
set current current + 1
;; remove grouped turtles from the pool of turtles to assign
set unassigned unassigned with [mygroup = -1]
]
end
to go
if ticks >= 300 [ stop ]
ask turtles [
if (mygroup != -1) [leader-activation] ;; individual level
if (mygroup != -1) [ checksocialfactors] ;; individual level
if (leaderactivation > 2) OR (leaderactivation = 2) [ set x n-values 5 [random 2]]
if (mygroup != -1) [ ilt-match];;relational level
ifelse (leaderactivation > 2) OR (leaderactivation = 2)[ set leadershipperception leadershipperception ][set leadershipperception 0]
if (mygroup != -1) [set leadershipactivationr leadershipperception + leaderactivation]
if (mygroup != -1) [collectivelevel];;collective level
if (mygroup != -1)[set leadershipactivation othersperception + leaderactivation]
]
update-variables
tick
end
to leader-activation ;; leader activation at the individual level
ifelse (motivation > 3.32) OR (motivation = 3.32) [set leaderactivation leaderactivation + 1 ][set leaderactivation leaderactivation + 0]
if (selfschemata > 0) AND (selfschemata < 5)[set leaderactivation leaderactivation + 0]
if (selfschemata > 4) AND (selfschemata < 8)[set leaderactivation leaderactivation + 0.5 ]
if (selfschemata > 7) AND (selfschemata < 12)[set leaderactivation leaderactivation + 1 ]
ifelse (status > 4) OR (status = 4) [set leaderactivation leaderactivation + 1][set leaderactivation leaderactivation - 1]
show leaderactivation
end
to checksocialfactors ;; turtle procedure to check social factors of the turtles
find-others
if any? others
[ find-others-activation
find-othersactivation-and-taskdemand
let lachange 0
let s (1 - leaderactivation) * neti
let t (leaderactivation + 0.2) * neti
ifelse (neti > 0) [set lachange lachange + (0.4 * s) - (0.1 * leaderactivation)] [set lachange lachange + (0.4 * t) - (0.1 * leaderactivation)]
set leaderactivation leaderactivation + lachange
ifelse (leaderactivation > maxleaderactivation)[ set leaderactivation maxleaderactivation][set leaderactivation leaderactivation]
]
end
to find-others ;; turtle procedure to find others in the group
set others other turtles with [mygroup = [mygroup] of myself]
end
to find-others-activation ;; turtle procedure to find others activation in a group
set othersactivation sum [leaderactivation] of others
end
to find-othersactivation-and-taskdemand
set neti othersactivation + 0.1 + err
end
to ilt-match ;; turtle procedure to match prospective leader's perceived characteristics and perceiver's ILT for various dimensions using hamming distance
find-others
find-yothers
let d 0
let xn 0
let yn 0
let x1 0
let y1 0
let n 1
while [n < 6];; there are 5 values
[ set x1 item xn x
set y1 item yn y
set n n + 1
if (x1 = y1) [ set d d + 0 ]
if (x1 > y1) [ set d d + (x1 - y1) ]
if (y1 > x1) [ set d d + (y1 - x1) ]
]
ifelse d <= 2
[ set leadershipperception leadershipperception + (5 - d)]
[ set leadershipperception leadershipperception + 0]
end
to collectivelevel
find-others
if any? others[
set othersperception sum [leadershipperception] of others
]
end
to find-yothers
if any? others[
set y n-values 5[random 2]
]
end
to update-variables
update-turtles
update-globals
end
to update-turtles
ask turtles[
if(leaderactivation > 2) [ set aLA? 1]
if(leadershipperception != 0) [ set aLP? 1]
if(leadershipactivation > 2) [set aLi? 1]
; if(leadershipactivationr > 2) [set aLir? 1]
;if(leaderactivation > 2) AND (leadershipperception = 0) AND (leadershipactivationr > 0)[ set aLAnLp? 1]
;if(leaderactivation < 2) AND (leadershipperception != 0) AND (leadershipactivationr > 0)[ set nLALp? 1]
; if(leaderactivation > 2) AND (leadershipperception != 0) AND (leadershipactivationr > 0)[ set LALP? 1]
;if(leaderactivation < 2) AND (leadershipperception = 0) AND (leadershipactivationr > 0)[ set nLAnLP? 1]
]
end
to update-globals
set percent-LA (count turtles with [ aLA? = 1]) / (count turtles) * 100
set percent-LP (count turtles with [aLP? = 1]) / (count turtles) * 100
set percent-LI (count turtles with [aLI? = 1]) / (count turtles) * 100
set percent-LIr (count turtles with [aLIr? = 1]) / (count turtles) * 100
;set percent-LAmp (count turtles with [ LA? = 1 AND motivation = 1 AND priorexp = 1]) / (count turtles) * 100
set probLA (count turtles with [aLA? = 1]) / (count turtles)
set probLP (count turtles with [aLP? = 1]) / (count turtles)
set probLI (count turtles with [aLi? = 1]) / (count turtles)
;set percent-aLAnLp (count turtles with [aLAnLp? = 1]) / (count turtles) * 100
;set percent-nLALp (count turtles with [nLALp? = 1]) / (count turtles) * 100
;set percent-LALP (count turtles with [LALP? = 1]) / (count turtles) * 100
;set percent-nLAnLP (count turtles with [nLAnLP? = 1]) / (count turtles) * 100
end
'''

Related

Rules of use of chooser in Netlogo Model

I am writing a model and want to add a button of chooser to assess alternative management options. Model was working fine as some variables graphs disappeared as I have added chooser for around all of turtles. I have also tried changing parameters from Turtle-own to global and vice versa and not rectified the error. I couldn't figure out the issus. Problem arise when i add chooser in irrigate procedure. Codes are bit longer, please bear with me. I will be grateful for the solution. Please help me to get out of difficult situation.
I have also shared before and after image of interface.
Before
and after
Thanks
Globals [farmlocations farmers-list water eva IWR-perday ASW maxgwater usedflow wdemand wtdmean wtdsd av-wdischarge wdischarge wd totannual-wdischarge current-demand wtd canal-wdischarge water-right alpha beta]
Breed [Farmers farmer]
farmers-own [ irrigation-turn season1-profit swh season2-profit yield myflow DWT total-gw-cost watercosts total-sw-cost raqba sw benefits evapotrans totaldemand swcost gwateruse wneed logging gwcost salinity aw totalcosts dws resources irrigation-turn gw AGW mywaterloss] ;swh= surface water height aw= old surface water height
Patches-own [gw-level]
links-own [flow]
to setup
clear-all
setup-farmers
import-data
reset-ticks
end
to setup-farmers
create-farmers 5 [ set irrigation-turn [0]
ask farmer 0 [ setxy min-pxcor + 1 2 set irrigation-turn 1]
ask farmer 1 [ setxy min-pxcor + 1 0 set irrigation-turn 2]
ask farmer 2 [ setxy min-pxcor + 1 4 set irrigation-turn 2]
ask farmer 3 [ setxy min-pxcor + 3 2 set irrigation-turn 3]
ask farmer 4 [ setxy min-pxcor + 3 0 set irrigation-turn 3]
]
set farmers-list sort turtles
set eva 0.3
ask farmers [set label who + 1
set size 1
set myflow 0
If land = "small farmers" [ set raqba random 5 + 3 ]
If land = "small and large farmers" [ ifelse random 3 = 1 [set raqba 10 + random 20 ] [ set raqba 5 + random 5]]
set total-sw-cost 500 * raqba
Set resources 1
set DWS 5 + 1 * (who)
set evapotrans 5 + 1 * (who)
set water-right 4.5 * raqba
Set logging 500 / DWS
Set salinity DWS / 5
set maxgwater 200
set wtdmean 10
set wtdSD 5
set sw 0
set swh 0
set aw 0
set gw-level 100 / dws
set gw 50000 + random 1000
Depth-to-Water-Table
ground-water-travel
water-losses
set alpha 1
set beta 0.7
]
set av-wdischarge 4.5
end
to Depth-to-Water-Table
set DWT Water-Table-Depth + DWS / 10
end
to ground-water-travel
set gw gw + 10 / (random-float 1 + DWT) * DWS
set AGW gw + (sum [gw-level] of patches in-radius 2)
set pcolor scale-color blue AGW 1 max [gw-level] of patches in-radius 2
end
to water-losses
Set mywaterloss waterloss + 5 * DWS ; 5 + 1* who
end
to go
tick
if ticks = 630 [ stop ]
calculate-sw
evaporate
irrigate
ask turtles [set color scale-color blue swh 30 3]
calculate-profits
update-output
end
to
calculate-sw
let list-element floor ticks / 5
ask item (ticks mod 5) farmers-list [
set canal-wdischarge item list-element totannual-wdischarge
set wdischarge canal-wdischarge - (canal-wdischarge * discharge-reduction)]
end
to evaporate
let list-element floor ticks / 5
ask item (ticks mod 5) farmers-list [
set current-demand item list-element IWR-perday
set Aw swh
if aw >= current-demand [set wneed 0]
set wneed 0
set wneed wneed + current-demand - aw
ask farmers [ set totaldemand current-demand * raqba + evapotrans]
if wneed < 0 [set wneed 0]
set swh swh - swh * eva
if swh < 0 [set swh 0]
]
end
to irrigate
If water-use-mangement = "Business as usual" [
ask turtles with [irrigation-turn = (ticks mod count farmers)]
[ set color red
set shape "person"
set pcolor brown + who
set ASW (Wdischarge - myWaterloss)* raqba
if ASW < 0 [set ASW 0]
set myflow ASW
ifelse wneed > 0 [
set swh myflow / raqba + aw
ifelse swh > current-demand [set swh current-demand ] [use-gw]
ifelse swh > aw [set usedflow (swh - aw) * raqba
if usedflow < 0 [ set usedflow 0]
set ASW ASW - usedflow
if ASW < 0 [ set ASW 0]
update-gw-use ]
[set usedflow 0]] [
set myflow 0
set swh aw + ((myflow ) / raqba)
if swh > current-demand [set swh current-demand ]]]]
end
to use-gw
Ask farmers [ let excess-demand (current-demand - swh) * raqba
if AGW > excess-demand [ ;set excess-demand AGW
set gwateruse agw - excess-demand
set dwt dwt + (0.2 + who / 20)
Ifelse DWT > 8 [ set gwcost excess-demand * 17.5] [set gwcost excess-demand * 5]
set agw agw - excess-demand
If AGW < 0 [ set AGW 0]
Ifelse excess-demand > 0.5 * current-demand [ifelse random 4 = 1 [ set salinity salinity + 0.1
set logging logging - 0.05]
[set salinity salinity + .05
set logging logging + 0.05 ]] [set salinity salinity - 0.05
set logging logging + 0.1]]]
set gw gw - 1
end
to update-gw-use
set salinity salinity + .04
set logging logging + 0.01
set gw gw - 1
set total-gw-cost total-gw-cost + gwcost
end
to calculate-profits
ask farmers [ if ticks mod 630 = 252 [ set total-sw-cost raqba * 500
set watercosts total-gw-cost + total-sw-cost
;update-yield
calculate-season1-profit
]]
ask farmers [ if ticks mod 630 = 0 [ set total-sw-cost raqba * 500
set watercosts total-gw-cost + total-sw-cost
calculate-season2-profit]]
end
to calculate-season1-profit
ask farmers [ set yield yield + ((usedflow / totaldemand ) * alpha + ( gwateruse / totaldemand)* beta) * 55 - 0.1 * (55 * salinity / 12) - 0.1 *(55 * logging / 12) ; 55mon/acre market price
let price 1350 ; price/ maund
set season1-profit yield * price - watercosts
]
end
to calculate-season2-profit
ask Farmers[ set yield yield + ((usedflow / totaldemand ) * alpha + (gwateruse / totaldemand)* beta ) * 100 - 0.1 * (100 * salinity / 18) - 0.1 * (100 * logging / 18) ; 100 mauns/acre market price
let price 2500 ; price/ maund
set season2-profit yield * price - watercosts]
end
to update-output
set-current-plot "Available-surface-water"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [swh] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [swh] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [swh] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [swh] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [swh] of turtle 4] [plot 0]
set-current-plot "Logging"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [logging] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [logging] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [logging] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [logging] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [logging] of turtle 4] [plot 0]
set-current-plot "Salinity"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [salinity] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [salinity] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [salinity] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [salinity] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [salinity] of turtle 4] [plot 0]
end
to import-data
ifelse ( file-exists? "totannual-wdischarge.txt" )
[
set totannual-wdischarge []
file-open "totannual-wdischarge.txt"
while [ not file-at-end? ]
[
set totannual-wdischarge sentence totannual-wdischarge (list file-read)
]
file-close
]
[ user-message "There is no totannual-wdischarge.txt file in current directory!" ]
;; Import water demand
ifelse ( file-exists? "IWR-perday.txt" )
[
set IWR-perday []
file-open "IWR-perday.txt"
while [ not file-at-end? ]
[
set IWR-perday sentence IWR-perday (list file-read)
]
file-close
]
[ user-message "There is no IWR-perday.txt file in current directory!" ]
end
to-report _clarify-duplicate-plot-pen-name [ name ]
let name-map [["Min" "Min"] ["MIn" "MIn_1"]]
let replacement filter [ rename -> first rename = name] name-map
let reported-name name
if not empty? replacement [
set reported-name item 1 replacement
]
report reported-name
end
The plots are working, it's just that they are showing 0. My best guess is that the text you have inside the chooser for the options is different than the text you are testing in your if statement, so the if statement is always false.
Try something like this:
ifelse water-use-mangement = "Business as usual"
[ ; all your irrigation code that you already have
]
[ print "ERROR: Management case not found"
]
This will print an error message if the text doesn't match.

Am I using n-of function incorrectly in netlogo?

I'm creating a code that is supposed to make a certain number of turtles have a status of 1 and the rest have a status of 0, the number is calculated using slider inputs from the interface. I've been trying to make the program use n-of but it turns more than the desired number of turtles into the 1 variety (the turtles are set to status 0 from before).
code below (switch for single is to change to only one turtle and tumble switch is for modelling tumbling and running):
to spawn
ifelse (single = false )
[create-turtles N [
set color black
set xcor 0
set ycor 0
set heading random-float 360
set status 0
if (tumble = true)
[
print (N * p1 / (p1 + p2))
ask n-of (N * p1 / (p1 + p2)) turtles [set status 1 set color red]]
]
]
[create-turtles 1 [
set color black
set xcor 0
set ycor 0
set heading random-float 360
pendown
]]
end
The problem is in your placement of the ask n-of statement. The code is the brackets after create-turtles N is executed for each turtle that is created, so each of the N created turtles is in turn asking n-of ... turtles to set status to 1. You will get many more turned turtles than you want. Moving the if (tumble ... block out of the create-turtles block should lead to its being executed only once, after all N turtles are created.
to spawn
ifelse (single = false ) [
create-turtles N [
set color black
set xcor 0
set ycor 0
set heading random-float 360
set status 0
]
if (tumble = true) [
print (N * p1 / (p1 + p2))
ask n-of (N * p1 / (p1 + p2)) turtles [set status 1 set color red]
]
]
[create-turtles 1 [
set color black
set xcor 0
set ycor 0
set heading random-float 360
pendown
]
]
end

Calculate index of dissimilarity in NetLogo

I'm want to calculate the index of dissimilarity in NetLogo. I have a world divided into different regions and want to examine how evenly species are distributed around the world.
Consider this example: A world is divided into 16 different regions. The world is populated with two types of ants, red and blue. It looks like this:
The world in the picture is produced with the following code:
globals[indexdissimilarity] ; where I want the index of dissimilarity to be stored.
to setup
ca
;Setting world.
resize-world 0 19 0 19
set-patch-size 15
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1 ][
while [x <= max-pxcor + 1][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 3
]
set x 5
set y y + 5
]
ask n-of (count patches * 0.85) patches[sprout 1[
set shape "bug"
set color red]]
ask n-of (count turtles * 0.50) turtles [set color blue]
dissimilarity
end
; Here is where I want to calculate the index of dissimilarity.
to dissimilarity
let tot_red (count turtles with [color = red])
let tot_blue (count turtles with [color = blue])
; set indexdissimilarity
end
My main issue is how to iterate parts of the calculations over each neighborhood.
Thanks!
I think I managed to solve it. Please, let me know if it looks correct. Here is the full updated code.
globals[indexdissimilarity
dis
]
patches-own [reg]
to setup
ca
;Setting world.
resize-world 0 19 0 19
set-patch-size 15
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1 ][
while [x <= max-pxcor + 1][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 3
]
set x 5
set y y + 5
]
ask patches [set reg [pcolor] of self]
ask n-of (count patches * 0.85) patches[sprout 1[
set shape "bug"
set color red]]
ask n-of (count turtles * 0.7) turtles [set color blue]
update
end
to update
;Dissimilarity index.
let tot_red (count turtles with [color = red])
let tot_blue (count turtles with [color = blue])
let neighb1 [reg] of turtles
let neighb remove-duplicates neighb1
set dis []
foreach neighb [i -> set dis lput abs((count turtles with [reg = i and color = red] / tot_red) - (count turtles with [reg = i and color = blue] / tot_blue)) dis]
set indexdissimilarity sum(dis) / 2
print(indexdissimilarity)
end

Netlogo: How to build connections between turtles conditional on their variables being in the same list?

I am trying to build a network model in NetLogo. Each turtle has a generation g and she has a child who belongs to generation g+1 and a parent who belongs to generation g-1. Each turtle has a variable called parent which is equal to turtle who + N , N is the number of turtles in a generation. So for example, if N = 10, turtle 5 is the parent of turtle 15 and turtle 3 is the parent of turtle 13.
I sorted the parent turtles according to their wealth level and divided this list into 10 sublist. In an N=20 example, the first sublist contains the poorest 2 parent turtle while the last one consists the richest two turtle. I want to build links between the two turtles who are in the same sublist and also between the children of these turtles. But there seems to be something wrong with my code. My code for connecting children is the following:
ask turtles with [generation = g] [
foreach pdist [
if member? parent ? [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g) and (member? parent ?)]
create-links-with n-of min (list new-links count candidates) candidates]]]]
And my code for connecting parents are:
ask turtles with [generation = g - 1] [
foreach pdist [
if member? turtle self ? [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g - 1) and (member? self ?)]
show candidates
create-links-with n-of min (list new-links count candidates) candidates]]]]
where C is the number of connections I'd ideally like to build, pdist is the lists of sublists i.e. agentsets containing parents with similar wealth levels.
I have no problem with creating plist and assigning parents to turtles correctly, if N = 20, each sublist contains 2 parents and each turtles have a candidates agentset containing one member. I don't know how to see that agent though. When I try show candidates, the output is like (turtle 24): (agentset, 1 turtle). So my first question is how to see that 1 turtle. I guess this is where the mistake is. I suspect that (member? parent ?) might be wrong part. If so, could somebody please tell me how to correctly write this condition?
I am providing my whole code below if you would like to reproduce it and see the results. The only function related to the question is update-links-with-seg, you don't need to worry about the rest, and yes I am sure that the problem has nothing to do with the rest of the code :) . I'd really appreciate a lot if somebody could tell me what is wrong. This is my term paper and I am stuck here since days and my deadline is approaching. Many thanks in advance.
turtles-own
[
wealth
wage
experience
rank
age
offers
generation
bestoffer
choiceset
from_friends
from_parent
random_arrival
child
parent
]
globals [
g
r
b
al
ah
Nmax
rmax
wealth_dist
sorted_parents
con_break_prob
pdist
]
to setup
clear-all
set rmax T
set Nmax N
set al 0.2
set ah 0.8
set b 0.1
set g 0
set con_break_prob 1
set wealth_dist []
set sorted_parents[]
set pdist []
reset-ticks
create-new-generation
update-links-no-seg
end ;;to setup
to create-new-generation
crt N
[set wealth 0 set experience 0 set rank 0 set age 0 set generation g set xcor random 30 set ycor random 30 set color red]
if g != 0 [
ask turtles with [generation = g] [set parent turtle (who - N)]
ask turtles with [generation = g - 1] [set child turtle (who + N)]]
end
to update-links-no-seg
ask links [if con_break_prob < random 1 [die]]
ask turtles [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = [generation] of myself)]
create-links-with n-of min (list new-links count candidates) candidates]]
end
to update-links-with-seg
if g != 0 [
set sorted_parents sort-on [wealth] turtles with [generation = g - 1]
set pdist []
let p 0
while [p <= 9][
let minlist (p * N) / 10
let maxlist ((p + 1) * N) / 10
let a sublist sorted_parents minlist maxlist
show a
set pdist lput a pdist
set p p + 1]
show pdist]
if C > 0 [
ifelse g = 0
[ask turtles with [generation = g][
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g)]
create-links-with n-of min (list new-links count candidates) candidates]]]
[ ask turtles with [generation = g] [
foreach pdist [
if member? parent ? [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g) and (member? parent ?)]
show candidates
create-links-with n-of min (list new-links count candidates) candidates]]]]
ask turtles with [generation = g - 1] [
foreach pdist [
if member? turtle self ? [
let new-links C - count my-links
if new-links > 0[
let candidates other turtles with [ (count my-links < C) and (generation = g - 1) and (member? self ?)]
show candidates
create-links-with n-of min (list new-links count candidates) candidates]]]]]
ask links [show end1 show end2]]
end
to go
if stop-ticking? [stop]
if any? turtles with [age = T] [
set wealth_dist []
let sorted_turtles sort-on [who] turtles with [age = T]
foreach sorted_turtles [set wealth_dist lput [wealth] of ? wealth_dist]
file-open "wealth.csv"
file-print wealth_dist
file-close
ask turtles with [age = T] [die]]
if any? turtles with [age = T / 2 ][
set g g + 1
create-new-generation]
update-links-with-seg
ask turtles [ set offers [0] set choiceset [0] set from_friends [0] set from_parent[0] set random_arrival [0]]
set r 1
while [r <= rmax] [
ask turtles [
ifelse r <= rank[
if random-float 1 < ah [set offers lput r offers]]
[if r <= experience + 1[
if random-float 1 < al [
set offers lput r offers]]]]
set r r + 1]
ask turtles with [age < T / 2] [pass-unwanted-offers-young]
ask turtles with [age >= T / 2] [pass-unwanted-offers-old]
apply-job-and-work
ask turtles [if random-float 1 < b [set rank 0 set wage 0]]
tick
end
to pass-unwanted-offers-young
foreach offers [
ifelse (? - 1 > experience) or (? <= rank)[
if any? link-neighbors with [(? - 1 <= experience) and (? > rank)] [
ask one-of link-neighbors with [(? - 1 <= experience) and (? > rank)][set from_friends lput ? from_friends]]]
[set random_arrival lput ? random_arrival]]
end
to pass-unwanted-offers-old
foreach offers [
ifelse (? - 1 > experience) or (? <= rank) [
ifelse (? - 1 <= [experience] of child) and (? > [rank] of child)
[ask child [set from_parent lput ? from_parent]]
[if any? link-neighbors with [(? - 1 <= experience) and (? > rank)] [
ask one-of link-neighbors with [(? - 1 <= experience) and (? > rank)][set from_friends lput ? from_friends]]]]
[set random_arrival lput ? random_arrival]]
end
to apply-job-and-work
ask turtles [
;if generation > 0[
;if is-turtle? parent[
;show [wealth] of parent
;show from_parent]]
;show random_arrival
;show from_friends
set choiceset []
foreach random_arrival[set choiceset lput ? choiceset]
foreach from_parent[set choiceset lput ? choiceset]
foreach from_friends[set choiceset lput ? choiceset]]
set r 1
while [r <= rmax] [
let vacancies ((Nmax / rmax) * (1 + rmax - r)) - count turtles with [rank = r]
let applicants count turtles with [member? r choiceset]
ask n-of min(list vacancies applicants) turtles with [member? r choiceset] [set rank r]
set r r + 1]
ask turtles[
set wage exp rank
if rank > experience [set experience rank]
set wealth wealth + wage
set age (age + 1)]
end
to-report stop-ticking?
ifelse g = 3 [report true] [report false]

How can I send each agent / robot to perform tasks separately on Steel's Mars explorer?

I'm doing the problem (Steel's Mars explorer). Achievement to position the clusters of rocks, obstacles, mother ship and create the 3 robots.
Also, I send my 3 robots to look for the rocks and they always turn to their mother ship, the problem I have is that when 1 of the 3 robots find a rock, all the others go back to the mother ship, and that's wrong, because it should be that every 1 go looking for stones and go turning to the mother ship.
This is my code:
globals [ ltcord ltcord2 xaa yaa xbb ybb xcc ycc candidates ]
breed [ robotsA robotA ]
breed [ obstacles obstacle ]
breed [ ships ship]
to setup
ca
set ltcord (list)
set ltcord2 (list)
set candidates (list)
valid-position
create-obstacless
create-clusters
create-ship
create-robotss
end
to go
general robotsA
end
to general [robot]
let x [xcor] of robot
let y [ycor] of robot
set x (item 0 x)
set y (item 0 y)
let bool false
ask patch x y [ set bool (pcolor = red) ]
while [ bool = false ]
[
walk 1 robot
set x [xcor] of robot
set y [ycor] of robot
set x (item 0 x)
set y (item 0 y)
ask patch x y [ set bool (pcolor = red) ]
]
ask patch x y [ set pcolor black ]
set bool false
while [ bool = false ]
[
walk 2 robot
set x [xcor] of robot
set y [ycor] of robot
set x (item 0 x)
set y (item 0 y)
if (round x = 50 and round y = 50)
[set bool true]
]
show "done"
end
to walk [op robot]
let x [xcor] of robot
let y [ycor] of robot
set x (item 0 x)
set y (item 0 y)
if op = 1
[
move-robot-to-cluster robot
]
if op = 2
[
move-robot-to-ship robot
]
ifelse is-obstacle robot = 0
[
ask robot [ fd 1 ]
]
[
ask patch (x - 1) y
[ if pcolor != blue
[ addCandidate (list (x - 1) y) ]
]
ask patch (x + 1) y
[ if pcolor != blue
[ addCandidate (list (x + 1) y) ]
]
ask patch x (y + 1)
[ if pcolor != blue
[ addCandidate (list x (y + 1)) ]
]
ask patch x (y - 1)
[ if pcolor != blue
[ addCandidate (list x (y - 1)) ]
]
let n length candidates
let rd random n
let candidato (item rd candidates)
let xi (item 0 candidato)
let yi (item 1 candidato)
ask robot
[
set xcor xi
set ycor yi
]
DeleteItemsCandidates
]
end
to-report is-obstacle [robot]
let x nobody
ask robot
[ set x min-one-of other obstacles in-radius 1 [distance myself] ]
if (x != nobody)
[ report 1 ]
report 0
end
to move-robot-to-cluster [robot]
ask robot
[
If any? Patches with[ pcolor = red]
[
set heading towards one-of patches with[ pcolor = red ]
]
]
end
to move-robot-to-ship [robot]
ask robot
[
set heading towards ship 10
]
end
to create-robotss
ask n-of 3 patches ;with [ not is-obstacle? ]
[
sprout-robotsA 1
[
set color white
set size 1
]
]
What can I improve or fix?
regards,