Compare two lists for similarity in Elixir - list

I have two lists of skills that I’d like to compare to provide a percentage score for how much of one list appears in the other:
user_skills = [
%{name: "Elixir"},
%{name: "Python"}
]
project_skills = [
%{name: "Elixir"},
%{name: "Erlang"},
%{name: "Ruby"}
]
What percentage of user_skills appear in project_skills? What we’d want here is a result of 50%.

You can be very simple and use Kernel.--/2 to calculate the difference first:
iex(5)> user_skills -- project_skills
# [%{name: "Python"}]
You can then use the length of the original list user_skills and the length of the above operation to get a percentage like figure:
iex(6)> 1 - (length(user_skills -- project_skills) / length(user_skills))
0.5
If you'd like to do more sophisticated comparisons and diffs, take a look at the myers_difference as well.

Related

Iterate through list to update particular items in the list

How can I update the values in a list for the indices that are equal to everyone in the selected group agents?:
persons-own [
grouped
flockmates
]
to create-trustConnection
set nrPersons count persons
set vector []
repeat nrPersons [set vector 0.4 vector]
end
to updateVector
let alonePersons (count persons with [grouped = false])
set flockmates n-of (random alonePersons) persons
ask flockmates [
foreach vector [ i ->
set item i vector 0.6
]
]
end
You can use the replace-item reporter to update an item in a list.
Let this [ 1 2 3 4 ]
Let new-list replace-item 3 this “a”
Print new-list
Note that this does not affect the original list: it reports a new list with the specified item replaced with the given value.
Changing an item in a list of lists of similar.. but again, the entire list
Of lists is created anew.
But maybe use links?
In the case of using a list for a turtle to track its relationship with other turtles, or of groups, links are useful, and simplify managing those relationships, and enable things that are very difficult with lists.
LINKS are just another kind of agent, specifically for recording a relationship between two turtles. They have a pair of built-in variables, end1 and end2 that refer to those two turtles. The -own variables of the link can be used to record properties of the relationship. Like “anniversary” or “affinity” or whatever! Links can be directional, so the “intimacy” value can be different depending on the “direction” of the relationship.
Directed-link-breed [ relationships relationship]
Relationships-own [ intimacy ]
to setup-all-relationships
Ask protestors
[ setup-relationship ]
End
To setup-relationship
;; link with everyone else
Create-relationships-to other protestor
[ set intimacy .5 ]
End
The relationship between two turtles can be obtained in several ways.
(Link (this turtle) (that turtle))
Refers to the link from this turtle to that turtle.
Out-Link-neighbors is used to get the set of all turtles linked to from this turtle.
You can also use turtles to represent groups, and links to record membership in that group.
In that case, the members of the group are link-neighbors of the group.
While perhaps not a feature of your model, this opens up the possibility of multiple groups and of agents being members of more than one group, or of tracking things like former members.
Here is a complete, minimal reproducible example of what I think you are looking for. Note that one can just paste it into NetLogo and it compiles and runs. I've made some assumptions here - in particular that intimacy is a protesters-own variable, which it was not in the code you provided, but which your textual description seemed to indicate. Again, using who numbers is not a good idea, but that is a different question and answer. If I have time tomorrow, I might be able to provide you with an example of how one might use agentsets, but if intimacy values can vary from agent-pair to agent-pair, then links is really the way to go.
breed [protesters protester]
globals [numberOfProtesters intimacyVector]
protesters-own [
intimacy
partOfGroup ;initially set to false for all agents
myNRelatedProtesters
]
to setup
clear-all
create-protesters 10
create-intimacyRelationship
reset-ticks
end
to create-intimacyRelationship
ask protesters [
set numberOfProtesters count protesters
set intimacy []
repeat numberOfProtesters [set intimacy lput 0.2 intimacy]
set partOfGroup false
]
end
to updateIntimacy
let nrUngroupedProtesters (count protesters with [partOfGroup = false])
let NRelatedProtesters n-of (random nrUngroupedProtesters) protesters
ask NRelatedProtesters [
foreach ([who] of NRelatedProtesters) [ i -> set intimacy replace-item i intimacy 0.8 ]
set partOfGroup true
]
ask NRelatedProtesters [ show intimacy ]
end
to go
let ProportionProtestersInSubgroup (count protesters with [partOfGroup = true])/ numberOfProtesters
ifelse ((count protesters with [partOfGroup = false])/
numberOfProtesters) > ProportionProtestersInSubgroup
[
updateIntimacy
]
[
stop
]
tick
end
Hope this gets you started.

Create accumulative list over time

I want to store values from a variable in a list, adding new variable output for every tick. Lets say the variable outputs a different value every tick. For simplicity this is determined by the formula; 2 * ticks (var = 2 * ticks), thus the list should look something like this after five ticks [0 2 4 6 8]. I cannot get this to work however. Since NetLogo does not allow taking values from the past, how would I go about this?
I now have something like this:
ask turtles[
let var_list [ ]
foreach var_list [
set var_list lput var var_list
]
print var_list
]
This however only gives empty lists or lists only showing the most recent var value (when I change let var_list [] to let var_list [ 0 ]. How can I get it to correctly input the variable value in the table for every tick?
You are using let to create a temporary local variables. There's no problem in retaining values across ticks, but you do need to use global or turtle/patch/link variables.
Here's a complete model to demonstrate
turtles-own [my-list]
to setup
clear-all
create-turtles 1
[ set my-list []
]
reset-ticks
end
to go
ask turtles
[ set my-list fput random 10 my-list
print my-list
]
tick
end

Netlogo list member? why not working

I have a problem with a NetLogo-list (V. 6.0.2) and the member? command, somehow not doing what I want it to do.
Each turtle builds its list "aware-of-who":
ask turtles [
foreach cheater-list [ x ->
if random-float 100 < 90
[set aware-of-who lput ([who] of x ) aware-of-who]
]
It builds the lists of who-numbers successfully for the turtles.
Then I ask turtles to look for those neighbors, that have their who-number in their "aware-of-who" list (i.a. are "aware of them"), with this code:
let punishers (turtles-on neighbors) with
[ member? ([who] of myself) ( [aware-of-who] of self) = true]
It does not seem to report true, even if from all I can tell it should (the respective who numbers do appear in their lists). The code also does report true if I ask for (turtles-on neighbors) with [ empty? aware-of-who = false].
Any ideas what's wrong here?
Thanx!!
It seems like a case of mixing up self and myself. In the following code:
let punishers (turtles-on neighbors) with
[ member? ([who] of myself) ( [aware-of-who] of self) = true]
The code between the square brackets is passed to the with reporter and runs in the context of the calling turtle's neighbors. So within the reporter, self is the neighbor and myself is the calling turtle. You have it the other way around.
That being said... don't do it this way! And by this, I mean: don't use lists of who numbers. It is almost never necessary to use who numbers in NetLogo. Whenever you think you need who numbers for something, come ask a question here: someone will most likely show you a better way to do it.
The way around who numbers is usually to store references to turtles directly, either in a list or an agentset. It seems like you already know how that works, since cheater-list is apparently a list of turtles.
To construct you list of cheaters that a turtle is aware of, you could just use filter:
let known-cheaters filter [ random-float 100 < 90 ] cheaters
Finding your punishers then becomes a bit nicer:
let punishers (turtles-on neighbors) with [
member? self [ known-cheaters ] of myself
]
Also note that you should almost never write = true or = false. If something is true or false, you can use it directly in an expression (with a not in front if you're interested in the case where it's false). For example, you can write:
turtles with [ not member? self cheaters ]
instead of:
turtles with [ member? self cheaters = false ]

Concept Behind The Transformed Data Of LDA Model

My question is related to Latent Dirichlet Allocation. Suppose we apply LDA on our dataset, then apply fit transform on that.
the output is a matrix that is a collection of five documents. Each document consists of three topics. othe output is below:
[[ 0.0922935 0.09218227 0.81552423]
[ 0.81396651 0.09409428 0.09193921]
[ 0.05265482 0.05240119 0.89494398]
[ 0.05278187 0.89455775 0.05266038]
[ 0.85209554 0.07338382 0.07452064]]
So, this is the matrix that will be sent to a classification method for an evaluation purpose.
For the classification part, we need the labels for each row. But we do not have the labels which means I have to create them by my own.
One approach could be getting the highest probability for each topic as the corresponding label.
For example, the labels may be like so:
[2,0,2,1,0,]
However, this is very simple example.
I can also consider two highest probability for each document if each documents only has two topics. So, the example would be like this:
[[ 0.0922935 0 0.81552423]
[ 0.81396651 0.09409428 0]
[ 0.05265482 0 0.89494398]
[ 0.05278187 0.89455775 0]
[ 0.85209554 0 0.07452064]]
As you can see I have the rule of keeping the same probability for each label if they have the highest probabilities.
Which approach is correct? Has anyone used any other approach that is more meaningful?
Many thanks in advance!

How to implement a numerical formula across the items in a netlogo list

I have to do some operations in netlogo using Lists. While i can do simple tasks with them i am not yet proficient enough to code my current requirements.
I have a scenario where turtles have variables called Current-Age and Previous-Age. And turtles can be born and die if they don't meet a certain threshold.
I want to implement the following formula for each patch.
Best-list = (-1/Previous-Age) * (Distance between Patch & Turtle) for all the turtles
Best = Min [ Best-list]
I know the steps involved but have been unsuccessful in coding them. Following are the steps:
Create a list with all the current turtles that are alive
Create a second list which contains the Previous-Age
Create a third list with the distance between an individual patch and each of the live turtles
Then create another list with the output from the the Best-List formula for all the turtles in the list
Finally find the Min value in the list and store the name/who# of turtle with the minimum value in a separate variable called Best-Turtle
This is the code that i tried but didn't work.
set turtle-list (list turtles)
set turtle-age-list n-values length(turtle-list) [0]
set turtle-patch-dist-list n-values length(turtle-list) [0]
set best-list n-values length(turtle-list) [0]
ask patches[
foreach turtle-list(
set turtle-age-list replace-item ?1 turtle-age-list Previous-Age of turtles with [turtle= ?1]
)
]
I couldn't proceed to the next steps since the above code itself was not correct.
Would appreciate help with the code, thanks in advance.
Regards
First, lists are probably not the simplest way to do this. However, if you must use lists for some reason, I think what you're asking for is possible. I'm not exactly sure what you mean with best- are you trying to have each patch assess which turtle is the best turtle for that patch, and store that variable in a global list? I'm going to assume that's what you mean, but if I'm misunderstanding I think you can adapt what I do here to what you need.
First, any list passed to foreach must be the same length. So, since you mean to do this per-patch, make sure that every patch calls the procedure of list creation, not just for checking the lists. Next, review the dictionary for n-values- the syntax for the reporter means you need to use the reporter you're trying to receive- using n-values length(turtle-list) [0] will just give you a list of zeroes that is the same length as the number of turtles.
So each patch needs to create these lists- make sure you either define the patches-own for the list variables, or just use let to define the lists inside the procedure. You would need a list of ordered turtles, their previous ages, and the distance from the patch calling the procedure to each turtle. Next, you can create a list that generates a value according to your formula. Then, you can use the position primitive to find the location of the minimum value in your formula-generated list and use that to index the turtle with that value.
It might look something like
to numerical
set best-turtle []
ask patches [
let turtle-list (sort turtles) ;;; list of sorted turtles
let turtle-prev-age-list n-values length(turtle-list) [ [i] -> [pre_age] of turtle i ] ;;; list of previous ages of turtles, in same order as above
let turtle-patch-dist n-values length(turtle-list) [ [i] -> distance turtle i ] ;;; list of distance from this patch to each turtle, in same order
set best-list n-values length(turtle-list) [ [i] -> ( ( -1 / ( item i turtle-prev-age-list ) ) * ( item i turtle-patch-dist ) ) ] ;;; list of calculated values for each turtle
let best-position position (min best-list) best-list ;;; gets the index of minimum value
set best-turtle lput item best-position turtle-list best-turtle ;;; adds the best turtle for this patch to the global list of best turtles
]
end
The above procedure assumes that your turtles have a pre_age variable, patches have a best-list variable, and the list of each patches 'best turtle' is held in the global variable best-turtle. From there, you can use foreach to ask turtles in the list to do something. Note that if a turtle's previous age is 0, you will get a divide by zero error.
turtles-own [age previous-age]
to-report evalfrom [_patch]
report (- distance _patch) / previous-age
end
to test
ca
crt 25 [
set age (10 + random 75)
set previous-age age - random 10
]
print min-one-of turtles [evalfrom (patch 0 0)]
end