# Stochastic optimization problem, ValueError

**URL:** <https://discourse.pymc.io/t/stochastic-optimization-problem-valueerror/10719>\
**Category:** v5\
**Tags:** modeling\
**Created:** [November 1, 2022, 4:41pm UTC](https://discourse.pymc.io/t/stochastic-optimization-problem-valueerror/10719 "2022-11-01T16:41:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrtkp9993](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mrtkp9993/32/5785_2.png) [@mrtkp9993](https://discourse.pymc.io/u/mrtkp9993)\
**Post date:** [November 1, 2022, 4:41pm UTC](https://discourse.pymc.io/t/stochastic-optimization-problem-valueerror/10719/1 "2022-11-01T16:41:11Z")

</div>

Hi everyone,

I am trying to solve following problem:

![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/2/21a26e4cd826d6405a9de607dfd7b065a05df58a.png)

Here is my code:

```auto
import numpy as np
import pymc as pm
from scipy.optimize import linprog

with pm.Model() as model:
    c1 = pm.Normal('c1', mu=2, tau=.5**-2)
    b1 = pm.Normal('b1', mu=0, tau=3)

    def lp_solve(c1=c1, b1=b1):
        obj = [c1.eval(), -3]

        lhs_ineq = [[-1, 1]]
        rhs_ineq = [b1.eval()]

        bnd = [(0, float("inf")),
               (0, float("inf"))]

        opt = linprog(c=obj, A_ub=lhs_ineq, b_ub=rhs_ineq, bounds=bnd, method="revised simplex")
        sol = np.array([opt['x'][0], opt['x'][1]], dtype=np.float32).flatten()
        return sol

    sols = pm.Deterministic(name="solutions", var=lp_solve(c1, b1))

    trace = pm.sample(20000)

```

I got following error:

```auto
ValueError Traceback (most recent call last)
Cell In [84], line 18
     15 sol = np.array([opt['x'][0], opt['x'][1]], dtype=np.float32).flatten()
     16 return sol
---> 18 sols = pm.Deterministic(name="solutions", var=lp_solve(c1, b1))
     20 trace = pm.sample(20000)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pymc\model.py:2034, in Deterministic(name, var, model, dims, auto)
   1973 """Create a named deterministic variable.
   1974 
   1975 Deterministic nodes are only deterministic given all of their inputs, i.e.
   (...)
   2031 var: var, with name attribute
   2032 """
   2033 model = modelcontext(model)
-> 2034 var = var.copy(model.name_for(name))
   2035 if auto:
   2036 model.auto_deterministics.append(var)

ValueError: order must be one of 'C', 'F', 'A', or 'K' (got 'solutions')

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![jessegrabowski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jessegrabowski/32/5010_2.png) [@jessegrabowski](https://discourse.pymc.io/u/jessegrabowski)\
**Post date:** [November 1, 2022, 5:28pm UTC](https://discourse.pymc.io/t/stochastic-optimization-problem-valueerror/10719/2 "2022-11-01T17:28:03Z")

</div>

This is happening because you are mixing numpy and aesara operations. `pm.Deterministic` expects an aesara tensor, but you are giving it a numpy array (np.ndarray.copy takes a string that defines the storage format of the array, while aesara.tensor.copy takes a string that defines the name of the symbol tensor object, hence the error).

If you want to use a scipy optimizer inside your model, you will need to wrap it into an aesara `Op`. Full docs on Ops are [here](https://aesara.readthedocs.io/en/latest/extending/creating_an_op.html), and an example inside a PyMC model can be found [here](https://www.pymc.io/projects/examples/en/latest/case_studies/blackbox_external_likelihood_numpy.html#aesara-op-without-grad).

Basically you need to tell it what datatypes and shapes to expect as inputs and outputs, then put your `lp_solve` code inside the `perform` method.

Note that doing this will not allow you to use NUTS, because your `Op` won’t have a gradient (unless you give it one).
