Reputation: 11
The data is:
name <- c("Gen1","Gen2","Gen3")
QuantityE <- c(200,100,50)
PriceE <- c(10,12,50)
QuantityAS <- c(100,50,10)
PriceAS <- c(1,5,7)
mydata <- data.frame(name, QuantityE, PriceE , QuantityAS,PriceAS )
I have the following objective function:
Minimize total cost when multiplying combinations of
((PriceE*QuantityE) + (PriceAS* QuantityAS))
Subject to constraints:
Total QuantityE = 300
Total QuantityAS = 0.06* QuantityE
What is the best approach to use, or what I can read up to solve the problem?
Upvotes: 0
Views: 151
Reputation: 11
For completeness, after some reading, found the right way to code the LP. There is neater ways of doing it, but this works for me.
name <- c("Gen1","Gen2","Gen3")
QuantityE <- c(200,100,50)
PriceE <- c(10,12,50)
QuantityAS <- c(100,50,10)
PriceAS <- c(1,5,7)
mydata <- data.frame(name, QuantityE, PriceE , QuantityAS,PriceAS )
#System Data
EnergyDemand <- 300 #Total QuantityE
CRRequired <- 0.06*EnergyDemand #Total Quantity AS
library(lpSolve)
#Set up Objective function, prices will be the co-ef's
obj.fun <- as.vector(stack(mydata[,c(3,5)])[1])
##Set up the constraints matrix
#This will set up individual quantityE and quantityAS coef's
D <- diag(1, NROW(obj.fun),NROW(obj.fun))
#This sets up coefficients with the ability to combine QuantityAS and QuantityE
E <- diag(1, NROW(name),NROW(name))
FA <- cbind(E,E)
#This sets up the cofficients for all quantityE
G <- matrix(c(rep(1,NROW(name)),rep(0,NROW(name))),1)
#This sets up the cofficients for all quantityAS
H <- matrix(c(rep(0,NROW(name)),rep(1,NROW(name))),1)
#This combines the above constraints into one matrix
constr <- rbind(D,FA,G,H)
#Set up directional constraints. All except the last 2 are <=
#This allows flexibility in choosing volumes
# The last two have to be equal to for Energy and AS demand
constr.dir <- c(rep("<=",NROW(constr)-2), rep("=",2))
#This sets up the rhs numbers for the matrix above
rhs <- c(QuantityE, QuantityAS, pmax(QuantityE, QuantityAS), EnergyDemand,CRRequired)
#This is the algorithm parameters
prod.sol <- lp("min", obj.fun, constr, constr.dir, rhs, compute.sens = TRUE)
a <- matrix(prod.sol$solution, nrow= length(name)) #decision variables values
rownames(a) <- name
colnames(a) <- c("Energy MW", "AS MW")
#This is the Summary of results
print(mydata) #This gives the initial dataset
a # This gives the combination of quantity used from Gen's
prod.sol #This gives the optimal minimized cost
Upvotes: 1