NetLogo two agentsets operations - intersection

I have two agentsets. Are there functions for finding:
An agentset of agents that are present in both (intersection)
An agentset of agents that are present in one and not the other
I'm finding it very difficult to implement this by hand, especially when it's needed inside of a triple ask
Ideal use would be similar to with syntax:
let cross set1 and-in set2
let uniq set1 with [color = red] not-in set2
Simple things like "Is agent A in the agentset X?" are problematic

For the first one you use the turtle-set primitive. For the second one you need the member? primitive, which also works on agentsets. As such:
to setup
ca
create-turtles 10 [set color red]
create-turtles 10 [set color blue]
let red-ones turtles with [color = red]
let blue-ones turtles with [color = blue]
;join 2 agent sets
let joinset (turtle-set red-ones blue-ones)
show joinset
let even-ones (turtles with [who mod 2 = 0])
;subtract even-ones from red-ones
let subtractset red-ones with [not member? self even-ones]
show subtractset
end

Related

Netlogo: creating specific links between turtles with information from csv

I'm working on a simulation of social behavior in an emergency situation, using anonymized data from an actual event. Below is the section of code I'm using to create the 'people' turtles.
to read-people-from-file [filename]
let rows bf csv:from-file filename
foreach rows
[[row] ->
create-people 1
[
set size .46
setxy (item 0 row) (item 1 row)
set age (item 2 row)
set gender (item 3 row)
set visited? (item 4 row)
set group-number (item 6 row)
set group-type (item 7 row)
if group-number < 300 and ((person group-number) = (other group-number))) [create-links-with other person]
]
]
end
Everything works except for the group-number based links. I've tried a few different variations of it, with no luck except where I attempted if group-number <300 [create-links-with other people which worked creating links but was functionally useless. What I'm trying to do is set it so that every person who came together (who has the same group number) has a link with other group members. It's specifically less than 300 because numbers 300 and above are for people who came alone or other designations.
I could theoretically create the links by hand after the turtles are created, but that seems both like a waste of time (I have a dataset of over 400) and like something that makes the code significantly less applicable to other scenarios.
How do I make Netlogo create links between members of the same group, based on what's in the CSV?
Actually, the dream answer: how do I make Netlogo create different breeds of links (based on group-type) between members of the same group-number, based on what's in the CSV?
Edit:
In the end, I had to separate creating links from the initial agent setup.
This was what worked:
to soclink ;;groups that came together have links based type of relationship
ask people [if group-number < 300
[
if group-type = 1 [ask other people with [group-number = [group-number] of myself] [create-coworker-with myself]]
if group-type = 2 [ask other people with [group-number = [group-number] of myself] [create-friend-with myself]]
if group-type = 3 [ask other people with [group-number = [group-number] of myself] [create-partner-with myself]]
if group-type = 4 [ask other people with [group-number = [group-number] of myself] [create-family-with myself]]
if group-type = 5 [ask other people with [group-number = [group-number] of myself] [create-multiple-with myself]]
show count links]]
end
For the first part, maybe this would work- once your people are created, with their numbers assigned to group-number:
to group-link
ask people [
let my-group other people with [ group-number = [ group-number] of myself ]
create-links-with my-group
]
end
For the dream answer- it might depend on how many breeds of links you need. As far as I know, link breeds must be predefined- you could not programatically generate links as needed for different groups (although I am no expert- there may be a way). If you have your link breeds already defined, for example:
undirected-link-breed [ link-as link-a ]
undirected-link-breed [ link-bs link-b ]
Pretend now that your group-type is either "a" or "b", and you can do something like
to specific-link-breeds
ask people [
let my-group other people with [ group-number = [ group-number] of myself ]
if group-type = "a" [
create-link-as-with my-group
]
if group-type = "b" [
create-link-bs-with my-group
]
]
ask link-as [
set color red
]
ask link-bs [
set color blue
]
end
Edit: Changed turtles to people as it should be- thanks Mattsap.

Build network with shortcut using torch

I now have a network with 2 inputs X and Y.
X concatenates Y and then pass to network to get result1. And at the same time X will concat result1 as a shortcut.
It's easy if there is only one input.
branch = nn.Sequential()
branch:add(....) --some layers
net = nn.Sequential()
net:add(nn.ConcatTable():add(nn.Identity()):add(branch))
net:add(...)
But when it comes to two inputs I don't actually know how to do it? Besides, nngraph is not allowed.Does any one know how to do it?
You can use the table modules, have a look at this page: https://github.com/torch/nn/blob/master/doc/table.md
net = nn.Sequential()
triple = nn.ParallelTable()
duplicate = nn.ConcatTable()
duplicate:add(nn.Identity())
duplicate:add(nn.Identity())
triple:add(duplicate)
triple:add(nn.Identity())
net:add(triple)
net:add(nn.FlattenTable())
-- at this point the network transforms {X,Y} into {X,X,Y}
separate = nn.ConcatTable()
separate:add(nn.SelectTable(1))
separate:add(nn.NarrowTable(2,2))
net:add(separate)
-- now you get {X,{X,Y}}
parallel_XY = nn.ParallelTable()
parallel_XY:add(nn.Identity()) -- preserves X
parallel_XY:add(...) -- whatever you want to do from {X,Y}
net:add(parallel)
parallel_Xresult = nn.ParallelTable()
parallel_Xresult:add(...) -- whatever you want to do from {X,result}
net:add(parallel_Xresult)
output = net:forward({X,Y})
The idea is to start with {X,Y}, to duplicate X and to do your operations. This is clearly a bit complicated, nngraph is supposed to be here to do that.

