Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Hello! Friend!!!

Thank you very much for reading haihong’s article, if there are any mistakes in the article, please point out ~

Self-introduction ଘ(੭, ᵕ)੭

Nickname: Haihong

Tag: programmer monkey | C++ contestant | student

Introduction: because of C language to get acquainted with programming, then transferred to the computer major, had the honor to get some national awards, provincial awards… Has been confirmed. Currently learning C++/Linux/Python

Learning experience: solid foundation + more notes + more code + more thinking + learn English well!

 

Little White stage of learning Python

The article is only used as my own study notes for the establishment of knowledge system and review

The problem is not to learn one more question to understand one more question

Know what is, know why!

Nonlinear programming

Nonlinear programming can be simply divided into two kinds, the objective function is convex or non-convex

Nonlinear programming of convex functions, such as fun=x2+y2+xyfun =x ^2 +y ^2 +xyfun =x2+y2+xy, has many common libraries, such as Cvxpy

For nonlinear programming of non-convex functions (finding extremum), the following methods can be tried:

  • Pure mathematical method, take the derivative and find the extremum
  • Neural network, deep learning (chain derivation process in back propagation algorithm)
  • scipy. optimize. minimize
scipy.optimize.minimize(fun,x0,args=(),method=None,jac=None,hess=None,hessp=None,bounds= None,constaints=() , Tol =None,Callback= None, options=None) fun: Objective function to find the minimum args: constant value constraints: method: method to find the extreme value, the default. XO: The initial guess of a variable, which is locally optimalCopy the code

Example 1

Find the minimum of 1 over x plus x

from scipy.optimize import minimize
import numpy as np

def fun(args):
    a = args
    v = lambda x:a/x[0] + x[0]
    return v

args = (1)
x0 = np.asarray((2))
res = minimize(fun(args), x0, method='SLSQP')
res
Copy the code

Example 2

Calculate the (2 + x1)/(1 + x2) – 3 x1 + 4 x3 (2 + x_1)/(1 + x_2) – 3 x_1 + 4 x_3 (2 + x1)/(1 + x2) – 3 x1 + 4 x3 minimum value, Where x1, x2, x3x_1, X_2, X_3x1, x2, x3 range from 0.1 to 0.9

# Runtime environment Vs Code
from scipy.optimize import minimize
import numpy as np

def fun(args) :
    a,b,c,d = args
    v = lambda x: (a + x[0]) / (b + x[1]) - c * x[0] + d * x[2]
    return v

def con(args) :
    x1min,x1max,x2min,x2max,x3min,x3max = args
    cons = ({'type':'ineq'.'fun':lambda x : x[0] - x1min},\
        {'type':'ineq'.'fun':lambda x:-x[0] + x1max},\
        {'type':'ineq'.'fun':lambda x:x[1] - x2min},\
        {'type':'ineq'.'fun':lambda x:-x[1] + x2max},\
        {'type':'ineq'.'fun':lambda x:x[2] - x3min},\
        {'type':'ineq'.'fun':lambda x:-x[2] + x3max})
    return cons

args = (2.1.3.4)
args1 = (0.1.0.9.0.1.0.9.0.1.0.9)
cons = con(args1)

x0 = np.asarray((0.5.0.5.0.5))
res = minimize(fun(args), x0, method='SLSQP',constraints=cons)
res.fun,res.success,res.x,res.status

# the results
(-0.773684210526435.True, array([0.9.0.9.0.1]), 0)
Copy the code

conclusion

Source of learning: STATION B and its classroom PPT, the code is reproduced

The essay is just a study note, recording a process from 0 to 1

Hope to help you, if there is a mistake welcome small partners to correct ~

I am haihong ଘ(੭, ᵕ)੭

If you think it’s ok, please give it a thumbs up

Thanks for your support ❤️