Netlogo - Origin in the corner with code - netlogo

I need to have a martix of patches which axis are long a power of 2 ( 2^n ) for example 16.
I thought this can be done or having the corner but i need to do this with a line of code because i want it to be set with the setup button.

You can do this with resize-world:
resize-world 0 (2 ^ n - 1) 0 (2 ^ n - 1)
You may also want to set the patch size with set-patch-size so that the view remains stays the same size no matter how many patches you have:
set-patch-size 400 / (2 ^ n)

Related

Basic question in NetLogo (create an equation)

I have a very basic question on NetLogo. However, I don't know how to solve it. I would like to run an equation and have the value of that equation used by all turtles. I made the following code:
to equation
ask turtles [
set 10 ^ ( - 0.1 + 2.0 * log Size 10 ) * 1000
]
end
Can someone help me?
Thanks
you need to assign the answer to the equation to some variable. For example (not tested):
turtles-own [the-answer]
to equation
ask turtles [
set the-answer 10 ^ ( - 0.1 + 2.0 * log Size 10 ) * 1000
]
end
The turtles-own statement sets up an attribute for each turtle so that each turtle has its own value of that attribute (and therefore different turtles can have different values because their size varies)
If the turtles all need the same value, the calculation could be done just once in order to improve runtime. Additionally consider saving that value as a global variable.
globals [the-answer]
to equation
set the-answer 10 ^ ( - 0.1 + 2.0 * log Size 10 ) * 1000
end

NetLogo - no more than 5 % of population has a certain value of variable

How can I give each tick a random amount of turtles a change in a binary variable (1 or 0), whereas no more than 5 % of the existing population at all times has a value of 0 in that variable?
In other words, I wish to have that the total amount of turtles having a variable value of 0 is between 0 % or 5 % of the total amount of turtles at every tick.
How can I achieve this?
My code is:
to setup
create-turtles 100
set var random 1 (only 5 % max shall have a 0 at start)
end
to start
change
end
to change
let %draw (random 1)
if (%draw < 0) … ; than I do not how to continue
end
The n-of primitive selects the specified number of agents. You want some number up to that, so you also need to randomly generate the number. Something like this:
to setup
create-turtles 100 [ set var 1 ] ; give them all value 1
ask n-of random 6 turtles [ set var 0 ] ; randomly selects 0 to 5 turtles, assigns value 0
end

Create 1000 turtles near each others

I have to create a lot of turtles forming a compact group of any shape, a simple 10x100 rectangle is enough. The important thing is that they must be near each others.
In c i would do something like this:
for(x = 1; x <= rows; x++)
{
for(y = 1; y <= columns; y++)
{
create_turtle(x,y);
}
}
And the equivalent in netlogo would be:
crt 1000
let n 0
let x 1
let y 1
while[y <= 10]
[
set x 1
while[x <= 100]
[
ask turtle n
[move-to patch x y]
set x x + 1
set n n + 1
]
set y y + 1
]
But it's not an elegant solution. Any suggestion?
Edit: More precisely I have to reproduce what has been done in this article: http://science.sciencemag.org/content/345/6198/795.full
Every turtle is a little robot.
And here you can see one way turtles could be positioned turtles schema
I'm using circle turtles like the robots of the article.
One of the trickiest things for programmers from other languages to do when learning NetLogo is getting rid of all the loops. Iterating through the agents or patches is embedded in the ask primitive, you don't need to code the iteration. ask also iterates in a random order so that repeated processes don't lead to any advantage to whichever agent happens to be first in the loop.
Also, when you create turtles, you can immediately give them instructions. You can also place them initially in an arbitrary position rather than move them there. Here is one solution that places them all in a rectangle that is 5 patches to the left/right of centre (0,0) and occupies half the height of the world.
create-turtles 1000 [ setxy random-float 10 - 5 random-ycor * 0.5 ]
From the edit, I think you are wanting them to be created at gridpoints rather than randomly within the space. If that is true, then select the patches you want and ask them to sprout a turtle.
let in-shape patches with [ pxcor >= -10 and pxcor <= 10 and pycor >= -10 and pycor <= 10 ]
ask in-shape [ sprout 1 ]
You will need to work out your own values and make sure they are within the world dimension.

Making box in the middle of the world by exact percentage from the overall space

