Template Models

64 views
Skip to first unread message

Alan Isaac

unread,
Jun 16, 2014, 8:39:29 AM6/16/14
to au-ec...@googlegroups.com
Please use this thread for questions and comments about the Template Models.

Adaora Isichei

unread,
Jun 16, 2014, 11:49:31 AM6/16/14
to au-ec...@googlegroups.com

Hey Professor Isaac,

I just want to be sure, are we meant to turn in only the First Approach to the Template model.

Alan Isaac

unread,
Jun 16, 2014, 2:40:57 PM6/16/14
to au-ec...@googlegroups.com
Yes, just the First Approach.
(It has four parts.)
Alan Isaac

Kevin Carrig

unread,
Jun 16, 2014, 2:59:20 PM6/16/14
to au-ec...@googlegroups.com
Quick question about a Python implementation for  the Template assignment. I am working through the tutorial for gridworld provided here: http://jasss.soc.surrey.ac.uk/14/2/5.html . However, the source code and the sample code provided on this page do not exactly align. For example, one script asks to import TorusGrid from gridworld, yet there is no class for TorusGrid in the source code (rather, just "Torus"). Is this a matter of working through the tutorial and identifying which class names it may be referring to and changing the tutorial syntax accordingly?





Alan G Isaac

unread,
Jun 16, 2014, 3:37:56 PM6/16/14
to AU-ECON-ABS on behalf of Kevin Carrig
Where are you getting your copy of `gridworld` from?
Looking here
http://econpy.googlecode.com/svn/trunk/abm/gridworld/gridworld.py
I see the TorusGrid class ...

I may have misunderstood your question.

Alan Isaac



On 6/16/2014 2:59 PM, Kevin Carrig via AU-ECON-ABS wrote:
> Quick question about a Python implementation for the Template assignment. I am working through the tutorial for gridworld provided here:
> http://jasss.soc.surrey.ac.uk/14/2/5.html <http://jasss.soc.surrey.ac.uk/14/2/5.html>. However, the source code and the sample code provided on this

Alan Isaac

unread,
Jun 16, 2014, 3:43:05 PM6/16/14
to au-ec...@googlegroups.com
I should add that this assignment is very straightforward in NetLogo.  You do not need Python for it!

Amanda Saville

unread,
Jun 16, 2014, 4:30:36 PM6/16/14
to au-ec...@googlegroups.com
This is in reference to my question about a stopping condition:

2)  Is there supposed to be a specific stopping criteria for our move procedures?  When I reread Part I, I feel like there should be 1) a possibility for a full Moore neighborhood to be occupied, but given my current world that seems unlikely and 2) that there should be some limitations on move if the neighborhood is full.

I had already implemented a forever button, but I think the part that was confusing me in the assignment (which I paid extra attention to because it was bolded) was this bullet:

the move action must terminate, even if all neighboring locations are occupied (This is a requirement imposed on your move algorithm.)

Dr. Isaac, is it possible for you to expand on what you mean by this bullet, if not a stopping criteria?  I am concerned that it seems unlikely (albeit not impossible) for any one of my turtles to find itself in a fully occupied Moore neighborhood with a radius of 4, because even when two seemingly come close together, they quickly move apart from one another... 

Alan G Isaac

unread,
Jun 16, 2014, 5:26:15 PM6/16/14
to au-ec...@googlegroups.com
Imagine the following strategy:
if not satisfied
1. pick a random neighboring patch `p`, then
2. if `p` is not occupied move there, otherwise goto 1.

A surrounded turtle will get stuck in this loop.

Of course as you note, being surrounded can be low probability.
But that is different than zero probability, and getting stuck
in a loop is a very bad outcome.

Hope that clarifies,
Alan Isaac

Kevin Carrig

unread,
Jun 16, 2014, 6:20:48 PM6/16/14
to au-ec...@googlegroups.com
Absolutely- but I can use all the practice I can get as I'm working through my final project in Python!


Kevin Carrig

unread,
Jun 17, 2014, 12:51:05 PM6/17/14
to au-ec...@googlegroups.com
I just wanted to confirm that a Python implementation of the Template Models is suitable for submission. 

Alan G Isaac

unread,
Jun 17, 2014, 3:09:22 PM6/17/14
to au-ec...@googlegroups.com
As I understand it, you are using `gridworld`.
You can do all parts this way, I believe.
(It would be easier I think to use NetLogo.)
Let me know of any problems you encounter.

Alan Isaac

Kevin Carrig

unread,
Jun 18, 2014, 11:05:36 PM6/18/14
to au-ec...@googlegroups.com
I realize we are expected to withhold code samples until after the deadline, but I'm guessing no one else in the course is implementing the Template models in Python. For the Template 4 model, I am struggling with the stopping condition. At the moment, I have the stopping condition set in a for-loop of range(1,maximum_iterations). For each iteration, the model checks the max size of all the agents, and as long as this value falls below 100, the model is run. However, I believe my assignment of the agent size subclass may be wrong. It seems as if there is a more "pythonic way" to move this stopping condition within a class, but I'm falling short on this one. Below is a sample of the code for the Template 4 model in Python:

""" Template Model 4: Probe Object State """


from Template_Model_Part_3 import *


class GUI04(GridWorldGUI):


  def gui(self):


    self.add_button('Set Up', 'setup')


    self.add_button('Run','run')


    self.add_button('Stop','stop')


    self.add_slider('Initial Number of Agents','n_agents',10,200,10)


    self.add_slider('Maximum Extraction Rate','agent_max_ext',0.0,2.0,0.1)


  def get_agent_sizes():


    agents=self.subject.get_agents(self.subject.AgentType)


    return list(agent.size for agent in agents)




class World04(World03):


  def schedule(self):


        ask(self.patches, 'produce')


        ask(self.agents, 'move')


        ask(self.agents, 'change_size')


  n_agents = number_of_agents


  agent_max_ext =agent_max_extract




if __name__ == '__main__':


  myworld = World04(topology=TorusGrid(shape=world_shape))


  mypatches = myworld.create_patches(Cell03)  #setup patches


  myagents = myworld.create_agents(New_Agent3, number=number_of_agents)


  myobserver = GUI04(myworld)


 


for i in range(1,maximum_iterations):  


    stopping_condition= max(myobserver.get_agent_sizes)


    if stopping_condition <100:  


        myworld.run()




"""


References




Isaac, A. G. (2011). The ABM template models: A reformulation with reference implementations. Journal of Artificial


Societies and Social Simulation 14(5), http://jasss.soc.surrey.ac.uk/14/2/5.html.




"""



Alan Isaac

unread,
Jun 19, 2014, 8:31:21 AM6/19/14
to au-ec...@googlegroups.com
It's just code blindness.  Look again at your inequality.  :-)
This is an example of why it is important to add print statements and error checks (assertions, in Python) and not just depend on our eyes.

Alan Isaac




On Wednesday, June 18, 2014 11:05:36 PM UTC-4, Kevin Carrig wrote:
For each iteration, the model checks the max size of all the agents, and as long as this value falls below 100, the model is run.

Kevin Carrig

unread,
Jun 19, 2014, 10:25:36 AM6/19/14
to au-ec...@googlegroups.com
Okay- I see the debugging error on my part. However, the ideal application of this loop is to report the stopping_condition value (i.e. the maximum value of the agent sizes) at each iteration. When the stopping condition hits 100, we want to call myworld.stop(). However, I don't think max(myobserver.get_agent_sizes) reports a float as I had wanted to. Here we leverage the other side of the stopping condition:

for i in range(1,maximum_iterations):  


    stopping_condition= max(myobserver.get_agent_sizes)


    if stopping_condition >= 100:  


        myworld.stop()



Alan G Isaac

unread,
Jun 19, 2014, 10:53:40 AM6/19/14
to au-ec...@googlegroups.com
So ... one way to test that is to add a print statement. E.g.,
print(myobserver.get_agent_sizes, type(myobserver.get_agent_sizes))
You'll find that your forgot to call the method (by using parentheses).

Alan Isaac

Kevin Carrig

unread,
Jun 19, 2014, 11:59:11 AM6/19/14
to au-ec...@googlegroups.com
I've added some print statements for debugging purposes, per your request. I guess my main issue is communicating between myworld and myobserver. One approach seems to be adding an additional parameter to myworld.run calling stopping condition (which=100). Then, in our World04 class, we redefine the run function by incorporating something to the effect of:

stopping_condition= max(myobserver.get_agent_sizes())

 
if stopping_condition >= 100:  


However, where exactly in the following run code from source to implement this stopping condition is a bit puzzling:


def run(self, maxiter=None):
"""Return None.  Run the simulation
by repeatedly calling the schedule.
"""
maxiter = maxiter or self.maxiter
self._stop = False
while self.keep_running() \
and (self._iteration < (maxiter or self._iteration+1)):
self._iteration += 1
#self.logger.debug('Begin iteration {0}'.format(self._iteration))
self.notify_observers('_begin_iteration')
#schedule is run once each iteration
self.schedule()
#updating can be less frequent
if not (self._iteration % self._update_frequency):
#self.logger.debug('_update')
self.notify_observers('update')
self.notify_observers('_end_iteration')
#self.logger.debug('End iteration {0}'.format(self._iteration))
self.clean_up()



Alan Isaac

unread,
Jun 19, 2014, 1:16:11 PM6/19/14
to au-ec...@googlegroups.com
Stopping Condition (gridworld)

As a general rule, a user of `gridworld` should not need to dig into its internals.

Can you elaborate on why the approach in the tutorial
http://jasss.soc.surrey.ac.uk/14/2/5.html#par6.3
does not seem to meet your needs?

Alan Isaac

unread,
Jun 19, 2014, 1:18:48 PM6/19/14
to au-ec...@googlegroups.com
Stopping Condition

Usually we do not set stopping_condition to a number and then test it against another number.  Rather we set stopping_condition to a boolean, determined e.g. by comparing two numbers.

Kevin Carrig

unread,
Jun 19, 2014, 2:03:12 PM6/19/14
to au-ec...@googlegroups.com
Great! Finally got the stopping condition to operate properly.

Adin Dobkin

unread,
Jun 19, 2014, 6:26:43 PM6/19/14
to au-ec...@googlegroups.com
I've more or less completed this successfully, excluding the moore neighborhood requirement.

From the models library sample, I was able to create the moore neighborhoods of radius 4, but when attempting to add in the at-point primitive in my movement function, I couldn't seem to work it out.  I know that in the NetLogo library, it mentions the use of relative coordinates to designate agents, and I'm assuming the other piece of the puzzle is the one turtle per square sample, but I feel like I can't quite connect the two in the right manner.  Is there a trick here?  I think I might be missing an additional piece that's needed to make something cohesive.

Alan Isaac

unread,
Jun 19, 2014, 7:09:36 PM6/19/14
to au-ec...@googlegroups.com
Moore neighborhood

Did you look at the code in the Moore and Von Neumann example?
(It includes an example of use.)



Message has been deleted

Adin Dobkin

unread,
Jun 19, 2014, 8:36:07 PM6/19/14
to au-ec...@googlegroups.com
I'm close to having it, but it doesn't 'look' right, at least I think.  I have the Moore neighborhood set up (for reporting purposes, at least), but I'm not sure that I'm properly implementing it so that a turtle moves to a random, unoccupied location in the set.  There's a chance I have it right, but something seems slightly off when I'm viewing the simulation

Alan Isaac

unread,
Jun 19, 2014, 10:05:19 PM6/19/14
to au-ec...@googlegroups.com
Visual Inspection

Visual inspection can be useful, but just reporting that "something seems slightly off" does not provide the kind of information needed to help you.  Good question asking requires that you be very specific and detailed.

One question you should be asking it, how can you test your code.  Here are a couple things you can do.

1. After your turtles move they should be one a patch with no other turtles.  Check that!  E.g.,
   ``ask turtles [if (any other turtles-here) [error "ooops!!"]``

2.  Make sure your neighborhoods look right.  E.g., change to the command line and enter
    ``ask patches [set pcolor black] ask one-of patches [ask patches at-points moore-offsets 4 false [set pcolor yellow]]``
    and make sure you're getting what you expected

And so on.  As the required reading emphasized, you MUST write tests as you go along, or else your code will almost surely be buggy.

Reply all
Reply to author
Forward
0 new messages