NetLogo / LevelSpace

This is the LevelSpace extension repository. LevelSpace allows you to run NetLogo models |: from inside NetLogo models :|
Other
19 stars 8 forks source link

Passing args into ls:create-models #89

Closed arthurhjorth closed 6 years ago

arthurhjorth commented 8 years ago

It's currently not possible to pass args into create-models. For instance, I was expecting the following

(ls:create-models 1 "models/Sample Models/Biology/Wolf Sheep Predation.nlogo" [ [id test ] -> show id show test ] "hello")

to work, but it doesn't. Shouldn't it work? Is there any way we can make it work?

Edit: for a little bit of context, ls:create-model implicitly passes the model's id in as an argument. I can see how it would be confusing to have the first appearing argument not actually map on to the first argument name in the arg list.

A solution might be to allow the argument to be implicit if you are not passing in anything else, but to require that you explicate the id and name it something particular if you are also passing in other arguments. Does that make sense?

qiemem commented 8 years ago

It's currently not possible to pass args into create-models.

That's correct. The procedure passed to ls:create-models is run in the parent model, not the child model, so there's no reason for arguments to be passed in. You have direct access to all the information in the parent model!

A solution might be to allow the argument to be implicit if you are not passing in anything else, but to require that you explicate the id and name it something particular if you are also passing in other arguments. Does that make sense?

No. What do you mean "explicate the id and name it something particular"? The argument you give the id argument a name no matter what.

arthurhjorth commented 8 years ago

Right, but imagine we want to do something like open a model, and initialize it with a dynamically determined number of turtles:

(ls:create-models 3 "path/to/model.nlogo" [ [ model-id total-turtles ] -> ls:ask model-id [set no-turtles total-turtles setup] ] (some-reporter + random 100))

To me, that seems like a useful thing to be able to do, and a good workflow for doing it. It is also the most similar to how we would normally create turtles, although of course the code would be run in the context of the turtle whereas this code is run in the context of the parent model. But it still offers flexibility and recognizable syntax that I think is a Good Thing. Do you not agree? (Genuinely curious.)

If you agree, then the question is how to handle adding arguments. Currently, the model-id is implicit, and it means that if you add more arguments, they end up being out of order (e.g. in the provided example, the reporter + random 100 is in position 0, but it actually maps on to arg1 inside the anon proc.)

Or maybe I'm thinking about this all wrong?

arthurhjorth commented 8 years ago

Alternatively, maybe we made the wrong decision about running the provided command in the context of the parent, and maybe it should be run in the context of the child? And people can use last ls:models to keep track of the model?

qiemem commented 8 years ago

You are thinking about this wrong.

First, in your example, total-turtles would not be passed into the ls:ask.

Second, in your example, (some-reporter + random 100) is only evaluated once for all models. That doesn't have anything to do with LevelSpace: that's just how arguments work in NetLogo (and any other eaglerly evaluated language).

The way to do your example is:

(ls:create-models 3 "path/to/model.nlogo" [ [ model-id ] ->
  ls:let total-turtles some-reporter + random 100
  ls:ask model-id [set no-turtles total-turtles setup] 
])

Alternatively, maybe we made the wrong decision about running the provided command in the context of the parent, and maybe it should be run in the context of the child?

Possibly. The primary reason we added the command was so that you get access to the model id. One problem with the last ls:models technique is that now you can create more than one model at a time. So you'd have to do something awful like sublist ls:models (length ls:models - 100) (length ls:models).