「数理計画法 ~線形計画法~」を参考にmatplotlibとWolframAlphaでやってみたい。


(オリジナルポスト)
https://qiita.com/momo10/items/824c2fd8a0f432a676f4

(参考)線形計画法超入門
https://qiita.com/Dason08/items/e1bafb9ddc766d1c8fd0

作業中です。整数にしか対応してません。勉強中。簡単に3D表示できますか?
アドバイスもらえると助かります。
完成形?を探しています。よろしくお願いします。

matplotlibでやってみたい

from sympy import *
import numpy as np
import matplotlib.pyplot as plt

var('problem x y')
var('x1 x2')
problem = -5 * x1 - 4 * x2
mymax = 10
x = np.linspace(0, mymax, mymax + 1)
y1 = (30 - 5 * x) / 2
y2 = (14 - x) / 2
y3 = np.zeros_like(x)
y4 = np.minimum(y1, y2)
plt.figure()
plt.plot(x, y1, label="mikannsei")
plt.plot(x, y1, label="5*x1 + 2*x2 <=30")
plt.plot(x, y2, label="1*x1 + 2*x2 <=14")
plt.plot(x, y2, label=str(problem) + " GOKEI")
plt.fill_between(x, y3, y4, where=y4 >= y3, facecolor='yellow', alpha=0.3)
plt.ylim(0, 8.5)
plt.xlim(0, 4.5)
plt.legend(loc=0)
plt.xticks(np.arange(0, mymax + 2, 1))
plt.yticks(np.arange(0, mymax + 2, 1))
plt.gca().set_aspect('equal')
plt.grid()
for i in range(8):
    for j in range(8):
        if 5 * i + 2 * j <= 30 and i + 2 * j <= 14:
            plt.text(i, j, -5 * i - 4 * j)
plt.show()

WolframAlphaでやってみたい

整数解:
{{x == 0, y == 0}, {x == 0, y == 1}, {x == 0, y == 2}, {x == 0, y == 3}, {x == 0, y == 4}, {x == 0, y == 5}, {x == 0, y == 6}, {x == 0, y == 7}, {x == 1, y == 0}, {x == 1, y == 1}, {x == 1, y == 2}, {x == 1, y == 3}, {x == 1, y == 4}, {x == 1, y == 5}, {x == 1, y == 6}, {x == 2, y == 0}, {x == 2, y == 1}, {x == 2, y == 2}, {x == 2, y == 3}, {x == 2, y == 4}, {x == 2, y == 5}, {x == 2, y == 6}, {x == 3, y == 0}, {x == 3, y == 1}, {x == 3, y == 2}, {x == 3, y == 3}, {x == 3, y == 4}, {x == 3, y == 5}, {x == 4, y == 0}, {x == 4, y == 1}, {x == 4, y == 2}, {x == 4, y == 3}, {x == 4, y == 4}, {x == 4, y == 5}, {x == 5, y == 0}, {x == 5, y == 1}, {x == 5, y == 2}}

整数解の数:
38