How to make an agent/turtle not see through multiple walls? (Netlogo) - netlogo

I am new to posting in stackoverflow.
Before I had a problem with agents/turtles seeing through the walls I make, but I have found the solution here:
Not see through the walls
The problem now is when there is another wall within the vision-cone, the valid line of sight (green patches) are incorrect. I'm currently still tinkering on how to fix this.
Here is my current code that I have been working on,
breed [walls wall]
walls-own [first-end second-end]
to setup
ca
reset-ticks
set-default-shape walls "line"
crt 1 [setxy 4 0]
ask turtles [facexy 0 0]
;color all cones in radius blue by default
let dist 10
let angle 80
ask turtles [ask patches in-cone dist angle [set pcolor blue]]
;; place a wall down.... the line of sight is blocked (keyword: line)
create-walls 1 [setxy 0 0]
;;This is an interpretation of a wall. Two points that define the edges.
ask wall 1 [set size 10]
;;my wall is vertical. you can do trig above and below to adjust for not vert lines.
ask wall 1 [set heading 45]
ask wall 1 [set color hsb 216 50 100]
;; I made it so that it could consider not vertical lines
ask wall 1 [ set first-end (list (round (sin heading * (size / 2))) (round (cos heading * (size / 2))))]
ask wall 1 [ set second-end (list (round((-1 * (size / 2) * sin heading))) (round ((-1 * (size / 2) * cos heading))))]
;;Test wall. UN-COMMENT THE SET BELOW TO SEE THE PROBLEM
; create-walls 1 [setxy 4 4]
; ask wall 2 [set size 10]
; ask wall 2 [set heading 90]
; ask wall 2 [ set first-end (list (round (sin heading * (size / 2))) (round (cos heading * (size / 2))))]
; ask wall 2 [ set second-end (list (round((-1 * (size / 2) * sin heading))) (round ((-1 * (size / 2) * cos heading))))]
; ask wall 2 [set color hsb 216 50 100]
ask turtle 0 [ask in-sight dist angle [set pcolor green]]
end
;;a turtle can see a patch if the line from the patch to the trutle isnt intersected by a wall.
to-report in-sight [dist angle]
let turtle-x xcor
let turtle-y ycor
report patches in-cone dist angle with
[
not any? walls with [intersects [pxcor] of myself [pycor] of myself turtle-x turtle-y ;; line 1
(first first-end)(last first-end)(first second-end)(last second-end)] ;; line 2
]
end
to-report counter-clockwise [x1 y1 x2 y2 x3 y3]
;; returns true if triplet creates counter clockwise angle (uses slopes)
;; (C.y-A.y) * (B.x-A.x) > (B.y-A.y*(C.x-A.x)
report (y3 - y1)*(x2 - x1)>(y2 - y1)*(x3 - x1)
end
to-report intersects [x1 y1 x2 y2 x3 y3 x4 y4]
;; line 1: x1 y1 x2 y2
;; line 2: x3 y3 x4 y4
;;DANGER: Doesnt work for colinear segments
report (counter-clockwise x1 y1 x3 y3 x4 y4) != (counter-clockwise x2 y2 x3 y3 x4 y4)
and (counter-clockwise x1 y1 x2 y2 x3 y3) != (counter-clockwise x1 y1 x2 y2 x4 y4)
end
This is what it looks like if you would uncomment the set of code for the test wall.
Problem
Any suggestions or tips would be very helpful!
Thank you in advance.

Related

How do I find find out probability of Y occurring depends on both X1 and X2 in Netlogo? Y , X1 and X2 are turtle variables

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
'''

Draw a semicircle in Netlogo

I want to create a semicircle, the center of my semicircle is at the end of the world, for example I want that the center of my semicircle is in the 50,20 position, and goes from 20 to -20.
I try with this, but I got an exception:
The point [ 60 , 20 ] is outside of the boundaries of the world and
wrapping is not permitted in one or both directions.
to mysemicircle
let cx 50 ;; x coordinate of patch you want to circle
let cy 20 ;; y coordinate of patch you want to circle
let r 10 ;; radius of the circle you want
let p2r ( 2 * pi * r ) ;; get circumference of the circle
let step p2r / 360 ;; make step lengths 1/360th of the circumference
crt 1 [ ;; create a single drawing turtle
setxy cx + r cy ;; move it to the highlight patch + the radius
pd ;; put the pen down
set heading 0 ;; make it face along the tangent
while [ p2r > 0 ] [ ;; make the turtle continue to move until the circle is drawn
lt 1
fd step
set p2r p2r - step
set color white
]
]
end
And If i use cx 40 and cy 20 the circle is not from y -20 to y 20, but is only from y 0 to y 20.
How Can I do that?
Maybe a procedure with arguments would be better for this, something like:
to setup
ca
resize-world -20 20 -20 20
draw-semi-circle 0 0 15 45 blue
draw-semi-circle 5 5 5 270 red
draw-semi-circle -10 -10 8 135 green
reset-ticks
end
to draw-semi-circle [ x y r a col ]
; Give arguments for x, y, radius, and angle of the semicircle
let half-p ( pi * r )
let step half-p / 180
ask patch x y [
set pcolor col
]
crt 1 [
setxy x y
set heading a
lt 90
set color col
back r
pd
fd 2 * r
rt 90
while [ half-p > step ] [
rt 1
fd step
set half-p half-p - step
]
]
end
To get an output like:

Not see through the walls

I am trying to model some building search in Netlogo, but now I am stuck, because I have no idea how to solve seeing through walls. My agent can see in cone lets say 60 degrees and 5 patches ahead. I can detect wall somewhere in my sight and i can detect other objects as well, problem is to detect if the obejct is behind wall or not. How can I solve it? Any ideas?
Here's a full working example. You can do some optimizations regarding which walls or patches need to be checked, rather than all of them, but I'll leave that to you...Essentially, You can imagine that you draw a line between the agent and all other patches that are in the vision of the agent and remove the patches such that there is an intersecting wall. The line-intersection problem is well studied and I put a link for you to look at. To make my calculations easier, I store the end points of the wall--you could use trig to calculate the end points if the wall wasn't vertical like in my case. I use some aesthetics too...
breed [ walls wall]
walls-own [first-end second-end]
to setup
ca
reset-ticks
set-default-shape walls "line"
crt 1 [ setxy 4 0]
ask turtles [facexy 0 0]
;;color all cones in radius blue by default
let dist 10
let angle 30
ask turtles [ ask patches in-cone dist angle [set pcolor blue]]
;; place a wall down...the line of sight is blocked (keyword: line)
create-walls 1 [ setxy 0 0 ]
;;This is an interpretation of a wall. Two points that define the edges.
ask wall 1 [set size 10]
ask wall 1 [set first-end (list 0 (size / 2))]
ask wall 1 [set second-end (list 0 (-1 * size / 2))]
;;my wall is vertical. You can do trig above and below to adjust for not vert lines.
ask wall 1 [ set heading 0]
ask wall 1 [set color hsb 216 50 100] ;;pretty blue =)
ask turtle 0 [ ask in-sight dist angle [ set pcolor green]]
end
;;a turtle can see a patch if the line from the patch to the turtle isn't intersected by a wall.
to-report in-sight [dist angle]
let turtle-x xcor
let turtle-y ycor
report patches in-cone dist angle with
[
not any? walls with [intersects [pxcor] of myself [pycor] of myself turtle-x turtle-y ;; line 1
(first first-end) (last first-end) (first second-end) (last second-end)] ;; line 2
]
end
;; See http://stackoverflow.com/questions/3838329/how-can-i-check-if-two-segments-intersect
;;counter clockwise method (doesn't consider colinearity)
to-report counter-clockwise [x1 y1 x2 y2 x3 y3]
;;returns true if triplet creates counter clockwise angle (uses slopes)
;(C.y-A.y) * (B.x-A.x) > (B.y-A.y) * (C.x-A.x)
report (y3 - y1) * (x2 - x1) > (y2 - y1) * (x3 - x1)
end
to-report intersects [x1 y1 x2 y2 x3 y3 x4 y4]
;;line 1: x1 y1 x2 y2
;;line 2: x3 y3 x4 y4
;;DANGER: Doesn't work for colinear segments!!!
;ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)
report (counter-clockwise x1 y1 x3 y3 x4 y4) != (counter-clockwise x2 y2 x3 y3 x4 y4)
and (counter-clockwise x1 y1 x2 y2 x3 y3) != (counter-clockwise x1 y1 x2 y2 x4 y4)
end

Drawing a super-ellipse with a turtle

Obviously, any shape drawable by other means can be drawn by a turtle. Circles and squares are easy
rt 1 fd .0
and
if ticks mod 100 = 0 [rt 90]
fd 1
Super-ellipses not so much. (regular ellipses are not trivial either.)
The Wikipedia article on super-ellipses if you need to be refreshed on the topic.
Any input is appreciated.
Using a pendown turtle is there way to make a super-ellipse that emerges from turtle movement?
I have 1/4 of it, I suppose you could piece-wise put the other three together. Other values of n are not tested here. (using the Wiki notation, plus phi as an angle of rotating the whole thing.) And the placement of reset-ticks, pen-down, is sloppy, I know.
to go2
clear-all
reset-ticks
let a 6
let b 5
let phi 0
let n 3.5
create-turtles 1 [
let iNdx 1
repeat 90 [
show iNdx
show cos(iNdx)
if cos(iNdx) > 0 and sin(iNdx) > 0 [
let tx (a * (cos(iNdx) ^ (2 / n)))
let ty (b * (sin(iNdx) ^ (2 / n)))
let tx2 tx * cos(phi) - ty * sin(phi)
let ty2 tx * sin(phi) + ty * cos(phi)
setxy tx2 ty2
]
pen-down
set iNdx iNdx + 1
]
]
end
The ellipse looks simpler, but you be the judge
to go
clear-all
reset-ticks
let a 6
let b 5
let phi 45
create-turtles 1 [
let iNdx 1
repeat 360 [
let tx (a * cos(iNdx))
let ty (b * sin(iNdx))
let tx2 tx * cos(phi) - ty * sin(phi)
let ty2 tx * sin(phi) + ty * cos(phi)
setxy tx2 ty2
pen-down
set iNdx iNdx + 1
]
]
end
a generalization and simplification as a procedure.
to Super-ellipse [x y a b m n]
create-turtles 1 [
let iNdx 1
repeat 360 [
setxy (x + (abs cos iNdx)^(2 / m) * a * (sgn cos iNdx))
(y + (abs sin iNdx)^(2 / n) * b * (sgn sin iNdx))
pendown
set iNdx iNdx + 1]
]
end
The generalized form of another answer seems to produce the sort of thing I was thinking of. the closer the pen starts to one of the foci the closer the drawing is to a square. the n<1 super-ellipses are not achieved.
globals[c]
breed [pens pen]
breed [foci focus]
foci-own [dist distx disty]
to setup
ca
create-pens 1 [set heading 45 fd 10 pendown set C self]
;create-foci 1 [setxy (random-xcor / 2) (random-ycor / 2)]
create-foci 1 [setxy 10 10]
create-foci 1 [setxy 10 -10]
create-foci 1 [setxy -10 -10]
create-foci 1 [setxy -10 10]
end
to go
repeat 5100
[
ask foci [
set dist distance c
set distx xcor - [xcor] of c
set disty ycor - [ycor] of c
]
ask c
[
set heading 90 + atan ( sum [distx / dist] of foci / sum [dist] of foci)
( sum [disty / dist] of foci / sum [dist] of foci)
FD .0125
]
]
end

Drawing an Emergent ellipse with a turtle.

This is an answer to a question posed in the comments in my possibly poorly worded question about super-ellipses.
in Netlogo it is natural to draw geometric shapes in ways that may seem strange in other languages.
ask turtle 1 [pendown
let d (pi * distance turtle 2) / 360
repeat 360 [face turtle 2 rt 90 fd d]
]
for instance inscribes makes turtle 1 draw a circle [360-gon] around turtle 2. I did not invoke any of the standard circle formulas but still get a circle.
Is it possible to draw an ellipse in this same vernacular with say one turtle drawing an ellipse (or super-ellipse)round two other turtles using them as the foci?
Essentially to make an ellipse you set the turtles heading to the weighted mean heading of the foci and update each step. It could be done in one line but that would be one ugly line.
globals [a b c]
to setup
ca
crt 1 [set heading 90 fd 10 pendown set C self]
crt 1 [setxy 5 10 set A self]
crt 1 [setxy 0 -10 set B self]
end
to go
repeat 5100 ;; ad hoc number
[
ask c
[
let Ax [xcor] of A - xcor
let Ay [ycor] of A - ycor
let Bx [xcor] of B - xcor
let By [ycor] of B - ycor
let da 1 / distance a
let db 1 / distance B
set heading 90 + atan ((ax * da + bx * dB) / (da + db))
((ay * da + by * db) / (da + db))
FD .0125 ;;
]
]
end