I am trying to model my agents effects on their patch landscape, ie harvesting grain over a period of time. I have a variable that patches-own penergy, I would like to plot the total penergy across the behaviorspace over time. I have an idea how to do this but I don't think I am doing it the most effective way.
To complicate matters, I have multiple grain types, ie ptype. I would like to plot these types independently.
to patch-health
if ptype = 1[
let new-penergy min(list 10 (penergy))
let diff (new-penergy - penergy)
set Wheat-health ((sum ptype1-health) + diff)
]
if ptype = 0 [
let new-penergy min(list 10 (penergy))
let diff (new-penergy - penergy)
set Barley-health ((sum ptype0-health) + diff)
]
end
to-report ptype1-health
if ptype = 1 [
report [penergy] of patches
]
end
to-report ptype0-health
if ptype = 0 [
report [penergy] of patches
]
end
It produces a list of each patches penergy but I added the sum pytpe0-health function to convert the list to a total penergy. The plot works but the model is bogged down and running very slow.
I want think this is easy but can't seem to figure it out. Is there a more efficient way to achieve similar results?
Related
So I am a literal beginner in R but we have been given a challenging task. I did what I could but I don't know how to check if code is correct. Can anyone help tell me if the code is correct and how to test-run my code?
This is my code:
colorgame = function(games){
money = 0
set.seed(100) # Pseudo-RNG, so we can generate results
for(i in 1:games){
diceRoll = sample(1:6,1) # Represents the 6 different colors
if(diceRoll = 1) {# 1 represents the color the player always bets on
money = money + 100
}else {money = money - 80}
} return(money/games)
}
And this is the question:
Three colored dice are being tossed and the player will bet money to one of the 6 colors. A player wins the same amount, twice the amount, or three times the amount of money he bet if the color he chose matches the colors from the 3 dice that has been rolled. Suppose that a player only bets one color for every round. Simulate this game and show that a player will eventually lose his money.
Are you writing R code without having R available to test it? You need to make three corrections to get it running:
In the if statement, use == instead of = (== is a logical comparison, = is an assignment).
In R, you have to use the next line after }. You have to change that before else and before return. But you could also leave those {} out in your case.
colorgame = function(games) {
money = 0
set.seed(100) # Pseudo-RNG, so we can generate results
for(i in 1:games) {
diceRoll = sample(1:6,1) # Represents the 6 different colors
if(diceRoll == 1) money = money + 100 # 1 represents the color the player always bets on
else money = money - 80
}
return(money/games)
}
I want to count the neighbors and the neighbors neighbors of a turtle to kind of find out a way to calculate the eigenvector-centrality. Since I can´t get the NW:extensions to work and my prof neither. I thought of building a method myself. Since I don´t have that much time until the first round of presentation, I try my best to just count the neighbors and the neighbors neighbors of a turtle.
I decided to have a turtles-own, which counts all neighbors and then sum all the neighbors and neighbors neighbors. But I am stuck in my head and cant get it to work.
set Neighborscount count(link-neighbors)
Anyone has any ideas?
Also a final way to get the eigenvector-centrality to work would be very much appreciated.
First, my main recommendation is to get NW working. I'd be happy to help with that.
Barring that, the below assumes you're working with undirected networks. If that's not the cast, let me know.
The problem with the method that you describe is that it will count some neighbors-of-neighbors multiple times. Instead, you can get the agent-set of neighbors-of-neighbors, and then just count it:
to-report neighbors-of-neighbors
report turtle-set [ link-neighbors ] of link-neighbors
end
Then, [ count neighbors-of-neighbors ] of turtle 0 will give the number of neighbors of neighbors of turtle 0.
Now, a few points:
This will include the turtle itself, since a turtle is always a link neighbor of its link neighbors. If you don't want to include the turtle itself, you can just throw an other in there: [ count other neighbors-of-neighbors ] of turtle 0.
This won't include turtles that are link neighbors of turtle 0, but are not linked to any other neighbors of turtle 0. To add those in, you might consider a procedure like this:
to-report turtles-in-link-radius [ n ]
let result turtle-set self
repeat n [
set result (turtle-set result [ link-neighbors ] of result)
]
report result
end
This can then be used, for instance, like so: [ count turtle-in-link-radius 2 ] of turtle 0 to count all turtles at most 2 hops from turtle 0. This reporter has the same behavior as nw:turtles-in-radius, though it will be significantly slower.
Now, I first noted that your idea would count some turtles multiple times. Given that you're interested in something like eigenvector-centrality, maybe this is what you want. In that case you can do this: [ sum [ count link-neighbors ] of link-neighbors ] of turtle 0 to get it without having to bother with a turtles-own. That said, if you do use a turtles-own, you can actually calculate the eigenvector centrality itself:
turtles-own [
...
next-ec ;; helper variable for iteratively computing eigenvector-centrality
eigenvector-centrality
...
]
to calculate-eigenvector-centralities
ask turtles [
set eigenvector-centrality 1
]
repeat 100 [ ;; the higher this number, the more accurate the value. NW uses 100
ask turtles [
set next-ec sum [ eigenvector-centrality ] of link-neighbors
]
let max-ec max [ next-ec ] of turtles
ask turtles [
set eigenvector-centrality next-ec / max-ec ;; normalize
]
]
end
Call calculate-eigenvector-centralities to calculate all turtles eigenvector centralities. The turtles' eigenvector centralities will then be stored in eigenvector-centrality. Note that this code won't work for disconnected networks: each component has to be normalized independently, which is what NW does, but there's no easy way to do that in NetLogo without using NW.
I am building a simulation of the epidermal cells in NetLogo.
I have two type of cells, progenitors and post mitotic cells. The progenitors are the cells which can divide into either progenitor or post mitotic.
Each run begins with a number of 239 progenitor cells with each their own identity (from 1 to 239) which is a turtles-own. Each time a progenitor divides, the identity is given to the offspring.
I want to count the number of different identities after a certain amount of ticks. I tried it with a monitor and with a list but it did not work.
Here is a small part of the code I have made:
turtles-own [ identity]
globals [ id-count]
to setup
ask patches [sprout-postmitotic 1]
ask n-of (( 22 / 100) * count postmitotic) patches [
ask postmitotic-here [
hatch-progenitor 1
[
set identity id-count
set id-count id-count + 1 ]
die ]
]
end
to go
ask progenitor[
if random-float 1 < l [
hatch-postmitotic 1
]
end
If you post code for a question like this, try to produce a minimal example to support the question. Here are two approaches. (The first illustrates the use of table:counts, which gives access to more information.)
extensions [table]
turtles-own [identity]
to setup
ca
crt 1000 [set identity random 1000]
end
to-report id-count-01
let _idcts table:counts [identity] of turtles
let _unique table:keys _idcts
report length _unique
end
to-report id-count-02
let _unique remove-duplicates [identity] of turtles
report length _unique
end
I want to create a continuous futures series, that is to eliminate a gap between two series.
First thing I want is to download all individual contracts from the beginning to the now, the syntax is always the same:
Quandl("CME/INSTRUMENT_MONTHCODE_YEAR")
1.INSTRUMENT is GC (gold) in this case
2.MONTHCODE is G J M Q V Z
3.YEAR is from 1975 to 2017 (the actual contract)
With the data, I start working from the last contract, in this case "CME/GCG1975" and with the next contract "CME/GCJ1975". Then I see the last 6 values (are the more recent because date is descending) of the first contract GCG1975
require(Quandl)
GCG1975 = Quandl("CME/GCG1975",order="asc", type="raw")
tail(GCG1975,6)
order can be asc desc (ascending or descending), type can be : raw (data frame) ts xts zoo
And it outputs:
Image: quandl-1.png = Last values of GCG1975
Then I just want the 6th row starting from the final, and I want to eliminate the columns "Last" "Change" (this could be before starting processing each individual contract):
Image: quandl-2.png = Last 6th value GCG1975
Then I want to find the row with date 1975-02-18 (last 6th value GCG1975) in the next contract (GCJ1975):
Image: quandl-3.png = 1975-02-18 on GCJ1975
Then I compute the difference between the "Settle" of the G contract and the "Settle" of the J contract.
Difference_contract = 183.6 - 185.4
Difference_contract = -1.8
So that means that the next or J contract is 1.5 points up respect the before contract so we have to sum -1.8 to all the following numbers of the J contract (Open, High, Low, Settle), including the row 1975-02-18. This:
Image: quandl-4.png = Differences between contracts
And then we have a continuous series like this:
Image: quandl-5.png = Continuous series
All this differences and sums to make a continuous series is done since the last contract until the actual contract.
I think I can't post this because I don't have 10 points of reputation and I can just post 2 image-links.
Any guidance would help me, any question you have ask me.
Thanks and hope everything is well.
RTA
Edit: I have uploaded the photos and its links on post to my dropbox so you must look into it because Stackoverflow don't allow to post more than 2 links without 10 points of reputation.
Dropbox file
I have a model in NetLogo where agents (turtles) move around a landscape and produce other agents (eggs) at a set rate. The latter don't move. My aim is to collect the the coordinates of eggs and measure things like nearest neighbour distance.
In RNetLogo I have some code to do this:
NLCommand("setup")
NLDoCommandWhile("day < 10", "go")
eggcoords <- list()
eggcoords <- NLGetAgentSet(c("who","xcor","ycor"), "eggs")
The problem is that the model slows to a crawl as the number of eggs increases. One solution to this is to kill off the eggs at the end of the day but store their details in a list that I can update each day without overwriting anything. And that's where I'm stuck.
Hope you can help.
If your day is constant number of ticks (say 24), you could do something like this:
Create end-of-day procedure in your NetLogo model where you clear the eggs.
Then call your model like this:
turtles <- list()
NLCommand("setup")
# run for 10 days:
for (day in 1:10) {
NLCommand("repeat 24 [go]")
agent_set <- NLGetAgentSet(c("who", "xcor", "ycor",
"min [ distance myself ] of other turtles"),
"turtles")
names(agent_set) <- c("who", "xcor", "ycor", "nnd")
agent_set$day = day
turtles[[day]] <- agent_set
NLCommand("end-of-day")
}
Note:
In the for loop go is executed 24 times with one NLCommand call
You can use any NetLogo reporter in agent.var argument to NLGetAgentSet so you can calculate the nearest neighbour distance on the fly
with min [ distance myself ] of other turtles.
The result (turtles) is a list of data frames.
You can bind them to one data frame with df_turtles <- do.call(rbind, turtles)