Skip to content

Instantly share code, notes, and snippets.

@MartinMacharia
Last active October 31, 2017 05:00
Show Gist options
  • Save MartinMacharia/d51da6edbf6b383ff6dff738e3dd5e4c to your computer and use it in GitHub Desktop.
Save MartinMacharia/d51da6edbf6b383ff6dff738e3dd5e4c to your computer and use it in GitHub Desktop.
constrOptim, One dimension problem
#One dimension problem
fun=function(X){
Y=5151-1735*(0.945^X[1])-3416
Py=100
R=Y*Py
Px=200
C=X[1]*Px
P=Y*Py-X[1]*Px
return(P)
#plot(X,P,type="l")
}
#Budget is 5000, cost of commodity (Py) is 100 and fertilizer cost
#(Px) is 200, Maximize P st X*Px<=5000, X>=0 {x>=0, -X>=-25}
A=matrix(c(-200,1,-200,1) ncol=1,byrow=T)
A
B=c(0,-25)
B
xinit=c(0.1)
xan=constrOptim(theta=xinit, f=fun, grad=NULL, ui=A, ci=B,control=list(fnscale=-1),method = "Nelder-Mead")#
xan$par
xan$value
#######################################################################
grid <- seq(-10,10,by=.1)
fun(grid)
plot(grid,fun(grid))
####################################################################
X=c(1:120)
Y=5151-1735*(0.945^X)
Py=100
R=Y*Py
Px=200
C=X*Px
Prof=Y*Py-X*Px
Prof
plot(X,Prof,type="l")
abline(v=10.38672)
abline(v=12.24375)
abline(v=24.35000)
abline(v=68.82307)
abline(h=497800)
###########################################################################
A=matrix(c(1,0),ncol=1,byrow=TRUE)
A
B=c(0,-25)
B
theta=c(0.1)
theta
E=A%*%theta-B
E
################################################################################
Warning messages:
1: In optim(theta.old, fun, gradient, control = control, method = method, :
one-dimensional optimization by Nelder-Mead is unreliable:
use "Brent" or optimize() directly
2: In optim(theta.old, fun, gradient, control = control, method = method, :
one-dimensional optimization by Nelder-Mead is unreliable:
use "Brent" or optimize() directly
################################################################################
#Single crop, single fertilizer
fun=function(X){
Y=5151-1735*(0.945^X[1])
Py=100
R=Y*Py
Px=200
C=X[1]*Px
P=Y*Py-X[1]*Px
return(P)
}
optim(par=c(0.1), fn=fun, gr = NULL,
method = c("Brent"),
lower = 0, upper = 1000,
control = list(fnscale=-1), hessian = T)
#What does par do??????
citation()
citation("constrOptim")
citation("stats")
sessionInfo()
Y=5151-1735*(0.945^2)-2000
Y
########2 dimension problem
fun=function(X){
Y=5151-1735*(0.945^X[1])-3416; Y1=3259-1191*(0.976^X[2])-2068
Py=100; Py1=150
R=Y*Py; R1=Y1*Py1
Px=200; Px1=200
P=(Y*Py-X[1]*Px)+(Y1*Py1-X[2]*Px1)
return(P)
}
# Assume a budget of 5000, cost of commodity Py is 100 and commodity Py1 is 150 and fertilizer cost of N and P
#Px is 200 which is similar to fertilizer cost Px1, Maximize P st Px*x1 + Px1*x2 <=5000, x1>=0,x2>=0
A=matrix(c(-200,-200,1,1),ncol=2,byrow=T)
B=c(-5000,0)
B
xinit=c(0.1, 0.1)
xan=constrOptim(theta=xinit, f=fun, grad=NULL, ui=A, ci=B,control=list(fnscale=-1),method = "Nelder-Mead")#
xan$par
xan$value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment