[Tutor] Structure of my simulation / monte-carlo

Vincent Davis vincent at vincentdavis.net
Sun Nov 1 15:47:04 CET 2009

Kent Johsnon writes
"This class has a lot of responsibilities:
- create applicants
- create institutions
- run a single match
- run multiple matches
- calculate statistics on the result of multiple matches
A principle of object-oriented design is that a class should have a
single responsibility. I would break your class up a bit using
multiple classes, perhaps."

I am trying to do what you recomend, as much as makes sense to me, but that
is why I ask the question so I should consider your answer.
This is what I hear you saying, (I don't mean to represent them as
sub-classes but more how they would operate on each other) Should I consider
making Institutions) a subclass of (Make Institutions)? I can't think of
anything that would make sense to inherit.

class Simulation:
    class Create Institutions:
        class Institutions:
    class create Applicants:
        class Applicants:
    class Match:
    class Multi Match:

I add I am thinking

class Simulation:
    def__init__:(self, results, stats.....repeat..)
    def create_applicants():
        class Applicants
    def creat_institutions():
        class Institutions
    def one_simulation(): # one or more
        class Match()
        class Results
    def repeat_simulation()
        repeat one_simulations
        class Results

After writing this out I now think you are right, more classes. Which means
I really need to play with function objects to understand how they are
passed down the layers

Simulation(GPA = random.gauss(50, 10), repeat = 100.....)
        Applicants(GPA)  # I need to make sure the GPA is calculated at each
applicant not back at Simulation.

ask if it will always be random.gauss? No, I would like it to be anything
from a constant to a much more complex function)

"I would caution you that each instance of RepeatMatch will then hold lots
of the other members, so keeping them around could be expensive"

This means they stay in memory? Is there a way to know how much room a
instance takes up, in memory or hard drive?
I could serialize them a save them to a sqlite when done with a simulation
correct? How is a questions for later.

"worried that you might find my responses too complex"
I am kinda working in a vacuum, with respect to python programing. Complex
and difficult are ok, The challenge is not knowing what I don't know. I need
to practice with function objects and run a few experiments still to make
sureI understand them.

DaveA and Kent Thanks for all your help, Vincent
To add a little more to what I am doing I am using CherryPy to host this
simulation. So the GPA function and other simulation variables can be
entered in a html form and passed to the Simulation class. Then I am
calculating results as well as using matplotlib to make some plots, these
results and plots then get show. I think I have most of the CherryPy stuff
figured out once I have the Simulations class setup to do the whole thing.
The only part with CherryPy I am still working on is displaying progress on
the browser as the simulations runs, it can take several minutes.

Vincent Davis

On Sun, Nov 1, 2009 at 6:02 AM, Dave Angel <davea at ieee.org> wrote:

> Vincent Davis wrote:
>> I ask this question in part because of a fee remarks from another question
>> I
>> ask "class attribute to initiate more classes"
>> Basically I am simulation the process of applicants to schools and trying
>> to
>> ask/answer some questions like "what conditions do you need to have an
>> optimal solution" Oh and to learn python. I basically had it working as a
>> script rather than using a class structure but it was very inflexible and
>> I
>> still needed to learn about classes.
>> What I have these classes
>> class Applicant: has lots of attributes (self.gpa = random.gauss(50, 10)
>> about the Applicant and def() defining how/why an applicant applies to an
>> institution,
>> class Institution: Lots of attributes (self.quality = random.gauss(50,
>> 10)) about the Institution and def() defining how the institution
>> considers
>> the applicant.
>> class Match: this defines the interaction of the population of Applicants
>> and Institutions, i.e. the rules of the game and returns the outcome i.e.
>> which Applicants went to which Institutions.
>> As of now I have been running 1 Match at a time. Which is to say generate
>> 8000 instances of Applicant and 300 instances of Institution and then run
>> the match Match(app_list, inst_list) and I do this with a short script.
>> So now I need to implement my monte-carlo. By that I mean that i want to
>> set
>> some of the initial condition such as GPA, and Quality and basically
>> re-run
>> the what I descried above, (generate applicant, institutions, match them)
>>  Then save the results.
>> So my plan way to make a new class. This class would define the Applicant
>> characteristics "self.gpa = random.gauss(mean, SD)" and the
>> institutions self.quality = random.gauss(mean, sd)
>> so it would look something like this
>> class RepeatMatch:
>>    def __int__(self, app_mean, app_sd, inst_mean, inst_sd, match_ repeat)
>>        self.app_mean = app_mean
>>        ……..
>>        self.match_repeat = match_repeat
>>   def makeApplicants():
>>   def makeInstitutions():
>>   def runMatches(self)
>>   # runs the match match_repeat number of times, saves results
>> # then I calculate some characteristics of the results
>>    def ratio_dist():
>>         # returns the Mean and sd of GPA/Quality
>> Does it make sense to do it this way? Is there a better/alternative way of
>> thinking about this. In the end I want to compare the results of repeated
>> simulations "RepeatMatch(50,2….) Compared to RepeatMatch(50,15….)"
>> This is way I had ask the earlier question "class attribute to initiate
>> more
>> classes"
>> Thanks
>> Vincent Davis
> I worried that you might find my responses too complex, but nobody else has
> responded, and it's been almost a day.
> I don't see anything wrong with your approach.  Since you're going to do
> multiple sets of data, it makes sense for an instance of a class
> (RepeatMatch) to hold the data for one such run.  In your original sample,
> it didn't make sense to me, but of course I didn't know where you were
> heading.  So I would add in instance attributes such as  self.applicants=[]
>  to your __init__() method of RepeatMatch.  (I suspect you're planning to do
> exactly that)
> I would caution you that each instance of RepeatMatch will then hold lots
> of the other members, so keeping them around could be expensive. So when
> run_matches() finishes its analysis, it might want to delete its lists of
> raw data (eg. self.applicants).  But other choices exist, and you can decide
> that when you see how the whole thing fits together.  Perhaps you'll only
> have one such instance at a time.  But if you're going to do multiple things
> with the results, you may want this object to hang onto the results, but
> throw away the raw data when all the necessary results have been calculated.
> Are you sure that the only distribution you're going to use is
> random.gauss() ?  If so, then you only need to pass mean and stddev to the
> RepeatMatch constructor, as you're doing.  But if you might need to compare
> that distribution with a different one, then you might want to use a
> function object, as I tried to describe earlier.
> DaveA
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091101/64689948/attachment-0001.htm>

More information about the Tutor mailing list