I've got some turtles which are looking around themselves. For each neighbor they've got, they save the value of "output-heat". The patch with the highest value will get the highest probability and the lowest value the lowest probability. I want the turtle to move to another patch. The moving should be dependent on the probabilities.
My code looks like this, but it doesn't work like it should:
ask turtles-here[
let temp_ahead [(output-heat + 1)^ Freedom] of patch-at 0 1
let temp_right_ahead [(output-heat + 1)^ Freedom] of patch-at 1 1
let temp_right [(output-heat + 1)^ Freedom] of patch-at 1 0
let temp_right_back [(output-heat + 1)^ Freedom] of patch-at 1 -1
let temp_back [(output-heat + 1)^ Freedom] of patch-at 0 -1
let temp_left_back [(output-heat + 1)^ Freedom] of patch-at -1 -1
let temp_left [(output-heat + 1)^ Freedom] of patch-at -1 0
let temp_left_ahead [(output-heat + 1)^ Freedom] of patch-at -1 1
set temp_ahead_kumulativ temp_ahead
set temp_right_ahead_kumulativ (temp_ahead_kumulativ + temp_right_ahead)
set temp_right_kumulativ (temp_right_ahead_kumulativ + temp_right)
set temp_right_back_kumulativ (temp_right_kumulativ + temp_right_back)
set temp_back_kumulativ (temp_right_back_kumulativ + temp_back)
set temp_left_back_kumulativ (temp_back_kumulativ + temp_left_back)
set temp_left_kumulativ (temp_left_back_kumulativ + temp_left)
set temp_left_ahead_kumulativ (temp_left_kumulativ + temp_left_ahead)
set propability_number (random-float (temp_left_ahead_kumulativ))
if propability_number < temp_ahead_kumulativ [right 0]
if propability_number < temp_right_ahead [right 45]
if propability_number < temp_right_kumulativ [right 90]
if propability_number < temp_right_back_kumulativ [right 135]
if propability_number < temp_back_kumulativ [right 180]
if propability_number < temp_left_back_kumulativ [left 135]
if propability_number < temp_left_kumulativ [left 90]
if propability_number < temp_left_ahead_kumulativ [left 45]
]
You need to turn all the if statements at the end into ifelse statements. The way you have it set up, a low random number will make the turtle turn in multiple directions.
ifelse propability_number < temp_ahead_kumulativ [right 0] [
ifelse propability_number < temp_right_ahead [right 45] [
....
ifelse propability_number < temp_left_kumulativ [left 90]
[left 45] ] ] ] ] ] ] ]
NOTE: I probably got the number of ] wrong, you will need to make sure that when you have the cursor at the last one, the [ at the top of the first line is highlighted.
Related
My program has an array for each turtle, and when I want to store the data in this array to the global variable matrix, the program does not work properly
I want to be able to implement an array for each turtle that stores data into a global variable matrix................................................................................................
The problem arises in:
``
let n 0 while [n < 40][ let tmp_num1 (array:item get_k n) matrix:set col_k CatNum n tmp_num
Here is my full code
`extensions [
array
matrix]
globals[
i
v
col_k
]
breed[cats cat]
breed[rats rat]
cats-own [
CatNum
data_from_rat
a_array
GetRatNum
get_k
k ;;
]
rats-own[
kg
num
]
to setup
clear-all
reset-ticks
set i 0
set v 0
creat-animals
creat-world
set col_k matrix:make-constant 40 40 0
ask cats[
set k 0
set a_array array:from-list n-values 20 [0]
set get_k array:from-list n-values 20 [0]
]
end
to go
ask n-of 20 rats[ ;;
if i < 20 [
set num i
set i i + 1
]
]
ask rats[
update
creat-ratnum
fd 3
rt random 90
]
ask n-of 40 cats[
if v < 40 [
set CatNum v
set v v + 1
]
]
ask cats[
let agsets other turtles-here with [breed = rats]
if count agsets >= 1
[
interaction(one-of agsets)
]
fd 3
lt random 90
;;print_array
]
tick
end
to creat-animals
create-cats 40[
set shape "cat"
set size 2
set color orange
setxy random-xcor random-ycor
]
create-rats 20[
set shape "mouse side"
set size 2
set color pink
setxy random-xcor random-ycor
]
end
to creat-world
ask patches[
set pcolor white
]
end
to creat-ratnum
loop[
if i >= 20 [stop]
set num i
set i i + 1
]
end
to update
set kg random-float 1
end
to interaction[provider]
set data_from_rat([kg] of provider)
set GetRatNum ([num] of provider)
let tmp_num (array:item get_k GetRatNum) ;;
ifelse tmp_num >= 1 [
set tmp_num tmp_num + 1
array:set get_k GetRatNum tmp_num ;;
;print (word "this is:" tmp_num)
][
set tmp_num tmp_num + 1
array:set get_k GetRatNum tmp_num
]
array:set a_array GetRatNum data_from_rat ;;
;;
let n 0
while [n < 40][
let tmp_num1 (array:item get_k n)
matrix:set col_k CatNum n tmp_num
]
end`
``
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.
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
'''
Sorry if I annoy you, but my teacher says that this code[1] is not nice enough and I should work with agentsets instead of invidual values.
Code[1] (first code worked very nice)
let temp_ahead [(output-heat + 1)^ Freedom] of patch-ahead 1
let temp_right_ahead [(output-heat + 1)^ Freedom] of patch-right-and-ahead 45 1
let temp_right [(output-heat + 1)^ Freedom] of patch-right-and-ahead 90 1
let temp_right_back [(output-heat + 1)^ Freedom] of patch-right-and-ahead 135 1
let temp_back [(output-heat + 1)^ Freedom] of patch-right-and-ahead 180 1
let temp_left_back [(output-heat + 1)^ Freedom] of patch-left-and-ahead 135 1
let temp_left [(output-heat + 1)^ Freedom] of patch-left-and-ahead 90 1
let temp_left_ahead [(output-heat + 1)^ Freedom] of patch-left-and-ahead 45 1
set temp_ahead_kumulativ temp_ahead
set temp_right_ahead_kumulativ (temp_ahead_kumulativ + temp_right_ahead)
set temp_right_kumulativ (temp_right_ahead_kumulativ + temp_right)
set temp_right_back_kumulativ (temp_right_kumulativ + temp_right_back)
set temp_back_kumulativ (temp_right_back_kumulativ + temp_back)
set temp_left_back_kumulativ (temp_back_kumulativ + temp_left_back)
set temp_left_kumulativ (temp_left_back_kumulativ + temp_left)
set temp_left_ahead_kumulativ (temp_left_kumulativ + temp_left_ahead)
set propability_number (random-float (temp_left_ahead_kumulativ))
if propability_number < temp_ahead_kumulativ [right 0]
if propability_number < temp_right_ahead [right 45]
if propability_number < temp_right_kumulativ [right 90]
if propability_number < temp_right_back_kumulativ [right 135]
if propability_number < temp_back_kumulativ [right 180]
if propability_number < temp_left_back_kumulativ [left 135]
if propability_number < temp_left_kumulativ [left 90]
if propability_number < temp_left_ahead_kumulativ [left 45]
My Code[2] (is not working as Code[1], what is wrong?):
;;all temperatrue-values around the turtle saved in list
set temperature_values [(output-heat + 1) ^ Freedom] of neighbors
;;build cumulative value of temperatures and put each value in list
let tempsum 0
set tempsum_list []
foreach temperature_values
[set tempsum (tempsum + ? )
set tempsum_list lput tempsum tempsum_list
]
;;create a random number between 0 and the last tempsum-value
let probability_number random-float tempsum + 1
if probability_number < item 0 tempsum_list
[left 45]
if (probability_number < item 1 tempsum_list)
[left 90]
if (probability_number < item 2 tempsum_list)
[left 135]
if (probability_number < item 3 tempsum_list)
[right 180]
if (probability_number < item 4 tempsum_list)
[right 135]
if (probability_number < item 5 tempsum_list)
[right 90]
if (probability_number < item 6 tempsum_list)
[right 45]
if (probability_number < item 7 tempsum_list)
[right 0]
in Code[2] I tried to work with agentsets but the result doesn't look like the result in Code[1]. In Code[1] my turtles are creating nice huddles but in Code[2] they don't do that. I'm a little bit desperate...
if anyone questions. the turtles should move in a higher probability to the patch with the higher value of "output-heat".
This line set propability_number (random-float (temp_left_ahead_kumulativ)) does not do the same thing as let probability_number random-float tempsum + 1.
Do some testing - have a print statement for probability_number after you create it, run it about 10 times and see if the numbers are what you expect.
You also have a problem with your if statements, as covered by your earlier question how can i set a probability for each action?. Again, the best way to learn is to do some testing to see how things work. The distinction between if and ifelse is very important to make you code do what you expect.
I have this code in which turtles are suppose to change direction when they encounter walls and closed gates.
It is okay at the beginning but then it will give this message.
OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
error while turtle 259 running OF
called by procedure GO
called by Button 'Go'
I can send the whole model.
if state = 1 [fd speed
ifelse [pcolor] of patch-at-heading-and-distance 0 1 = black or [pcolor] of patch-at-heading-and-distance 0 1 = red
[ lt random-float 90]
[fd 1]
; ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
; [ fd 1 ]
if (pxcor >= 89 and pxcor <= 90) and (pycor > 5 and pycor < 10) [facexy (87 + random 3) 25 fd speed]
;if (pxcor >= -10 and pxcor <= 1) and (pycor >= 6 and pycor <= 23 ) [facexy ( 7 + random 3) (26 + random 3) fd speed]
if (pxcor >= 85 and pxcor <= 90) and (pycor = 26) [
let choice random 2
fd speed
if choice = 1 [leftbench]
if choice = 0 [facexy (87 + random 3) 76]
]
if (pxcor >= 83 and pxcor <= 92) and (pycor >= 75 and pycor <= 77) [rightbench fd speed]
if pcolor = brown and not any? other turtles-here
[ move-to patch-here
set seated? true
set pcolor orange
]
]
Does your model have wrapping at the world edges turned off in one or both directions?
If so, then your code has to handle the possibility that sometimes this patch:
patch-at-heading-and-distance 0 1
won't exist, if the turtle is near a world edge. If that happens, then patch-at-heading-at-distance will return nobody, and when you try and do:
[pcolor] of patch-at-heading-and-distance 0 1 = black
you get the error, because no pcolor can be retrieved from a nonexistent patch.
The typical way to code around this is to do something like:
let target patch-at-heading-and-distance 0 1
if is-patch? target and ...
Finally, I wonder whether you really mean patch-at-heading-and-distance 0 1. It's equivalent to patch-at 0 1, which refers to the patch north of the turtle; not sure why you'd want that. If you want to test the patch ahead of the turtle (the patch the turtle will land in if it uses fd), use patch-ahead.