Netlogo modifying library Mouse Drag Multiple Example to a non wrapping world - mouse

Could anyone give some hints (or a full solution!) to how to convert the model library code example 'Mouse Drag Multiple Example' to a non wrapping world. My attempts destroy the selection rectangle (the sides of the rectangle pile up at the edge - because each part is treated separately). Similarly the selected turtles just pile up at the edge. Thank you.
Here's the main procedure from the model - it gives a runtime error on the second setxy when the side of the selection rectangle gets to the edge of the non-wrapping world:
to handle-drag
;; remember where the mouse pointer was located when
;; the user pressed the mouse button
let old-x mouse-xcor
let old-y mouse-ycor
if selected? old-x old-y [ ;; selected? is a reporter defined below
while [mouse-down?] [
let new-x mouse-xcor
let new-y mouse-ycor
;; we need to move both the selected turtles and the sides
;; of the selection rectangle by the same amount that the
;; mouse has moved. we do this by subtracting the current
;; mouse coordinates from the previous mouse coordinates
;; and adding the results to the coordinates of the turtles
;; and sides.
ask selected
[ setxy xcor + new-x - old-x
ycor + new-y - old-y ]
ask sides
[ setxy xcor + new-x - old-x
ycor + new-y - old-y ]
set old-x new-x
set old-y new-y
;; update the view, otherwise the user can't see
;; what's going on
display
]
]
end

This is completely untested. It sounds like what you need is to pretest the potential position to make sure if doesn't go past the edge of the world and, if it does, reduce the amount of movement accordingly.
Try amending this section:
ask selected
[ setxy xcor + new-x - old-x
ycor + new-y - old-y ]
ask sides
[ setxy xcor + new-x - old-x
ycor + new-y - old-y ]
with:
let move-x new-x - old-x
let proposed-max-x max [x-cor] of selected + move-x
let proposed-min-x min [x-cor] of selected + move-x
if proposed-max-x > max-pxcor + 0.5 [let move-x max-pxcor + 0.5 - max [x-cor] of selected]
if proposed-min-x < min-pxcor - 0.5 [let move-x min-pxcor - 0.5 + min [x-cor] of selected]
ask selected
[ setxy xcor + move-x ]
You will need to add in the sides (this only does selected) and do similar for y coordinates.

Related

How can I separate left- and right-vision field of a turtle in NetLogo?

I want to separate left- and right-vision fields of a turtle in NetLogo.
Left-vision field means angles are -179 to -1 degrees while right-vision field means angles are from 1 to 179 if an angle of front of a turtle is 0 degrees.
In NetLogo, patch-right-and-ahead and patch-left-and-ahead can separate left- and right-vision fields of a turtles, but those codes is only a angle incorporate a turtle.
And, in-cone can not separate the vision fields.
Is there any codes to program this in NetLogo?
You can save the turtle's current heading, turn it 90-degrees left and see what's on the left side of the original heading with
scan x 180
then turn it 90-degrees right from original heading and see what's on the right side of the original heading with
scan x 180
then turn it back to its original heading.
Here's code that illustrates this. Set the scan angle to something smaller than 180 to see more clearly what's happening. As you step through the go step it rotates and highlights what it is seeing.
globals [ scan-angle]
turtles-own [ actual-heading ]
to setup
clear-all
set scan-angle 180
create-turtles 1 [ set size 4 set heading 0 set actual-heading heading]
reset-ticks
end
to go
ask turtle 0 [ set heading (heading + 30) set actual-heading heading]
scansides scan-angle
tick
end
to scansides [ angle ]
ask patches [ set pcolor black]
ask turtle 0
[
;; scan left side
set heading (actual-heading - 90)
ask patches in-cone 10 angle [ set pcolor red]
;; scan right side
set heading (actual-heading + 90)
ask patches in-cone 10 angle [ set pcolor green]
set heading actual-heading
]
end

Netlogo: measure mean distance between start and end patches

I am teaching myself how to create ABMs in Netlogo using the book of Railsback & Grimm 2012. I am having trouble with one book exercise which is on butterflies following "virtual" corridors. Basic idea is that butterflies go uphill for mating using the differences in height as guide. I need to calculate the width of the corridors dividing the number of patches used by the butterflies over the average distance the butterflies fly from the start patch to the end patch. I am
struggling with plotting this corridor width, which I am coding like this:
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
I then created a plot in the interface with the command:
plot corridor-width
The error message I get reads:
Division by zero. error while observer running / called by procedure
CORRIDOR-WIDTH called by plot 'Corridor width' pen 'default' update
code called by procedure SETUP called by Button 'setup'
I believe there is something wrong with the way I am coding distance start-patch but I have surfed the web and looked at several codes and I cannot spot my mistake. My whole code looks like this:
globals [ q ] ;; q is the probability that butterfly moves directly to highest patch
turtles-own [ start-patch ]
patches-own [ elevation used? ] ;; patches property of elevation and whether the patch has been used by butterfly or not.
to setup
ca
;; Let's create patches and asign them an elevation and color by using ask patches statement
ask patches
[
;; Elevation decreases linearly with distance from the center of hills. Hills are at (30,30) and
;; (120,120) coordinates. The first hill is 100 units high whereas the second one is 50
let elev1 100 - distancexy 30 30
let elev2 50 - distancexy 120 100
ifelse elev1 > elev2
[ set elevation elev1 ]
[ set elevation elev2 ]
set pcolor scale-color green elevation 0 100
set used? false
]
;; Create 50 butterflies
crt 50
ask turtles [
set size 6
;; set their initial location as their initial patch
setxy random-pxcor random-pycor
set start-patch patch-here
;; have the butterfly draw its path with the pen-down statement
pen-down
]
reset-ticks
;; Initialize the q parameter
set q 0.4
end
;; The master schedule
to go
ask turtles [ move ]
plot corridor-width
tick
if ticks >= 1000
[
let final-corridor-width corridor-width
write "Corridor width: " print final-corridor-width
;export-plot "Corridor width" (word "Corridor-width-output-for-q-" q ".csv")
stop
]
end
;; let's code the butterfly procedure of movement
to move
if elevation >=
[ elevation ] of max-one-of neighbors [ elevation ]
[ stop ]
ifelse random-float 1 < q ;; Decide whether to move to the highest sorrounding
;; patch with p=q
[ uphill elevation ] ;; move deterministically uphill
[ move-to one-of neighbors ] ;; or move randomly
set used? true
end
to-report corridor-width
let patches-visited count patches with [used?]
let mean-distance mean [distance start-patch] of turtles
report patches-visited / mean-distance
end
What happens when the mean-distance is 0?
let mean-distance mean [distance start-patch] of turtles
Essentially, in your setup, you set all the turtle's start-patch to their current patch. So, if you ask all the turtles how far away they are from their start patch, they will all tell you 0 units away.
So, [distance start-patch] of turtles is filled with a list of all 0's.
Thus, a mean of a list of all 0s is 0 causing your divide by 0 error.
Maybe in this situation, you want to report 0 instead...so
ifelse mean-distance = 0
[ report 0]
[report patches-visited / mean-distance]

why plabel replace previous plabel in this netlogo model for displaying patch and turtle coordination?

with the help of #JenB I wrote a code to help understand forward 1, patch and turtle coordination. The code is attached at the end.
However, I noticed an error: I built two buttons setup and test, after click setup and then click test a few times, we will notice some of turtle 1's plabels will replace previous ones, which is not at all intended and should not happen; whereas, turtle 0 seems to have no such problem.
to setup
clear-all
create-ordered-turtles 2
[
ifelse (who mod 2) = 0
[
set heading heading + 15
]
[
set heading 135
]
pen-down
; forward 1
]
end
to test
ask turtles [ ;; for each turtle
fd 1
;; get patch corrdination
let patch-cor (word "px" pxcor ":" "py" pycor)
;; get turtle corrdination
let turtle-cor (word "x" (precision xcor 2) ":" "y" (precision ycor 2))
;; get distance between current and previous turtle corrdinations
;; get distance between current and origin turtle corrdinations
let distanceTurtle precision (distancexy 0 0) 1
let patch-color color ;; get turtle's color
set label patch-cor ;; let this turtle's label show current patch-corrdination
set label-color patch-color
ask patch-at -1 0 ;; focus on patch 1 unit left to where this turtle is on
[
set plabel patch-cor ;; let this patch label show turtle's patch corrdination
set plabel-color patch-color
]
ask patch-at 4 0 ;; focus on patch 4 unit right to where this turtle is on
[
set plabel turtle-cor ;; let this patch label show turtle's own corrdination
set plabel-color patch-color
]
ask patch-at 5 0 ;; focus on patch 5 unit right to where this turtle is on
[
set plabel distanceTurtle ;; let this patch label show distance between current turtle and origin
set plabel-color patch-color
]
]
end

Find the presence of other turtles in a given direction upto a distance

