Counting interactions with linked nodes [netlogo] - networking

My model has links with defined duration, and I am trying to register the new links and the old ones in two different vectors.
The Problem: When I run the simulation the new links are stored correctly, but the old ones appear duplicated in the csv file. I am making a mistake at some point and I really need some help. If there is a more elegant way of doing it, I appreciate the tips! Thanks all for the collaboration!
ifelse not link-neighbor? myself
[
create-new-links-with partner in-radius 0.1
ask new-links
[
set registernew []
set link-duration max (list duration maxduration)
set link-creation time:copy dt
set link-end time:plus link-creation link-duration "year"
set link-installment invtransf
set meets 1
set registernew ([(list link-creation link-duration link-end link-installment meets end1 end2) ] of new-links)
add-records
set breed old-links
]
]
[
ask old-links
[
set registerold [ ]
set meets time:copy dt
set registerold ([(list link-creation link-duration link-end link-installment meets end1 end2) ] of old-links)
]

I haven't checked this, but I suspect that the problem is that you have bidirectional links WITH, not unidirectional links TO or FROM, so the old-links get reported by each end separately which duplicates their presence in the list.
Again I haven't tried this but you might set a flag-variable in each link at the start of each large pass, like, reported? to false, and then deep in the loop when you are about to report it see if it's already been reported and if not, go ahead ( report and set the flag ) and if it has, don't report it again but interrupt the run with a user-message and examine what just happened.
Wade

Related

Why is net logo showing a runtime error using with command

first time poster.
I am a student working in netlogo.
I am running the Game if Thrones model and have an error.
The code works, but is appears when the humans defeat the nightlong, before the text comes up, I get an error message.
Code is:
If season = “winter” and count whitewalkers = 0 [
set season “spring”
ask patches with [ snow? ][
set snow? false
if else resources = 0 [
set pcolor brown
][
set pcolor green
]
]
user-message “The Night King has been defeated! Summer is back.”
stop
]
The error message is:
WITH expected a true/false value from (patch 70 253), but got 0 instead. Error while observer running WITH Called bu procedure GO Called by button “go”
Many thanks
I have tried removing the brackets, but I believe they are needed.
I have put more space in between the brackets, but that makes no difference.
NetLogo does not perform type inference and the ? at the end of such primitives is a convention to indicate to the reader of the code that it is a boolean type, but not a language feature. NetLogo’s compiler does not immediately assign a boolean value to a variable that has a ? in its name, but simply assumes that it is an integer and assigns 0.
You probably need to set the snow? variable to a default value before using it elsewhere in your code, probably on top of your model’s setup procedure.
Here is a simple piece of code to illustrate how to initialize boolean variables in NetLogo:
patches-own [snow?]
to show-snow
show "before assigning bool value"
show [snow?] of patches
ask patches [ set snow? false ]
show "after assigning bool value"
show [snow?] of patches
end
enter image description here

Turn off search continuation results in python-ldap?

Using python-ldap.search_s() function (https://www.python-ldap.org/en/python-ldap-3.3.0/reference/ldap.html#ldap.LDAPObject.search_s) with params...
base = DC=myorg,DC=local
filterstr = (&(sAMAccountName={login})(|(memberOf=CN=zone1,OU=zones,OU=datagroups,DC=myorg,DC=local)(memberOf=CN=zone2,OU=zones,OU=datagroups,DC=myorg,DC=local)))
...to try to match against a specific AD user.
Yet when I look at the result returned (with login = myuser), I see something like:
[
(u'CN=zone1,OU=zones,OU=datagroups,DC=myorg,DC=local', {u'sAMAccountName': ['myuser']}),
(None, [u'ldap://DomainDnsZones.myorg.local/DC=DomainDnsZones,DC=myorg,DC=local']),
(None, [u'ldap://ForestDnsZones.myorg.local/DC=ForestDnsZones,DC=myorg,DC=local']),
(None, [u'ldap://myorg.local/CN=Configuration,DC=myorg,DC=local'])
]
where there are multiple other hits in the list (besides the myuser sAMAccountName match) that have nothing to do with the search filter.
Looking at the docs (https://www.python-ldap.org/en/python-ldap-3.3.0/faq.html) these appear to be "search continuations" / referrals that are included when the search base is at the domain level and it says that they can be turned off by including the code like...
l = ldap.initialize('ldap://foobar')
l.set_option(ldap.OPT_REFERRALS,0)
as well as trying
ldap.set_option(ldap.OPT_REFERRALS,0)
l = ldap.initialize('ldap://foobar')
...yet adding this code does not change the behavior at all and I get the same results (see https://www.python-ldap.org/en/python-ldap-3.3.0/reference/ldap.html?highlight=set_option#ldap.set_option).
Am I misunderstanding something here? Anyone know how to get these to stop popping up? Anyone know the structure of the tuples that this function returns (the docs do not describe)?
Just talked to someone else more familiar with python-ldap and was told that OPT_REFERRALS is controlling if you automatically follow the referral, but it doesn't stop AD from sending them.
For now, the only approach they recommended was to filter these values with something like:
results = ldap.search_s(...)
results = [ x for x in results if x[0] is not None ]
Noting that the structure of the results returned from search_s() is
[
( dn, {
attrname: [ value, value, ... ],
attrname: [ value, value, ... ],
}),
]
When it's a referral it's a DN of None and the entry dict is replaced with an array of URI's.
* (Note that in the search_s call you can request specific attributes to be returned in your search too)
* (Note that since my base DN is a domain level path, using the ldap.set_option(ldap.OPT_REFERRALS,0) snippet was still useful just to stop the search_s() from actually going down the referral paths (which was adding a few seconds to the search time))
Again, I believe that this problem is due to the base DN being a domain level path (unless there is some other base_dn or search.filter I could use for that fact that the group users are scattered across various AD paths in the domain that I'm missing).

Use input variable in assert or specify the data to assert

I have a unit test for a function that adds data (untransformed) to the database. The data to insert is given to the create function.
Do I use the input data in my asserts or is it better to specify the data that I’m asserting?
For eample:
$personRequest = [
'name'=>'John',
'age'=>21,
];
$id = savePerson($personRequest);
$personFromDb = getPersonById($id);
$this->assertEquals($personRequest['name'], $personFromDb['name']);
$this->assertEquals($personRequest['age'], $personFromDb['age']);
Or
$id = savePerson([
'name'=>'John',
'age'=>21,
]);
$personFromDb = getPersonById($id);
$this->assertEquals('John', $personFromDb['name']);
$this->assertEquals(21, $personFromDb['age']);
I think 1st option is better. Your input data may change in future and if you go by 2nd option, you will have to change assertion data everytime.
2nd option is useful, when your output is going to be same irrespective of your input data.
I got an answer from Adam Wathan by e-mail. (i took his test driven laravel course and noticed he uses the 'specify' option)
I think it's just personal preference, I like to be able to visually
skim and see "ok this specific string appears here in the output and
here in the input", vs. trying to avoid duplication by storing things
in variables." Nothing wrong with either approach in my opinion!
So i can't choose a correct answer.

How to use create-<breeds>-with between two breed turtle agents?

I've been stuck by this issue for a long time. I have two networks in my model, so I want to create different types of links with different breed turtle agentsets.
Let's name the 1st turtle agentset T1 and the 2nd T2, so what I did is the following:
breed [T1s T1]
undirected-link-breed [TL1s TL1]
breed [T2s T2]
undirected-link-breed [TL2s TL2]
;;Got error report
ask T1s [create-TL1s-with other n-of 10 T1s]
The last line gave an error reporting that "You cannot use breeded and unbreeded links in the same world". I'm quite confused about what this means.
And then, I changed the last line to:
ask T1s [create-links-with other n-of 10 T1s]
It worked this time, but if that's the case, how can I define two different types of links, i.e., TL1 and TL2, with different turtle agentsets T1s and T2s?
Can anybody help me out? I really appreciate it!
Thanks
That error means that you've created some links that have no breed (probably with create-link-with) before creating links with a breed, or vice-versa. If you want to use link breeds, you can never use create-link-with, create-link-to, or create-link-from. You must always use create-<breed>-with, create-<breed>-to, and create-<breed>-from.
So, search your code for instances of create-link-with, create-link-to, or create-link-from and either delete them or change them to create-<breed>-with, create-<breed>-to, or create-<breed>-from. If you're still getting the error, call clear-all or clear-links to make sure you've removed all unbreeded links.

Lua: Doing arithmetic in for k,v in pairs(tbl) loops

I have a table such as the following:
mafiadb:{"Etzli":{"alive":50,"mafia":60,"vigilante":3,"doctor":4,"citizen":78,"police":40},"Charneus":{"alive":29,"mafia":42,"vigilante":6,"doctor":14,"citizen":53,"police":33}}
There are more nested tables, but I'm just trying to keep it simple for now.
I run the following code to extract certain values (I'm making an ordered list based on those values):
sortmaf={}
for k,v in pairs(mafiadb) do
sortmaf[k]=v["mafia"]
end
That's one of the codes I run. The problem I'm running into is that it doesn't appear you can do arithmetic in a table loop. I tried:
sortpct={}
for k,v in pairs(mafiadb) do
sortpct[k]=(v["alive"]*100)/(v["mafia"]+v["vigilante"]+v["doctor"]+v["citizen"]+v["police"])
end
It returns that I'm attempting to do arithmetic on field "alive." What am I missing here? As usual, I appreciate any consideration in answering this question!
Editing:
Instead of commenting on the comment, I'm going to add additional information here.
The mafiadb database I've posted IS the real database. It's just stripped down to two players instead of the current 150+ players I have listed in it. It's simply structured as such:
mafiadb = {
Playername = {
alive = 0
mafia = 0
vigilante = 0
doctor = 0
police = 0
citizen = 0
}
}
Add a few hundred more playernames, and there you have it.
As for the error message, the exact message is:
attempt to perform arithmetic on field 'alive' (nil value)
So... I'm not sure what the problem is. In my first code, the one with sortmaf, it works perfectly, but suddenly, it can't find v["alive"] as a value when I'm trying to do arithmetic? If I just put v["alive"] by itself, it's suddenly found and isn't nil any longer. I hope this clarifies a bit more.
This looks like a simple typo to me.
Some of your 150 characters is not well written - probably they don't have an "alive" property, or it's written incorrectly, or it's not a number. Try this:
for k,v in pairs(mafiadb) do
if type(v.alive) ~= 'number' then
print(k, "doesn't have a correct alive property")
end
end
This should print the names of the "bad" characters.

Resources