# Signomial Programming¶

Signomial programming finds a local solution to a problem of the form:

where each \(f\) is monomial while each \(g\) and \(h\) is a posynomial.

This requires multiple solutions of geometric programs, and so will take longer to solve than an equivalent geometric programming formulation.

In general, when given the choice of which variables to include in the positive-posynomial / \(g\) side of the constraint, the modeler should:

- maximize the number of variables in \(g\),
- prioritize variables that are in the objective,
- then prioritize variables that are present in other constraints.

The `.localsolve`

syntax was chosen to emphasize that signomial programming returns a local optimum. For the same reason, calling `.solve`

on an SP will raise an error.

By default, signomial programs are first solved conservatively (by assuming each \(h\) is equal only to its constant portion) and then become less conservative on each iteration.

## Example Usage¶

```
"""Adapted from t_SP in tests/t_geometric_program.py"""
import gpkit
# Decision variables
x = gpkit.Variable('x')
y = gpkit.Variable('y')
# must enable signomials for subtraction
with gpkit.SignomialsEnabled():
constraints = [x >= 1-y, y <= 0.1]
# create and solve the SP
m = gpkit.Model(x, constraints)
print m.localsolve(verbosity=0).summary()
assert abs(m.solution(x) - 0.9) < 1e-6
```

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 \(x_k\), however, you may do so by putting that position (a dictionary formatted as you would a substitution) as the `xk`

argument.

## Sequential Geometric Programs¶

The method of solving local GP approximations of a non-GP compatible model can be generalized, at the cost of the general smoothness and lack of a need for trust regions that SPs guarantee.

For some applications, it is useful to call external codes which may not be GP compatible. Imagine we wished to solve the following optimization problem:

This problem is not GP compatible due to the sin(x) constraint. One approach might be to take the first term of the Taylor expansion of sin(x) and attempt to solve:

```
"Can be found in gpkit/docs/source/examples/sin_approx_example.py"
import numpy as np
from gpkit import Variable, Model
x = Variable("x")
y = Variable("y")
objective = y
constraints = [y >= x,
x <= np.pi/2.,
x >= np.pi/4.,
]
m = Model(objective, constraints)
print m.solve(verbosity=0).summary()
```

```
Cost
----
0.7854
Free Variables
--------------
x : 0.7854
y : 0.7854
```

We can do better, however, by utilizing some built in functionality of GPkit.
For simple cases with a single Variable, GPkit looks for `externalfn`

metadata:

```
"Can be found in gpkit/docs/source/examples/external_sp2.py"
import numpy as np
from gpkit import Variable, Model
x = Variable("x")
def y_ext(self, x0):
"Returns constraints on y derived from x0"
if x not in x0:
return self >= x
return self >= x/x0[x] * np.sin(x0[x])
y = Variable("y", externalfn=y_ext)
m = Model(y, [np.pi/4 <= x, x <= np.pi/2])
print m.localsolve(verbosity=0).summary()
```

```
Cost
----
0.7854
Free Variables
--------------
x : 0.7854
y : 0.7854
```

However, for external functions not intrinsically tied to a single variable it’s best to use the full ConstraintSet API, as follows:

Assume we have some external code which is capable of evaluating our incompatible function:

```
"""External function for GPkit to call. Can be found
in gpkit/docs/source/examples/external_function.py"""
import numpy as np
def external_code(x):
"Returns sin(x)"
return np.sin(x)
```

Now, we can create a ConstraintSet that allows GPkit to treat the incompatible constraint as though it were a signomial programming constraint:

```
"Can be found in gpkit/docs/source/examples/external_constraint.py"
from gpkit.exceptions import InvalidGPConstraint
from external_function import external_code
class ExternalConstraint(object):
"Class for external calling"
varkeys = {}
def __init__(self, x, y):
# We need a GPkit variable defined to return in our constraint. The
# easiest way to do this is to read in the parameters of interest in
# the initiation of the class and store them here.
self.x = x
self.y = y
def as_posyslt1(self, _):
"Ensures this is treated as an SGP constraint"
raise InvalidGPConstraint("ExternalConstraint cannot solve as a GP.")
def as_gpconstr(self, x0, _):
"Returns locally-approximating GP constraint"
# Unpacking the GPkit variables
x = self.x
y = self.y
# Creating a default constraint for the first solve
if not x0:
return (y >= x)
# Returns constraint updated with new call to the external code
else:
# Unpack Design Variables at the current point
x_star = x0["x"]
# Call external code
res = external_code(x_star)
# Return linearized constraint
return (y >= res*x/x_star)
```

and replace the incompatible constraint in our GP:

```
"Can be found in gpkit/docs/source/examples/external_sp.py"
import numpy as np
from gpkit import Variable, Model
from external_constraint import ExternalConstraint
x = Variable("x")
y = Variable("y")
objective = y
constraints = [ExternalConstraint(x, y),
x <= np.pi/2.,
x >= np.pi/4.,
]
m = Model(objective, constraints)
print m.localsolve(verbosity=0).summary()
```

```
Cost
----
0.7854
Free Variables
--------------
x : 0.7854
y : 0.7854
```

which is the expected result. This method has been generalized to larger problems, such as calling XFOIL and AVL.

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

argument