I switched gears from yesterday and now have a workable solution to the pairs problem I posted. However, now I am being defeated by the simplest of issues. I keep getting a mixture of errors. 1)Maketerrain is the first module that runs on setup. It errors saying I can't use maketerrain in an observer context because it patch/turtle only. I've never had a module give such an error, and I use modular code-writing routinely. So this is completely flummoxing me. 2)Wherever I put "tick" (end of go, start of go, middle of go, in some other procedure) further breaks the code, with the same error--"tick is observer and thus will not run because this is X context." I have checked brackets and parentheses more than a dozen times--taking a break and coming back at it fresh to see if I missed something. No errors there that I have been able to find. I am using Netlogo 6.1.1. I am now dizzyingly confused.
Rather than post another lengthy set of code at this juncture, I want to take another few cracks at it myself. So I am asking, can anyone possibly offer a principle or two I can use to try to debug this? Like, what establishes context? What can I do to make the context be what I want and not what gets "implied" by earlier commands? How can I use a module in which patches and turtles do something to set themselves up and still have it run on setup? (Note--moving stuff to the interface did not solve the problem.) What sets the "go" procedure context, what should it be to run, and what can someone do to make it be what it needs to be? What do I do to place tick--a command I have never had problems with before--correctly?
I have scoured stackoverflow, netlogo manual pages and programming guide, books I have, and still cannot debug what seems to be a very very very simple issue.
Have a look at the first few lines of the Maketerrain procedure. NetLogo interprets the context from the primitives used. If the first line is something like:
set color red
then NetLogo 'knows' that this is a procedure to be run by turtles (that is, it is turtle context) because only turtles can set color because color is a turtle variable. If, however, it starts:
ask turtles
[ set color red
then it is observer context because the external 'observer' instructs turtles to do things (roughly).
Context is actually an incredibly important concept in NetLogo programming - as you do something like the following, you are constantly changing context:
to demo
ask patches
[ ask one-of turtles-here
[ set color red
]
]
end
It starts in the observer context, then iterates through the patches doing something. In a sense, it is thinking like a patch (and is in patch context). Then you open the next code block with [ and switch to the turtle context so that 'I' (a randomly selected turtle on the current patch) can change colour. Then each ] to end a code block backs out of the layers of context.
Related
I'm new to Anylogic and created a simple traffic model. Only use 'carSource', 'CarMoveTo', 'Car Dispose' blocks to set the car routes. But After I ran the model, it worked for a while, then all the cars froze without any error occurring. ’Events‘ panel also stopped. How to solve it?
Most likely your model is running into an infinite loop somewhere in the logic. The first place to check would be all your loops that might become infinite,e.g Do loops, Do-while loops, iterator for loops where you perhaps change the counter variable manually...
If you have the professional version of AnyLogic the best option is to run the model in debug mode until you get this to the point where it freezes and then press pause. You will then see where in the code the model is getting stuck.
If this does not work you might need to start putting traceln in major functions and see ing you can spot the last traceln that gets printed and keep on adding more and more until you can find the point between two traceln where the model freezes
I had the same problem, that after a certain time, all cars froze and there wasn't a signle error.
The problem on my side was that the stop line was too close to the intersection, so I moved it a little bit farther.
My goal is to build a 5x5 grid of images. In the following code, row, col and rowcol were created as variables local to the sprite, and newcol, newrow and cats are global. (By the way, is it possible to tell which variables are local and which are global? It's easy to forget or make mistakes.)
The result is a 5x1 grid only, as seen here.
I am unclear as to the order of execution of these statements. Does when I start as a clone get called before or after add_cat gets called the second time? My tentative conclusion is that it gets called afterwards, yet the clone's global variables seem to contain their values from beforehand instead.
When I attempted to debug it with ask and say and wait commands, the results varied wildly. Adding such pauses in some places fixed the problem completely, resulting in a 5x5 grid. In other places, they caused a 1x5 grid.
The main question is: How to fix this so that it produces a 5x5 grid?
Explanation
Unfortunately, the execution order in Scratch is a little bizarre. Whenever you edit a script (by adding or removing blocks, editing inputs, or dragging the entire script to a new location in the editor), it gets placed at the bottom of the list (so it runs last).
A good way to test this out is to create a blank project with the following scripts:
When you click the green flag, the sprite will either say "script one" or "script two", depending on which runs first. Try clicking and dragging one of the when green flag clicked blocks. The next time you click the green flag, the sprite will say whichever message corresponds to the script you just dragged.
This crazy order can make execution incredibly unpredictable, especially when using clones.
The solution
The only real solution is to write code that has a definite execution order built-in (rather than relying on the whims of the editor). For simpler scripts, this generally means utilizing the broadcast and wait block to run particular events in the necessary order.
For your specific project, I see two main solutions:
Procedural Solution
This is the most straightforward script, and it's probably what I would choose to go with:
(row and col are both sprite-only variables)
Because clones inherit all sprite-only variable values when they are created, each clone will be guaranteed to have the correct row and col when it is created.
Recursive Solution
This solution is a bit harder to understand than the first, so I would probably avoid it unless you're just looking for the novelty:
I'm trying to create a variable in Netlogo that changes based on its value last tick. I've followed this question thread in how to create such a variable, but I'm having a bit of trouble creating the initial conditions, because at tick=0, there is no myvar-last-tick because there was no last tick, so Netlogo autosets myvar-last-tick to 0. How can I create myvar and myvar-last-tick such that when tick=0, myvar-last-tick is the same as myvar only at tick 0? To be clear I would like to program the variables such that they follow a pattern like
this, although for my actual program the rate of decline would not be constant like in this example.
Before you call your go procedure (i.e., your schedule), you should call a setup procedure. Usually, the very first thing your setup procedure should do is all of your global-variable initializations (perhaps by calling a setup-globals procedure). You can initialize myvar-last-tick and myvar to anything you like. Just make sure the the result of your first call to go will produce the starting outcome you want.
.nlogo file
I am getting this error while running iterations using behavior space
The tick counter has not been started yet. Use RESET-TICKS.
error while observer running TICKS
called by procedure __EVALUATOR
I am not sure why this is happening. I have included reset-ticks in the "set" routine.
In addition in the behavior space dialog wizard, i also included reset-ticks as the final command to be executed. Yet i am getting this error.
Below is my setup and go code:
to setup
clear-all
setup-citizens
setup-parties
update-support
reset-ticks
end
to go
ask parties [ adapt set my-old-size my-size ]
update-support
election
plot-voter-turnout
plot-volatility
if (Turnout-100%? = false) [plot-citizen-comparison]
tick
end
You are using ticks in your BehaviorSpace experiment's "stop condition", so I think it's nearly certain that's where the "error while observer running TICKS" error must be coming from, given that the stack trace doesn't refer to a procedure name.
Here's my best guess at what's going on here: under some conditions, your setup procedure fails, and therefore never reaches the call to reset-ticks at the end of setup. Then BehaviorSpace tries to run your stop condition, resulting in the error you see.
This guess has some problems:
Why BehaviorSpace would only be showing you the eventual ticks error, and not the error causing setup to fail, I don't know.
I have no idea why your setup procedure would be failing.
Nonetheless, that's the best I can offer you without doing a more in-depth investigation.
I was facing the same issue not too long ago. I'm pretty sure that the issue goes back to how Netlogo shares the global variables/states among the threads. I suspect that one thread starts the go procedure while another thread hasn't called the reset-ticks yet.
A temporary fix to this is to call reset-ticks if it hasn't been called already at the beginning of your go procedure.
carefully [let t ticks][reset-ticks]
For those people who found this question by searching for the error "The tick counter has not been started yet. Use RESET-TICKS." but are not actually using BehaviorSpace - the question (and therefore the accepted answer) does not apply to your situation. Instead, you have almost certainly forgotten to initialise the model before trying to run it, probably by hitting the go button without first hitting the setup button.
The reset-ticks command starts the tick counter (makes the internal clock available) so that the tick command can advance the clock. By convention, a procedure named setup has all the commands to initialise the model, including reset-ticks, creating turtles etc. Similarly, a procedure called go contains all the commands to actually run the model like moving turtles around, including tick. Also by convention, these procedures are run by pressing buttons named setup and go respectively.
I have a *.shp file that I've upload and i'm using as part of my model (calculating shortest paths). This is quite a big shape file with thousands of road links and intersections and bridges represented by nodes. I was hoping to speed up the running of behavior space by not loading this map every time, and so created a separate procedure for loading the map and defining link weights etc. In this procedure I have clear-all - reset ticks so everything is effectively wiped if i load a new map. In the setup i define turtle attributes for each run. Between each run I use clear-all-plots and clear-output, and reset-ticks. When i run this model behavior space starts to run slowly after a few setups, even with a table output. However, if i combine the load-map and setup-files together i.e. the map is load for every new behavior space run, then the speed is maintained throughout.
Example - runs slow, but the maps is not reloaded everytime
to-load-map
Clear-all
... code for loading map
reset-ticks
end
to-setup-model
clear-all-plots
clear-outputs
... code for setting up turtle variables
reset-ticks
end
Example (maintains speed - but has to load map)
To-setup
clear-all
...code for loading map
...code for setting up turtle variables
reset-ticks
end
My question: am i missing something that would help to speed things up while not having to reload the map?
Not knowing anything else about your model, I wonder if you essentially have a "memory leak" with lots of information accumulating in global variables that are not getting purged every time by the to-setup-model procedure. Are there perhaps other global variables you can explicitly reinitialize in to-setup-model that might help free up some of this space? For instance, do you have large tables hanging around between runs that only gain more key-value pairs and never get trimmed back down? Just a thought.
I almost always define a clear-most procedure that clears everything out except the big data I don't want to load/compute every time. Unfortunately, that means I must list the variables to initialize out in detail, but I like to free as much as possible between runs to keep things speedy.
-- Glenn