Skip to content

Instantly share code, notes, and snippets.

@MartinMacharia
Last active December 14, 2021 08:40
Show Gist options
  • Select an option

  • Save MartinMacharia/c690150b1100f557c40d8744f16617f8 to your computer and use it in GitHub Desktop.

Select an option

Save MartinMacharia/c690150b1100f557c40d8744f16617f8 to your computer and use it in GitHub Desktop.
Implementation of constrOptim in maximization of an objective function
#Appendix 2: Optimization algorithm
#Specify the function to be maximized. By default constrOptim performs minimization but the problem can be maximized by setting the fnscale to -1
LP_sol=function(X){
Y_R=5625-2897*(0.966^X[1])-2728 #Rice N response function
Y_M=3159-1191*(0.976^X[2])-1968 #Maize N response function
Y_B=1415-715*(0.950^X[3])-700 #Bean N response function
Y_FM=2100-923*(0.944^X[4])-1177 #Finger millet N response
Y_R1=5665-828*(0.871^X[5])-4837 #Rice P response function
Y_M1=4474-770*(0.898^X[6])-3704 #Maize P response function
Y_B1=1138-263*(0.848^X[7])-875 #Bean P response function
Y_FM1=2101-537*(0.798^X[8])-1564 #Finger millet P response
Y_PP=2538-487*(0.758^X[9])-487 #Pigeon Pea P response
Y_M2=2502-251*(0.94^X[10])-2251 #Maize K response function
Y_PP1=2535-127*(0.666^X[11])-2408 #Pigeon Pea K response
P_R=700;P_M=700;P_B=1000;P_FM=700;P_PP=1500 #Expected commodity prices
P_N=1100; P_P=1200;P_K=1200; #Nutrient cost computed from fertilizer prices per Kg
P=(Y_R*P_R-X[1]*P_N)+(Y_M*P_M-X[2]*P_N)+(Y_B*P_B X[3]*P_N)+(Y_FM*P_FM-X[4]*P_N)+
(Y_R1*P_R-X[5]*P_P)+(Y_M1*P_M-X[6]*P_P)+(Y_B1*P_B-X[7]*P_P)+(Y_FM1*P_FM-X[8]*P_P)+(Y_PP*P_PP-X[9]*P_P)+
(Y_M2*P_M-X[10]*P_K)+(Y_PP1*P_PP-X[11]*P_K) #Net return to fertilizer use function
return(P)
}
# Assume a budget of TSH 500000,the cost of using 1 kg urea, TSP and KCl to be 1000, 1200, 1200 respectively and expected commodity prices per Kg for Rice, beans, finger millet, pigeon pea and maize to be 700, 1000, 700, 1500 and 700 respectively
#Inequality constraints are stated as:
#1100*X1+1100*X2+1100*X3+1100*X4+1200*X5+1200*X6+1200*X7+1200*X8+1200*X9+1200*X10+1200*X11<=500000
#X1>=0;X2>=0;X3>=0;X4>=0;X5>=0;X6>=0;X7>=0;X8>=0;X9>=0;X10>=0;X11>=0;X12>=0
#All X should be within 0 to 150
#Constraints need to be specified in the form Ax-b>=0
A=matrix(c(-1100,-1100,-1100,-1100,-1200,-1200,-1200,-1200,-1200,-1200,-1200,
1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,-1,0,0,
0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,-1,0,
0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,-1),ncol=11,byrow=T)
B=c(-400000,0,-150,0,-150,0,-150,0,-150,0,-150,0,-150,0,-150,0,-150,0,-150,0,-150,0,-150)
#Initial guesses of the X values to be maximized
xinit=c(1,1,1,1,1,1,1,1,1,1,1)
# Nelder-Mead method is used to perform the maximization, other options exist e.g. BFGS
xan=constrOptim(theta=xinit, f=LP_sol, grad=NULL, ui=A, ci=B, control=list(fnscale=-1),method = "Nelder-Mead")
#Finally the solution is accessed as below
xan$par
xan$value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment