Stochastic Optimization in Python

I am trying to combine cvxopt(optimization solver) and PyMC (sampler) to solve convex stochastic optimization problems .

For reference, installing both packages using pipis simple:

pip install cvxopt
pip install pymc 

Both packages work independently of each other perfectly. Here is an example of how to solve the LP problem with cvxopt:

# Testing that cvxopt works
from cvxopt import matrix, solvers

# Example from http://cvxopt.org/userguide/coneprog.html#linear-programming

c = matrix([-4., -5.])
G = matrix([[2., 1., -1., 0.], [1., 2., 0., -1.]])
h = matrix([3., 3., 0., 0.])
sol = solvers.lp(c, G, h)
# The solution sol['x'] is correct: (1,1)

However, when I try to use it with PyMC (for example, putting the distribution on one of the coefficients), PyMC gives an error:

import pymc as pm
import cvxopt

c1 = pm.Normal('c1', mu=-4, tau=.5**-2)

@pm.deterministic
def my_lp_solver(c1=c1):
    c = matrix([c1, -5.])
    G = matrix([[2., 1., -1., 0.], [1., 2., 0., -1.]])
    h = matrix([3., 3., 0., 0.])
    sol = solvers.lp(c, G, h)
    solution = np.array(sol['x'],dtype=float).flatten()
    return solution

m = pm.MCMC(dict(c1=c1, x=x))
m.sample(20000, 10000, 10)

I get the following PyMC error:

<ipython-input-21-5ce2909be733> in x(c1)
     14 @pm.deterministic
     15 def x(c1=c1):
---> 16     c = matrix([c1, -5.])
     17     G = matrix([[2., 1., -1., 0.], [1., 2., 0., -1.]])
     18     h = matrix([3., 3., 0., 0.])

TypeError: invalid type in list

Why? Is there any way to make cvxoptgood play with PyMC?

Story:

, - , PyMC . , , , LP . , LP , LP.

, PyMC LP. LP ( ), PyMC , , .

, , ( cvxopt)

+4
1

c1, pm.Normal, numpy array, float(c1), :

>>> @pm.deterministic
... def my_lp_solver(c1=c1):
...     c = matrix([float(c1), -5.])
...     G = matrix([[2., 1., -1., 0.], [1., 2., 0., -1.]])
...     h = matrix([3., 3., 0., 0.])
...     sol = solvers.lp(c, G, h)
...     solution = np.array(sol['x'],dtype=float).flatten()
...     return solution
... 
     pcost       dcost       gap    pres   dres   k/t
 0: -8.1223e+00 -1.8293e+01  4e+00  0e+00  7e-01  1e+00
 1: -8.8301e+00 -9.4605e+00  2e-01  1e-16  4e-02  3e-02
 2: -9.0229e+00 -9.0297e+00  2e-03  2e-16  5e-04  4e-04
 3: -9.0248e+00 -9.0248e+00  2e-05  3e-16  5e-06  4e-06
 4: -9.0248e+00 -9.0248e+00  2e-07  2e-16  5e-08  4e-08
Optimal solution found.
+5

All Articles