Python implements various optimization algorithms

coldplay.xixi
Release: 2020-12-02 17:03:58
forward
6008 people have browsed it

python video tutorialThe column introduces various optimization algorithms

Python implements various optimization algorithms

##Related free learning recommendations: python video tutorial

Bisection method

For details, see rres. This code causes the algorithm to run twice

def asdf(x):
    rres=8*x**3-2*x**2-7*x+3
    return rres

i=2
left=0
right=1
while i>0 :
    i = i-1
    ans = 0.1
    mid1 = (left + right + ans) / 2
    mid2 = (left + right - ans) / 2
    a=asdf(mid1)
    c=asdf(mid2)
    if a > c :
        right = mid1
    else :
        left = mid2
b=(left+right) / 2
print("左极限=%s,右极限=%s,极小值x=%s"%(left,right,b))
Copy after login
左极限=0.45,右极限=0.775,极小值x=0.6125
Copy after login
Harvest:

This is the first code I implemented. After learning the algorithm, the logical framework is basically there, and the only thing that needs to be clarified is the corresponding python language. So I started looking for "how to define a function" (see mofan's Youku for details), the format of "loop body" and "if conditional statement" (https://blog.csdn.net/qq_39407518/article/details/79822498) "Mathematical symbols" (see mofan's Youku for details), and the use of print

1.def is the middle finger definition of Python, which is generally used to define functions. If deep learning is required to build a network, it can be used to define the network. One thing worth noting is that

return must be added on a new line after the function.

I don’t know why, but if it is not added, the function formula will be like a vase, just like a result that cannot be output.

2. The most confusing thing is logic. The logic was not clear at first, or there was an omission in the code, which led me to put left and right in the loop body. The result is predictable. But it was also because of this error that I knew how to use debug in pycharm. It was quite simple and Baidu came up with it instantly.

3. I don’t know why, but the print multiple variables outputted together in the video I watched cannot be used in my pycharm, and the result is very strange. Maybe it's because I'm on win10 and not ios. print If multiple variables are output together, it must be print("Name: %s, Name 2: %s" % (a, b)). The result output is name: a, Name 2: b

Question: 1. Why add return?
return means to output any variable value in this def as the result. Generally speaking, the relational expression of the output function is named so that when you call this function, the function value corresponding to the variable can be displayed. Otherwise, it will only run without results and will have no effect.

Grid method - three-point equal division method

import numpy as np
def qwer(x):
    third = np.exp(x) - 5*x
    return third

left = 1
right = 2
mid1 =float(left+right) / 2
mid2 = (left+mid1) / 2
mid3 = (mid1+right) /2
a = qwer(mid1)
b = qwer(mid2)
c = qwer(mid3)
i = 5
while i > 0:
    i=i-1
    if a > b:
        if c > b :
            #b
            right = mid1
            mid1 = mid2
            a=b
            mid2 = (left + mid1) / 2
            mid3 = (mid1 + right) / 2
            b = qwer(mid2)
            c = qwer(mid3)
        else:#b>c
            #c
            left = mid1
            mid1 = mid3
            a = c
            mid2 = (left + mid1) / 2
            mid3 = (mid1 + right) / 2
            b = qwer(mid2)
            c = qwer(mid3)
    else:#b>a
            if a > c:
                #C
                left = mid1
                mid1 = mid3
                a = c
                mid2 = (left + mid1) / 2
                mid3 = (mid1 + right) / 2
                b = qwer(mid2)
                c = qwer(mid3)
            else:#b>a&c>a
                # a
                left = mid2
                right = mid3
                mid2 = (left + mid1) / 2
                mid3 = (mid1 + right) / 2
                b = qwer(mid2)
                c = qwer(mid3)

print("最小值=%s"%mid1)
print("函数值=%s"%a)
Copy after login
最小值=1.609375
函数值=-3.047189552275773
Copy after login

About data variables in python. The results of the first run were obviously wrong, so I used debugging. It turned out that mid1 was always 1 instead of 1.5, so we started to understand the data variables. At first, I guessed that all variables in Python are of integer type by default, but based on the results of the dichotomy, I realized that this guess was wrong, so there was no need to change the variable format of the entire file. So I added a float in front of the mid1 formula, and the result was displayed as 1.5. But if I surround the entire formula with () and add float in front, the result is still 1. I don't quite understand why. But I know that the data format of Python is determined based on the input amount. That is to say, if your input amount is an integer, then the calculation output directly related to it must be an integer type, and it is an integer type that does not use carry. Before I used the float/.0 methods, mid1~3 were all integers.

