How to randomly allocate my agents?

40 views
Skip to first unread message

Mustafa Ali Saba

unread,
Jan 12, 2020, 10:25:13 PM1/12/20
to netlogo-users

Hi dear friends,

I have a world like this.

to draw-simple
  let top
-wall 0
  let bottom
-wall 0
 
  ask patches
[set pcolor 7]
 
  repeat
81 [
    ask patch top
-wall ((horizontal-corridor-width + 1) / 2) [
     
set pcolor 0
   
]
   
set top-wall (top-wall + 1)
 
]

  repeat
81 [
    ask patch bottom
-wall (-((horizontal-corridor-width + 1) / 2)) [
     
set pcolor 0
   
]
   
set bottom-wall (bottom-wall + 1)
 
]
end

horizontal-corridor-width is a slider from 1 to 20. The drawing is as follows.

core view.png

I want to distribute my agents along the hallway without traspassing the walls. I have tried something like this.
to create-pedestrians-simple
 
  create
-east-crowd num-pedestrians-east [
   
set color red
   
set size 1
    setxy
40 0
   
set heading 270
 
]

  create
-west-crowd num-pedestrians-west [
   
set color blue
   
set size 1
    setxy
random-xcor random-ycor
   
set heading 90
 
]
end

east-crowd and west-crowd are breeds and the variables num-pedestrians-east and num-pedestrians-west are sliders from 0 to 1600 agents.

Hope to be clear enough.

Thank all of you, dear comunity.

Pradeesh Kumar K V

unread,
Jan 13, 2020, 9:33:30 AM1/13/20
to netlogo-users
Hello Mustafa,

In order to set the agents within the corridor, you need to specify the max. y as the y coordinate of  top wall patches and min y as the y of bottom wall patches.

For example, if your top wall y coordinate is 10, then you need to define setxy random-xcor random 10. In this case all the agents will be between y coordinate 0 and 10

If your bottom wall y coordinate is -10, then you need to define setxy random-xcor random -10 for set of agents below the '0' y coordinate.

You will have to use a random function to distribute the agents uniformly between the top half and bottom half of the '0' y coordinate.

Hope this helps.

Pradeesh

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/6a9d0c05-a675-4b2a-a27a-ad63b02ce7b0%40googlegroups.com.

Aaron Andre Brandes

unread,
Jan 21, 2020, 5:12:48 PM1/21/20
to Mustafa Ali Saba, netlogo-users

Hi Mustafa,

Here is an answer to your question, along with a couple of suggestions.

 

Since a single agent in your model is a pedestrian, I suggest you use names  similar to those below, which would be consistent with variable names such as num-pedestrians-west

 

It is custom, but not requirement to call the initialization function setup, and to clear values at the beginning.

Notice in create-corridor that it is possible to address a whole set of patches at once (although your method works too).

I use the function ceiling to identify the same patches you do. For example if horizontal-corridor-width is 20

patch 0 10.5 is the same as patch 11, and ceiling 10.5 = 11. while

patch 0 -10.5 is the same as patch -10, and ceiling -10.5 = 10

 

The question you were specifically asking is answered by the function

random-corridor-ycor

The approach is similar to that suggested by Pradeesh. I use random-float rather than random, in order to get non-integer values, and don’t treat agents above and below the x axis separately

Code:

 

breed [ pedestrians-east pedestrian-east ]

breed [ pedestrians-west pedestrian-west ]

 

to setup

  clear-all

  reset-ticks

 

  create-corridor

 

   create-pedestrians-east num-pedestrians-east [

    set color red

    set size 1

    setxy random-xcor random-corridor-ycor

    set heading 270

  ]

 

  create-pedestrians-west num-pedestrians-west [

    set color blue

    set size 1

    setxy random-xcor random-corridor-ycor

    set heading 90

  ]

end

 

to create-corridor

  ask patches [set pcolor 7]

  ask patches with [ pycor  = corridor-top or pycor  = corridor-bottom ] [

    set pcolor 0

  ]

end

 

to-report corridor-top

  report ceiling ((horizontal-corridor-width + 1) / 2.0)

end

 

to-report corridor-bottom

  report ceiling ((horizontal-corridor-width + 1) / -2.0)

end

 

to-report random-corridor-ycor

  ; The smallest y coordinate should be above the corridor bottom

  ; The size of the occupiable corridor space is horizontal-corridor-width - 1

  report corridor-bottom + 1 + random-float (horizontal-corridor-width - 1)

end

 

  • Aaron

 

Aaron Brandes, Software Developer

Center for Connected Learning and Computer-Based Modeling

 

 

From: <netlog...@googlegroups.com> on behalf of Mustafa Ali Saba <alisaba...@gmail.com>
Date: Sunday, January 12, 2020 at 10:25 PM
To: netlogo-users <netlog...@googlegroups.com>
Subject: [netlogo-users] How to randomly allocate my agents?

 

 

Hi dear friends,

 

I have a world like this.

 

to draw-simple
  let top
-wall 0
  let bottom
-wall 0
 
  ask patches
[set pcolor 7]
 
  repeat
81 [
    ask patch top
-wall ((horizontal-corridor-width + 1) / 2) [
     
set pcolor 0
   
]
   
set top-wall (top-wall + 1)
 
]

  repeat
81 [
    ask patch bottom
-wall (-((horizontal-corridor-width + 1) / 2)) [
     
set pcolor 0
   
]
   
set bottom-wall (bottom-wall + 1)
 
]
end


horizontal-corridor-width is a slider from 1 to 20. The drawing is as follows.

 

I want to distribute my agents along the hallway without traspassing the walls. I have tried something like this.

to create-pedestrians-simple
 
  create
-east-crowd num-pedestrians-east [
   
set color red
   
set size 1
    setxy
40 0
   
set heading 270
 
]

  create
-west-crowd num-pedestrians-west [
   
set color blue
   
set size 1
    setxy random
-xcor random-ycor
   
set heading 90
 
]
end

 

east-crowd and west-crowd are breeds and the variables num-pedestrians-east and num-pedestrians-west are sliders from 0 to 1600 agents.

 

Hope to be clear enough.

 

Thank all of you, dear comunity.

--

George BUTLER

unread,
Jan 22, 2020, 3:46:45 PM1/22/20
to Mustafa Ali Saba, netlogo-users
Hi Mustafa,

You simply need to set the ycor of your pedestrians as a function of the position of your walls. The following code should do just fine as it places all of your blue pedestrians in between the walls :

to create-pedestrians-simple
  let limit (abs([pycor] of one-of patches with [pcolor = 0]) - 1)

 
  create-east-crowd num-pedestrians-east [
    set color red
    set size 1
    setxy 40 0
    set heading 270 ]

  create-west-crowd num-pedestrians-west [
    set color blue
    set size 1
    setxy random-xcor ((random (2 * limit) ) - limit )
    set heading 90 ]
end




George J. Henry BUTLER STREESE


--
Reply all
Reply to author
Forward
0 new messages