Skip to content

Commit

Permalink
allow string keys in x0 (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Sep 8, 2019
1 parent efda227 commit 34313b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 7 additions & 4 deletions gpkit/constraints/sgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,17 @@ def results(self):

def _fill_x0(self, x0):
"Returns a copy of x0 with subsitutions added."
x0 = KeyDict(x0) if x0 else KeyDict()
x0kd = KeyDict()
x0kd.varkeys = self.varkeys
if x0:
x0kd.update(x0)
for key in self.varkeys:
if key in x0:
if key in x0kd:
continue # already specified by input dict
elif key in self.substitutions:
x0[key] = self.substitutions[key]
x0kd[key] = self.substitutions[key]
# undeclared variables are handled by individual constraints
return x0
return x0kd

def init_gp(self, substitutions, x0=None):
"Generates a simplified GP representation for later modification"
Expand Down
8 changes: 5 additions & 3 deletions gpkit/tests/t_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,17 +533,19 @@ def test_trivial_sp2(self):
def test_sp_initial_guess_sub(self):
x = Variable("x")
y = Variable("y")
x0 = 2
x0 = 3
y0 = 2
with SignomialsEnabled():
constraints = [y + x >= 4, y <= x]
objective = x
m = Model(objective, constraints)
try:
sol = m.localsolve(x0={x: x0, y: y0}, verbosity=0,
solver=self.solver)
sol = m.localsolve(x0={"x": x0, y: y0}, verbosity=0,
mutategp=False, solver=self.solver)
except TypeError:
self.fail("Call to local solve with only variables failed")
self.assertEqual(m.program.gps[0].x0[x], 3)
self.assertEqual(m.program.gps[0].x0["y"], 2)
self.assertAlmostEqual(sol(x), 2, self.ndig)
self.assertAlmostEqual(sol["cost"], 2, self.ndig)

Expand Down

0 comments on commit 34313b0

Please sign in to comment.