码迷,mamicode.com
首页 > 编程语言 > 详细

R语言解决最优化运营研究问题-线性优化(LP)问题

时间:2019-03-08 16:46:14      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:比较   false   handler   obj   sele   5.0   block   reset   ESS   

使用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   1  321  78966
## I 3337   13   0   0   0    0 187652
## J    1 7174  64   0   1  520 487066
## K 4215  705   0   0   0    0 278340
## L    1  680  64   1   0  184  63816
## M  645  315   0   0   0    0  55020
## N    0 1573   1   0   0  218 116250
## O    1 1571  66   0   1   91 108126
## P    1 1336  64   0   0  253 109996
## Q    0  523  65   0   1  200  56020
## R  148  149   0   0   0    0  17228
## S    1  890  66   0   0  118  69876
## T    0  796   0   0   0    0  47760

至于Verizon,我们可以看到大多数公司都有2GB和10GB的混合计划,以利用2GB计划中更便宜的总速率,但从10GB计划中降低每GB速率。

比较AT&T和Verizon Wireless的总体成本

<span style="color:#333333"><code><span style="color:#000000">comp</span> <span style="color:#687687"><-</span> <span style="color:#000000">as.data.frame</span><span style="color:#687687">(</span><span style="color:#000000">cbind</span><span style="color:#687687">(</span><span style="color:#000000">result.att</span><span style="color:#687687">[</span>,<span style="color:#009999">5</span><span style="color:#687687">]</span>, <span style="color:#000000">result.vzw</span><span style="color:#687687">[</span>,<span style="color:#009999">7</span><span style="color:#687687">]</span><span style="color:#687687">)</span><span style="color:#687687">)</span>
<span style="color:#000000">comp</span><span style="color:#687687">$</span><span style="color:#000000">lowest</span> <span style="color:#687687"><-</span> <span style="color:#000000">ifelse</span><span style="color:#687687">(</span><span style="color:#000000">comp</span><span style="color:#687687">[</span>,<span style="color:#009999">1</span><span style="color:#687687">]</span> <span style="color:#687687">></span> <span style="color:#000000">comp</span><span style="color:#687687">[</span>,<span style="color:#009999">2</span><span style="color:#687687">]</span>, <span style="color:#dd1144">"vzw"</span>, <span style="color:#dd1144">"att"</span><span style="color:#687687">)</span>
<span style="color:#000000">colnames</span><span style="color:#687687">(</span><span style="color:#000000">comp</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">"ATT"</span>, <span style="color:#dd1144">"VZW"</span>, <span style="color:#dd1144">"Lowest"</span><span style="color:#687687">)</span>
<span style="color:#000000">comp</span></code></span>
技术图片
##      ATT    VZW Lowest
## A   8040   8690    att
## B  21000  22690    att
## C  90600  92816    att
## D 135600 135272    vzw
## E  52815  51876    vzw
## F  24600  23772    vzw
## G 175800 172660    vzw
## H  85585  78966    vzw
## I 201000 187652    vzw
## J 465600 487066    att
## K 295200 278340    vzw
## L  64215  63816    vzw
## M  57600  55020    vzw
## N 107520 116250    att
## O 103800 108126    att
## P 105455 109996    att
## Q  59180  56020    vzw
## R  17820  17228    vzw
## S  64500  69876    att
## T  47760  47760    att

第二种情景

现在我们知道根据我们当前的行数和用途选择什么提供商和计划。然而,公司可能希望购买的数据超过他们现在消费的数据,因为数据的使用一直在增长,并且预计会继续这样做,其次,他们希望避免潜在的超额费用。

所以现在,我将建立一个新变量,作为公司过去使用的数据的百分比。

<span style="color:#333333"><code><span style="color:#000000">buffer</span> <span style="color:#687687"><-</span> <span style="color:#000000">c</span><span style="color:#687687">(</span><span style="color:#009999">1.2</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">c</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:#000000">usage</span><span style="color:#687687">[</span><span style="color:#000000">i</span>,<span style="color:#009999">3</span><span style="color:#687687">]</span> <span style="color:#687687">*</span> <span style="color:#000000">buffer</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">c</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:#000000">usage</span><span style="color:#687687">[</span><span style="color:#000000">i</span>,<span style="color:#009999">3</span><span style="color:#687687">]</span> <span style="color:#687687">*</span> <span style="color:#000000">buffer</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>
技术图片
<span style="color:#333333"><code><span style="color:#000000">result.att</span></code></span>
技术图片
##    3GB 4GB 5GB 6GB   Cost
## A   97   0  36   1   8980
## B  253   0  96   1  23440
## C 1510   0   0   0  90600
## D 2260   0   0   0 135600
## E  150   0 600   0  60000
## F  410   0   0   0  24600
## G 2930   0   0   0 175800
## H    0   0 687 404  98795
## I 3350   0   0   0 201000
## J 7513   0 246   1 471790
## K 4920   0   0   0 295200
## L  248   0 681   1  72865
## M  960   0   0   0  57600
## N 1282   0 510   0 120270
## O 1730   0   0   0 103800
## P  860   0 794   0 119090
## Q    0   0 757  32  67545
## R  297   0   0   0  17820
## S  753   0 321   1  72565
## T  796   0   0   0  47760
<span style="color:#333333"><code><span style="color:#000000">result.vzw</span></code></span>
技术图片
##    1GB  2GB 4GB 6GB 8GB 10GB   Cost
## A    1   57  66   0   1    9   9086
## B    1  231  66   0   1   51  23726
## C    1 1318  65   0   1  125  96276
## D    1 2109  65   1   0   84 139626
## E    0  504   3   0   0  243  54750
## F   85  325   0   0   0    0  24260
## G    0 2899   3   0   0   28 176950
## H    1  581  65   1   0  443  83846
## I 2665  685   0   0   0    0 190340
## J    1 6678  65   0   1 1015 506876
## K 3090 1830   0   0   0    0 282840
## L    1  593  65   0   1  270  67276
## M  390  570   0   0   0    0  56040
## N    0 1439   2   0   0  351 121580
## O    0 1513   1   0   0  216 112450
## P    0 1199  66   0   1  388 115450
## Q    1  440  64   0   0  284  59336
## R   59  238   0   0   0    0  17584
## S    0  860   0   0   0  215  73100
## T    1  707  64   0   0   24  49356
<span style="color:#333333"><code><span style="color:#000000">comp</span> <span style="color:#687687"><-</span> <span style="color:#000000">as.data.frame</span><span style="color:#687687">(</span><span style="color:#000000">cbind</span><span style="color:#687687">(</span><span style="color:#000000">result.att</span><span style="color:#687687">[</span>,<span style="color:#009999">5</span><span style="color:#687687">]</span>, <span style="color:#000000">result.vzw</span><span style="color:#687687">[</span>,<span style="color:#009999">7</span><span style="color:#687687">]</span><span style="color:#687687">)</span><span style="color:#687687">)</span>
<span style="color:#000000">comp</span><span style="color:#687687">$</span><span style="color:#000000">lowest</span> <span style="color:#687687"><-</span> <span style="color:#000000">ifelse</span><span style="color:#687687">(</span><span style="color:#000000">comp</span><span style="color:#687687">[</span>,<span style="color:#009999">1</span><span style="color:#687687">]</span> <span style="color:#687687">></span> <span style="color:#000000">comp</span><span style="color:#687687">[</span>,<span style="color:#009999">2</span><span style="color:#687687">]</span>, <span style="color:#dd1144">"vzw"</span>, <span style="color:#dd1144">"att"</span><span style="color:#687687">)</span>
<span style="color:#000000">colnames</span><span style="color:#687687">(</span><span style="color:#000000">comp</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">"ATT"</span>, <span style="color:#dd1144">"VZW"</span>, <span style="color:#dd1144">"Lowest"</span><span style="color:#687687">)</span>
<span style="color:#000000">comp</span></code></span>
技术图片
##      ATT    VZW Lowest
## A   8980   9086    att
## B  23440  23726    att
## C  90600  96276    att
## D 135600 139626    att
## E  60000  54750    vzw
## F  24600  24260    vzw
## G 175800 176950    att
## H  98795  83846    vzw
## I 201000 190340    vzw
## J 471790 506876    att
## K 295200 282840    vzw
## L  72865  67276    vzw
## M  57600  56040    vzw
## N 120270 121580    att
## O 103800 112450    att

 

还有问题吗?联系我们!

 

R语言解决最优化运营研究问题-线性优化(LP)问题

标签:比较   false   handler   obj   sele   5.0   block   reset   ESS   

原文地址:https://www.cnblogs.com/tecdat/p/10496406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!