I wish to find out whether in a given turtle's heading there is another agent present upto a given distance.
Here the Distance is "D".
Note:
Any agent present before D in the given direction should be also considered.
Even the direction doesn't coincide with the other's agent centre but just touches it ,even then that agent should be considered.
Problem:
No turtle-ahead procedure available. Combination of patch-ahead and turtles-on not applicable due to patch-size>> turtle-size.
Possible approach:
1.Represent the turtle's heading by the equation of a line.
to-report calculate-line[x y angle]
let m tan angle
let A m
let B -1
let C (- m * x + y)
report (list A B C)
end
to-report heading-to-angle [ h ]
report (90 - h) mod 360
end
let line-equ calculate-line (xcor) (ycor) (heading-to-angle heading)
2.Calculate the perpendicular distance from other turtles here, Check if there are within a range that the size of other turtles.
to-report value[A X1 Y1];;A is list of coefficents of line, x1 and y1 are coordinates of red turtle
if-else(abs((item 0 A * X1 + item 1 A * Y1 + item 2 A) / (sqrt((item 0 A ^ 2) + (item 1 A ^ 2) ))) < [size] of wall )
[ report "true"][report "false"]
end
3.To check if the red turtle is within D. One could obtain a line perpendicular to black one and compute the red turtle distance from it to check if it is less than or equal to D. But then that adds more complication.(Though one can simplify rotate the turtle by 90 left or right and get the line equation.)
This is what I meant by my comment. Run this code (as its own model). What it does is turn all the turtles on a few 'ahead' patches a different colour. I know this is not what you are trying to do, but the agentset candidates is a relatively small number of turtles. These are the only ones that you have to check whether they are on the correct path. So instead of turning them a different colour you could check the direction that they are from your initial turtle.
to setup
clear-all
set-patch-size 25
resize-world -10 10 -10 10
create-turtles 1000
[ setxy random-xcor random-ycor
set color yellow
set size 0.4
]
ask one-of turtles
[ set color red
set size 1
check-from-me 5
]
end
to check-from-me [howfar]
let counter 0
let candidates turtles-here
while [counter < howfar]
[ set counter counter + 1
set candidates (turtle-set candidates turtles-on patch-ahead counter)
]
ask candidates [set color red]
end
to-report check-wall
let return false
hatch 1[
set color black
set size ([size] of one-of walls) / 2
show (2.5 * ([size] of myself))
while [distance myself < (2.5 * ([size] of myself))]
[
fd ([size] of one-of walls) / 64
if any? walls in-radius size
[
set return true
]
show distance myself
]
]
report return
end
The above works. But still is approx. I am looking for better solution with probably less maths as one elucidated in the question.

Netlogo Mekka Model - need directions

I'm working on this assignment for my university with Netlogo and I'm really stuck.
I just started out using Netlogo and I'm trying to recreate Mekka, together with some pilgrims.
I've been trying out a lot of different codes, adding new ones, trying it out, deleting some, but this is what I came up with this far:
turtles-own
[direction ;; 1 follows right-hand wall, -1 follows left-hand wall
way-is-clear? ;; reporter - true if no wall ahead
checked-following-wall?]
globals [halfedge]
breed [agents agent ]
agents-own [ around visible ]
to setup
create-agents 500 [
set color green
set size 2
; distribute agents randomly
setxy pxcor = halfedge pycor = halfedge
set heading random 360
; ensure that each is on its own patch
while [any? other agents-here] [ fd 1 ]
]
end
to bounce
if [pcolor] of patch-at dx 0 = blue [
set heading (- heading)
]
if [pcolor] of patch-at 0 dy = blue [
set heading (180 - heading)
]
end
to go
ask agents [ count-those-around ]
ask agents [ move ]
end
; store the number of agents surrounding me within
; local-radius units
; and the agents that I can see within visible-radius
to count-those-around
set around count agents with [self != myself] in-radius
local-radius
set visible agents with [self != myself] in-radius
visible-radius
end
to move
;; turn right if necessary
if not wall? (90 * direction) and wall? (135 * direction) [ rt 90 * direction ]
;; turn left if necessary (sometimes more than once)
while [wall? 0] [ lt 90 * direction ]
;; move forward
fd 1
end
; face towards the most popular local spot
to face-towards
face max-one-of visible [around]
end
; face away from the most popular local spot
to face-away
set heading towards max-one-of visible [around] - 180
end
to setup-center
clear-all
set halfedge int (edge / 2)
ask patches[
if (pxcor = (- halfedge) and pycor >= (- halfedge) and pycor <= (0 + halfedge) )
[set pcolor blue] ;; ... draws left edge in blue
if ( pxcor = (0 + halfedge) and pycor >= (- halfedge) and pycor <= (0 + halfedge) )
[set pcolor blue] ;; ... draws right edge in blue
if ( pycor = (- halfedge) and pxcor >= (- halfedge) and pxcor <= (0 + halfedge) )
[set pcolor blue] ;; ... draws bottom edge in blue
if ( pycor = (0 + halfedge) and pxcor >= (- halfedge) and pxcor <= (0 + halfedge) )
[set pcolor blue] ;; ... draws upper edge in blue
]
end
The idea is that first, a square is setup resembling the kaaba.
After that, the turtles are set up.
They are supposed to all walk around the wall in a counter-clockwise direction.
There's supposed to be one 'leader' that would lead all the pilgrims around the kaaba.
Right now the kaaba is successfully drawn, the only problem is that turtles are not supposed to be spawn in there or run into it (therefore the bump code).
Also, they are randomly going around, and I have no idea how to make them move in a Counter-Clickwise formation, following one differently coloured leader.
Could any of you guys help me out? I would be eternally thankful!
You may be trying to learn too much all at once by writing a big program all at once.
Start by writing a really small program; get it working; attempt to make a very small improvement to it, and get that working; and so on. If at any point you get stuck, come here, show your code with has at most one thing broken about it, and ask one question about the one issue in particular that you're currently stuck on. That's the most effective way to get help.
A few random coding tips:
This isn't valid code:
setxy pxcor = halfedge pycor = halfedge
setxy expects two numbers, but you're passing it two booleans: pxcor = halfedge is true or false, and pycor = halfedge is true or false, too. I think you might mean just setxy halfedge halfedge.
agents with [self != myself] can be replaced with simply other agents.