I want to make box in the middle of the Netlogo world
I managed to make a box but in the corner of my space the location of the origin is corner and the max pxcor = 9 and the maxpycor = 9
The code for 25%
to setup-area-25%
ask patches with [pxcor >= 5 and pycor >= 5] [ set pcolor blue ]
end
and the other code for 50 %
to setup-area-50%
ask patches with [pxcor >= -5 and pycor >= 5] [ set pcolor blue ]
end
I want to make blue area represent 25 and 50 % of the world but in the middle of the world I tried to use in radius but it did not give me a right area.
Thanks in advance
Here is a quick and dirty way that won't necessarily give you the exact percentage that your looking for, but could be good enough, depending on what you are trying to do:
to make-box [ pct box-color ]
let side round sqrt (count patches * (pct / 100))
let x min-pxcor + ceiling ((world-width - side) / 2)
let y min-pycor + ceiling ((world-height - side) / 2)
ask patches with [
pxcor >= x and pxcor < x + side and
pycor >= y and pycor < y + side
] [
set pcolor box-color
]
end
Calling make-box 25 red should give you a red square that is about 25% of the overall area, calling make-box 50 blue should give a blue square that is about 50%, etc.
The code uses the square root of the desired box area as the side of the box to draw. Not all numbers are perfect squares, however, and this is why you don't always get the exact percentage that you are looking for. You could try to look for the closest factor pair instead, but in some cases, they're just not very square. For example, 50% of the default NetLogo world size is 544.5 patches. If we round this up, we get 545 patches: not a perfect square. The closest factor pair that will give you exactly 545 is 109 * 5, which is probably not what you want.
Edit:
Here is a version that uses the closest factor pair, thereby always giving an area equal to the desired percentage of the world (rounded to an integer number of patches, but that can't be avoided). Just be warned that the box may end up being much more rectangular than square; so much that it very well may wrap around the world. You'll have to vary world-size or requested percentage if you want to avoid that.
to make-box [ pct box-color ]
let n round (count patches * (pct / 100))
let h height (floor sqrt n) n
let w (n / h)
let x min-pxcor + ceiling ((world-width - w) / 2)
let y min-pycor + ceiling ((world-height - h) / 2)
ask patches with [
pxcor >= x and pxcor < x + w and
pycor >= y and pycor < y + h
] [
set pcolor box-color
]
end
to-report height [ h n ]
report ifelse-value (n mod h = 0) [ h ] [ height (h - 1) n ]
end
The algorithm for finding the closest factor pair loosely follows this answer.

Facebook Puzzle(Probability)

Below is one of the facebook puzzle:
I am not able to understand how to proceed for this.
You are given C containers, B black balls and an unlimited number of white balls. You want to distribute balls between the containers in a way that every container contains at least one ball and the probability of selecting a white ball is greater or equal to P percent. The selection is done by randomly picking a container followed by randomly picking a ball from it.
Find the minimal required number of white balls to achieve that.
INPUT
The first line contains 1 <= T <= 10 - the number of testcases.
Each of the following T lines contain three integers C B P separated by a single space 1<= C <= 1000; 0 <= B <= 1000; 0 <= P <= 100;
OUTPUT
For each testcase output a line containing an integer - the minimal number of white balls required. (The tests will assure that it's possible with a finite number of balls)
SAMPLE INPUT
3
1 1 60
2 1 60
10 2 50
SAMPLE OUTPUT
2
2
8
EXPLANATION
In the 1st testcase if we put 2 white balls and 1 black ball in the box the probability of selecting a white one is 66.(6)% which is greater than 60%
In the 2nd testcase putting a single white ball in one box and white+black in the other gives us 0.5 * 100% + 0.5 * 50% = 75%
For the 3rd testcase remember that we want at least one ball in each of the boxes.
You will probably have to do the following:
Initial no. of white balls Nw = 1.
Given the number of white balls, Nw, find the configuration that gives the maximum probability of picking a white ball.
Check if this probability is greater than P.
If yes, then Nw is your answer, else, increment Nw and goto 1.
Ofcourse the challenge is to find the best configuration in Step 1.
EDIT: The problem now boils down to given W white balls, B black balls, and C containers, find the configuration that gives the maximum probability of picking a white ball.
P = ( w1/(w1+b1) + w2/(w2+b2) + ... + wc/(wc+bc) ) /c.
Max(P) = Max ( w1/(w1+b1) + w2/(w2+b2) + ... + wc/(wc+bc) )
Given: summation(wi) = W, summation(bi) = B, wi + bi >= 1
I am guessing that the configuration shall be such that if there are N containers having white balls, then atleast N-1 shall have only 1 white ball and no black balls and at most 1 container shall have both white balls and black balls. Just a guess though...