how to create turtles in a specific area on the patch in Ntelogo - netlogo

I am making and assignment. The assignment is to make a model of Democracy. I have made a parliament house with coordinates (4, 6). I have made 3 political parties. Now, I want to make the people who will vote. I make them randomly using random-xcor and random-ycor in setxy, but they some of them are made on the parliament house. How can I stop this from happening. I want the turtles to be made everywhere except in the parliament house. Here is the code. Please help me.
breed [people p]
breed [party1 p1]
breed [party2 p2]
breed [party3 p3]
party1-own [vote]
party2-own [vote]
party3-own [vote]
to setup
clear-all
setup-patches
setup-people
setup-parties
reset-ticks
end
to setup-patches
ask patches [
ifelse pxcor >= 4 and pycor >= 6
[set pcolor white]
[set pcolor brown]
]
end
to setup-people
set-default-shape people "person"
create-people 100
ask people [setxy random-xcor random-ycor]
end
to setup-parties
set-default-shape party1 "person"
set-default-shape party2 "person"
set-default-shape party3 "person"
create-party1 1
create-party2 1
create-party3 1
ask party1 [setxy 15 -1]
ask party2 [setxy 15 -3]
ask party3 [setxy 15 -5]
ask party1 [set color blue]
ask party2 [set color green]
ask party3 [set color yellow]
end

to setup-people
set-default-shape people "person"
ask n-of 100 (patches with [pcolor != white]) [sprout-people 1]
end

Related

How to restrict Old vs. Young: in netlogo?

