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

When to use strings vs varkeys #668

Closed
pgkirsch opened this issue Apr 17, 2016 · 9 comments
Closed

When to use strings vs varkeys #668

pgkirsch opened this issue Apr 17, 2016 · 9 comments
Assignees

Comments

@pgkirsch
Copy link
Contributor

I have a large SP (some say the largest SP that GPkit has ever seen) that I want to solve, but unfortunately if I just let all initial values for negynomial vars be 1, the first solve returns primal infeasible and then the next (feasibility-finding) solve is unbounded (this probably deserves a ticket of its own in the future) and returns UNKNOWN. To circumvent this, up until now I have been solving my constituent models first and using their results as the x0 dict. This doesn't to be working anymore because when I do the following in nominal_math:

        for var in negy.varlocs:
            if var not in x0:
                print var

it prints out all the variables I thought I was giving in my x0. Any ideas?

A code excerpt to show what I'm doing:

    def __init__(self):

            ........
            .........
            vt_sol = vts.localsolve(verbosity=0)
            fu_sol = fus.localsolve(verbosity=0)
            lg_sol = lgs.localsolve(verbosity=0)
            ht_sol = hts.localsolve(verbosity=0)
            wi_sol = wis.localsolve(verbosity=0)

            init = vt_sol['variables'].copy()
            init.update(fu_sol['variables'])
            init.update(lg_sol['variables'])
            init.update(ht_sol['variables'])
            init.update(wi_sol['variables'])
            init.update({
                         'x_{CG}': 15,
                         'x_{CG_{fu}}': 15,
                         'x_{CG_{ht}}': 38,
                         'x_{CG_{lg}}': 16,
                         'x_{CG_{vt}}': 35,
                        })

            self.init = init

        substitutions = {
                         'W_{eng}': 10000,
                         'x_{CG_{eng}}': 15,
                         'x_{CG_{wing}}': 15,
                        }

        lc = LinkedConstraintSet([hlc, vt, fu, lg, ht, wi])
        Model.__init__(self, objective,
                             lc,
                             substitutions)

    def test(self):
        sol = self.localsolve(x0=self.init, verbosity=5)
@whoburg whoburg added this to the Next release milestone Apr 17, 2016
@whoburg
Copy link
Collaborator

whoburg commented Apr 17, 2016

I'm guessing for @bqpd to debug this it may be helpful to have the actual code to reproduce -- could we get a commit hash and a line of code to run? Understood that monial_math also needs to be modified to add the print statement above for debugging.

@pgkirsch
Copy link
Contributor Author

pgkirsch commented Apr 17, 2016

commit hash: 24c21f4c37a7ee7d432936347b64ce38af8c2dfa (gpkitmodels, aircraft_improvements branch)
This commit is a working version of the code, but the issue of vars not being correctly subbed in x0 still appears.
code to run: ipython aircraft.py

Note: as described in #596 a line of geometric_program.py needs to be changed for this model to work.

@pgkirsch
Copy link
Contributor Author

Hmmm there are 4 different x_{CG} keys in my x0 dict. I guess my init.update doesn't work as I would like it to. Also, how do the model names affect things?

@pgkirsch
Copy link
Contributor Author

As kind of a MWE, I feel like one (or both) of the things I try below should work as intended. The dictionaries printed below localsolve commands are just the x0 dicts.

In [1]: from gpkit import Variable, SignomialsEnabled, Model

In [2]: x = Variable('x')

In [3]: y = Variable('y')

In [4]: with SignomialsEnabled():
    m = Model(x, [x + y >= 1, y <= 0.5], name='Air')
   ...:     

In [5]: sol = m.localsolve()
Beginning signomial solve.
{y_Air: 1, x_Air: 1}
{y_Air: 0.5, x_Air: 0.5}
Solving took 2 GP solves and 0.0887 seconds.

Cost
----
 0.5

Free Variables
--------------
x : 0.5 
y : 0.5 

Sensitivities
-------------


In [6]: m.localsolve(x0={x.varkeys.keys()[0]: 0.4})
Beginning signomial solve.
{x: 0.4, y_Air: 1, x_Air: 1}
{y_Air: 0.5, x_Air: 0.5}
Solving took 2 GP solves and 0.0396 seconds.

Cost
----
 0.5

Free Variables
--------------
x : 0.5 
y : 0.5 

Sensitivities
-------------

Out[6]: 
{'constants': {},
 'cost': 0.50000000000000022,
 'freevariables': {y_Air: 0.5, x_Air: 0.50000000000000022},
 'sensitivities': {'constants': {},
  'constraints': {'\n        1 <= 2*x_Air**0.5*y_Air**0.5\n        y_Air <= 0.5': {'1 <= 2*x_Air**0.5*y_Air**0.5': {'1': 2.0,
     '1 <= 2*x_Air**0.5*y_Air**0.5': 2.0,
     '2*x_Air**0.5*y_Air**0.5': 2.0},
    'y_Air <= 0.5': {'0.5': 1.0, 'overall': 1.0, 'y_Air': 1.0}}},
  'la': array([ 1.,  2.,  1.]),
  'nu': array([ 1.,  2.,  1.])},
 'signomialstart': {x: 0.40000000000000002, y_Air: 1, x_Air: 1},
 'variables': {y_Air: 0.5, x_Air: 0.50000000000000022}}

In [7]: m.localsolve(x0={'x':0.4})
Beginning signomial solve.
{y_Air: 1, x_Air: 1, 'x': 0.4}
{y_Air: 0.5, x_Air: 0.69692630281888834}
{y_Air: 0.5, x_Air: 0.51181272094965147}
{y_Air: 0.5, x_Air: 0.50006737019589742}
Solving took 4 GP solves and 0.0711 seconds.

Cost
----
 0.5

Free Variables
--------------
x : 0.5 
y : 0.5 

@whoburg
Copy link
Collaborator

whoburg commented Apr 17, 2016

This is not a solution, but a minor comment: Variable objects have a key attribute that returns the varkey, so you can replace x.varkeys.keys()[0] with x.key.

@pgkirsch
Copy link
Contributor Author

Ah yes..whoops

@pgkirsch
Copy link
Contributor Author

I believe I have resolved the pressing issue at hand ( simply removing the init.update(wi_sol['variables']) line worked) but I believe this ticket should remain open because working with the x0 dict still seems fairly opaque to me, and I'm not sure if my efforts are achieving what I'm trying to do.

@whoburg
Copy link
Collaborator

whoburg commented Apr 20, 2016

Things we'd like to document better related to this issue:

  1. when it's ok to use strings vs varkeys (in substitution dics, x0 dicts, etc)
  2. How model names are added and stripped from x0 and substitution dicts

@bqpd
Copy link
Contributor

bqpd commented Apr 20, 2016

I believe preferred syntax is x0={m["x"]: 0.4} right now...

I'm wondering if we shouldn't start expecting strings in fewer places, so as to reduce confusion with named models like Air.

@bqpd bqpd modified the milestones: Docs, Next release May 12, 2016
@bqpd bqpd changed the title Initializing a composed model using results of constituent model solves When to use strings vs varkeys May 12, 2016
@bqpd bqpd removed this from the Documentation milestone Mar 21, 2018
@bqpd bqpd mentioned this issue Mar 21, 2018
7 tasks
@bqpd bqpd closed this as completed Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants