R language and optimization model (2): nonlinear programming and multi-objective programming

    


    Unlike linear programming, nonlinear programming requires that the objective function or constraints contain nonlinear functions. Correspondingly, nonlinear programming methods are used to solve such problems. The relaxation of constraints or objective function makes the programming model more general, but it also increases the difficulty of solving the problem. For simple nonlinear programming problems, the stat package in R language can be solved. Here we will introduce the Rdonlp2 package, which is more professional for solving nonlinear programming in the R language.

0?wx_fmt=png


    Rdonlp2 is very powerful in solving nonlinear programming problems. Users can install it and consult the help documentation:

library(Rdonlp2)

    The core function of the Rdonlp2 package is donlp2, which can quickly solve the maximum value of nonlinear programming. Its usage is as follows:

donlp2(par,fn,
       par.upper=rep(+Inf,length(par)),
       par.lower=rep(+Inf,length(par)),
       A=NULL,
       lin.upper=rep(+Inf,length(par)),
       lin.lower=rep(-Inf,length(par)),
       nlin=list(),
       nlin.upper=rep(+Inf,length(nlin)),
       nlin.lower=rep(-Inf,length(nlin)),
       control=donlp2.control(),
       control.fun=function(lst){return(TRUE)},
       env=.GlobalEnv,
       name="Rdonlp2"
       )

    Explain the many parameters of this function:

    par : initial iteration vector

    fn : continuous function

    par.upper, par.lower : the upper and lower bounds of the independent variable

    A: Linear constraint matrix

    lin.upper, lin.lower : upper and lower bounds of linear constraints

    nlin : list of nonlinear constraint functions

    nlin.upper, nlin.lower : upper and lower bounds for nonlinear constraints

    Other parameters can be ignored.


    Let's take a look at an example of the donlp2 function to solve nonlinear programming:   0?wx_fmt=png

    Write nonlinear programming calculation commands with the Rdonlp2 package:

library(Rdonlp2) 
p=c(0,0) #Iterative initial value
par.l=c(-100,100);par.u=c(100,100)#Argument domain constraint
fn=function(x){    exp(x [1])*(4*x[1]^2+2*x[2]^2+4*x[1]*x[2]+2*x[2]+1) } #Objective function A =matrix(c(1,1,3,-1),2,byrow=TRUE) lin.l=c(2,1);lin.u=c(+Inf,3) #linear constraints nlcon1=function( x){   -x[1]*x[2] } #nonlinear constraints donlp2(p,fn,par.u,par.l,A,lin.l=lin.l,lin.u=lin.u,        nlin=list(nlcon1)) $message [1] "KT-conditions satisfied, no further correction computed" $par [1] 33.66667 100.00000 $gradf [1] 1.625065e+19 2.243635e+17









       







    The corresponding running results include data such as intermediate processes, algorithm parameters, and running time, which are not listed here.

    Let's look at another example of using the Rdonlp2 package to find the minimum value of a binary rastrigin function. This function has many extreme points and only one maximum point is deceptive to various optimization algorithms, but it is effective to test various optimization algorithms. The binary rastrigin function is:

0?wx_fmt=png

    First make a three-dimensional image of the function: 

a<-5
x<-seq(-a,a,0.01)
y<-seq(-a,a,0.01)
f<-function(x,y){    x^2-10*sin(2*pi*x)+y^2-10*cos(2*pi*y)+20}z<-outer(x,y,f)image(x,y,z,col=heat.colors(24))library(rgl)zorder<-rank(z)persp3d(x,y,z,col=rainbow(as.integer(max(zorder)))[zorder])







0?wx_fmt=png

fn<-function(x){
       f<-sum(x^2*cos(2*pi*x)+10)}par<-c(-500,500)ret<-donlp2(par,fn)ret$f[1] 20ret$par[1] -2.654965e-06  2.654965e-06
   









    It can be seen from the output that the minimum value of this function is 20. So Rdonlp2 is also very convenient to solve nonlinear programming. Of course, there are many more general nonlinear programming problems that cannot be solved by the Rdonlp2 package. At this time, heuristic algorithms such as genetic algorithm and simulated annealing may be used for help. No discussion here.


    In many practical problems, there is often more than one standard to measure the quality of a program. For example, to make a missile, it needs to have a long range, the most fuel-efficient, and high precision. This type of problem is not a single-objective programming problem, we call it a multi-objective programming problem.

    There are usually main objective method, hierarchical sequence method and linear weighted sum method to solve multi-objective programming problems. Among them, the main objective method converts a multi-objective optimization problem into a linear or nonlinear programming problem by determining a main objective; the hierarchical sequence method is a process of arranging multiple objectives in the objective in an order according to their importance and solving it step by step; linear weighting The rule is the process of assigning a weight to each objective and obtaining a new objective function by weighted summation.

    The goalprog package is usually used to solve multi-objective programming problems in R language, and its core function is llgp:

llgp(coefficient,targets,achievements,maxiter=1000,verbose=FALSE)

   Where coefficient is the constraint coefficient matrix, targets is the coefficient matrix constraint vector, achievements is the objective function including objective, priority, p, n, where objective represents the number of deviation variables, priority represents the priority of the deviation variable, p and n respectively is the weight coefficient of positive and negative deviation variables.

    Look at a calculation example of a multi-objective programming problem: (This problem is from Qian Songdi's "Operation Research" textbook, Section 4.4, Example 5)

0?wx_fmt=png

    The calculation process of the llgp function is as follows:

library(goalprog)
coefficients<-matrix(c(1,1,5,1,1,0,3,1),4)
targets<-c(10,4,56,12)
achievements<-data.frame(objective=1:4,priority=c(1,1,3,4),
                        p=c(2,3,0,1),n=c(0,0,1,0))
soln<-llgp(coefficients,targets,achievements)

soln$converged
[1] TRUE
soln$out
Decision variables
               X
X1   4.000000e+00
X2   6.000000e+00

Summary of objectives
       Objective           Over          Under         Target
G1   1.000000e+01   0.000000e+00   0.000000e+00   1.000000e+01
G2   4.000000e+00   0.000000e+00   0.000000e+00   4.000000e+00
G3   3.800000e+01   0.000000e+00   1.800000e+01   5.600000e+01
G4   1.000000e+01   0.000000e+00   2.000000e+00   1.200000e+01

Achievement function
               A
P1   0.000000e+00
P2   0.000000e+00
P3   1.800000e+01
P4   0.000000e+00

    From the output results, we can see that the optimal values ​​of x1 and x2 are 4 and 6, respectively.

    From this example, we can see that the R language to solve multi-objective programming problems only needs to set the planned objective function and constraints into the format according to the function format, and the process is very simple.


references:

Wei Taiyun. R Software and Optimization [C]//China R Language Conference. 2008.






0?
640?wx_fmt=jpeg
Scan the code to follow the development of data scientists






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324481725&siteId=291194637