Skip to content

Commit

Permalink
Merge branch 'master' into issue1088
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Mar 21, 2021
2 parents f26e804 + 1b09b7f commit bc0231e
Show file tree
Hide file tree
Showing 29 changed files with 629 additions and 94 deletions.
59 changes: 59 additions & 0 deletions docs/source/advancedcommands.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
Advanced Commands
*****************

.. _migp:

Choice Variables
================
If MOSEK 9 is installed, GPkit supports discretized free variables with the ``mosek_conif`` solver.
Choice variables are free in the sense of having multiple possible choices, but discretized in the
sense of having a limited set of possible solutions.

.. literalinclude:: examples/migp.py

If solved with the mosek_conif solver, the script above will print::

Optimal Cost
------------
[ 1.5 2.15 2.8 3.22 ... ]

~~~~~~~~
WARNINGS
~~~~~~~~
No Dual Solution
----------------
This model has the discretized choice variables [x] and hence no dual
solution. You can fix those variables to their optimal value and get
sensitivities to the resulting continuous problem by updating your model's
substitions with `sol["choicevariables"]`.
~~~~~~~~

Swept Variables
---------------
numerator : [ 0.5
1.15
1.8
2.45
3.1
3.75
4.4
5.05
5.7
6.35
7 ]

Free Variables
--------------
x : [ 1
1
1
2
2
2
2
2
2
3
3 ]

Note that the optimal values for ``x`` are discretized, clicking from 1
to 2 to 3 during the sweep, and that the solution has no dual variables.


Derived Variables
=================

Expand Down
10 changes: 10 additions & 0 deletions docs/source/examples/migp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import numpy as np
from gpkit import *

x = Variable("x", choices=range(1,4))
num = Variable("numerator", np.linspace(0.5, 7, 11))

m = Model(x + num/x)
sol = m.solve(verbosity=0)

print(sol.table())
59 changes: 59 additions & 0 deletions docs/source/examples/migp_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

Optimal Cost
------------
[ 1.41 2.14 2.68 3.13 ... ]

~~~~~~~~
WARNINGS
~~~~~~~~
Freed Choice Variables
----------------------
This model has the discretized choice variables [x], but since the 'cvxopt' solver doesn't support discretization they were treated as continuous variables.
~~~~~~~~

Swept Variables
---------------
numerator : [ 0.5
1.15
1.8
2.45
3.1
3.75
4.4
5.05
5.7
6.35
7 ]

Free Variables
--------------
x : [ 0.707
1.07
1.34
1.57
1.76
1.94
2.1
2.25
2.39
2.52
2.65 ]

Variable Sensitivities
----------------------
numerator : [ +0.5
+0.5
+0.5
+0.5
+0.5
+0.5
+0.5
+0.5
+0.5
+0.5
+0.5 ]

Most Sensitive Constraints (in last sweep)
------------------------------------------
(none)

17 changes: 11 additions & 6 deletions docs/source/examples/performance_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,14 @@ def setup(self):
sol = M.solve(verbosity=0)
print(sol.diff("solution.pkl", showvars=vars_of_interest, sortbymodel=False))

# this will only make an image when run in jupyter notebook
# from gpkit.interactive.sankey import Sankey
from gpkit.interactive.sankey import Sankey
variablesankey = Sankey(sol, M).diagram(AC.wing.A)
sankey = Sankey(sol, M).diagram(width=1200, height=400, maxlinks=30)
sankey # pylint: disable=pointless-statement
try:
from gpkit.interactive.sankey import Sankey
variablesankey = Sankey(sol, M).diagram(AC.wing.A)
sankey = Sankey(sol, M).diagram(width=1200, height=400, maxlinks=30)
# the line below shows an interactive graph if run in jupyter notebook
sankey # pylint: disable=pointless-statement
except (ImportError, ModuleNotFoundError):
print("Making Sankey diagrams requires the ipysankeywidget package")

from gpkit.interactive.references import referencesplot
referencesplot(M, openimmediately=False)
2 changes: 1 addition & 1 deletion docs/source/examples/relaxation_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Relaxed Constants
Model Sensitivities (sorts models in sections below)
-------------------
+2.0 : Relax2.OriginalValues
<1E-8 : Relax2
<1e-8 : Relax2

Free Variables
--------------
Expand Down
4 changes: 4 additions & 0 deletions docs/source/examples/simple_sp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
# full interim solutions are available
print("x values of each GP solve (note convergence)")
print(", ".join("%.5f" % sol["freevariables"][x] for sol in m.program.results))

