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: https://wiki.python.org/moin/UsingPickle
Lets get started.
Import Pickle in your file with the following line added to the top of your Agent.py 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/' + problem.name + ".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 test.py
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" ) )
A.Solve(problem)
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 .
test.py 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" ) )
A.Solve(problem)
and be ran by this command.
python test.py "Basic Problem E-09"
The final test.py can be found here:
https://gist.github.com/onaclov2000/d1d7fc01b22b98e0098e