Solve Optimization problem with Nonlinear Objective and Constraints

Share:

Description

Sequential Quatratic Programming (SQP) method is implemented to find solution for general nonlinear optimization problem (with nonlinear objective and constraint functions). The SQP method can be find in detail in Chapter 18 of Jorge Nocedal and Stephen J. Wright's book. Linear or nonlinear equality and inequality constraints are allowed. It accepts the input parameters as a constrained matrix. The function NlcOptim is to solve generalized nonlinear optimization problem:

min f(x)

s.t. ceq(x)=0

c(x)≤ 0

Ax≤ B

Aeq x ≤ Beq

lb≤ x ≤ ub

Usage

1
2
3
NlcOptim(X = NULL, objfun = NULL, confun = NULL, A = NULL, B = NULL,
  Aeq = NULL, Beq = NULL, lb = NULL, ub = NULL, tolX = 1e-05,
  tolFun = 1e-06, tolCon = 1e-06, maxnFun = 1e+07, maxIter = 4000)

Arguments

X

Starting vector of parameter values.

objfun

Nonlinear objective function that is to be optimized.

confun

Nonlinear constraint function. Return a ceq vector and a c vector as nonlinear equality constraints and an inequality constraints.

A

A in the linear inequality constraints.

B

B in the linear inequality constraints.

Aeq

Aeq in the linear equality constraints.

Beq

Beq in the linear equality constraints.

lb

Lower bounds of parameters.

ub

Upper bounds of parameters.

tolX

The tolerance in X.

tolFun

The tolerance in the objective function.

tolCon

The tolenrance in the constraint function.

maxnFun

Maximum updates in the objective function.

maxIter

Maximum iteration.

Value

Return a list with the following components:

p

The optimum solution.

fval

The value of the objective function at the optimal point.

lambda

Lagrangian multiplier.

grad

The gradient of the objective function at the optimal point.

hessian

Hessian of the objective function at the optimal point.

Author(s)

Xianyan Chen, Xiangrong Yin

References

Nocedal, Jorge, and Stephen Wright. Numerical optimization. Springer Science & Business Media, 2006.

Examples

 1
 2
 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
library(MASS)
###ex1
objfun=function(x){
 return(exp(x[1]*x[2]*x[3]*x[4]*x[5]))
}
#constraint function
confun=function(x){
 f=NULL
 f=rbind(f,x[1]^2+x[2]^2+x[3]^2+x[4]^2+x[5]^2-10)
 f=rbind(f,x[2]*x[3]-5*x[4]*x[5])
 f=rbind(f,x[1]^3+x[2]^3+1)
 return(list(ceq=f,c=NULL))
}

x0=c(-2,2,2,-1,-1)
NlcOptim(x0,objfun=objfun,confun=confun)

####ex2
obj=function(x){
 return((x[1]-1)^2+(x[1]-x[2])^2+(x[2]-x[3])^3+(x[3]-x[4])^4+(x[4]-x[5])^4)
}
#constraint function
con=function(x){
 f=NULL
 f=rbind(f,x[1]+x[2]^2+x[3]^3-2-3*sqrt(2))
 f=rbind(f,x[2]-x[3]^2+x[4]+2-2*sqrt(2))
 f=rbind(f,x[1]*x[5]-2)
 return(list(ceq=f,c=NULL))
}

x0=c(1,1,1,1,1)
NlcOptim(x0,objfun=obj,confun=con)


##########ex3
obj=function(x){
 return((1-x[1])^2+(x[2]-x[1]^2)^2)
}
#constraint function
con=function(x){
 f=NULL
 f=rbind(f,x[1]^2+x[2]^2-1.5)
 return(list(ceq=NULL,c=f))
}

x0=as.matrix(c(-1.9,2))
objfun(x0)
confun(x0)
NlcOptim(x0,objfun=obj,confun=con)


##########ex4
objfun=function(x){
 return(x[1]^2+x[2]^2)
}
#constraint function
confun=function(x){
 f=NULL
 f=rbind(f,-x[1] - x[2] + 1)
 f=rbind(f,-x[1]^2 - x[2]^2 + 1)
 f=rbind(f,-9*x[1]^2 - x[2]^2 + 9)
 f=rbind(f,-x[1]^2 + x[2])
 f=rbind(f,-x[2]^2 + x[1])
 return(list(ceq=NULL,c=f))
}

x0=as.matrix(c(3,1))
NlcOptim(x0,objfun=objfun,confun=confun)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.