# use x0 to give the solution, reducing number of GPs needed
m.localsolve(verbosity=0, x0={x: 0.9, y:0.1})
assert len(m.program.results) == 2
Binary file added docs/source/figures/referencesplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 17 additions & 15 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,44 @@ Installation
************

1. If you are on Mac or Windows, we recommend installing `Anaconda <http://www.continuum.io/downloads>`_. Alternatively, `install pip and create a virtual environment <https://packaging.python.org/guides/installing-using-pip-and-virtualenv/>`_.
2. (optional) Install the MOSEK solver as directed below
3. Run ``pip install gpkit`` in the appropriate terminal or command prompt.
4. Open a Python prompt and run ``import gpkit`` to finish installation and run unit tests.
2. (optional) Install the MOSEK 9 solver with ``pip install Mosek``, then a license as described below
3. (optional) Install the MOSEK 8 solver as described below
4. Run ``pip install gpkit`` in the appropriate terminal or command prompt.
5. Open a Python prompt and run ``import gpkit`` to finish installation and run unit tests.

If you encounter any bugs please email ``gpkit@mit.edu``
or `raise a GitHub issue <http://github.com/convexengineering/gpkit/issues/new>`_.


Installing MOSEK
================
GPkit interfaces with two off the shelf solvers: cvxopt, and MOSEK.
Installing MOSEK 8
==================
GPkit interfaces with two off the shelf solvers: cvxopt, and MOSEK (versions 8 and 9).
Cvxopt is open source and installed by default; MOSEK requires a commercial licence or (free)
academic license.
academic license. In MOSEK version 8 GPkit uses the command-line interface ``mskexpopt`` solver, while
in MOSEK 9 it uses the more active exponential-cone interface (and hence supports :ref:`migp`).

Mac OS X
- If ``which gcc`` does not return anything, install the `Apple Command Line Tools <https://developer.apple.com/downloads/index.action?=command%20line%20tools>`_.
- Download `MOSEK 8 <https://www.mosek.com/downloads/>`_, then:
- Move the ``mosek`` folder to your home directory
- Follow `these steps for Mac <http://docs.mosek.com/7.0/toolsinstall/Mac_OS_X_installation.html>`_.
- Request an `academic license file <http://license.mosek.com/academic>`_ and put it in ``~/mosek/``
- Request an `academic license file <http://license.mosek.com/academic>`_ and put it in ``~/mosek/``

Linux
- Download `MOSEK 8 <https://www.mosek.com/downloads/>`_, then:
- Move the ``mosek`` folder to your home directory
- Follow `these steps for Linux <http://docs.mosek.com/7.0/toolsinstall/Linux_UNIX_installation_instructions.html>`_.
- Request an `academic license file <http://license.mosek.com/academic>`_ and put it in ``~/mosek/``
- Request an `academic license file <http://license.mosek.com/academic>`_ and put it in ``~/mosek/``

Windows
- Make sure ``gcc`` is on your system path.
- To do this, type ``gcc`` into a command prompt.
- If you get ``executable not found``, then install the 64-bit version (x86_64 installer architecture dropdown option) with GCC version 6.4.0 or older of `mingw <http://sourceforge.net/projects/mingw-w64/>`_.
- In an Anaconda command prompt (or equivalent), run ``cd C:\Program Files\mingw-w64\x86_64-6.4.0-posix-seh-rt_v5-rev0\`` (or whatever corresponds to the correct installation directory; note that if mingw is in ``Program Files (x86)`` instead of ``Program Files`` you've installed the 32-bit version by mistake)
- Run ``mingw-w64`` to add it to your executable path. For step 3 of the install process you'll need to run ``pip install gpkit`` from this prompt.
- Download `MOSEK 8 <https://www.mosek.com/downloads/>`_, then:
- Follow `these steps for Windows <http://docs.mosek.com/7.0/toolsinstall/Windows_installation.html>`_.
- Request an `academic license file <http://license.mosek.com/academic>`_ and put it in ``C:\Users\(your_username)\mosek\``
- Make sure ``gcc`` is on your system path.
- To do this, type ``gcc`` into a command prompt.
- If you get ``executable not found``, then install the 64-bit version (x86_64 installer architecture dropdown option) with GCC version 6.4.0 or older of `mingw <http://sourceforge.net/projects/mingw-w64/>`_.
- In an Anaconda command prompt (or equivalent), run ``cd C:\Program Files\mingw-w64\x86_64-6.4.0-posix-seh-rt_v5-rev0\`` (or whatever corresponds to the correct installation directory; note that if mingw is in ``Program Files (x86)`` instead of ``Program Files`` you've installed the 32-bit version by mistake)
- Run ``mingw-w64`` to add it to your executable path. For step 3 of the install process you'll need to run ``pip install gpkit`` from this prompt.
- Request an `academic license file <http://license.mosek.com/academic>`_ and put it in ``C:\Users\(your_username)\mosek\``

Debugging your installation
===========================
Expand Down
2 changes: 1 addition & 1 deletion docs/source/signomialprogramming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Example Usage

When using the ``localsolve`` method, the ``reltol`` argument specifies the relative tolerance of the solver: that is, by what percent does the solution have to improve between iterations? If any iteration improves less than that amount, the solver stops and returns its value.

If you wish to start the local optimization at a particular point :math:`x_k`, however, you may do so by putting that position (a dictionary formatted as you would a substitution) as the ``xk`` argument.
If you wish to start the local optimization at a particular point :math:`x_0`, however, you may do so by putting that position (a dictionary formatted as you would a substitution) as the ``x0`` argument.

.. _sgp:

Expand Down
48 changes: 44 additions & 4 deletions docs/source/visint.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
Visualization and Interaction
*****************************

Variable Reference Plots
========================

Code in this section uses the `CE solar model <https://github.com/convexengineering/solar/tree/gpkitdocs>`_.

.. code:: python
from solar.solar import *
Vehicle = Aircraft(Npod=3, sp=True)
M = Mission(Vehicle, latitude=[20])
M.cost = M[M.aircraft.Wtotal]
sol = M.localsolve()
from gpkit.interactive.references import referencesplot
referencesplot(M, openimmediately=True)
Running the code above will produce two files in your working directory:
``referencesplot.html`` and ``referencesplot.json``, and (unless you specify
``openimmediately=False``) open the former in your web browser,
showing you something like this:

.. figure:: figures/referencesplot.png

`Click to see the interactive version of this plot. <https://web.mit.edu/eburn/www/referencesplot/referencesplot.html>`_

When a model's name is hovered over its connections are highlighted, showing in
red the other models it imports variables from to use in its constraints and in
blue the models that import variables from it.

By default connections are shown with equal width ("Unweighted").
When "Global Sensitivities" is selected, connection width is proportional to
the sensitivity of all variables in that connection to the importing model,
corresponding exactly to how much the model's cost would decrease if those variables were relaxed
in only that importing model. This can give a sense of which connections are vital to
the overall model. When "Normalized Sensitivities" is selected, that
global weight is divided by the weight of all variables in the importing model,
giving a sense of which connections are vital to each subsystem.


.. _sankey:
Sensitivity Diagrams
====================
Expand All @@ -9,9 +49,9 @@ Requirements
- Jupyter Notebook
- `ipysankeywidget <https://github.com/ricklupton/ipysankeywidget>`__
- Note that you'll need to activate these widgets on Jupyter by runnning

- ``jupyter nbextension enable --py --sys-prefix widgetsnbextension``

- ``jupyter nbextension enable --py --sys-prefix ipysankeywidget``

Example
Expand All @@ -25,10 +65,10 @@ Code in this section uses the `CE solar model <https://github.com/convexengineer
Vehicle = Aircraft(Npod=3, sp=True)
M = Mission(Vehicle, latitude=[20])
M.cost = M[M.aircraft.Wtotal]
sol = M.localsolve("mosek_cli")
sol = M.localsolve()
from gpkit.interactive.sankey import Sankey
Once the code above has been run in a Jupyter notebook, the code below will create interactive hierarchies of your model's sensitivities, like so:

.. figure:: figures/Mission.gif
Expand Down
3 changes: 2 additions & 1 deletion fulltests.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
python -c "from gpkit.tests import run; run()"
python -c "from gpkit.tests import run; run()"
rm *.pkl
rm solution.*
rm referencesplot.*
3 changes: 2 additions & 1 deletion gpkit/constraints/bounded.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from .. import Variable
from .set import ConstraintSet
from ..small_scripts import appendsolwarning
from ..small_scripts import appendsolwarning, initsolwarning


def varkey_bounds(varkeys, lower, upper):
Expand Down Expand Up @@ -77,6 +77,7 @@ def process_result(self, result):
def check_boundaries(self, result):
"Creates (and potentially prints) a dictionary of unbounded variables."
out = defaultdict(set)
initsolwarning(result, "Arbitrarily Bounded Variables")
for i, varkey in enumerate(self.bound_varkeys):
value = result["variables"][varkey]
c_senss = [result["sensitivities"]["constraints"].get(c, 0)
Expand Down
Loading

0 comments on commit bc0231e

Please sign in to comment.