Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConstraintSets should contain non-ConstraintSets #1475

Merged
merged 6 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions docs/source/examples/performance_modeling_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@ Cost
Constraints
-----------
Mission
"definition of Wburn":
Wfuel[:-1] >= Wfuel[1:] + Wburn[:-1]
"require fuel for the last leg":
Wfuel[3] >= Wburn[3]
FlightSegment
AircraftP
"fuel burn rate":
Wburn[:] >= 0.1·D[:]
"lift":
Aircraft.W + Wfuel[:] <= 0.5·rho[:]·CL[:]·S·V[:]²
"performance":
WingAero
"definition of D":
D[:] >= 0.5·rho[:]·V[:]²·CD[:]·S
"definition of Re":
Re[:] = rho[:]·V[:]·c/mu[:]
"drag model":
CD[:] >= 0.074/Re[:]^0.2 + CL[:]²/π/A/e[:]
FlightState
(no constraints)
"definition of Wburn":
Wfuel[:-1] >= Wfuel[1:] + Wburn[:-1]
"require fuel for the last leg":
Wfuel[3] >= Wburn[3]

FlightSegment
AircraftP
"fuel burn rate":
Wburn[:] >= 0.1·D[:]
"lift":
Aircraft.W + Wfuel[:] <= 0.5·rho[:]·CL[:]·S·V[:]²
"performance":
WingAero
"definition of D":
D[:] >= 0.5·rho[:]·V[:]²·CD[:]·S
"definition of Re":
Re[:] = rho[:]·V[:]·c/mu[:]
"drag model":
CD[:] >= 0.074/Re[:]^0.2 + CL[:]²/π/A/e[:]

FlightState
(no constraints)

Aircraft
"definition of W":
Aircraft.W >= Aircraft.Fuselage.W + Aircraft.Wing.W
"components":
Fuselage
(no constraints)
Wing
"definition of mean chord":
c = (S/A)^0.5
"parametrization of wing weight":
Aircraft.Wing.W >= S·Aircraft.Wing.rho
"definition of W":
Aircraft.W >= Aircraft.Fuselage.W + Aircraft.Wing.W
"components":
Fuselage
(no constraints)

Wing
"definition of mean chord":
c = (S/A)^0.5
"parametrization of wing weight":
Aircraft.Wing.W >= S·Aircraft.Wing.rho

Optimal Cost
------------
Expand Down Expand Up @@ -108,9 +108,9 @@ Constraint Differences
+++ added in argument

@@ -41,4 +41,3 @@
c = (S/A)^0.5
"parametrization of wing weight":
Aircraft.Wing.W >= S·Aircraft.Wing.rho
c = (S/A)^0.5
"parametrization of wing weight":
Aircraft.Wing.W >= S·Aircraft.Wing.rho
- Wburn[:] >= 0.2·D[:]

**********************
Expand Down
44 changes: 22 additions & 22 deletions docs/source/examples/relaxation_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Cost

Constraints
-----------
"minimum relaxation":
C >= 1
"relaxed constraints":
x <= C·x_max
x_min <= C·x
"minimum relaxation":
C >= 1
"relaxed constraints":
x <= C·x_max
x_min <= C·x

Optimal Cost
------------
Expand Down Expand Up @@ -61,11 +61,11 @@ Cost

Constraints
-----------
"minimum relaxation":
C[:] >= 1
"relaxed constraints":
x <= C[0]·x_max
x_min <= C[1]·x
"minimum relaxation":
C[:] >= 1
"relaxed constraints":
x <= C[0]·x_max
x_min <= C[1]·x

Optimal Cost
------------
Expand Down Expand Up @@ -105,18 +105,18 @@ Cost
Constraints
-----------
Relax2
"original constraints":
x <= x_max
x >= x_min
"relaxation constraints":
"x_max":
Relax2.x_max >= 1
x_max >= Relax2.OriginalValues.x_max/Relax2.x_max
x_max <= Relax2.OriginalValues.x_max·Relax2.x_max
"x_min":
Relax2.x_min >= 1
x_min >= Relax2.OriginalValues.x_min/Relax2.x_min
x_min <= Relax2.OriginalValues.x_min·Relax2.x_min
"original constraints":
x <= x_max
x >= x_min
"relaxation constraints":
"x_max":
Relax2.x_max >= 1
x_max >= Relax2.OriginalValues.x_max/Relax2.x_max
x_max <= Relax2.OriginalValues.x_max·Relax2.x_max
"x_min":
Relax2.x_min >= 1
x_min >= Relax2.OriginalValues.x_min/Relax2.x_min
x_min <= Relax2.OriginalValues.x_min·Relax2.x_min

Optimal Cost
------------
Expand Down
17 changes: 11 additions & 6 deletions gpkit/constraints/array.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"Implements ArrayConstraint"
from .set import ConstraintSet
from .single_equation import SingleEquationConstraint


class ArrayConstraint(SingleEquationConstraint, ConstraintSet):
class ArrayConstraint(SingleEquationConstraint, list):
"""A ConstraintSet for prettier array-constraint printing.

ArrayConstraint gets its `sub` method from ConstrainSet,
Expand All @@ -14,10 +13,16 @@ class ArrayConstraint(SingleEquationConstraint, ConstraintSet):
"""
def __init__(self, constraints, left, oper, right):
SingleEquationConstraint.__init__(self, left, oper, right)
ConstraintSet.__init__(self, constraints)
list.__init__(self, constraints)
self.constraints = constraints

def __iter__(self):
yield from self.constraints.flat

def lines_without(self, excluded):
"Returns lines for indentation in hierarchical printing."
return self.str_without(excluded).split("\n")

def __bool__(self):
"Allows the use of '=' NomialArrays as truth elements."
if self.oper != "=":
return NotImplemented
return all(bool(p) for p in self.flat())
return False if self.oper != "=" else bool(self.constraints.all())
2 changes: 1 addition & 1 deletion gpkit/constraints/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(self, cost, constraints, substitutions,
cost_hmap = cost.hmap.sub(self.substitutions, cost.varkeys)
if any(c <= 0 for c in cost_hmap.values()):
raise InvalidPosynomial("cost must be a Posynomial")
self.hmaps = [cost_hmap] + list(self.flathmaps(self.substitutions))
self.hmaps = [cost_hmap] + list(self.as_hmapslt1(self.substitutions))
## Generate various maps into the posy- and monomials
# k [j]: number of monomials (rows of A) present in each constraint
self.k = [len(hm) for hm in self.hmaps]
Expand Down
Loading