learning
lp solving with R & Julia
solve
max z=3x+4y
x+y<=100
2x+y<=150
x+2y<=130
R
library(tidyverse)
library(lpSolve)
library(lpSolveAPI)
tmp=lp(
direction="max",
objective.in=c(3,4),
const.mat=matrix(c(1,1,2,1,1,2),nrow=3,byrow=T),
const.dir=c("<=","<=","<="),
const.rhs=c(100,150,130))
str(tmp)
List of 29
$ direction : int 1
$ x.count : int 2
$ objective : num [1:2] 3 4
$ const.count : int 3
$ constraints : num [1:4, 1:3] 1 1 1 100 2 1 1 150 1 2 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:4] "" "" "const.dir.num" "const.rhs"
.. ..$ : NULL
$ int.count : int 0
$ int.vec : int 0
$ bin.count : int 0
$ binary.vec : int 0
$ num.bin.solns : int 1
$ objval : num 317
$ solution : num [1:2] 56.7 36.7
$ presolve : int 0
$ compute.sens : int 0
$ sens.coef.from : num 0
$ sens.coef.to : num 0
$ duals : num 0
$ duals.from : num 0
$ duals.to : num 0
$ scale : int 196
$ use.dense : int 0
$ dense.col : int 0
$ dense.val : num 0
$ dense.const.nrow: int 0
$ dense.ctr : num 0
$ use.rw : int 0
$ tmp : chr "Nobody will ever look at this"
$ status : int 0
$ timeout : int 0
- attr(*, "class")= chr "lp"
Julia
using CSV, DataFrames,PrettyTables,JuMP,GLPK
function run()
model=Model(GLPK.Optimizer)
# set_optimizer()
@variable(model,x>=0)
@variable(model,y>=0)
@objective(model,Max,3x+4y)
@constraint(model,x+y<=100)
@constraint(model,2x+y<=150)
@constraint(model,x+2y<=130)
optimize!(model)
print(model)
println(value(x))
println(value(y))
println(value(objective_value(model)))
end
run (generic function with 1 method)
Max 3 x + 4 y
Subject to
x + y <= 100
2 x + y <= 150
x + 2 y <= 130
x >= 0
y >= 0
56.666666666666664
36.66666666666667
316.6666666666667