拉格朗日乘子法

  • 拉格朗日乘子法

f(x_1,x_2,...,x_n)

df(x_1,x_2,...,x_n)=\frac{\partial f}{\partial x_1}dx_1+\frac{\partial f}{\partial x_2}dx_2+...+\frac{\partial f}{\partial x_n}dx_n=\sum_{i=1}^n\frac{\partial f}{\partial x_i}dx_i

  • 约束条件


  •  函数求极值的必要条件:\bigtriangledown f=\sum_{i=1}^m\lambda_i \bigtriangledown g_i
  • 引入拉格朗日函数:L(x_1,x_2,...,x_n,\lambda_1,\lambda_2,...,\lambda_m)=f(x_1,x_2,...,x_n)-\sum_{i=1}^m\lambda_ig_i(x_1,x_2,...,x_m)


  • example

max:x^2y^3

obj\,to:x+2y=3

\begin{matrix} \frac{\partial L}{\partial x}=2y^3x-\lambda=0\\ \frac{\partial L}{\partial y}=3x^2y^2-2\lambda=0\\ \frac{\partial L}{\partial \lambda}=-x-2y+3=0 \end{matrix}                         \begin{matrix} x=\frac{6}{5}\\ y=\frac{9}{10}\\ \lambda=\frac{2387}{1250} \end{matrix}

import geatpy as ea
import numpy as np
 
 
@ea.Problem.single
def evalVars(Vars): 
    x1 = Vars[0]
    x2 = Vars[1]
    
    f = (x1)**2 *\
        (x2)**3 
    
    CV = np.array([ abs(x1+2*x2-3)-1e-5 ,
                        ])   
    return f, CV
 
problem = ea.Problem(name='test',
                     M=1,  
                     maxormins=[-1], 
                     Dim=2,                         
                     varTypes=[0, 0],
                     lb=[0, 0],
                     ub=[ 5,  5],
                     evalVars=evalVars)
 
algorithm = ea.soea_SEGA_templet(problem,
                ea.Population(Encoding='RI', NIND=200),
                MAXGEN=5000,              
                logTras=1,              
                trappedValue=1e-8,   
                maxTrappedCount=10)     
 
res = ea.optimize(algorithm, seed=1, verbose=True, \
                  drawing=1, outputMsg=True, drawLog=True, \
                  saveFlag=True, dirName='result')

===================================================================================
gen |  eval  |    f_opt    |    f_max    |    f_avg    |    f_min    |    f_std    
-----------------------------------------------------------------------------------
 16 |  3400  | 6.22418E-01 | 6.22418E-01 | 6.22418E-01 | 6.22418E-01 | 0.00000E+00 
 17 |  3600  | 6.22418E-01 | 6.22418E-01 | 6.22418E-01 | 6.22418E-01 | 0.00000E+00 
 18 |  3800  | 6.22418E-01 | 6.22418E-01 | 6.22418E-01 | 6.22418E-01 | 0.00000E+00 
 19 |  4000  | 6.28392E-01 | 6.28392E-01 | 6.23888E-01 | 6.22349E-01 | 2.60049E-03 
 20 |  4200  | 6.28392E-01 | 6.28392E-01 | 6.24390E-01 | 6.22349E-01 | 2.82789E-03 
 21 |  4400  | 6.28402E-01 | 6.28402E-01 | 6.24262E-01 | 6.22287E-01 | 2.78615E-03 
 22 |  4600  | 1.04062E+00 | 1.04062E+00 | 6.35917E-01 | 6.22277E-01 | 7.04912E-02 
 23 |  4800  | 1.04062E+00 | 1.04062E+00 | 6.38870E-01 | 6.22277E-01 | 7.88210E-02 
 24 |  5000  | 1.04062E+00 | 1.04062E+00 | 6.36575E-01 | 6.22277E-01 | 7.26059E-02 
 25 |  5200  | 1.04062E+00 | 1.04062E+00 | 6.32069E-01 | 6.22275E-01 | 5.94260E-02 
 26 |  5400  | 1.04062E+00 | 1.04062E+00 | 6.34012E-01 | 6.22277E-01 | 6.51523E-02 
 27 |  5600  | 1.04062E+00 | 1.04062E+00 | 6.40842E-01 | 6.22376E-01 | 8.16478E-02 
 28 |  5800  | 1.04062E+00 | 1.04062E+00 | 6.52142E-01 | 6.22401E-01 | 1.02467E-01 
 29 |  6000  | 1.04063E+00 | 1.04063E+00 | 6.61585E-01 | 6.22411E-01 | 1.15557E-01 
 30 |  6200  | 1.04063E+00 | 1.04063E+00 | 6.79895E-01 | 6.22458E-01 | 1.36341E-01 
Execution time: 0.050093650817871094 s
Evaluation number: 6200
The best objective value is: 1.040625027632516
The best variables are: 
1.113896369934082	0.9430503845214844

你可能感兴趣的:(分析学基础与进阶,人工智能,算法,机器学习,数学建模,抽象代数)