-1

I'm tackling a simple linear optimisation problem. I have a bunch of crops with have their associated land requirements per tonne and their water footprints per tonne. I want to know how many tonnes of each crop I need in order to minimise the water footprint. For the first run, I'll have created a linear optimisation with only a land constraint. The land must equal 2958700. However, when I run my code my solution says "0" for all crops. Can someone help pls.

land<-c(0.03448276,0.09090909,0.06346154,0.25099602,0.26731171)
water_footprint<-c(2990.3, 5980.1, 31679.5,8802.3,16404.3)

crop_name<-c("banana","guava","mango","maize","rice")

crop_dataframe<-data.frame(crop_name,land,water_footprint)

const.mat<-land
const.dir<-c("=")
const.rhs<-2958700

library(lpSolve)
min_waterfootpint<-lp(direction = "min", water_footprint, const.mat, const.dir, const.rhs)$solution
paste(min_waterfootpint)
Zara Liew
  • 1
  • 3
  • 2
    Where is `land`? – ThomasIsCoding Dec 30 '19 at 14:44
  • What is the solution status? – Erwin Kalvelagen Dec 30 '19 at 15:01
  • Re: Erwin Kalvelagen - The GAMS guy! Funny seeing you here. To the OP you may want to investigate the ompr and roi packages, https://dirkschumacher.github.io/ompr/ http://roi.r-forge.r-project.org/ ompr is a model management platform that calls roi functions. roi allows calls to various solvers including lpSolve You can also formulate a model using roi functions directly, but ompr makes model management easier. Note that roi has a function ROI_write that writes out a formulation in standard form for visual inspection to help debug. – SteveM Dec 30 '19 at 16:02
  • Sorry! I didn't copy in the land variable – Zara Liew Dec 31 '19 at 07:00

1 Answers1

0

I think you should process land to a matrix as an input for your lp, i.e.,

const.mat<-t(matrix(land))

then you will get

> paste(min_waterfootpint)
[1] "0"                "0"                "0"                "11787836.3170858"
[5] "0"  
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Thanks! This worked. However, would you know why [4] (i.e. maize), which has a water footprint of 8802.3, had a number larger than 0 in the solution when [1] (i.e. bananas) actually has a a lower water footprint of 2990.3? – Zara Liew Jan 01 '20 at 15:10
  • @ZaraLiew Maybe you should understand your question from a mathematical perspective....bananas has lower water footprint, but its unit land is just 0.03448276, which means you will have more in terms of quantity when you want to meet the land constraint 2958700 – ThomasIsCoding Jan 01 '20 at 19:10