left = 1.0
right = 2.0
mid1 =(left+right) / 2
Copy after login
Or instead of adding float in front of mid1, just click a dot after the input amount.

I really want to complain about print. It’s so troublesome. I have to get %s every time. And sometimes they can’t be put together! ! ! !

Fibonacci method

def fibonacci(n):
    i=0
    a = 0
    b = 1
    for i in range(n):
        i=i+1
        c = a+b
        a = b
        b = c
    return c
def bn(x):
    ert = x**2 - 6*x + 2
    return ert
z = 2
p = 0
left = 0.00000
right = 10.00000
L1 = right - left
while z < 100:
    m = fibonacci(z)
    l = L1/m
    k = 1.000/m
    if k < 0.03:
        print("n=%s,Fn=%s"%(z,m))
        L2 = l*fibonacci(z-1)
        t = left + L2
        r = right -L2
        while p < 3:
            p = p + 1
            l3 = t - r
            e= bn(t)
            o = bn(r)
            if e>o :
                right = t
                t = r
                r = left + l3
            else:#o>e
                left = r
                r = t
                t = right - l3
        break
    else:
        z = z + 1

okk=(left+right)/2
okky=bn(okk)
print(left)
print(right)
print("极小值x=",okk)
print("极小值y=",okky)
Copy after login

Don’t ask me what I have mastered, ask me how much I love python’s precision expression after writing this code:-) I decided to just write mathematical formulas in the future The code will add a lot of 0

fibonacci function definition after the small mathematical points of the input amount. Every time after debugging, my hands are shaking O(∩_∩)O~

Golden Section Method

def gold(x):
    gg= x**2 - 6*x + 9
    return gg

left = 1
right = 7
ans = 0.4
a = left + 0.618 * (right - left)
b = left + 0.382*(right - left)
gga = gold(a)
ggb = gold(b)
i = 0
while i < 7:
    print("i=%s" % i)
    print("left=%s,right=%s" % (left, right))
    print("x左=%s,x右=%s" % (a, b))
    print("y左=%s,y右=%s" % (ggb, gga))
    c = right - left
    if c > 0.4:
        i = i + 1
        if gga > ggb:
            right = a
            a = b
            b = left + 0.382*(right - left)
            gga = ggb
            ggb = gold(b)
        else:#gga<ggb
            left = b
            b = a
            a = left + 0.618 * (right - left)
            ggb = gga
            gga = gold(a)
    else:
        break
Copy after login

I don’t know when I had obsessive-compulsive disorder. As long as there is a "~" under the code, I must get rid of it. Laugh and cry. This one is very simple. The first four are all simple except for Fibonacci.

Indirect method - quadratic interpolation method

def yy(x):
    y=x**4-4*x**3-6*x**2-16*x+4
    return y

def xing(xm1,xm2,xm3,fm1,fm2,fm3):
    yxxx=0.5000*((xm2**2-xm3**2)*fm1+(xm3**2-xm1**2)*fm2+(xm1**2-xm2**2)*fm3)/((xm2-xm3)*fm1+(xm3-xm1)*fm2+(xm1-xm2)*fm3)
    return yxxx

x1 = -1.0000
f1 = yy(x1)
x3 = 6
f3 = yy(x3)
x2 = 0.50000*(x1+x3)
f2 = yy(x2)
xp = xing(x1,x2,x3,f1,f2,f3)
fp = yy(xp)
a = abs(xp-x2)
while abs(xp-x2) > 0.05000:
    a = abs(xp - x2)
    if xp > x2:
        if fp > f2:
            x3=xp
            f3=fp
            xp = xing(x1, x2, x3, f1, f2, f3)
            fp = yy(xp)
            print("ans=%s" % a)
            print("left=%s,right=%s" % (x1, x3))
            print("x*=%s,fp*=%s" % (xp, fp))
            print("x2=%s,f2=%s" % (x2, f2))
            print("******************")
        else:#f2>fp
            x1 = x2
            f1 = f2
            x2 = xp
            f2 = fp
            xp = xing(x1, x2, x3, f1, f2, f3)
            fp = yy(xp)
            print("ans=%s" % a)
            print("left=%s,right=%s" % (x1, x3))
            print("x*=%s,fp*=%s" % (xp, fp))
            print("x2=%s,f2=%s" % (x2, f2))
            print("******************")
    else:#xp<x2
        if fp > f2:
            x1 = xp
            f1 = fp
            xp = xing(x1, x2, x3, f1, f2, f3)
            fp = yy(xp)
            print("ans=%s" % a)
            print("left=%s,right=%s" % (x1, x3))
            print("x*=%s,fp*=%s" % (xp, fp))
            print("x2=%s,f2=%s" % (x2, f2))
            print("******************")
        else:
            x3 = x2
            f3 = f2
            x2 = xp
            f2 = fp
            xp = xing(x1, x2, x3, f1, f2, f3)
            fp = yy(xp)
            print("ans=%s" % a)
            print("left=%s,right=%s" % (x1, x3))
            print("x*=%s,fp*=%s" % (xp, fp))
            print("x2=%s,f2=%s" % (x2, f2))
            print("******************")
