convexengineering / gpkit

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

uncaught ValueError raised by as_posyslt1 in external solver setup #662

Closed codykarcher closed 8 years ago

codykarcher commented 8 years ago

So something has happened in the last 4 hours that made all my stuff break:

from gpkit import VectorVariable, Variable, Model, units
from gpkit.tools import te_exp_minus1
import gpkit
import numpy as np

CD = Variable("C_D", "-", "Drag Coefficient")

# Aero Model
class XFOIL():
    def __init__(self):
        self.pathname = "/Users/codykarcher/Xfoil/bin/./xfoil"

    def cd_model(self, x0, max_iter=100):
        return 0.05

class SimpleCDModel(ConstraintSet):

    def as_posyslt1(self):
        raise ValueError("SimpleCDModel is not allowed to solve as a GP.")

    def as_gpconstr(self, x0):
        if not x0:
            return (CDp >= 0.01)
        else:
            xfoilcd = XFOIL().cd_model(x0)
        return (CDp >= xfoilcd)

constraints += [SimpleCDModel([])]

objective = CD

m = Model(objective, constraints)

# m.controlpanel(fn_of_sol=Weights_pie)
sol=m.localsolve()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-4daed9d84287> in <module>()
     34 
     35 # m.controlpanel(fn_of_sol=Weights_pie)
---> 36 sol=m.localsolve()

/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/constraints/prog_factories.pyc in solvefn(self, solver, verbosity, skipsweepfailures, *args, **kwargs)
     77                       solver, verbosity-1, *args, **kwargs)
     78         else:
---> 79             self.program, solvefn = genfunction(self, verbosity-1)
     80             result = solvefn(solver, verbosity-1, *args, **kwargs)
     81             solution.append(result)

/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/constraints/prog_factories.pyc in programify(self, verbosity, substitutions, **kwargs)
     32         if not substitutions:
     33             substitutions = self.substitutions
---> 34         prog = program(self.cost, self, substitutions, verbosity, **kwargs)
     35         if return_attr:
     36             return prog, getattr(prog, return_attr)

/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/constraints/signomial_program.pyc in __init__(self, cost, constraints, substitutions, verbosity)
     47         CostedConstraintSet.__init__(self, cost, constraints, substitutions)
     48         try:
---> 49             _ = self.as_posyslt1()  # should raise an error
     50             # TODO: is there a faster way to check?
     51         except TypeError:

/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/constraints/set.pyc in as_posyslt1(self)
    126         for constraint in self:
    127             constraint.substitutions = self.substitutions
--> 128             posys = constraint.as_posyslt1()
    129             self.posymap.append(len(posys))
    130             posylist.extend(posys)

/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/constraints/set.pyc in as_posyslt1(self)
    126         for constraint in self:
    127             constraint.substitutions = self.substitutions
--> 128             posys = constraint.as_posyslt1()
    129             self.posymap.append(len(posys))
    130             posylist.extend(posys)

<ipython-input-23-e748cdf2278c> in as_posyslt1(self)
    123 
    124     def as_posyslt1(self):
--> 125         raise ValueError("SimpleCDModel is not allowed to solve as a GP.")
    126 
    127     def as_gpconstr(self, x0):

ValueError: SimpleCDModel is not allowed to solve as a GP.

This is pretty high priority for me... I can't really do anything as long as this isn't working...

codykarcher commented 8 years ago

Reverted to 61394ebe155464089d416a015806cd763467aa3f and at least things are working again.

whoburg commented 8 years ago
  1. I believe this is due to 4c63942a90dd9f05d70ee0aa02e9ce3dbfcbd034, and that the fix is to change your as_posyslt1 to raise a TypeError instead of ValueError.
  2. Apologies that this change was not communicated effectively. We decided that a TypeError is the more appropriate thing to be raised here.
  3. 4c63942a90dd9f05d70ee0aa02e9ce3dbfcbd034 occurred a few commits after 61394ebe155464089d416a015806cd763467aa3f, so I am surprised that reverting to 61394ebe155464089d416a015806cd763467aa3f fixed the issue for you -- I would have expected you would have to revert to something before 4c63942a90dd9f05d70ee0aa02e9ce3dbfcbd034.
  4. The code above does not run for me -- I will post a working version in another comment below.
whoburg commented 8 years ago

working version of the original example (main change is switching ValueError to TypeError):

from gpkit import VectorVariable, Variable, Model, units
from gpkit.constraints.set import ConstraintSet

CD = Variable("C_D", "-", "Drag Coefficient")

# Aero Model
class XFOIL():
    def __init__(self):
        self.pathname = "/Users/codykarcher/Xfoil/bin/./xfoil"

    def cd_model(self, x0, max_iter=100):
        return 0.05

class SimpleCDModel(ConstraintSet):

    def as_posyslt1(self):
        raise TypeError("SimpleCDModel is not allowed to solve as a GP.")

    def as_gpconstr(self, x0):
        if not x0:
            return (CD >= 0.01)
        else:
            xfoilcd = XFOIL().cd_model(x0)
        return (CD >= xfoilcd)

constraints = [SimpleCDModel([])]

objective = CD

m = Model(objective, constraints)

# m.controlpanel(fn_of_sol=Weights_pie)
sol=m.localsolve()
codykarcher commented 8 years ago

Yup, that was it. Not sure about your third point... It is a mystery.

Thanks for the assistance!