mobook / MO-book

Hands-On Optimization with Python
MIT License
153 stars 40 forks source link

Issue on page /notebooks/05/01-milk-pooling.html #78

Closed jon-nowacki closed 3 months ago

jon-nowacki commented 8 months ago

Your issue content here.

This block

m_est = milk_pooling_bilinear(m_convex.p())
report_solution(m_est)

fig, ax = plt.subplots(figsize=(9, 5))
ax.plot(p_plot, f_plot, lw=2)
ax.set_xlabel("Pool composition p")
ax.set_ylabel("Profit")
ax.grid(True)

ax.plot(m_convex.p(), m_convex.profit(), "ro", ms=10)
ax.axhline(m_convex.profit(), color="r", linestyle="--")
ax.axvline(m_convex.p(), color="r", linestyle="--")
ax.annotate(
    "convex approximation",
    xy=(m_convex.p(), m_convex.profit()),
    xytext=(0.036, 106000),
    ha="right",
    fontsize=14,
    arrowprops=dict(shrink=0.1, width=1, headwidth=5, facecolor="black"),
)

ax.plot(m_est.p(), m_est.profit(), "go", ms=10)
ax.axhline(m_est.profit(), color="g", linestyle="--")
ax.annotate(
    "local max found",
    xy=(m_convex.p(), m_est.profit()),
    xytext=(0.045, 105000),
    ha="left",
    fontsize=14,
    arrowprops=dict(shrink=0.1, width=1, headwidth=5, facecolor="black"),
)
plt.rcParams.update({"font.size": 14})
plt.tight_layout()
plt.show()

errors out like this:

ERROR:pyomo.common.numeric_types:evaluating object as numeric value: z[Farm A,Customer 1]
    (object: <class 'pyomo.core.base.var._GeneralVarData'>)
No value for uninitialized NumericValue object z[Farm A,Customer 1]
'Milk pooling v3 (Bilinear formulation)'

Pool composition = 0.0400
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-11-c3111e1a4d66>](https://localhost:8080/#) in <cell line: 2>()
      1 m_est = milk_pooling_bilinear(m_convex.p())
----> 2 report_solution(m_est)
      3 
      4 fig, ax = plt.subplots(figsize=(9, 5))
      5 ax.plot(p_plot, f_plot, lw=2)
7 frames
[/usr/local/lib/python3.10/dist-packages/pyomo/common/numeric_types.py](https://localhost:8080/#) in value(obj, exception)
    308             tmp = obj(exception=True)
    309             if tmp is None:
--> 310                 raise ValueError(
    311                     "No value for uninitialized NumericValue object %s" % (obj.name,)
    312                 )

ValueError: No value for uninitialized NumericValue object z[Farm A,Customer 1]
alessandrozocca commented 3 months ago

Thanks for spotting the error. The model m_est was built but never solved. The latest commit fixes this by adding the line

SOLVER_LO.solve(m_est)