convexengineering / gplibrary

Useful subsystem models
MIT License
10 stars 11 forks source link

how do I write test methods #59

Closed whoburg closed 8 years ago

whoburg commented 8 years ago

What's the best practice for writing test methods for models in the model library? I'd like the general philosophy to be:

I don't want to use .substitutions.update in test(self, ...) because then calling test mutates the substitutions of the model, which is unexpected.

Should models also expose some sort of defaults() method that gives default substitutions?

Can we get an example of a simple model with a test method?

bqpd commented 8 years ago

How about test as a class method? On Mar 31, 2016 10:20, "Warren Hoburg" notifications@github.com wrote:

What's the best practice for writing test methods for models in the model library? I'd like the general philosophy to be:

  • models don't have default values placed directly in the variables or constraints
  • test methods substitute some default values and choose a reasonable objective, and then solve

I don't want to use .substitutions.update in test(self, ...) because then calling test mutates the substitutions of the model, which is unexpected.

Should models also expose some sort of defaults() method that gives default substitutions?

Can we get an example of a simple model with a test method?

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/hoburg/gpkit-models/issues/59

bqpd commented 8 years ago

example:

class Atmosphere(ConstraintSet):
    # ...
    @classmethod
    def test(cls):
        import numpy.testing as npt
        from gpkit import Model
        cs = cls()
        substitutions = {}  # ...
        m = Model(cs["\\rho"], cs, substitutions)
        sol = m.solve()
        # test the solution

Atmosphere.test()
whoburg commented 8 years ago

sounds good.

pgkirsch commented 8 years ago

@bqpd: @whoburg and I discussed using this same structure for making different variants on models e.g.

@classmethod
def standalone_737(cls):
     objective = ...
     substitutions = {...}
    m  = Model(....)
    return m

@classmethod
def aircraft_737(cls): # for full aircraft model
    substitutions = {....} # shorter substitutions dict
    m  = Model(....)
    return m

How do you feel about this? I'm currently in the middle of implementing it for my GPjet models as a test before porting over to gpkit models and (hopefully) ending GPjet for good.

pgkirsch commented 8 years ago

Where the original

class VerticalTail(Model):
...

becomes

class VerticalTail(ConstraintSet):
...
bqpd commented 8 years ago

Ooh, I like that!

pgkirsch commented 8 years ago

Sweet. One thing I can't figure out: does this mean ConstraintSet will need a __getitem__ method to be able to do something like objective = cs['W_{vt}']?

bqpd commented 8 years ago

Yeah, I'm going to rename constraintset to something else so it can have user-facing things like that.

bqpd commented 8 years ago

But for now you could just use strings.

pgkirsch commented 8 years ago

what do you mean use strings?

pgkirsch commented 8 years ago

One thing I have figure out is how to do it with CostedConstraintSet, which also works pretty well I think

bqpd commented 8 years ago

Oh, derp. I was thinking for substitutions. For cost there's no nice way to do it right now.

bqpd commented 8 years ago

Yeah, costedconstraintset has getitem