Saturday, March 12, 2016

Pickling for easier testing

Today I want to talk about Pickling. This approach allows you to save off a variable/data for re-use later.

Why would you want to do this? Well suppose you're like me and you're working on an AI agent, and you have a LOT of problems you need to run through, and all these problems take a little bit of time, and say 50 of them are already solved by your agent, but that 51st isn't, instead of running all 50 over and over (unless your agent is learning things, in which case, you're good). You can pickle the passed in variable, and then make a simple "test" function that will call only the one you want to call so that it's way easier to test out individual parts.

For more general information about pickling take a look here:

Lets get started.

Import Pickle in your file with the following line added to the top of your file:
import pickle

(I"m going to be using some verbiage from my AI course, however the basic idea is this, the agent we have starts with normal class name, __init__ and must implement a Solve function).

Make the start of your Solve function look like this:

   def Solve(self,problem):
      pickle.dump( problem, open( './pickles/' + + ".p", "wb" ) )

Finally Make a new directory called pickles so pickle can save it.

Finally run your agent like you normally would.

If you notice there should be a bunch of .p files in your pickles directory.

Next lets setup a "test" agent that allows you to run a SINGLE test (I know folks on our forums have asked about this, and well... here it is!

Create a new file called

Inside it you would add the following

import pickle
from Agent import Agent

A = Agent()
problem  = pickle.load( open( "./pickles/Basic Problem E-09.p", "rb" ) )

Replacing the .p filename with whatever you choose.

You can make this more "generic" if you'd like by accepting command line input and then you run it like this . Basic Problem E-09 and it would run the one you command it to. Pretty Slick right?

That would look like this:

import pickle
from Agent import Agent
from sys import argv

A = Agent()
script, name = argv
problem  = pickle.load( open( "./pickles/" +  name + ".p", "rb" ) )

and be ran by this command.

python "Basic Problem E-09"

The final can be found here: