• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

拓端数据|R语言代写解决最优化运营研究问题-线性优化(LP)问题 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 原文:http://tecdat.cn/?p=3432

使用R中的线性编程工具来解决优化问题。

优化通常用于运营研究领域,以解决生产计划,运输网络设计,仓库位置分配和调度等问题,我们尝试最大化或最小化具有决策变量和约束数量的线性函数。

在这里,我使用了我的一个咨询项目,帮助我们的投资组合公司选择一个无线供应商,其中包含可以满足所有要求(总线数和汇总数据量)的数据计划组合,同时花费最少的金钱。

这种优化通常可以在Excel求解器中解决。但是,由于我有20个投资组合公司有2个提供商和2个方案进行分析,要在Excel中完成,我将不得不运行80次。使用R会容易得多。

加载包

<span style="color:#333333"><code><span style="color:#990000"><strong>library</strong></span><span style="color:#687687">(</span><span style="color:#000000">lpSolve</span><span style="color:#687687">)</span></code></span>

加载数据

<span style="color:#333333"><code><span style="color:#000000">usage</span> <span style="color:#687687"><-</span> <span style="color:#000000">read.csv</span><span style="color:#687687">(</span><span style="color:#dd1144">"usage.csv"</span><span style="color:#687687">)</span>
<span style="color:#000000">plan</span> <span style="color:#687687"><-</span> <span style="color:#000000">read.csv</span><span style="color:#687687">(</span><span style="color:#dd1144">"wireless_data_plan.csv"</span><span style="color:#687687">)</span></code></span>

使用数据

<span style="color:#333333"><code><span style="color:#000000">head</span><span style="color:#687687">(</span><span style="color:#000000">usage</span><span style="color:#687687">)</span></code></span>
##   Company Num_Lines Data_Usage
## 1       A       134      397.5
## 2       B       350     1037.5
## 3       C      1510     3462.5
## 4       D      2260     4437.5
## 5       E       750     2875.0
## 6       F       410      612.5
<span style="color:#333333"><code><span style="color:#000000">str</span><span style="color:#687687">(</span><span style="color:#000000">usage</span><span style="color:#687687">)</span></code></span>
## 'data.frame':    20 obs. of  3 variables:
##  $ Company   : Factor w/ 20 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ Num_Lines : int  134 350 1510 2260 750 410 2930 1091 3350 7760 ...
##  $ Data_Usage: num  398 1038 3462 4438 2875 ...

我们可以看到,我们在数据集中共有20家公司,平均线数和过去3个月的月度数据使用量。

现在,我查看摘要统计信息和公司数据的直方图。

  • 行数:我们可以看到平均行数约为1800,但大多数公司的行数少于2000行。只有一家公司有超过7000条线路的异常值。
  • 数据使用情况:每行的平均使用量约为2.5GB,范围从1GB到4GB。
<span style="color:#333333"><code><span style="color:#000000">summary</span><span style="color:#687687">(</span><span style="color:#000000">usage</span><span style="color:#687687">$</span><span style="color:#000000">Num_Lines</span><span style="color:#687687">)</span></code></span>
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   134.0   779.2  1083.0  1774.0  1909.0  7760.0
<span style="color:#333333"><code><span style="color:#000000">summary</span><span style="color:#687687">(</span><span style="color:#000000">usage</span><span style="color:#687687">$</span><span style="color:#000000">Data_Usage</span><span style="color:#687687">/</span><span style="color:#000000">usage</span><span style="color:#687687">$</span><span style="color:#000000">Num_Lines</span><span style="color:#687687">)</span></code></span>
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.004   1.674   2.527   2.547   3.075   4.475
<span style="color:#333333"><code><span style="color:#000000">par</span><span style="color:#687687">(</span><span style="color:#000000">mfrow</span> <span style="color:#687687">=</span> <span style="color:#000000">c</span><span style="color:#687687">(</span><span style="color:#009999">1</span>,<span style="color:#009999">2</span><span style="color:#687687">)</span><span style="color:#687687">)</span>
<span style="color:#000000">hist</span><span style="color:#687687">(</span><span style="color:#000000">usage</span><span style="color:#687687">$</span><span style="color:#000000">Num_Lines</span>, <span style="color:#000000">main</span> <span style="color:#687687">=</span> <span style="color:#dd1144">"Number of Lines"</span>, <span style="color:#000000">xlab</span> <span style="color:#687687">=</span> <span style="color:#dd1144">"Number of Lines"</span><span style="color:#687687">)</span>
<span style="color:#000000">hist</span><span style="color:#687687">(</span><span style="color:#000000">usage</span><span style="color:#687687">$</span><span style="color:#000000">Data_Usage</span><span style="color:#687687">/</span><span style="color:#000000">usage</span><span style="color:#687687">$</span><span style="color:#000000">Num_Lines</span>, <span style="color:#000000">main</span> <span style="color:#687687">=</span> <span style="color:#dd1144">"Data Usage"</span>, <span style="color:#000000">xlab</span> <span style="color:#687687">=</span> <span style="color:#dd1144">"Data Usage - GB"</span><span style="color:#687687">)</span></code></span>

​ 计划数据

<span style="color:#333333"><code><span style="color:#000000">head</span><span style="color:#687687">(</span><span style="color:#000000">plan</span><span style="color:#687687">)</span></code></span>
##   Wireless_Carrier Data_GB Plan_Rate
## 1              ATT       3        60
## 2              ATT       4        75
## 3              ATT       5        85
## 4              ATT       6       100
## 5              VZW       1        56
## 6              VZW       2        60
<span style="color:#333333"><code><span style="color:#000000">str</span><span style="color:#687687">(</span><span style="color:#000000">plan</span><span style="color:#687687">)</span></code></span>
## 'data.frame':    10 obs. of  3 variables:
##  $ Wireless_Carrier: Factor w/ 2 levels "ATT","VZW": 1 1 1 1 2 2 2 2 2 2
##  $ Data_GB         : int  3 4 5 6 1 2 4 6 8 10
##  $ Plan_Rate       : int  60 75 85 100 56 60 70 80 90 100

我们还可以看到我们有来自AT&T和Verizon Wireless的不同级别的数据计划供我们选择。此分析的目标是选择具有最低总成本的不同数据计划组合的运营商,同时满足线路数量和总数据要求

创建目标函数,约束和约束方向对象

<span style="color:#333333"><code><span style="color:#000000">obj.fun.att</span> <span style="color:#687687"><-</span> <span style="color:#000000">plan</span><span style="color:#687687">[</span><span style="color:#009999">1</span><span style="color:#687687">:</span><span style="color:#009999">4</span>, <span style="color:#009999">3</span><span style="color:#687687">]</span>
<span style="color:#000000">obj.fun.vzw</span> <span style="color:#687687"><-</span> <span style="color:#000000">plan</span><span style="color:#687687">[</span><span style="color:#009999">5</span><span style="color:#687687">:</span><span style="color:#009999">10</span>, <span style="color:#009999">3</span><span style="color:#687687">]</span>
<span style="color:#000000">constr.att</span> <span style="color:#687687"><-</span> <span style="color:#000000">matrix</span><span style="color:#687687">(</span><span style="color:#000000">c</span><span style="color:#687687">(</span><span style="color:#009999">1</span>, <span style="color:#009999">1</span>, <span style="color:#009999">1</span>, <span style="color:#009999">1</span>, <span style="color:#000000">as.vector</span><span style="color:#687687">(</span><span style="color:#000000">plan</span><span style="color:#687687">[</span><span style="color:#009999">1</span><span style="color:#687687">:</span><span style="color:#009999">4</span>, <span style="color:#009999">2</span><span style="color:#687687">]</span><span style="color:#687687">)</span><span style="color:#687687">)</span>, <span style="color:#000000">ncol</span> <span style="color:#687687">=</span> <span style="color:#009999">4</span>, <span style="color:#000000">byrow</span> <span style="color:#687687">=</span> <span style="color:#990073">TRUE</span><span style="color:#687687">)</span>
<span style="color:#000000">constr.vzw</span> <span style="color:#687687"><-</span> <span style="color:#000000">matrix</span><span style="color:#687687">(</span><span style="color:#000000">c</span><span style="color:#687687">(</span><span style="color:#009999">1</span>, <span style="color:#009999">1</span>, <span style="color:#009999">1</span>, <span style="color:#009999">1</span>, <span style="color:#009999">1</span>, <span style="color:#009999">1</span>, <span style="color:#000000">as.vector</span><span style="color:#687687">(</span><span style="color:#000000">plan</span><span style="color:#687687">[</span><span style="color:#009999">5</span><span style="color:#687687">:</span><span style="color:#009999">10</span>, <span style="color:#009999">2</span><span style="color:#687687">]</span><span style="color:#687687">)</span><span style="color:#687687">)</span>, <span style="color:#000000">ncol</span> <span style="color:#687687">=</span> <span style="color:#009999">6</span>, <span style="color:#000000">byrow</span> <span style="color:#687687">=</span> <span style="color:#990073">TRUE</span><span style="color:#687687">)</span>
<span style="color:#000000">constr.dir</span> <span style="color:#687687"><-</span> <span style="color:#000000">c</span><span style="color:#687687">(</span><span style="color:#dd1144">"="</span>, <span style="color:#dd1144">">="</span><span style="color:#687687">)</span></code></span>

我们有两个目标函数,因为我们希望找到AT&T和Verizon成本最低的计划组合。并且有两个限制因素。一个是总行数和总(合并)数据量。对于总行数,我希望数据计划具有完全相同的数量,因此我使用“=”。但是对于总的数据量,只要有比所消耗的数据更多的数据,就可以接受。所以我用“> =”表示数据量约束。

创建空矩阵以存储结果

<span style="color:#333333"><code><span style="color:#000000">result.att</span> <span style="color:#687687"><-</span> <span style="color:#000000">matrix</span><span style="color:#687687">(</span><span style="color:#009999">0</span>, <span style="color:#000000">nr</span> <span style="color:#687687">=</span> <span style="color:#009999">20</span>, <span style="color:#000000">nc</span> <span style="color:#687687">=</span> <span style="color:#009999">5</span><span style="color:#687687">)</span>
<span style="color:#000000">row.names</span><span style="color:#687687">(</span><span style="color:#000000">result.att</span><span style="color:#687687">)</span> <span style="color:#687687"><-</span> <span style="color:#000000">usage</span><span style="color:#687687">$</span><span style="color:#000000">Company</span>
<span style="color:#000000">colnames</span><span style="color:#687687">(</span><span style="color:#000000">result.att</span><span style="color:#687687">)</span> <span style="color:#687687"><-</span> <span style="color:#000000">c</span><span style="color:#687687">(</span><span style="color:#dd1144">"3GB"</span>, <span style="color:#dd1144">"4GB"</span>, <span style="color:#dd1144">"5GB"</span>, <span style="color:#dd1144">"6GB"</span>, <span style="color:#dd1144">"Cost"</span><span style="color:#687687">)</span>
<span style="color:#000000">result.vzw</span> <span style="color:#687687"><-</span> <span style="color:#000000">matrix</span><span style="color:#687687">(</span><span style="color:#009999">0</span>, <span style="color:#000000">nr</span> <span style="color:#687687">=</span> <span style="color:#009999">20</span>, <span style="color:#000000">nc</span> <span style="color:#687687">=</span> <span style="color:#009999">7</span><span style="color:#687687">)</span>
<span style="color:#000000">row.names</span><span style="color:#687687">(</span><span style="color:#000000">result.vzw</span><span style="color:#687687">)</span> <span style="color:#687687"><-</span> <span style="color:#000000">usage</span><span style="color:#687687">$</span><span style="color:#000000">Company</span>
<span style="color:#000000">colnames</span><span style="color:#687687">(</span><span style="color:#000000">result.vzw</span><span style="color:#687687">)</span> <span style="color:#687687"><-</span> <span style="color:#000000">c</span><span style="color:#687687">(</span><span style="color:#dd1144">"1GB"</span>, <span style="color:#dd1144">"2GB"</span>, <span style="color:#dd1144">"4GB"</span>, <span style="color:#dd1144">"6GB"</span>, <span style="color:#dd1144">"8GB"</span>, <span style="color:#dd1144">"10GB"</span>, <span style="color:#dd1144">"Cost"</span><span style="color:#687687">)</span></code></span>

创建循环以针对每个提供商为每个投资组合公司运行解算器

<span style="color:#333333"><code><span style="color:#990000"><strong>for</strong></span> <span style="color:#687687">(</span><span style="color:#000000">i</span> <span style="color:#990000"><strong>in</strong></span> <span style="color:#009999">1</span><span style="color:#687687">:</span><span style="color:#009999">20</span><span style="color:#687687">)</span> <span style="color:#687687">{</span>
  <span style="color:#000000">rhs</span> <span style="color:#687687"><-</span> <span style="color:#000000">usage</span><span style="color:#687687">[</span><span style="color:#000000">i</span>,<span style="color:#009999">2</span><span style="color:#687687">:</span><span style="color:#009999">3</span><span style="color:#687687">]</span>
  <span style="color:#000000">prod.sol</span> <span style="color:#687687"><-</span> <span style="color:#000000">lp</span><span style="color:#687687">(</span><span style="color:#dd1144">"min"</span>, <span style="color:#000000">obj.fun.att</span>, <span style="color:#000000">constr.att</span>, <span style="color:#000000">constr.dir</span>, <span style="color:#000000">rhs</span>, <span style="color:#000000">compute.sens</span> <span style="color:#687687">=</span> <span style="color:#990073">TRUE</span>, <span style="color:#000000">all.int</span> <span style="color:#687687">=</span> <span style="color:#990073">TRUE</span><span style="color:#687687">)</span>
  <span style="color:#000000">result.att</span><span style="color:#687687">[</span><span style="color:#000000">i</span>,<span style="color:#009999">5</span><span style="color:#687687">]</span> <span style="color:#687687"><-</span> <span style="color:#000000">prod.sol</span><span style="color:#687687">$</span><span style="color:#000000">objval</span>
  <span style="color:#000000">result.att</span><span style="color:#687687">[</span><span style="color:#000000">i</span>, <span style="color:#009999">1</span><span style="color:#687687">:</span><span style="color:#009999">4</span><span style="color:#687687">]</span> <span style="color:#687687"><-</span> <span style="color:#000000">prod.sol</span><span style="color:#687687">$</span><span style="color:#000000">solution</span>
<span style="color:#687687">}</span>
<span style="color:#990000"><strong>for</strong></span> <span style="color:#687687">(</span><span style="color:#000000">i</span> <span style="color:#990000"><strong>in</strong></span> <span style="color:#009999">1</span><span style="color:#687687">:</span><span style="color:#009999">20</span><span style="color:#687687">)</span> <span style="color:#687687">{</span>
  <span style="color:#000000">rhs</span> <span style="color:#687687"><-</span> <span style="color:#000000">usage</span><span style="color:#687687">[</span><span style="color:#000000">i</span>,<span style="color:#009999">2</span><span style="color:#687687">:</span><span style="color:#009999">3</span><span style="color:#687687">]</span>
  <span style="color:#000000">prod.sol</span> <span style="color:#687687"><-</span> <span style="color:#000000">lp</span><span style="color:#687687">(</span><span style="color:#dd1144">"min"</span>, <span style="color:#000000">obj.fun.vzw</span>, <span style="color:#000000">constr.vzw</span>, <span style="color:#000000">constr.dir</span>, <span style="color:#000000">rhs</span>, <span style="color:#000000">compute.sens</span> <span style="color:#687687">=</span> <span style="color:#990073">TRUE</span>, <span style="color:#000000">all.int</span> <span style="color:#687687">=</span> <span style="color:#990073">TRUE</span><span style="color:#687687">)</span>
  <span style="color:#000000">result.vzw</span><span style="color:#687687">[</span><span style="color:#000000">i</span>,<span style="color:#009999">7</span><span style="color:#687687">]</span> <span style="color:#687687"><-</span> <span style="color:#000000">prod.sol</span><span style="color:#687687">$</span><span style="color:#000000">objval</span>
  <span style="color:#000000">result.vzw</span><span style="color:#687687">[</span><span style="color:#000000">i</span>, <span style="color:#009999">1</span><span style="color:#687687">:</span><span style="color:#009999">6</span><span style="color:#687687">]</span> <span style="color:#687687"><-</span> <span style="color:#000000">prod.sol</span><span style="color:#687687">$</span><span style="color:#000000">solution</span>
<span style="color:#687687">}</span></code></span>

AT&T优化结果

<span style="color:#333333"><code><span style="color:#000000">result.att</span></code></span>
##    3GB 4GB 5GB 6GB   Cost
## A  134   0   0   0   8040
## B  350   0   0   0  21000
## C 1510   0   0   0  90600
## D 2260   0   0   0 135600
## E  438   0 311   1  52815
## F  410   0   0   0  24600
## G 2930   0   0   0 175800
## H  286   0 805   0  85585
## I 3350   0   0   0 201000
## J 7760   0   0   0 465600
## K 4920   0   0   0 295200
## L  594   0 335   1  64215
## M  960   0   0   0  57600
## N 1792   0   0   0 107520
## O 1730   0   0   0 103800
## P 1406   0 247   1 105455
## Q  316   0 472   1  59180
## R  297   0   0   0  17820
## S 1075   0   0   0  64500
## T  796   0   0   0  47760

正如我们在这里看到的,大多数分配是3GB计划,这是有道理的,因为大多数公司使用的不到3GB。但是,如果公司使用超过3GB,由于每GB成本较低,似乎更好地使用更高的数据计划。

Verizon优化结果

<span style="color:#333333"><code><span style="color:#000000">result.vzw</span></code></span>
##    1GB  2GB 4GB 6GB 8GB 10GB   Cost
## A    0   69  65   0   0    0   8690
## B    0  258  66   0   1   25  22690
## C    1 1405  64   1   0   39  92816
## D   82 2178   0   0   0    0 135272
## E    1  528  65   0   1  155  51876
## F  207  203   0   0   0    0  23772
## G  785 2145   0   0   0    0 172660
## H    1  704  64   0    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[R]R语言里的异常处理与错误控制发布时间:2022-07-18
下一篇:
R语言均值,中位数和模式发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap