convexengineering / gpkit

Geometric programming for engineers
http://gpkit.readthedocs.org
MIT License
203 stars 40 forks source link

Savetxt with strict table choices #1530

Closed pgkirsch closed 3 years ago

pgkirsch commented 3 years ago

For a certain audience I just want to save the bare minimum data to a solution text file, but currently it seems that

sol.savetxt("solution.txt", tables=("freevariables", "constants")) 

still prints the cost, warnings (if applicable) and most sensitive constraints.

Let me know if this is a deliberate design decision, but would you be open to only writing the free variables and constants in the example shown above?

bqpd commented 3 years ago

Not a deliberate decision, and I like the proposed syntax! I'll just pass through kwargs to table.

bqpd commented 3 years ago

oh, it seems like it already does that actually? if you want to not print the constraints, also specify "printmodel=False"

pgkirsch commented 3 years ago

Oh my bad, I thought I had tested that as well but evidently not. Closing!

pgkirsch commented 3 years ago

Btw, super minor but it seems like there is a missing newline when the optimal cost table is not included:

from gpkit import Variable, Model, Vectorize                                     

class System(Model):                                                             
    def setup(self):                                                             
        with Vectorize(1):                                                       
            self.Fleet = Fleet()                                                 
        constraints = [self.Fleet]                                               
        self.cost = sum(self.Fleet.z)                                            
        return constraints                                                       

class Fleet(Model):                                                              
    def setup(self):                                                             
        with Vectorize(1):                                                       
            self.Vehicle = Vehicle()                                             

        self.z = z = Variable("z")                                               
        constraints = [                                                          
            z >= sum(self.Vehicle.y**2),                                         
            self.Vehicle,                                                        
        ]                                                                        
        return constraints                                                       

class Vehicle(Model):                                                            
    def setup(self):                                                             
        x = Variable("x", 1)                                                     
        self.y = y = Variable("y")                                               
        constraints = [y >= x]                                                   
        return constraints                                                       

m = System()                                                                     
sol = m.solve()                                                                  
sol.savetxt("1530-0.txt")                                                        
sol.savetxt("1530-1.txt", tables=("freevariables", "constants"))                 
sol.savetxt("1530-2.txt", showvars=("z"), printmodel=False)                      
sol.savetxt("1530-3.txt", tables=("freevariables", "constants"), printmodel=False)
sol.savetxt("1530-4.txt", tables=("freevariables", "constants"), showvars=("z")) 
sol.savetxt("1530-5.txt", showvars=("z")) 

1530-4.txt looks like:

System                                                                           
======                                                                           

Cost Function                                                                    
-------------                                                                    
 z[0]                                                                            

Constraints                                                                      
-----------                                                                      
  Fleet                                                                          
   z[:] ≥ 0 + y[:]²[0]                                                           

   Vehicle                                                                       
    y[:] ≥ x[:]Free Variables           <---- missing newline                                         
--------------                                                                   
  | System.Fleet                                                                 
z : [ 1        ]    
bqpd commented 3 years ago

yeah fixed in the branch i emailed you!

On Tue, Dec 1, 2020, 15:26 pgkirsch notifications@github.com wrote:

Closed #1530 https://github.com/convexengineering/gpkit/issues/1530.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/convexengineering/gpkit/issues/1530#event-4059688369, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALKAGDQ5HNENJ2JWCVYAHLSSV3SVANCNFSM4UIMCOGQ .

bqpd commented 3 years ago

so it turns out there was a bug, that newline one :p

On Tue, Dec 1, 2020, 15:42 Edward Burnell eburn@mit.edu wrote:

yeah fixed in the branch i emailed you!

On Tue, Dec 1, 2020, 15:26 pgkirsch notifications@github.com wrote:

Closed #1530 https://github.com/convexengineering/gpkit/issues/1530.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/convexengineering/gpkit/issues/1530#event-4059688369, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALKAGDQ5HNENJ2JWCVYAHLSSV3SVANCNFSM4UIMCOGQ .