Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Optimization and Mathematical Programming in R and ROI - R Optimization Infrastructure.
1. Prepared by VOLKAN OBAN
Optimization and Mathematical
Programming in R.
&
ROI - R Optimization Infrastructure
Reference: https://cran.r-project.org/web/views/Optimization.html
7. > set.objfn(model, c(-0.05, -0.04, -0.06))
> # Add the constraints
> add.constraint(model, c(1, 1, 1), "=", 1)
> add.constraint(model, c(1, 1, -1), ">", 0)
> add.constraint(model, c(1, -2, 0), "<", 0)
> # Set the upper and lower bounds
> set.bounds(model, lower=c(0.1, 0.1, 0.1), upper=c(1, 1, 1))
> # Compute the optimized model
> solve(model)
[1] 0
> get.variables(model)
[1] 0.3333333 0.1666667 0.5000000
> get.objective(model)
[1] -0.05333333
> # Get the value of the constraint
> get.constraints(model)
[1] 1 0 0
Reference: http://horicky.blogspot.com.tr/search?q=unconstrained
Example: Regression
RSS = ( Y - X b )' ( Y - X b )
= Y Y' - 2 Y' X b + b X' X b
# Sample data
n <- 100
x1 <- rnorm(n)
x2 <- rnorm(n)
y <- 1 + x1 + x2 + rnorm(n)
X <- cbind( rep(1,n), x1, x2 )
# Regression
r <- lm(y ~ x1 + x2)
# Optimization
library(quadprog)
s <- solve.QP( t(X) %*% X, t(y) %*% X, matrix(nr=3,nc=0), numeric(),
0 )
coef(r)
s$solution # Identical
> coef(r)
(Intercept) x1 x2
0.9973739 0.9234925 0.9275238
> s$solution # Identical
[1] 0.9973739 0.9234925 0.9275238
Ref: http://zoonek.free.fr/blosxom/R/2012-06-01_Optimization.html
8. Example: Gradient Descent algorithm in R
The example provides a code listing Gradient Descent algorithm in R solving a two-dime
nsional nonlinear optimization function.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# define a 2D basin function, optima is at (0,0)
basin <- function(x) {
x[1]^2 + x[2]^2
}
# define the derivative for a 2D basin function
derivative <- function(x) {
c(2*x[1], 2*x[2])
}
# definition of the gradient descent method in 2D
gradient_descent <- function(func, derv, start, step=0.05, tol=1e-8) {
pt1 <- start
grdnt <- derv(pt1)
pt2 <- c(pt1[1] - step*grdnt[1], pt1[2] - step*grdnt[2])
while (abs(func(pt1)-func(pt2)) > tol) {
pt1 <- pt2
grdnt <- derv(pt1)
pt2 <- c(pt1[1] - step*grdnt[1], pt1[2] - step*grdnt[2])
print(func(pt2)) # print progress
}
pt2 # return the last point
}
# locate the minimum of the function using the Gradient Descent method
result <- gradient_descent(
basin, # the function to optimize
derivative, # the gradient of the function
c(runif(1,-3,3), runif(1,-3,3)), # start point of the search
0.05, # step size (alpha)
1e-8) # relative tolerance for one step
# display a summary of the results
print(result) # coordinate of fucntion minimum
print(basin(result)) # response of fucntion minimum
# display the function as a contour plot
x <- seq(-3, 3, length.out=100)
9. 40
41
42
43
44
45
y <- seq(-3, 3, length.out=100)
z <- basin(expand.grid(x, y))
contour(x, y, matrix(z, length(x)), xlab="x",ylab="y")
# draw the optima as a point
points(result[1], result[2], col="red", pch=19)
# draw a square around the optima to highlight it
rect(result[1]-0.2, result[2]-0.2, result[1]+0.2, result[2]+0.2, lwd=2)
Example:
> library(lpSolve)
> ac<- matrix (c(2, 7, 7, 2, 7, 7, 3, 2, 7, 2, 8, 10, 1, 9, 8, 2), 4, 4)
> ac
[,1] [,2] [,3] [,4]
[1,] 2 7 7 1
[2,] 7 7 2 9
[3,] 7 3 8 8
[4,] 2 2 10 2
> library(lpSolve)
> lp.assign(ac)
Success: the objective function is 8
> lp.assign(ac)$solution
[,1] [,2] [,3] [,4]
[1,] 0 0 0 1
[2,] 0 0 1 0
[3,] 0 1 0 0
[4,] 1 0 0 0
11. [1] 28.60000 0.00000 0.00000 31.82759
> get.constraints(lprec)
[1] 92.3000 6.8640 391.2928
Example:
The Rsymphony package in R can solve this problem for you. You will need a vector of the distances ci,jci,j and 0'
s for each yiyi as well as a matrix to take care of the constraints (the three summations starting after "subject to").
If you aren't familiar with the constraint matrix, each column contains the coefficients for that variable. The help file
for the Rsymphony package has a couple examples that should help with that. Don't forget to include columns for
the yiyi in the matrix as well. You will also need to set the xi,jxi,j and yiyi to be binary by using
library(Rsymphony)
> obj <- c(2.8, 5.4, 1.4, 4.2, 3.0, 6.3, 0, 0)
> mat <- matrix(c(1,0,0,1,0,0,0,1,0,1,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0,1,0,0,1
,0,0,0,1,0,1,0,0,0,0,-3,0,1,0,0,0,0,-3,1), nrow = 6)
> dir <- c("==", "==", "==", "<=", "<=", "<=")
> rhs <- c(1, 1, 1, 0, 0, 2)
> max <- FALSE
> types <- "B"
> fac.loc <- Rsymphony_solve_LP(obj, mat, dir, rhs, types = types, max = ma
x, write_lp = TRUE)
> fac.loc
$solution
[1] 1 0 1 0 1 0 1 1
$objval
[1] 7.2
$status
TM_UNBOUNDED
237
Example:
>library(clue)
>
> matrix1 <- matrix(c( 3, 2, 1, 4, 4, 4, 4,
+ 4, 4, 4, 4, 1, 2, 3,
+ 4, 1, 3, 2, 4, 4, 4,
+ 4, 4, 4, 1, 2, 3, 4,
+ 2, 4, 3, 4, 1, 4, 4,
+ 4, 3, 4, 4, 4, 2, 1,
+ 4, 1, 2, 4, 3, 4, 4), nrow=7, byrow=TRUE
)
>
> matrix1
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 3 2 1 4 4 4 4
[2,] 4 4 4 4 1 2 3
[3,] 4 1 3 2 4 4 4
[4,] 4 4 4 1 2 3 4
[5,] 2 4 3 4 1 4 4
[6,] 4 3 4 4 4 2 1
[7,] 4 1 2 4 3 4 4
>
> solve_LSAP(matrix1, maximum = FALSE)
Optimal assignment:
1 => 3, 2 => 5, 3 => 2, 4 => 4, 5 => 1, 6 => 7, 7 => 6