NetLogo / Tortoise

Compiler and runtime engine for NetLogo models that runs in JavaScript 🐢
https://netlogoweb.org
Other
56 stars 27 forks source link

Different behavior between Desktop and Web: carefully #215

Closed CIVITAS-John closed 4 years ago

CIVITAS-John commented 4 years ago

Hi all,

I encountered another discrepancy between desktop and web NetLogo again. The following code aims to sprout a given number (initial-residents) on an agent set. First, all patches in the set will be filled with at least one resident; second, if there are no patches matching this condition (the second block of carefully), the resident will be sprouted in a random patch matching another condition.

However, while on NetLogo Desktop it successfully gives me the correct number of residents, on Web it only sprouts the first resident in households. I am testing more to figure out the source of this.

Best, John Chen

CIVITAS-John commented 4 years ago

I looked into the generated Javascript code, which states:

        try {
          world.observer.getGlobal("houses")._optimalOneOfWith(function() {
            return Prims.lt(ListPrims.length(SelfManager.self().getPatchVariable("owners")), world.observer.getGlobal("min-residents-per-household"));
          }).ask(function() { procedures["SPROUT-RESIDENT"](); }, true);
        } catch (_error_8113_8122) {
          world.observer.getGlobal("houses")._optimalOneOfWith(function() {
            return Prims.lt(ListPrims.length(SelfManager.self().getPatchVariable("owners")), world.observer.getGlobal("max-residents-per-household"));
          }).ask(function() { procedures["SPROUT-RESIDENT"](); }, true);
        }

Could it because of the different way that _optimalOneOfWith implements?

CIVITAS-John commented 4 years ago

It works when I change the code to:

Yes, my previous way of implementation is hacky. However, the underlying bug might need to resolve nevertheless..

LaCuneta commented 4 years ago

Thanks for raising the issue and for the research. It does appears to be the problem is that the _optimalOneOfWith() does not raise an exception when no item is found. A more minimal reproduction, the below throws an error in desktop but runs without a problem in web:

  clear-all
  ask one-of patches with [pcolor = blue] [set pcolor red]
LaCuneta commented 4 years ago

So this is actually I little trickier than I thought at first glance. The docs for one-of state that with an empty agentset it returns Nobody but with an empty list it throws an error. So the problem with the example above is with ask not throwing the error when used with Nobody, not one-of or _optimalOneOfWith. However, one-of in Netlogo Web doesn't throw the error when given an empty list, so it does still have a problem. Both of the below fail to error in NLW.

to agentset-check
  clear-all
  let ps (patches with [ pcolor = blue ])
  let one one-of ps ; `one` has a value of `Nobody`
  ask one [ set pcolor red ] ; should error here with `ASK expected input to be an agent or agentset but got NOBODY instead.`
end 

to list-check
  clear-all
  let ps (sort patches with [ pcolor = blue ]) ; `ps` is an empty list due to `sort`
  let one one-of ps ; should error here with `ONE-OF got an empty list as input.`
  ask one [ set pcolor red ]
end
CIVITAS-John commented 4 years ago

Hi Jeremy,

Thanks for the response! I wonder if there are any differences between an agentset and a list of agents - are there any performance implications?

Best, John Chen

Jeremy B notifications@github.com 于2020年6月22日周一 下午2:29写道:

So this is actually I little trickier than I thought at first glance. The docs for one-of https://ccl.northwestern.edu/netlogo/docs/dictionary.html#one-of state that with an empty agentset it returns Nobody but with an empty list it throws an error. So the problem with the example above is with ask not throwing the error when used with Nobody, not one-of or _optimalOneOfWith. However, one-of in Netlogo Web doesn't throw the error when given an empty list, so it does still have a problem.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/NetLogo/Tortoise/issues/215#issuecomment-647727765, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC523N574GXTAC6VDCV2RMTRX6WJ5ANCNFSM4ODUEP4Q .