2

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".

King-Ink
  • 2,096
  • 12
  • 18
  • In addition to JenB's answer, I want to recommend learning how to indent code properly. Otherwise, it's difficult for others to read your code, and it's easy for you to make mistakes. There are several ways of doing this that are good for NetLogo. I edited your code to illustrate one option. – Mars Nov 08 '15 at 07:14
  • 1
    Could the problem be that `neighbors` is not the same patches as the patches picked out by using `patch-left-and-ahead` and `patch-right-and-ahead`? Because they will often not be the same patches, depending on the exact position of a turtle within its patch and its exact heading. (For example, unless turtles are always on patch centers, `patch-ahead 1` is sometimes the same patch the turtle is already standing on...!) – Seth Tisue Nov 08 '15 at 18:25
  • yes i think so too. i tried it now with the coordinates of the neighbors. and turn the turtles the coordinates with facexy. is there a other way except of neighbors? – skipperdipper Nov 08 '15 at 21:33

1 Answers1

2

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.

Community
  • 1
  • 1
JenB
  • 17,620
  • 2
  • 17
  • 45
  • i tested every value and every list. i get right results. i think the problem is, that the neighbors are not asked from the top to the bottom around in the right direction. it's always different. i also need the coordinates of the patches. but i don't know how to do that. – skipperdipper Nov 08 '15 at 11:11
  • it is certainly true that the agents are asked in a random order when you ask an agentset. This is intentional - otherwise the first asked agent would always get an advantage. For example, in a trading simulation, the first agent would always get the best price. But if all you are trying to do is select a neighbor to face with weighted probabilities, look at the rnd extension – JenB Nov 08 '15 at 18:45
  • yes i tried it now with facexy. i made lists of the x-coordinates and y-coordinates of the neighbors. but it don't look any better... – skipperdipper Nov 08 '15 at 22:16