Optimasi
~ UAS Optimasi ~
| Kontak | : \(\downarrow\) |
| dsciencelabs@outlook.com | |
| https://www.instagram.com/dsciencelabs/ | |
| RPubs | https://rpubs.com/dsciencelabs/ |
Linear Programming Optimization
A car company produces 2 models, model A and model B. Long-term
projections indicate an expected demand of at least 100 model A cars and
80 model B cars each day. Because of limitations on production capacity,
no more than 200 model A cars and 170 model B cars can be made daily. To
satisfy a shipping contract, a total of at least 200 cars much be
shipped each day. If each model A car sold results in a $2000 loss, but
each model B car produces a $5000 profit, how many of each type should
be made daily to maximize net profits?
Jawab:
\[
z = 5000x - 2000y
\] Constraint
\[ x + y = 100 \] \[ x >= 80 \] \[ y >= 100 \]
mencari value x dan y untuk memaksimalkan t
library(lpSolve)obj.fun=c(5000,-2000)
constr=matrix(c(1,1,1,0,0,1),ncol = 2, byrow = TRUE) #
constr.dir=c("=",">=",">=")
constr.rhs=c(200,80,100)
mod=lp("max",obj.fun,constr,constr.dir,constr.rhs,compute.sens = TRUE)
mod$solution ## [1] 100 100
dari output diatas memiliki solusi x = 100 dan y = 100. Untuk mendapatkan keuntungan maksimum, perusahaan mobil harus menjual 100 mobil Model A (Y) dan 100 mobil Model B (x).
x=100
y=100
z = (5000*x)-(2000*y)
options("scipen"=100, "digits"=4)
cat("Net profit =", z) ## Net profit = 300000
maksimum profit adalah 3000000.
Nonlinear Programming optimization
Minimizing with inequality constraint without gradients
library(nloptr)# objective function
eval_f0 <- function( x, a, b ){
return( sqrt(x[2]) )
}# constraint function
eval_g0 <- function( x, a, b ) {
return( (a*x[1] + b)^3 - x[2] )
}# define parameters
a <- c(2,-1)
b <- c(0, 1)res1 <- nloptr( x0=c(1.234,5.678),
eval_f=eval_f0,
lb = c(-Inf,0),
ub = c(Inf,Inf),
eval_g_ineq = eval_g0,
opts = list("algorithm"="NLOPT_LN_COBYLA",
"xtol_rel"=1.0e-8),
a = a,
b = b )
print( res1 )##
## Call:
## nloptr(x0 = c(1.234, 5.678), eval_f = eval_f0, lb = c(-Inf, 0),
## ub = c(Inf, Inf), eval_g_ineq = eval_g0, opts = list(algorithm = "NLOPT_LN_COBYLA",
## xtol_rel = 0.00000001), a = a, b = b)
##
##
## Minimization using NLopt version 2.7.1
##
## NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because
## xtol_rel or xtol_abs (above) was reached. )
##
## Number of Iterations....: 50
## Termination conditions: xtol_rel: 0.00000001
## Number of inequality constraints: 2
## Number of equality constraints: 0
## Optimal value of objective function: 0.544331053951819
## Optimal value of controls: 0.3333 0.2963