how can i set a probability for each action?

2019-08-07 15:38发布

问题:

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]

 ]

回答1:

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.