Skip to content

Commit

Permalink
ConstraintSets can now contain non-ConstraintSets (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Mar 4, 2020
1 parent 0ce75bc commit c7efc4f
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 223 deletions.
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

0 comments on commit c7efc4f

Please sign in to comment.