File: MpsTests.Rout.save

package info (click to toggle)
r-cran-linprog 0.9-4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 204 kB
  • sloc: sh: 13; makefile: 2
file content (254 lines) | stat: -rw-r--r-- 6,997 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

R version 2.10.1 (2009-12-14)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library( linprog )
Loading required package: lpSolve
> 
> ## example of Steinhauser, Langbehn and Peters (1992)
> ## Production activities
> cvec <- c(1800, 600, 600)  # gross margins
> names(cvec) <- c("Cows","Bulls","Pigs")
> 
> ## Constraints (quasi-fix factors)
> bvec <- c(40, 90, 2500)  # endowment
> names(bvec) <- c("Land","Stable","Labor")
> 
> ## Needs of Production activities
> Amat <- rbind( c(  0.7,   0.35,   0 ),
+                c(  1.5,   1,      3 ),
+                c( 50,    12.5,   20 ) )
> 
> ## solve the model
> result1a <- solveLP( cvec, bvec, Amat, TRUE )
> 
> ## Write to a (virtual) MPS file
> mpsFile <- file()
> writeMps( mpsFile, cvec, bvec, Amat, "Steinhauser" )
> 
> ## write the lines of this file to the output file
> mpsLines <- readLines( mpsFile )
> close( mpsFile )
> print( mpsLines )
 [1] "NAME          Steinhauser"           
 [2] "ROWS"                                
 [3] " N  obj"                             
 [4] " L  Land"                            
 [5] " L  Stable"                          
 [6] " L  Labor"                           
 [7] "COLUMNS"                             
 [8] "    Cows      obj               1800"
 [9] "    Cows      Land               0.7"
[10] "    Cows      Stable             1.5"
[11] "    Cows      Labor               50"
[12] "    Bulls     obj                600"
[13] "    Bulls     Land              0.35"
[14] "    Bulls     Stable               1"
[15] "    Bulls     Labor             12.5"
[16] "    Pigs      obj                600"
[17] "    Pigs      Stable               3"
[18] "    Pigs      Labor               20"
[19] "RHS"                                 
[20] "    RHS       Land                40"
[21] "    RHS       Stable              90"
[22] "    RHS       Labor             2500"
[23] "ENDATA"                              
> 
> ## Write to a (virtual) MPS file again (for readMps)
> mpsFile <- file()
> writeMps( mpsFile, cvec, bvec, Amat, "Steinhauser" )
> 
> ## delete all LP objects
> rm( cvec, bvec, Amat )
> 
> ## Read LP data from MPS file and solve it.
> lpModel <- readMps( mpsFile, TRUE, TRUE )
> close( mpsFile )
> 
> ## Print the model and its result
> lpModel
$name
[1] "Steinhauser"

$cvec
 Cows Bulls  Pigs 
 1800   600   600 

$bvec
  Land Stable  Labor 
    40     90   2500 

$Amat
       Cows Bulls Pigs
Land    0.7  0.35    0
Stable  1.5  1.00    3
Labor  50.0 12.50   20

$res


Results of Linear Programming / Linear Optimization

Objective function (Maximum): 93600 

Iterations in phase 1: 0
Iterations in phase 2: 2
Solution
      opt
Cows   44
Bulls  24
Pigs    0

Basic Variables
        opt
Cows   44.0
Bulls  24.0
S Land  0.8

Constraints
       actual dir bvec free  dual dual.reg
Land     39.2  <=   40  0.8   0.0      0.8
Stable   90.0  <=   90  0.0 240.0     15.0
Labor  2500.0  <= 2500  0.0  28.8   1375.0

All Variables (including slack variables)
          opt cvec min.c    max.c   marg marg.reg
Cows     44.0 1800   900 2400.000     NA       NA
Bulls    24.0  600   450 1200.000     NA       NA
Pigs      0.0  600  -Inf 1296.000 -696.0     6.25
S Land    0.8    0    NA  731.092    0.0       NA
S Stable  0.0    0  -Inf  240.000 -240.0    15.00
S Labor   0.0    0  -Inf   28.800  -28.8  1375.00


> all.equal( result1a, lpModel$res )
[1] TRUE
> 
> 
> ## example 1.1.3 of Witte, Deppe and Born (1975)
> ## Two types of Feed
> cvec <- c(2.5, 2 )  # prices of feed
> names(cvec) <- c("Feed1","Feed2")
> 
> ## Constraints (minimum (<0) and maximum (>0) contents)
> bvec <- c(-10, -1.5, 12)
> names(bvec) <- c("Protein","Fat","Fibre")
> 
> ## Matrix A
> Amat <- rbind( c( -1.6,  -2.4 ),
+                c( -0.5,  -0.2 ),
+                c(  2.0,   2.0 ) )
> 
> ## solve the model
> result2a <- solveLP( cvec, bvec, Amat )
> 
> ## Write to a (virtual) MPS file
> mpsFile <- file()
> writeMps( mpsFile, cvec, bvec, Amat, "Steinhauser" )
> 
> ## write the lines of this file to the output file
> mpsLines <- readLines( mpsFile )
> close( mpsFile )
> print( mpsLines )
 [1] "NAME          Steinhauser"           
 [2] "ROWS"                                
 [3] " N  obj"                             
 [4] " L  Protein"                         
 [5] " L  Fat"                             
 [6] " L  Fibre"                           
 [7] "COLUMNS"                             
 [8] "    Feed1     obj                2.5"
 [9] "    Feed1     Protein           -1.6"
[10] "    Feed1     Fat               -0.5"
[11] "    Feed1     Fibre                2"
[12] "    Feed2     obj                  2"
[13] "    Feed2     Protein           -2.4"
[14] "    Feed2     Fat               -0.2"
[15] "    Feed2     Fibre                2"
[16] "RHS"                                 
[17] "    RHS       Protein            -10"
[18] "    RHS       Fat               -1.5"
[19] "    RHS       Fibre               12"
[20] "ENDATA"                              
> 
> ## Write to a (virtual) MPS file again (for readMps)
> mpsFile <- file()
> writeMps( mpsFile, cvec, bvec, Amat, "Steinhauser" )
> 
> ## delete all LP objects
> rm( cvec, bvec, Amat )
> 
> ## Read LP data from MPS file and solve it.
> lpModel <- readMps( mpsFile, TRUE )
> close( mpsFile )
> 
> ## Print the model and its result
> lpModel
$name
[1] "Steinhauser"

$cvec
Feed1 Feed2 
  2.5   2.0 

$bvec
Protein     Fat   Fibre 
  -10.0    -1.5    12.0 

$Amat
        Feed1 Feed2
Protein  -1.6  -2.4
Fat      -0.5  -0.2
Fibre     2.0   2.0

$res


Results of Linear Programming / Linear Optimization

Objective function (Minimum): 10.4545 

Iterations in phase 1: 2
Iterations in phase 2: 0
Solution
          opt
Feed1 1.81818
Feed2 2.95455

Basic Variables
            opt
Feed1   1.81818
Feed2   2.95455
S Fibre 2.45455

Constraints
           actual dir  bvec    free     dual dual.reg
Protein -10.00000  <= -10.0 0.00000 0.568182  3.60000
Fat      -1.50000  <=  -1.5 0.00000 3.181818  1.35000
Fibre     9.54545  <=  12.0 2.45455 0.000000  2.45455

All Variables (including slack variables)
              opt cvec     min.c    max.c     marg marg.reg
Feed1     1.81818  2.5 -3.666667 5.000000       NA       NA
Feed2     2.95455  2.0 -3.000000 3.750000       NA       NA
S Protein 0.00000  0.0 -0.568182      Inf 0.568182     3.60
S Fat     0.00000  0.0 -3.181818      Inf 3.181818     1.35
S Fibre   2.45455  0.0        NA 0.833333 0.000000       NA


> all.equal( result2a, lpModel$res )
[1] TRUE
>