python color entire pandas dataframe rows based on column values

I have a script that downloads a .csv and does some manipulation and then emails panda dataframes in a nice html format by using df.to_html.
I would like to enhance these tables by highlighting, or coloring, different rows based on their text value in a specific column.
I tried using pandas styler which appears to work however I can not convert that to html using to_html. I get a "AttributeError: 'str' object has no attribute 'to_html"
Is there a another way to do this?
As an example lets say my DF looks like the following and I want to highlight all rows for each manufacturer. i.e Use three different colors for Ford, Chevy, and Dodge:
Year Color Manufacturer
2011 Red Ford
2010 Yellow Ford
2000 Blue Chevy
1983 Orange Dodge
I noticed I can pass formatters into to_html but it appears that it cannot do what I am trying to accomplish by coloring? I would like to be able to do something like:
def colorred():
return ['background-color: red']
def color_row(value):
if value is "Ford":
result = colorred()
return result
df1.to_html("test.html", escape=False, formatters={"Manufacturer": color_row})
Surprised this has never been answered as looking back at it I do not believe this is even possible with to_html formatters. After revisiting this several times I have found a very nice solution I am happy with. I have not seen anything close to this online so I hope this helps someone else.
d = {'Year' : [2011, 2010, 2000, 1983],
'Color' : ['Red', 'Yellow', 'Blue', 'Orange'],
'Manufacturer' : ['Ford', 'Ford', 'Chevy', 'Dodge']}
df =pd.DataFrame(d)
print (df)
def color_rows(s):
df = s.copy()
#Key:Value dictionary of Column Name:Color
color_map = {}
#Unqiue Column values
manufacturers = df['Manufacturer'].unique()
colors_to_use = ['background-color: #ABB2B9', 'background-color: #EDBB99', 'background-color: #ABEBC6',
'background-color: #AED6F1']
#Loop over our column values and associate one color to each
for manufacturer in manufacturers:
color_map[manufacturer] = colors_to_use[0]
colors_to_use.pop(0)
for index, row in df.iterrows():
if row['Manufacturer'] in manufacturers:
manufacturer = row['Manufacturer']
#Get the color to use based on this rows Manufacturers value
my_color = color_map[manufacturer]
#Update the row using loc
df.loc[index,:] = my_color
else:
df.loc[index,:] = 'background-color: '
return df
df.style.apply(color_rows, axis=None)
Output:
Pandas row coloring
Since I do not have the cred to embed images here is how I email it. I convert it to html with the following.
styled = df.style.apply(color_rows, axis=None).set_table_styles(
[{'selector': '.row_heading',
'props': [('display', 'none')]},
{'selector': '.blank.level0',
'props': [('display', 'none')]}])
html = (styled.render())

Cluster in Haskell

How can I define a cluster in Haskell using list comprehension?
I want to define a function for the cluster :
( a b c ) = [ a <- [1 .. 10],b<-[2 .. 10], c = (a, b)]
In your comment you gave the example [(1,2,1),(1,3,1),(1,4,1),(1,5,1),(1,6,1),(1,7,1)].
In that example, only the middle number changes, the other two are always 1. You can do this particular one with
ones = [(1,a,1)| a<-[1..7]]
However, you might want to vary the other ones. Let's have a look at how that works, but I'll use letters instead to make it clearer:
> [(1,a,b)| a<-[1..3],b<-['a'..'c']]
[(1,1,'a'),(1,1,'b'),(1,1,'c'),(1,2,'a'),(1,2,'b'),(1,2,'c'),(1,3,'a'),(1,3,'b'),(1,3,'c')]
You can see that the letters are varying more frequently than the numbers - the b<-[1..3] is like an outer loop, with c<-['a'..'c'] being the inner loop.
You could copy the c into the first of the three elements of the tuple:
> [(b,a,b)| a<-[1..3],b<-['a'..'b']]
[('a',1,'a'),('b',1,'b'),('a',2,'a'),('b',2,'b'),('a',3,'a'),('b',3,'b')]
Or give each its own varying input
> [(a,b,c)| a<-[1..2],b<-['a'..'b'],c<-[True,False]]
[(1,'a',True),(1,'a',False),(1,'b',True),(1,'b',False),(2,'a',True),(2,'a',False),(2,'b',True),(2,'b',False)]

Octave vectorize strsplit return value into separate variables

I have a file with a list of records which I parse one line at a time. Each record is newline delimited and each value is space delimited. This is just a simplified example, but it has a similar structure to the real data.
Bob blue pizza
Sally red sushi
The first value is a name, then their favorite color, then their favorite food. Let's say this is in a processing loop and I want to set variables up for each value. For the first line, my values should look like this.
friendsName = "Bob";
favoriteColor = "blue";
favoriteFood = "pizza";
I read in the line and start out with
lineInFile = "Bob blue pizza";
strsplit seems like a good idea, but it outputs a cell array instead of a matrix of strings and I end up with
strsplit(lineInFile, " ") =
{
[1,1] = Bob
[1,2] = blue
[1,3] = pizza
}
I want something like
{friendsName,favoriteColor,favoriteFood} = strsplit(lineInFile, " ");
This gives me error: invalid lvalue function called in expression
Arrays can be used as lvalues, so I tried
cell2mat(strsplit(lineInFile, " "))
ans = Bobbluepizza
That's not what I want.
This worked.
[friendsName favoriteColor favoriteFood] = strsplit(lineInFile, " "){1,:}