<jupyter_start><jupyter_text>선형계획법 Linear Programming<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import numpy.linalg as nl
import scipy.optimize as so<jupyter_output><empty_output><jupyter_text>ref : * Wikipedia [link](https://en.wikipedia.org/wiki/Linear_programming)* Stackoverflow [link](https://stackoverflow.com/questions/62571092/)* Tips & Tricks on Linux, Matlab, vim, LaTex, etc [link](http://tipstrickshowtos.blogspot.com/2012/04/how-to-render-argmax-argmin-operator-in.html)  Problem description문제 설명 * Area of the farm 농장의 넓이 : $L = 10 (km^2)$* Types of crops : wheat or rice작물의 종류 : 밀 또는 쌀* Available fertilizer 사용 가능한 비료의 양 : $F = 10 (kg)$* Available pesticide 사용 가능한 살충제의 양 : $P = 5 (kg)$|   | Wheat 밀 | rice 쌀 ||:-----:|:-----:|:-----:|| Needed Fertilizer per unit area $(kg/km^2)$단위면적 당 필요 비료 양 $(kg/km^2)$ | $F_1$ | $F_2$ || Needed Pesticide per unit area $(kg/km^2)$단위면적 당 필요 살충제 양 $(kg/km^2)$ | $P_1$ | $P_2$ || Selling price per unit area $(\$/km^2)$단위면적 당 매출 $(\$/km^2)$ | $S_1$ | $S_2$ || Planting area $(km^2)$재배 면적 $(km^2)$ | $x_1$ | $x_2$ |* Under the constraints, what are the areas of wheat and rice maximizing the overall selling price?제한조건 하에서 매출을 최대로 하는 밀과 쌀의 재배 면적? $$\underset{x_1, x_2}{\arg\max} \left(S_1 x_1 + S_2 x_2\right)$$ subject to 제한조건 $$\begin{align}    x_1 + x_2 & \le L \\    F_1 x_1 + F_2 x_2 & \le F \\    P_1 x_1 + P_2 x_2 & \le P \\    x_1, x_2 & \ge 0\end{align}$$ In matrix form 행렬 형태로는: $$\underset{x_1, x_2}{\arg\max} \begin{bmatrix} S_1 & S_2    \end{bmatrix}\begin{pmatrix} x_1 \\ x_2    \end{pmatrix}$$ subject to 제한조건 $$\begin{align}    \begin{bmatrix}        1 & 1 \\        F_1 &  F_2 \\        P_1 &  P_2 \\    \end{bmatrix}    \begin{pmatrix}        x_1 \\        x_2     \end{pmatrix} & \le     \begin{pmatrix}        L \\        F \\        P    \end{pmatrix} \\    \begin{pmatrix}        x_1 \\        x_2     \end{pmatrix}& \ge 0\end{align}$$  Parameters Example매개변수 예<jupyter_code>L = 10


F = 10
F1 = 2
F2 = 3


P = 5
P1 = 2
P2 = 1


S1 = 20
S2 = 25<jupyter_output><empty_output><jupyter_text>Visualization 시각화 $$\begin{align}    x_1 + x_2 & \le L \\    F_1 x_1 + F_2 x_2 & \le F \\    P_1 x_1 + P_2 x_2 & \le P \\    x_1, x_2 & \ge 0\end{align}$$ $$\begin{align}    x_2 & \le -x_1 + L \\    x_2 & \le -\frac{F_1}{F_2} x_1 + \frac{F}{F_2} \\    x_2 & \le -\frac{P_1}{P_2} x_1 + \frac{P}{P_2} \\    x_1 & \ge 0 \\    x_2 & \ge 0\end{align}$$<jupyter_code>x1 = np.linspace(0, 2.5, 101)
x2 = np.linspace(0, 5, 101)
X1, X2 = np.meshgrid(x1, x2)

C = S1 * X1 + S2 * X2

C[X2 > (-F1 * X1 + F) / F2] = np.nan
C[X2 > (-P1 * X1 + P) / P2] = np.nan

plt.pcolor(X1, X2, C, shading="auto")
plt.xlabel("$x_1$")
plt.ylabel("$x_2$")

plt.title("$S_1 x_1 + S_2 x_2$")
plt.colorbar()

plt.grid(True)<jupyter_output><empty_output><jupyter_text>`scipy.optimize.linprog()`<jupyter_code>c_T = -np.array((S1, S2))


A_ub = np.array(
    (
        (1, 1),
        (F1, F2),
        (P1, P2),
    )
)


b_ub = np.array(
    ((L, F, P),)
).T


bounds = (
    (0, None),
    (0, None),
)


result = so.linprog(c_T, A_ub, b_ub, bounds=bounds)
result<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1888535031847134, 'fraction_numerical': 0.05318471337579618, 'mean_word_length': 2.779783393501805, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '143053', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1577, 'n_words': 345, 'path': '15_optimization/050_Linear_Programming.ipynb', 'repo_name': 'kangwon-naver/nmisp', 'size': '3140'}