1library( linprog )
2
3## example of Steinhauser, Langbehn and Peters (1992)
4## Production activities
5cvec <- c(1800, 600, 600)  # gross margins
6names(cvec) <- c("Cows","Bulls","Pigs")
7
8## Constraints (quasi-fix factors)
9bvec <- c(40, 90, 2500)  # endowment
10names(bvec) <- c("Land","Stable","Labor")
11
12## Needs of Production activities
13Amat <- rbind( c(  0.7,   0.35,   0 ),
14               c(  1.5,   1,      3 ),
15               c( 50,    12.5,   20 ) )
16
17## solve the model
18result1a <- solveLP( cvec, bvec, Amat, TRUE )
19
20## Write to a (virtual) MPS file
21mpsFile <- file()
22writeMps( mpsFile, cvec, bvec, Amat, "Steinhauser" )
23
24## write the lines of this file to the output file
25mpsLines <- readLines( mpsFile )
26close( mpsFile )
27print( mpsLines )
28
29## Write to a (virtual) MPS file again (for readMps)
30mpsFile <- file()
31writeMps( mpsFile, cvec, bvec, Amat, "Steinhauser" )
32
33## delete all LP objects
34rm( cvec, bvec, Amat )
35
36## Read LP data from MPS file and solve it.
37lpModel <- readMps( mpsFile, TRUE, TRUE )
38close( mpsFile )
39
40## Print the model and its result
41lpModel
42all.equal( result1a, lpModel$res )
43
44
45## example 1.1.3 of Witte, Deppe and Born (1975)
46## Two types of Feed
47cvec <- c(2.5, 2 )  # prices of feed
48names(cvec) <- c("Feed1","Feed2")
49
50## Constraints (minimum (<0) and maximum (>0) contents)
51bvec <- c(-10, -1.5, 12)
52names(bvec) <- c("Protein","Fat","Fibre")
53
54## Matrix A
55Amat <- rbind( c( -1.6,  -2.4 ),
56               c( -0.5,  -0.2 ),
57               c(  2.0,   2.0 ) )
58
59## solve the model
60result2a <- solveLP( cvec, bvec, Amat )
61
62## Write to a (virtual) MPS file
63mpsFile <- file()
64writeMps( mpsFile, cvec, bvec, Amat, "Steinhauser" )
65
66## write the lines of this file to the output file
67mpsLines <- readLines( mpsFile )
68close( mpsFile )
69print( mpsLines )
70
71## Write to a (virtual) MPS file again (for readMps)
72mpsFile <- file()
73writeMps( mpsFile, cvec, bvec, Amat, "Steinhauser" )
74
75## delete all LP objects
76rm( cvec, bvec, Amat )
77
78## Read LP data from MPS file and solve it.
79lpModel <- readMps( mpsFile, TRUE )
80close( mpsFile )
81
82## Print the model and its result
83lpModel
84all.equal( result2a, lpModel$res )
85