I am trying to simulate the following situation. Young and old agents.
The old have few social contacts, slower and at the same time have a greater chance of dying.
Young people have more contacts .
Which group should be vaccinated first ?
I created two groups, assigned an infection algorithm, and assigned a social distance. But at the same time I do not see the relationship, the agents behave identically, where is the error?
I'm a beginner and don't want to complicate the model. please don't answer difficult I'm just learning
my code
;;breed [humans human]
;;humans-own [ old young ]
breed [young p-young]
breed [old p-old]
young-own [
days-after-infected
days-of-sick
]
old-own [
days-after-infected
days-of-sick ]
to setup
clear-all
setup-old
setup-young
setup-patches
reset-ticks
end
to setup-old
create-old population-old
[
setxy random-xcor random-ycor
set color 4
set shape "person"
]
end
to setup-young
create-young population-young
[
setxy random-xcor random-ycor
set shape "person"
set color 96
]
end
to setup-patches
ask patches [set pcolor 61]
end
;:ask old [random 360]
;;ask young [forward 1]
to go
move-old
move-young
transmission-old
transmission-young
incubation
sickness
tick
end
to move-old
ask old [
right random 360
forward 1
]
end
to move-young
ask young [
right random 360
forward 3
]
end
to add-carrier
ask one-of young
[set color orange]
end
to transmission-old
ask young with [color = orange]
[let susceptible-person one-of old in-radius 3 with [color = 4]
if susceptible-person != nobody
[ask susceptible-person
[if random 100 < transmission_rate
[set color orange]
]
]
]
end
to transmission-young
ask young with [color = orange]
[let susceptible-person one-of young in-radius 2 with [color = 96]
if susceptible-person != nobody
[ask susceptible-person
[if random 100 < transmission_rate
[set color orange]
]
]
]
end
to incubation
ask old with [color = orange]
[ ifelse days-after-infected < incubation_period
[set days-after-infected days-after-infected + 1]
[set color red]
]
ask young with [color = orange]
[ ifelse days-after-infected < incubation_period
[set days-after-infected days-after-infected + 1]
[set color red ]
]
end
to sickness
ask old with [ color = red ]
[ifelse days-of-sick < 14
[set days-of-sick days-of-sick + 1]
[ifelse random 100 < mortality
[die]
[ set color blue ]
]
]
ask young with [ color = red ]
[ifelse days-of-sick < 14
[set days-of-sick days-of-sick + 1]
[ifelse random 100 < mortality
[die]
[ set color blue ]
]
]
end
I am trying to infect some agents from others, while I am not interested in the incubation period and trying to link age (less movement and less contact) with morbidity.
But I see that the result is not presentable, there is no dependence on age and morbidity.
The transmission-old is your problem. You copypasted from transmission-young but forgot to replace ask young with ask old
to transmission-old
ask old with [color = orange]
[let susceptible-person one-of old in-radius 3 with [color = 4]
if susceptible-person != nobody
[ask susceptible-person
[if random 100 < transmission_rate
[set color orange]
]
]
]
end
I've been fighting for a week, I changed the concept, I think it's easier. How to describe the last block. If there is one infectious neighbor, he will transmit his infection with faithfulness....
breed [olds humanA]
breed [youngs humanB]
globals
[
humans
]
to setup
clear-all
create-olds humans_number * shareA
[
set color 64
set xcor random-xcor
set ycor random-ycor
set shape "person"
]
create-youngs humans_number * (1 - shareA)
[
set color 45
set xcor random-xcor
set ycor random-ycor
set shape "person"
]
set humans (turtle-set olds youngs)
ask olds
[
create-links-with (up-to-n-of (links_number_old * homophily_old) (other olds))
create-links-with (up-to-n-of (links_number_old * ( 1 - homophily_old)) (other
humans))
]
ask youngs
[
create-links-with (up-to-n-of (links_number_young * homophily_young) (other
youngs))
create-links-with (up-to-n-of (links_number_young * ( 1 - homophily_young))
(other humans))
ask patches
[
]
ask n-of 1 humans
[
set infected 1
set color red
]
reset-ticks
end
*to go
ask humans
[
if any? link-neighbors != red ]
[
set humans ......*
end

NetLogo Trouble detecting turtles in radius

I'm trying to have a car visit 50 different cities in the shortest route possible, and every time he visits a place he changes the colour of the city form blue to yellow and moves on, trouble is I'm finding errors trying to find a way to implement this. I'm getting many errors in the last line of code, where it says CAR expected 1 input, a number. Any help would be appreciated.
breed [cities city]
breed [cars car]
cars-own [history travelled-distance]
to setup
clear-all
reset-ticks
setup-patches
setup-turtles
end
to setup-turtles
ask n-of 50 patches with [pcolor = 55 and not any? other turtles-here][sprout-cities 1 [set color blue set size 2 set shape "square"]]
create-cars 1[
setxy -90 -90
set color red
set size 5
]
end
to setup-patches
ask patches [set pcolor green]
ask n-of 100 patches [set pcolor brown ask neighbors [set pcolor brown]]
end
to go
ask cars [
pendown
if history <= 50
[ ;set heading towards city in-radius 10]]
move-to min-one-of cities in-radius 360 [distance myself]
]
if car in-radius 5 = true [set color = yellow]]
end
set heading towards one-of cities in-radius 10

netlogo: how to separate two armies in 4 corners?

I have to do a program where two base fields are created for each of the two armies of agents that are going to meet. How do I put every army to its corners? Soldiers and generals are created for each army and have to be born in one of their corners aleatory. In what I have done, soldiers and generals are already created, but they are not being separated by the army. (I wanted to do [army 1] [army 2]).
breed [ soldiers soldier ]
soldiers-own [ army ]
breed [ generals general ]
generals-own [ army ]
globals [ converted-soldiers death-by-energy death-by-combat ]
turtles-own[ energy max-energy ]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
set-patch-size 15
ask patches[
if (distancexy 20 20) < base-size [set pcolor yellow]
if (distancexy -20 -20) < base-size [set pcolor yellow]
if (distancexy -20 20) < base-size [set pcolor pink]
if (distancexy 20 -20) < base-size [set pcolor pink]
]
end
to setup-turtles
create-soldiers init-soldiers [
set shape "person"
set color red
set energy init-energy
set max-energy 100
setxy random-xcor random-ycor
while [pcolor = black or pcolor = yellow]
[setxy random-xcor random-ycor]
]
create-generals init-generals [
set shape "person"
set color green
set energy init-energy
set max-energy 100
setxy random-xcor random-ycor
while [pcolor = black or pcolor = pink]
[setxy random-xcor random-ycor]
]
end

How do I ask an agent that isn't me or one of my neighbours?

