netlogo: Check whether a certain turtle is ahead of current moving turtle - shapes

A. In NetLogo, I want to check which type of turtle is ahead of the current moving turtle. I try to do this through looking at the shape or color of the turtle ahead. Context: i want the moving turtle to check whether it meets a moving enemy, if so, the moving turtle should avoid the moving enemy
I tried this line as condition:
if [color] of turtles-on patch-ahead 0 = yellow [do this]
B. And I would like to check whether a certain turtle is overlapping the current moving turtle with this code as condition.
if [color] of turtles-here = yellow [do this]
It doesn't give an error, but it doesn't do as intended.

[color] of turtles-here
returns a list of colors. yellow returns a number. So you're comparing a list with a number rather than a number with a number.
I think you'd like:
if any? turtles-here with [color = yellow] [do something]

Related

How do I correctly describe this 4x4 square in my K-map?

I am trying to find a (SoP)-expression using the embedded K-map. I have a box of size 4x4 which is a permitted use however I am having a hard time understanding how I could implement it.
To me the 4x4 box represents that the output is always 1 independet on any of the variables. Then I'd like to use the 2x4 box to the right and produce:
1 OR (Qc AND !Qd), but this does not produce the correct result.
I can see several alternative ways to produce the correct result. My questions are specifically:
Why can't I use the 4x4 box, or perhaps, how do I represent it correctly?
How do I know when I can represent parts of the output as a 4x4 box?
Perhaps Im missing something more fundamental.
Thx in advance.
The point of placing rectangles in a K-map is to eliminate variables from an expression. When the result of a rectangle is the same for the variable values X and X', then the variable X is not needed and can be removed. You do this by extending an existing rectangle by doubling the size and eliminating exactly one variable, where every other variable stays the same. For the common/normal K-map with four variables this works with every such rectangle because in a way the columns/rows are labelled/positioned. See the following example:
The rectangle has eliminated the variables A and B, one variable at a time when the size of the rectangle has been extended/doubled. This results in the function F(A,B,C,D) = C'D'. But check the following K-map of four variables:
Notice that the columns for the D variable has been changed (resulting in a different function overall). When you try to extend the red rectangle to catch the other two 1 values as well, you are eliminating two variables at the same time (B and D). As you cannot grow the rectangle anymore, you are left with two rectangles, resulting in the function F(A,B,C,D) = BC'D' + B'C'D (which can be simplified to C' * (BD' + B'D)).
The practice in placing rectangles in the K-map isn't just placing the biggest rectangle possible, but to eliminate variables in the right way. To answer your questions, you can always start with the smallest rectangle and extend/double its size to eliminate one variable. See the following example:
The green rectangle grows in these steps:
Start with A'BC'D'E
Eliminate the (only) variable A by growing "down", resulting in BC'D'E
Eliminate the (only) variable D by growing "right", resulting in BC'E.
But now, the rectangle cannot grow/double its size anymore because that would eliminate the variable E, but also somehow eliminate the variable C. You cannot eliminate the variable E, because you have 0 values to the left of the green rectangle and 1 values to the right of the green rectangle (all in the left half of the K-map, where you have the value C'). The only way to increase/grow the rectangle is to get the "don't care" values to eliminate the B variable (not shown here).
The overall function for this K-map would be F(A,B,C,D,E) = C'E + DE' + CD' (from three 2x4 rectangles).

How to iterate over a pandas data frame when using the basemap function to check the result

I am trying to write apython script that will do a geometric transformation for the long/lat values, namely the rotation. However, I want to resulted long/lat to preserve the location, for example if the otiginal coordinate is in the land I want the rotated one to be also in land. Therefore, I have used basemap
I wrote the following script and it works fine, till I tried to add a while loop. My objective from adding the while loop is to compare the original value with the rotated one and if they dont match, the rotation angle will change till they match. I will show my concept without adding the while loop first:
def rotation(dfc):
# Rotation
choose a rotation angle that will match the location of the rotated
coordinates, and set the result to 1 if they match
while row['result']=0
choose another rotation angle
do the rotation again
check the result of the rotated points
if still they dont match stay in the loop and choose another alpha
if not break and go to the next row
I know it needs simple steps, but I am not able to figure out how to add the loop for row wise functions in dataframes
Actually this problem can be solved without using df.iterrows.
I just initiated a variable to be equal 0 and then added a while loop. So my pseudocode looks like :
counter=0
while counter != len(df['col1'])
choose new random variable
do the calculation using dfc.apply()
re-calculate counter

Netlogo: Choosing an agent to run a procedure from variable values

I am creating a taxi-like simulation in which vehicles search for customers. When a vehicle is within 1 unit distance of the customer, it picks up the customer and takes it to its destination (and begins searching for customers again, etc.).
My issue: if 2 vehicles come within 1 unit distance of the same customer within the same time-step, I need the vehicle with the highest [rating] (vehicles-own variable) to always get the customer. This is my code (updated from a previous post):
to find-customers
if (distance closest-customer <= 1)
[ask closest-customer [check-for-vehicles]]
end
to check-for-vehicles
set competitors vehicles in-radius 1
determine-highest-rated
end
to determine-highest-rated
set highest-rated max-one-of competitors [rating]
ask highest-rated [set color red]
end
[competitors] and [highest-rated] are waiting-customer-own variables. closest-customer is min-one of waiting-customers (distance myself). Normally [set color red] would run the procedure in which the vehicle gets the customer transport information. When forcing two vehicles on the same customer, the correct vehicle always sets its color red. Sometimes the incorrect vehicle also sets its color red. When I check the variable values for the waiting-customer, the correct vehicle is identified as highest-rated, yet sometimes the other vehicle still turns red. There is obviously a mistake with how I've set up the procedures. If anyone has suggestions on how to fix this (or how to approach this task differently) it would be appreciated.
I think the reason why your ifelse always return false is because the ifelse is placed at the same time step as the initialization of identificationfor each taxi.
The way netlogo works when running some function to an agenset is, each agent, one by one will individually run the function in random order, not in the same time.
So, if the ifelse is placed just right after the identification is set and in the same function, it will always return false since the other vehicles haven't got their identification set yet. Then it will directly run make-deal before even compare it to other-vehicles.
for example:
ask vehicle [
set identification ...
ifelse ... [..]
[..]
]
is different with
ask vehicle [set identification ...]
ask vehicle [ifelse .... [...][...] ]
The first one will make each agent set their identification and do the ifelse statement before make other agent do.
The second one will make every agent set their identification first then make every agent do the ifelse statement.
The conclusion is, I suggest you to separate the find-costumer function and the make-deal function.
I hope this helps

Turtles dying according to slider

I want to make a certain number of turtles (controlled by a slider) die every year. So far I got this, it is probably very straightforward but I can't seem to make it work.
Thanks a lot!
to hunting
let huntedturtles (count turtles = hunted-turtles) ; Hunted-turtles is the slider.
if ticks mod 365 = 0
[ask huntedturtles [die]]
set hunted hunted + hunted-monkeys
end
You're getting an error like ERROR: ASK expected this input to be an agent or agentset, but got a TRUE/FALSE instead, correct?
count turtles = hunted-turtles is checking whether or not the total number of turtles is equal to hunted-turtles. I don't think that's what you want. Instead, you probably want something like
let huntedturtles n-of hunted-turtles turtles
That's going to randomly choose hunted-turtles turtles.
Side note: huntedturtles and hunted-turtles are easily confusable variable names. Consider making the slider num-hunted-turtles or something similar, and the set of hunted turtles themselves hunted-turtles.

Is there a workaround for using the > operator on more than one turtle breed?

My model has a a number of turtle breeds and I want them to avoid each other according to size. So turtle 1 with size = 1 will avoid turtle 2 with size = 2 and so on.
The code that generates the error is:
ask turtles with [color = green]
[if not any? turtles in-radius vision with [size > self][avoid]
And the error I get is
"The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a number and a turtle."
I think I understand the error but my question is is there a workaround for this problem?
Thanks
This has nothing to do with breeds. The problem is the one described by the error: you can't compare size directly with self (which is a turtle, not a number).
You need to do:
with [ size > [ size ] of myself ]
(And note that inside the with block, you need to use myself instead of self.)