calling external code: conditional depends on variable not in constraints
See original GitHub issueAgain related to #590, I’d like to do this:
from gpkit import VectorVariable, Variable, units, SignomialProgram
import gpkit
from gpkit.constraints.set import ConstraintSet
Re = Variable("Re", 1e5, "-", "Reynolds Number")
CL = Variable("C_L", "-", "Lift Coefficient")
CD = Variable("C_D", "-", "Drag Coefficient")
def xfoil(x0):
return [CD >= 0.01 + .03*CL**2]
class AeroModel(ConstraintSet):
def as_gpconstr(self, x0):
gpconstr = super(AeroModel, self).as_gpconstr(x0)
if x0 and x0["Re"] >= 1e6:
constraints = xfoil(x0)
else:
constraints = [CD >= 1. + CL**2]
return ConstraintSet([gpconstr, constraints])
constraints = AeroModel([CL >= 1.0])
objective = CD
m = SignomialProgram(objective, constraints, require_signomial=False)
m.localsolve('cvxopt', verbosity=1)
But I’m getting:
Beginning signomial solve.
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-5-0f0a49bf286c> in <module>()
25
26 m = SignomialProgram(objective, constraints, require_signomial=False)
---> 27 m.localsolve('cvxopt', verbosity=1)
/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/constraints/signomial_program.py in localsolve(self, solver, verbosity, x0, rel_tol, iteration_limit, **kwargs)
105 appear to be converging, you may wish to increase the iteration limit by
106 calling .localsolve(..., iteration_limit=NEWLIMIT).""" % len(self.gps))
--> 107 gp = self.gp(x0, verbosity=verbosity-1)
108 self.gps.append(gp) # NOTE: SIDE EFFECTS
109 try:
/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/constraints/signomial_program.py in gp(self, x0, verbosity)
134 def gp(self, x0=None, verbosity=1):
135 """Get a GP approximation of this SP at x0"""
--> 136 return GeometricProgram(self.cost, self.as_gpconstr(x0),
137 self.substitutions, verbosity=verbosity)
/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/constraints/set.py in as_gpconstr(self, x0)
165
166 When x0 is none, may return a default guess."""
--> 167 cs = ConstraintSet([constr.as_gpconstr(x0) for constr in self])
168 cs.substitutions.update(self.substitutions)
169 return cs
<ipython-input-5-0f0a49bf286c> in as_gpconstr(self, x0)
14 def as_gpconstr(self, x0):
15 gpconstr = super(AeroModel, self).as_gpconstr(x0)
---> 16 if x0 and x0["Re"] >= 1e6:
17 constraints = xfoil(x0)
18 else:
/Users/codykarcher/Dropbox (MIT)/Research/gpkit/gpkit/keydict.pyc in __getitem__(self, key)
94 keys = self.keymap[key]
95 if not keys:
---> 96 raise KeyError("%s was not found." % key)
97 values = []
98 for key in keys:
KeyError: 'Re was not found.'
I’d like to be able to pass in the constants, but apparently they aren’t in the x0
vector? Thoughts?
Note, the example as defined in #590 works fine.
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (10 by maintainers)
Top Results From Across the Web
Custom Condition Checks - Configuration Language | Terraform
Check custom requirements for variables, outputs, data sources, and resources and provide better error messages in context.
Read more >How do I use extern to share variables between source files?
Using extern is only of relevance when the program you're building consists of multiple source files linked together, where some of the variables...
Read more >Documentation - Conditional Types - TypeScript
Conditional Type Constraints Often, the checks in a conditional type will provide us with some new information. Just like with narrowing with type...
Read more >Where variables can be used - GitLab Docs
Not supported are variables defined in the GitLab Runner config.toml and variables ... or some values depending whether the variables were defined or...
Read more >Conditional Expressions, Assignments and Equations - GAMS
If the condition is not satisfied, no assignment is made. ... Note: Logical conditions used with the dollar operator cannot contain variables.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
related to #590
@cjk12, what’s the current status of this problem?