In Netlogo, how can I create an agentset for a turtle that contains all other turtles except them and their in-link-neighbors?
Thank you,
Thomas
This is so close to creating an agentset of turtles excluding neighbors, but doesn't quite work:
to setup
ca
create-turtles 10 [setxy random-xcor random-ycor]
ask turtles[create-link-to one-of other turtles]
end
to go
ask one-of turtles[
show in-link-neighbors
let poss turtles with [not member? self in-link-neighbors]
show poss
]
end
The above code came from: this previous answer
This will do the job, although it's not pretty.
to setup
ca
create-turtles 10 [setxy random-xcor random-ycor set color yellow set shape "circle"]
ask turtles[create-link-to one-of other turtles]
end
to go
ask one-of turtles[
set color green
ask in-link-neighbors [set color green]
ask one-of turtles with [color != green] [set shape "person"]
]
ask turtles [set color yellow]
end
The most straightforward way is:
turtles with [not link-neighbor? myself]
Here's an example showing this in action:
observer> crt 10 [ create-links-with other turtles ]
turtles> fd 10
observer> ask turtle 0 [ show link-neighbors ]
(turtle 0): (agentset, 9 turtles)
links> if random 2 = 0 [ die ]
observer> ask turtle 0 [ show link-neighbors ]
(turtle 0): (agentset, 7 turtles)
observer> ask turtle 0 [ show turtles with [not link-neighbor? myself]]
(turtle 0): (agentset, 3 turtles)
That's for undirected links. If your links are directed, substitute in-link-neighbor? or out-link-neighbor?, as appropriate.

How to stop the ticks using a breed in netlogo?

I want to create a Democracy Model. I have created 4 breed for that. One for people who will vote, the rest 2 for parties. In my code, I wand to stop the model when any of the parties reach a total number of 100 votes. I can't figure it out. Please help me. Here's my code:
breed [people p]
breed [party1 p1]
breed [party2 p2]
breed [party3 p3]
party1-own [vote]
party2-own [vote]
party3-own [vote]
to setup
clear-all
setup-patches
setup-people
setup-parties
reset-ticks
end
to setup-patches
ask patches [
ifelse pxcor >= 4 and pycor >= 6
[set pcolor white]
[set pcolor brown]
]
end
to setup-people
set-default-shape people "person"
create-people number-of-people
ask people [setxy random-float -16 random-float -16]
end
to setup-parties
set-default-shape party1 "person"
set-default-shape party2 "person"
set-default-shape party3 "person"
create-party1 1
create-party2 1
create-party3 1
ask party1 [setxy 15 -1]
ask party2 [setxy 15 -3]
ask party3 [setxy 15 -5]
ask party1 [set color blue]
ask party2 [set color green]
ask party3 [set color yellow]
end
to go
start-voting
ask party1 [
if (vote) >= 100 [stop]
]
ask party2 [
if (vote) >= 100 [stop]
]
ask party3 [
if (vote) >= 100 [stop]
]
tick
end
to start-voting
let x random 3
ifelse x = 2
[ask party3 [set vote vote + 1]]
[
ifelse x = 1
[ask party2 [set vote vote + 1]]
[ask party1 [set vote vote + 1]]
]
ifelse show-votes?
[
ask party1 [set label vote]
ask party2 [set label vote]
ask party3 [set label vote]
]
[
ask party1 [set label ""]
ask party2 [set label ""]
ask party3 [set label ""]
]
end
It looks like the problem with your stop procedure is that a turtle can only stop its own role in a procedure. check out the procedure manual regarding buttons for a more thorough explanation, where it is stated:
In a turtle or patch forever button, the button won't stop until every turtle or patch stops -- a single turtle or patch doesn't have the power to stop the whole button
So it looks like what's happening is that your first group is stopping the procedure, but since that happens after voting happens, and the other turtles are not stopped, the votes will continue to be added and the procedure will continue to run. In this case, it's probably better to have a global-level stop condition, as below. Note that when the observer queries a turtle using of, the variable is returned in a list, so item 0 is needed below.
to go
start-voting
if ( item 0 [vote] of party1 > 100 ) or ( item 0 [vote] of party2 > 100 ) or ( item 0 [vote] of party3 > 100 ) [
stop
]
tick
end