Copy after login
This formula seems very troublesome, so you should be more careful when writing it. Last time I put the 2 under the semicolon, and the result was very large, so it is better to convert it to 0.5 (PS: Don’t forget the long river of 0).

Although the code is very long, it is mainly because there are too many prints. I planned to print it at the beginning, but the final part would be missed. I'm too lazy to think of other methods, so let's just do this

Indirect method - Newton's method

def fd(x):
    y = 4*x**3-12*x**2-12*x-16
    return y
def fdd(x):
    ys = 12*x**2-24*x-12
    return ys

i = 1
x0 = 3.00000
ans = 0.001
while i < 7:
    fd0 = fd(x0)
    fdd0 = fdd(x0)
    if abs(fd0) > ans:
        x1 = x0 - (fd0/fdd0)
        x0 = x1
        print("次数:%s,所得的值x:%s"%(i,x1))
        i = i + 1
    else:#fd0<0.001
        print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
        print("Bingo!顺利通关!祝您开学愉快!")
        print("Boss  X=%s"%x0)
        break
Copy after login

In the beginning, "written as" in while caused the run to fail. Then, debug cannot be used. After checking online, I found out that "no Internet connection" and "no breakpoint selected". Finally, I wanted to try to output the content in else, but found that the screen was refreshed after running. So after changing it to i<7, it still didn't work, so I thought about adding a break to jump out of the loop, and it worked.

Then I just debugged and found out that i 1 is in the if. Because there is no way to 1, i=6 always exists, and it keeps looping. Because it doesn’t matter if you add break or i 1, it’s OK.

Just an hour and a half ago, I successfully finished optimizing the six major codes, all by hand, without any external force. Happy!

This is the first set of python codes I implemented by myself, which are mathematical formulas assembled in python language. When I first started, I knew roughly what needed to be reflected in the language, but it was not clear. So I looked for a few dichotomies on the Internet. They are all different, but the framework is similar. However, if we want to use our formula, we need to change a lot. Then I started to analyze our problem, and I found that it generally requires two parts, one part function definition and one part loop body. But I don’t know how to define functions, how to write mathematical formulas, how to make variables, which means I don’t know how to do some small things, so I chose to go directly to Baidu. Because I know I’m a good reader, and I’m better at getting the key points from reading than from extracting them from a video. Find knowledge points with purpose and have a firmer grasp.
So I started writing the first one - the dichotomy. I found that I made a lot of mistakes and a lot of them were very basic. But I still didn’t choose the video. Instead, I looked for these questions directly on Baidu, because you might not have found the point after the video. Of course, this is a step-by-step process, not just putting the program in place and changing it bit by bit.
With the success of the first two, I found that I have confidence in these codes, and it seems that I have seen through their disguise and grasped the essence. In addition, I also realized that my learning ability seems to have improved a lot since August, and I have more effective learning methods. There has been a certain awakening in all aspects. Except for the first one where I found a few erroneous codes, the others were all written according to my own logic. After the logic was understood, if I didn’t know how to translate a certain part of the language, I would just go to Baidu. In fact, these routines are all the same or The routines for transforming mathematical formulas are the same.
I also realized that assembly is actually the most difficult language. What I have learned so far is because many of them need to be defined and figured out by myself. They need to remember a lot of instructions and cannot be flexible. But for others, you just need to write down some corresponding ones. python is really simple. Moreover, I found that I seemed to have opened the door to a new world today. I fell in love with this thing that is full of spirituality, full of rigorous beauty, and the unknown changes. I found that I seemed to fall in love with code. It may not be limited to python, these languages ​​​​are full of challenges. I think when you are in doubt, you need to trust your intuition, at least I found it to be very accurate

The above is the detailed content of Python implements various optimization algorithms. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jianshu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template