File: MpsTests.R

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 (84 lines) | stat: -rw-r--r-- 2,112 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
library( linprog )

## 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 )

## 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
all.equal( result1a, lpModel$res )


## 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 )

## 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
all.equal( result2a, lpModel$res )