ロジスティクス・ネットワーク設計問題

# これなに
[ロジスティクス・ネットワーク設計問題](http://www.orsj.or.jp/~wiki/wiki/index.php/%E3%80%8A%E3%83%AD%E3%82%B8%E3%82%B9%E3%83%86%E3%82%A3%E3%82%AF%E3%82%B9%E3%83%8D%E3%83%83%E3%83%88%E3%83%AF%E3%83%BC%E3%82%AF%E8%A8%AD%E8%A8%88%E5%95%8F%E9%A1%8C%E3%80%8B)の実行例

需要を満たしつつ輸送費と生産費の和が最小となるように、どこで何をどれだけ生産し、どう輸送するかを求める。

# Pythonで試す
諸元

```py3:python
製品 = list('AB')
需要地 = list('PQ')
工場 = list('XY')
レーン = (2, 2)
```

輸送費表

```py3:python
import numpy as np, pandas as pd
tbdi = pd.DataFrame(((j, k) for j in 需要地 for k in 工場), columns=['需要地', '工場'])
tbdi['輸送費'] = [1,2,3,1]
tbdi
```

<table>
    <tr>
      <th></th>
      <th>需要地</th>
      <th>工場</th>
      <th>輸送費</th>
    </tr>
    <tr>
      <th>0</th>
      <td>P</td>
      <td>X</td>
      <td>1</td>
    </tr>
    <tr>
      <th>1</th>
      <td>P</td>
      <td>Y</td>
      <td>2</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Q</td>
      <td>X</td>
      <td>3</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Q</td>
      <td>Y</td>
      <td>1</td>
    </tr>
</table>

需要表

```py3:python
tbde = pd.DataFrame(((j, i) for j in 需要地 for i in 製品), columns=['需要地', '製品'])
tbde['需要'] = [10, 10, 20, 20]
tbde
```

<table>
    <tr>
      <th></th>
      <th>需要地</th>
      <th>製品</th>
      <th>需要</th>
    </tr>
    <tr>
      <th>0</th>
      <td>P</td>
      <td>A</td>
      <td>10</td>
    </tr>
    <tr>
      <th>1</th>
      <td>P</td>
      <td>B</td>
      <td>10</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Q</td>
      <td>A</td>
      <td>20</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Q</td>
      <td>B</td>
      <td>20</td>
    </tr>
</table>

生産表

```py3:python
tbfa = pd.DataFrame(((k, l, i, 0, np.inf) for k, nl in zip(工場, レーン)
    for l in range(nl) for i in 製品), columns=['工場', 'レーン', '製品', '下限', '上限'])
tbfa['生産費'] = [1, np.nan, np.nan, 1, 3,  np.nan, 5, 3]
tbfa.dropna(inplace=True)
tbfa.ix[4, '上限'] = 10
tbfa
```

<table>
    <tr>
      <th></th>
      <th>工場</th>
      <th>レーン</th>
      <th>製品</th>
      <th>下限</th>
      <th>上限</th>
      <th>生産費</th>
    </tr>
    <tr>
      <th>0</th>
      <td>X</td>
      <td>0</td>
      <td>A</td>
      <td>0</td>
      <td>inf</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>X</td>
      <td>1</td>
      <td>B</td>
      <td>0</td>
      <td>inf</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>Y</td>
      <td>0</td>
      <td>A</td>
      <td>0</td>
      <td>10.000000</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>Y</td>
      <td>1</td>
      <td>A</td>
      <td>0</td>
      <td>inf</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>Y</td>
      <td>1</td>
      <td>B</td>
      <td>0</td>
      <td>inf</td>
      <td>3.0</td>
    </tr>
</table>

解く

```py3:python
from ortoolpy import logistics_network
_, tbdi2, _ = logistics_network(tbde, tbdi, tbfa)
```

結果：生産量(ValY)

```py3:python
tbfa
```

<table>
    <tr>
      <th></th>
      <th>工場</th>
      <th>レーン</th>
      <th>製品</th>
      <th>下限</th>
      <th>上限</th>
      <th>生産費</th>
      <th>VarY</th>
      <th>ValY</th>
    </tr>
    <tr>
      <th>0</th>
      <td>X</td>
      <td>0</td>
      <td>A</td>
      <td>0</td>
      <td>inf</td>
      <td>1.0</td>
      <td>v9</td>
      <td>20.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>X</td>
      <td>1</td>
      <td>B</td>
      <td>0</td>
      <td>inf</td>
      <td>1.0</td>
      <td>v10</td>
      <td>10.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>Y</td>
      <td>0</td>
      <td>A</td>
      <td>0</td>
      <td>10.000000</td>
      <td>3.0</td>
      <td>v11</td>
      <td>10.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>Y</td>
      <td>1</td>
      <td>A</td>
      <td>0</td>
      <td>inf</td>
      <td>5.0</td>
      <td>v12</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>Y</td>
      <td>1</td>
      <td>B</td>
      <td>0</td>
      <td>inf</td>
      <td>3.0</td>
      <td>v13</td>
      <td>20.0</td>
    </tr>
</table>

結果：輸送量(ValX)

```py3:python
tbdi2
```

<table>
    <tr>
      <th></th>
      <th>需要地</th>
      <th>工場</th>
      <th>輸送費</th>
      <th>製品</th>
      <th>VarX</th>
      <th>ValX</th>
    </tr>
    <tr>
      <th>0</th>
      <td>P</td>
      <td>X</td>
      <td>1</td>
      <td>A</td>
      <td>v1</td>
      <td>10.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>P</td>
      <td>X</td>
      <td>1</td>
      <td>B</td>
      <td>v2</td>
      <td>10.0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Q</td>
      <td>X</td>
      <td>3</td>
      <td>A</td>
      <td>v3</td>
      <td>10.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Q</td>
      <td>X</td>
      <td>3</td>
      <td>B</td>
      <td>v4</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>P</td>
      <td>Y</td>
      <td>2</td>
      <td>A</td>
      <td>v5</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>P</td>
      <td>Y</td>
      <td>2</td>
      <td>B</td>
      <td>v6</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>Q</td>
      <td>Y</td>
      <td>1</td>
      <td>A</td>
      <td>v7</td>
      <td>10.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>Q</td>
      <td>Y</td>
      <td>1</td>
      <td>B</td>
      <td>v8</td>
      <td>20.0</td>
    </tr>
</table>

以上