How can I make an apple fall from a tree? I created the shape "apple" already, and I have the two main element (a tree and an apple) but they're not linked to each other. Can someone help me?
Since this looks like a homework problem I'll just get you started.
NetLogo is not a gaming platform so it doesn't have "sprites" or collision-detections built in. You have to do all the math yourself.
You need to reset the x and y coordinates of your apple each iteration of the GO step.
Here's the key equations you will need:
;; https://en.wikipedia.org/wiki/Equations_for_a_falling_body
;; distance fallen = 1/2 g t^2
set distance-fallen 0.5 * 9.8 * time * time ;; in meters and seconds
set height ( branch-height - distance-fallen )
set ycor height
Related
Please let me preface this with I am not a programmer by trade. I am a social scientist using M&S for some research. That being said, my goal is to use ABM (Netlogo) to create a model of state antifragility. That is a model of states that get better under stress rather than deteriorate or are resilient (return to their pre-stress levels).
The idea for this model is simple. Create agents (states) that have three properties-loops, capacity and performance. The Capacity of a state is defined as it's Agility + it's Learning + it's Power Conversion. ( I've done some regression analysis to see what the relationship between each of these variables and stress is and they are listed below. ) The state also has two loops-fragile and antifragile that are always running, but depending on the performance value and the stress value only one of those loops will activate to update the capacity/performance values to deal with the next stress. Finally, a state's performance ties all those together. That is, it is a function of capacity and stress.
States randomly roam about the world full of shocks (red patches with an intensity value). When the state comes across the patch, it interacts with that patch. To interact, it activates a loop and then performs and update of its capacity and it's performance for the next stress. This happens until a state dies (it's too fragile and fails) or it becomes the maxium value of antifragile.
UPDATED CODE BELOW ( 22 July) JenB, I took your advice and stripped it down to it's basic parts by removing the looping functions. Eventually, I'd like to get there in a future iteration, but for now I removed it. The code below is my stripped down, updated version. Where I am stuck is the stop movement function.
Starting with the latter, I am still unsure how to set something true or false. I kept getting an error about a string not a value for that command. (Again, I am really, really a beginner at this and even reading the Netlogo Dictionary isn't that easy for me.)
Now that I think about it there is a third thing. I'd also like to randomize the value of the red patches ("shocks"). I believe the way I have it set up right now is that they all have the same value, but I'd like some to be bigger or smaller.
Thank all of you for all your help in this journey!
Please see the code below:
breed [states state]
states-own [learning powerconversion agility performance category]
patches-own [intensity]
;; TO SET UP ;;
to setup
clear-all
grow-shocks
set-default-shape states "dot"
create-states 10 [
setxy random-xcor random-ycor
set color blue
set size 2.5
set performance random 100
set learning random 10
set powerconversion random 10
set agility random 10
]
reset-ticks
end
;; TO GROWN SHOCKS ;;
to grow-shocks ;; to grow shocks in the international environment do the following:
ask n-of number-shocks patches [
set pcolor red ;; make them red
set intensity random 10 ;; set their intensity to a random number between 0-10
]
end
;; TO GO ;;
to go
ask states [ ;; ask states to do the following : move, interact, update-category (AF, R, F)
move
interact
update-category
]
tick
end
;; TO MOVE ;;
to move ;; to move do the following:
right random 15
left random 15
forward 1
if abs pxcor = max-pxcor
[ set heading (- heading) ]
if abs pycor = max-pxcor
[ set heading (180 - heading) ]
end
;; TO INTERACT ;;
to interact ;; to interact with shocks do the following:
if pcolor = red [
set pcolor black set intensity 0 ;; if the patch is red, turn the patch black and set intensity to 0
update-performance ;; update-performance
update-category
]
end
;; TO UPDATE PERFORMANCE ;;
to update-performance ;; to update a state's performance do the following:
set performance (((-0.13 * learning ^ 3) + (1.89 * learning ^ 2) + (-5.72 * learning) + 8.13 ) +( 0.09 * agility ^ 3) + (1.29 * agility ^ 2) + (-3.45 * agility) + 5.57 +( 0.02 * powerconversion ^ 3) + (-0.89 * powerconversion ^ 2) + (9.93 * powerconversion) + -17.51 )
end
;; TO UPDATE STATE TYPE;;
to update-category ;; to update the state's type do the following:
if performance > 75 [ ;; if the state has a performance value of over 75, turn it green indicating an antifragile state
set color green
]
if performance < 74 and performance > 35 [ ;; if the state has a performance value between 35 and 74, turn it yellow indicating a robust state
set color yellow
]
if performance < 34 and performance > 5 [ ;; if the state has a performance value between 1 and 34, turn it red indicating a failed state
set color red
]
So I am guessing the line throwing the too large error is this one:
to activate-antifragileloop
set performance e ^ performance
end
This seems like a logic error to me. From the rest of your code, I am guessing performance numbers are generally in the range 0 to 100. Do you realise that e^100 = 2.6881171 x 10^43? It seems unlikely that you want performance to switch between numbers like 100 and numbers with 43 digits. I can't imagine a real world situation that you could be trying to represent where a key characteristic of the entities in the system has such a wildly varying value.
On the question of stopping, I would have another variable named something like done? that starts as false then you set it to true when you want it to stop. Then you can simply have ask states with [not done?] instead of ask states for any code that moves them around etc.
You said you are not a programmer. One of the best things a beginner can do is to make only minimal changes at a time. You have several problems with your code - this should almost never happen. Your life will be much easier if you only make one change at a time and get it working correctly before moving on. This is particularly important with NetLogo where it can be very difficult to work out whether something is working correctly because you are modelling systems that interact - how do you know whether the interaction is producing the results or a bug is producing the results?
I was trying to model an agent-based anylogic model where pedestrian agents will maintain a distance of 6 feet between them when moving in the continuous space. How I can do that?
I tried to put a circle of 6 feet radius around the agent and iterated whether any other agent is within that "socialDistance" circle. If yes, then the person agent will move (current distance between agents + 6 units) to maintain social distance. I used "moveTo" & I know it's not the right command. Additionally, my distance calculation is also not right. For example, if the agents are in 5 feet distance initially, I command asks to move (5+6=11 feet), which should not be the situation. Moreover, sometimes, my code may push the agent out of the simulation area (seems to me).
So, in summary, can someone help me to model that 6 feet distance between agents? It will be a great help.
Code (wrong!) that I tried:
for (person socialD : get_Main().people)
{
double pedSocialX = socialD.getX() -this.getX();
double pedSocialY = socialD.getY()- this.getY();
if (this.socialDistance.contains(pedSocialX, pedSocialY))
{
double a= socialD.getX()+ 6;
double b= socialD.getY()+ 6;
moveTo( a, b);
//[this is wrong command, what should be a good command?]
traceln ("social distance not mainted");
}
else
traceln (" social Distance maintained");
errors:
In setup, I draw a bunch of turtles--as small circles--to display two curves defined by functions. A very simple way to do this is
ask patches with [pycor = (myfunction pxcor)] [sprout 1 [...]]
and that's what my code does at present. It's kind of wasteful, since every patch has to be consulted--in random order--for each curve, but it's simple and easy to read, and it only happens during setup.
However, there's a little bit of a pause as the curves are constructed. If I move the speed slider all the way to the right, the pause is not noticeable. If I wrap the curve display routines in no-display and display, the user doesn't see the curves being constructed, but the speed is unchanged, AFAICS. If I move the slider to the left, it takes a long time to construct the curves even with no-display; the user doesn't see the points being placed one by one, but nevertheless has to wait while twiddling her/his thumbs.
Is there a way to set the model speed programmatically (for normal, "headfull" use)? I don't want to tell users "Move the speed slider to the right, then press setup, then move it back to the center before pressing go.
If not, maybe I'll code the curves properly using loops, but I thought I'd ask. Seems like there would be a way to do this, but I haven't found anything in the dictionary or programming docs so far.
(edit: no-display, if it did help, isn't available in NetLogo Web, which I am targetting along with regular NetLogo.)
I don't believe there is. However, you are asking all patches, when you could simply ask the pxcor values. This should speed it up a lot - square root of the number of iterations if a square world. Something like:
to testme
clear-all
let counter min-pxcor
while [counter <= max-pxcor]
[ let fn-out (function counter)
if fn-out >= min-pycor and fn-out <= max-pycor
[ ask patch counter fn-out [ set pcolor red]
]
set counter counter + 1
]
end
to-report function [#invalue]
report #invalue ^ 2
end
I have been wanting to try and make a neural net that controlls a game.
I'm very close to my goal, but yet I feel like I have messed up somewhere!
I'm getting values that are reasonably close to what I'm expecting. But there is one problem...
I decided to try it with the Flappy Bird concept...
I have 3 inputs currently.
1) Height from ground (goes toward 1 the closer it gets)
2) Distance
to nearest wall in the top of the screen
3) ... bottom of the screen.
3 Hidden neurons, and one output.
It kind of jump when it gets to a lower wall. But it ALSO jumps when it reaches a upper wall. Thus hitting it in steath of dropping down lower.
My question really, is there some way of turning that jump into drops insteath?
Or do any of you suspect where I might start looking?
1) I know i have trouble with the sigmoid function. I have no idea how to write it actually. It says
f(x) = 1 / (1 + e^x)
Can I just swap e with a number or something? What is that character? I never really heard about it in the physics math I had...
The derivative is actually easier it seems.
But this is the right formula, right?
fd(x) = f(x) * (1 - f(x))
The function
f(x) = 1 / (1 + e^(-x))
is just an approximation of the step function that is also differentiable.
(where e is the natural logarithm.)
You can find a number of examples of Neural Networks playing Flappy Bird. For example, this developer seems to only use 2 inputs, 2 hidden, and 1 output. His inputs only seem to be distance to the top wall and distance to the bottom wall.
I think this is a better solution than your 3 inputs - "distance from the bottom of the screen" is probably not an effective feature.
Note: This is also dependent on how you are calculating distance to each wall.
I have distributed turtles on the world having size x and I wish to increase their size to y but the I want to keep their location of their lower further point same (Check figure below). How can one accomplish this?
EDIT:
I wished to write a procedure that could be applicable for all turtle heading, that is if the turtle is heading 0 or 90 or 45. Direct math in such case could be complicated.
As Seth said, this should be relatively straight forward math. If you don't want to do the math, though, and this is only for visual purposes, you could make a new turtle shape, where the "bottom" of the shape is actually at the center. Then, when turning, it will like the turtle is turning around their bottom. When changing size, again, the "bottom" will stay in the same place.