Pre-Class Assignment: Reviewing another agent-based model built using Object Oriented Programming (OOP)#

Day 09#

CMSE 202#

Image from https://en.wikipedia.org/wiki/Ant_colony_optimization_algorithms

Put your name here

#

✅ Put your group member names here

#

Goals for today’s pre-class assignment#

  1. Practice reviewing pre-written code in a variety of programming styles (linear, functional, and object oriented)

  2. Edit pre-existing code to alternate its functionality

  3. Practice documenting code using Docstrings

Table of contents#


Back to ToC

Part 0: Download some additional files#

For this assignment, you’ll need the following files:

  • ant_script.py

  • ant_function.py

  • ant_class.py

For now, just download these files and make sure they are in the same location as this notebook. You don’t need to look at them in detail yet. You will be prompted to explore the similarities and differences between the files as you work through this assignment.

You can find the files here:

https://raw.githubusercontent.com/msu-cmse-courses/cmse202-supplemental-data/main/code_samples/ant_script.py

https://raw.githubusercontent.com/msu-cmse-courses/cmse202-supplemental-data/main/code_samples/ant_function.py

https://raw.githubusercontent.com/msu-cmse-courses/cmse202-supplemental-data/main/code_samples/ant_class.py


Back to ToC

1. Problem Description: Ant foraging#

Fairly complex behaviors can be simulated using the simple rules of an agent-based model. This is sometimes called, emergent behavior. We are going to look into ways of simulating ant forging behaviors using a set of the following simple rules. The rules are evaluated in order and only one rule can be completed per iteration.

  1. If ant has food and is home, consume food.

  2. If ant has food, drop some pheromones and head home.

  3. If ant is at food, pick it up.

  4. If ant doesn’t have food and smells pheromones, head in the smelliest direction.

  5. Move randomly.

Ants then live in an environment with three properties; direction to home, smell (pheromones) and food. The Pheromones “dissolve” as time goes by and the food goes away as ants pick it up.

✅ Do This: Watch the following simulation of ants forging using this behavior by executing the cell below.

Note: We are using the %run IPython built-in magic command to run this simulation. For this to work, the ant_script.py needs to be in the same location as this notebook.

%run ant_script

In the visualization above, the color of the background signifies the amount of food. Light yellow indicates lots of food and dark purple indicates a little. Black represents no food.

✅ Do This: Without looking at the code, discuss how you would write your own simulation for this problem by answering the following questions.

Question 1: What data would you need to keep track of the environment in this simulation?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 2: How would you structure your program to solve this problem?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 3: What would some stub code or pseudocode look like for this program?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)


Back to ToC

2. Linear Programming#

In this class and in the future, you will spend a lot of time looking at code that you didn’t write. This could be in the form of package documentation and examples, code provided to you by a collaborator, or a classmate’s code for your semester project.

To continue building comfort with digesting unfamiliar code, you’re going to do a “code review” of an implementation of this simulation using linear programming. A “code review” is a common concept in programming where you analyze someone else’s code to figure out what it is doing, identify problems and make improvements. For now, just focus on seeing what it is doing.

✅ Do This: Analyze the ant_script.py file and answer the following questions:

Question 4: How does this design compare with the design you brainstormed? What did you like? What did you dislike?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 5: Was there anything in the design that was a surprise?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)


Back to ToC

3. Functional Programming#

Moving along in your code review process, you’re going to consider the same basic simulation written as collection of functions.

✅ Do This: Run the following cell to make sure you have the ant_function.py file in the right place and that the code runs.

import ant_function
ant_function.run()

✅ Do This: Analyze the ant_function.py file and answer the following questions:

Question 6: How is the ant_function.py file different from the ant_script.py version?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 7: What do you find to be better about the ant_script.py implementation of the simulation?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 8: What do you find to be better about the ant_function.py implementation of the simulation?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)


Back to ToC

4. Object oriented programming#

Finally, lets pause your code review and spend a moment thinking about how you could implement the ant forging simulation as a object oriented program.

✅ Do This: Discuss how you would break down this simulation into logical objects (object oriented programming) by answering the following question:

Question 9 : What objects could you implement? What would be their corresponding attributes and methods?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

✅ Do This: Continuing your code review exercises, run and analyze the ant_class.py file and answer the following questions:

import ant_class
ant_class.run()

Question 10: What are the disadvantages of using a class based approach?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 11: What are the advantages of using a class based approach?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Making some minor modifications#

Now that you understand how the ant_class.py script works, you’re going to try to modify the code a bit to make it visualize the model in a slightly different way.

✅ Do This: Using a text editor of your choosing, edit the ant_class.py file so that it visualizes the pheromones instead of the food. You should:

  • Use a different colormap for the pheromone visualization.

  • Add a colorbar to the visualize and label it “pheromones.”

  • Use a fixed colormap range so that it doesn’t keep changing as the animation runs.

Important note: Since your jupyter notebook has already imported ant_class, you can’t just import the file again to incorporate your changes because Python doesn’t like to import a module twice. You actually need to reload the class to get it to use your new version. The following cell takes care of this for you.

Make sure you understand how it works and then run your new version of the ant_class.py file.

# Use the importlib module to access the "reload()" function to make sure changes to ant_class.py are used
import importlib
importlib.reload(ant_class)
ant_class.run()

✅ Do This: Now that you’ve had a chance to review the code and modify it to visualizes the pheromones left by the ants. Can you find anything about the provided solution to the ant foraging model that you would change? Do you feel like the rules are implemented correctly? Do you feel like any of the rules should be change? Things you might want to think about:

  • What happens if an ant already has food but ends up in a location with food?

  • How are the ants detecting the pheromones around them? What sort of neighborhood are they searching in?

Once you’ve had a chance to think about more ways that you might want to change the ant_class.py file feel free to try making those changes! You should be able to test your changes by re-running the cell above.


Back to ToC

5. Python Docstrings#

One final Python related topic we want to introduce as you continue building your new Python skills is the concept of a “Docstring”. Docstrings are a really useful way of adding descriptive information to your code so that it is easier for other folks to use and easier for you to remember what you were doing with your own code from the past! In fact, when you use the ? functionality to understand what a Python method is doing, the information that you get back is from the docstring.

Here is some information about what docstrings are and why they are useful: https://www.pythonforbeginners.com/basics/python-docstrings

The ant class contained in ant_class.py include some docstrings, but others are missing.

✅ Do This: Read though the code and add appropriate docstrings to describe the parts of the class that have not yet been described.

A docstring might look like:

'''
This function does things and takes the following arguments.
'''

Of course, you’ll need to be a bit more descriptive than that! Look at ant_function.py to get some ideas.


Back to ToC

Part 6: Follow-up Questions#

Copy and paste the following questions into the appropriate box in the assignment survey include below and answer them there. (Note: You’ll have to fill out the assignment number and go to the “NEXT” section of the survey to paste in these questions.)

  1. In considering the various methods that one might use to solve this problem or others, what do you see as the advantages and disadvantages of each of these programming approaches:

    1. Linear programming

    2. Functional programming

    3. Object oriented programming


Back to ToC

Assignment wrap-up#

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://cmse.msu.edu/cmse202-pc-survey" 
	width="800" 
	height="800px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, you’re done!#

Submit this assignment by uploading it to the course Desire2Learn web page. Go to the “Pre-class assignments” folder, find the appropriate dropbox link, and upload it there.

See you in class!

© Copyright 2024, Department of Computational Mathematics, Science and Engineering at Michigan State University