mathematical optimization - LPSolve with R - Multiple datasets as input -


i working on lpsolve r , input data in form of multiple csv files, each file having table. 2 of tables mentioned below:

production data

route data

description constraints -

  • there routes originating each production house
  • total outflow production house = sum(route volumes) of routes originating it
  • production house total outflow <= production capacity
  • route volume decision variable depends on other variables not mentioned in post

the mathematical representation of constraints follows:

`production total outflow = ∑(route volume) (production house id table_1)==(originating prod house id table_2)` 

production total outflow <= production capacity

in reality, have thousands of rows. tried write following code above 2 constraints. there 2 constraints:

#reading data files routedata = read.csv("route.csv", header = true) productiondata = read.csv("production.csv", header = true)  #fetching variable columns routeid = routedata$routeid productionid = productiondata$productionid productioncapacity = productiondata$supply.capacity  numberofcolumns = length(routeid) + length(productionid) #4+2 decision variables model <- make.lp(nrow=0, ncol=numberofcolumns, verbose="important")  for(i in 1:length(productionid)){   add.constraint(model, 1, "<=", productioncapacity[i]) #something wrong here } #i haven't attempted write other constraint 

i unable proceed further write constraints. please guys. haven't shared objective has lot of other constraints too.

here 1 example tries evenly distribute route volume on production houses

library(lpsolveapi)  prodcap <- setnames(c(50,100), c(1,2)) route <- data.frame(rid=1:4, pid_from=rep(1:2, each=2)) route_volume <- 125 # example  nvars <- nrow(route)+1 # example: evenly distribute production house output relative capacity lprec <- make.lp(0, nvars)  set.objfn(lprec, obj=1, indices=nvars)  # capacity constraints (i in seq(1, length(prodcap))) {     route_ids <- which(route[,"pid_from"]==i)     add.constraint(lprec, xt=rep(1, length(route_ids)), type="<=", rhs=prodcap[i], indices=route_ids) }  # total outflow constraint add.constraint(lprec, xt=rep(1, nrow(route)), type="=", rhs=route_volume, indices=seq(1, nvars-1))  # example: define last decision variable maximum flow on each production house (i in seq(1, length(prodcap))) {     route_ids <- which(route[,"pid_from"]==i)     add.constraint(lprec, xt=c(rep(1/prodcap[i], length(route_ids)), -1), type="<=", rhs=0, indices=c(route_ids, nvars)) }  # solve status <- solve(lprec) if(status!=0) stop("no solution found, error code=", status) get.variables(lprec)[seq(1, nrow(route))] #[1] 41.66667  0.00000 83.33333  0.00000 

note if have thousands of routes / production houses might more efficient pre-allocate constraints in make.lp , use set.row instead of add.constraint. here example this, , route_volume additional decision variable, requested in comment:

library(lpsolveapi)  prodcap <- setnames(c(50,100), c(1,2)) route <- data.frame(rid=1:4, pid_from=rep(1:2, each=2)) route_volume <- 125 # example  # first nrow(route) vars outflows,  # 1 variable maximum flow (relative capacity) on production house # 1 last variable route volume nvars <- nrow(route)+2  ncons <- 2*length(prodcap)+3  # pre-allocate constraints lprec <- make.lp(ncons, nvars)  # set objective: minimize maximum flow relative capacity (example) set.objfn(lprec, obj=1, indices=nvars-1)  # capacity constraints rownum <- 1 (i in seq(1, length(prodcap))) {     route_ids <- which(route[,"pid_from"]==i)     set.row(lprec, row=rownum, xt=rep(1, length(route_ids)), indices=route_ids)     set.rhs(lprec, prodcap[i], constraints=rownum)     rownum <- rownum + 1 }  # total outflow constraint ("=" resolves 2 constraints) set.row(lprec, row=rownum, xt=c(rep(1, nrow(route)), -1), indices=c(seq(1, nvars-2), nvars)) set.rhs(lprec, 0, constraints=rownum) rownum <- rownum + 1 set.row(lprec, row=rownum, xt=c(rep(-1, nrow(route)), 1), indices=c(seq(1, nvars-2), nvars)) set.rhs(lprec, 0, constraints=rownum) rownum <- rownum + 1  # additional constraint route volume set.row(lprec, row=rownum, xt=-1, indices=nvars) set.rhs(lprec, -125, constraints=rownum) #example: route_volume >= 125 rownum <- rownum + 1  # example: define second last decision variable maximum flow (relative capacity) on production houses # rhs 0, preset (i in seq(1, length(prodcap))) {     route_ids <- which(route[,"pid_from"]==i)     set.row(lprec, row=rownum, xt=c(rep(1/prodcap[i], length(route_ids)), -1), indices=c(route_ids, nvars-1))     set.rhs(lprec, 0, constraints=rownum)     rownum <- rownum + 1 }  # solve status <- solve(lprec) if(status!=0) stop("no solution found, error code=", status) get.variables(lprec)[seq(1, nrow(route))] 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -