简体   繁体   中英

My Code is not working and I am not getting any error (code Shared)

import numpy as np
import matplotlib.pyplot as plt
import numdifftools as nd
from numpy import linalg as LA
from numpy import array

# Function definition
#function = ((x2-x1)**4)+(8*x1*x2)-x1+x2+3

def funct(x):
    return ((x[1]-x[0])**4)+(8*x[0]*x[1])-x[0]+x[1]+3

x = np.array([1.5, 1.5])  # initial Value
y=funct(x)
grad=nd.Gradient(funct)(x)
norm_grad_square=np.dot(grad,grad)
p=-grad
N=15000
epsilon = 10^(-6)
alpha_bar=5
c=0.1
rho=0.8
alpha=alpha_bar
curve_x = [x]
curve_y = [y]
i=1
while(i<=N) or (norm_grad_square>=epsilon):
    p= -grad
    misc=np.dot(grad,p)
    j=1
    while (funct(x+alpha*p)>(funct(x)+(c*alpha*misc)) or j<=1000):
        alpha_new=rho*c
        alpha=alpha_new
        j+=1
    x=x+(alpha*p)
    y=funct(x)
    grad=nd.Gradient(funct)(x)
    norm_grad_square=np.dot(grad,grad)
    curve_x.append(x)
    curve_y.append(y)
    i+=1
     
if i==N:
    print("Maximun iterations reached but convergence did not happen")
    print("x= ", x,"function= ",y,"Gradient= ",grad)
else: 
    print("x= ",x,"function= ",y,"Gradient= ",grad)  

My code is not running and no error is shown. I am running an algorithm to calculate an array until I reach the minimum of the function The array contains the independent variables. I am calculating the gradient of the function using inbuilt function.

You got an endless loop or infinite loop , right?

When symptom "infinite loop", then, as the comments suggest, debug the exit-conditions of your (while-)loops.

Debugging with print

For example:

  1. reduce to the required variables and statements
  2. then add print statements inside the loop to watch the exit-conditions
import numpy as np
import numdifftools as nd

def funct(x):
    return ((x[1]-x[0])**4)+(8*x[0]*x[1])-x[0]+x[1]+3

x = np.array([1.5, 1.5])  # initial Value
# ..
grad=nd.Gradient(funct)(x)
norm_grad_square=np.dot(grad,grad)   # used in exit-condition of loop
# ..
N=15000     # used in exit-condition of loop
epsilon = 10^(-6)     # used in exit-condition of loop
# ...
i=1  # used in exit-condition of loop
while(i<=N) or (norm_grad_square>=epsilon):
    print(i,N,norm_grad_square,epsilon)  # debug print
    print(f"{(i<=N)} or {(norm_grad_square>=epsilon)}")
    # remainder of your loop body: removed for simplicity
    i+=1

Prints an endless loop of:

1 15000 289.9999999999993 -16
True or True
2 15000 289.9999999999993 -16
True or True
..
15000 15000 289.9999999999993 -16
True or True
15001 15000 289.9999999999993 -16
False or True
..
356977 15000 289.9999999999993 -16
False or True

The ellipses shows omitted output and I interrupted the look with CTRL + C after a few seconds.

We can observe how i is exceeding N over 15000 .

Logical analysis

Didn't we expect to let the loop-counter variable i increase with each iteration and approximate to the constant maximum N .

Let's isolate the loop and its exit-conditions to inspect only i .

N=15000
# omitted to focus on: i, N
i=1
while(i<=N):
    # loop body
    i+=1

# end of loop
if i==N:
    print("Maximun iterations reached but convergence did not happen")

print(i, N) # debug final i compared to N  

Does not print expected maximum-message but:

15001 15000

The good news is, that the loop ended with a finite i . This is not (i<=N) and not i==N but i > N or at least i >= N .

Now we could conclude: the other side of the exit-condition (right-hand of the or ) seems unmet or contain a bug.

To do

I left further debugging to you. I would start setting N much lower, start with N = 3 and slightly increase under close monitoring of the other exit-condition, eg

N = 3
i = 1
while(i<=N) or (norm_grad_square>=epsilon):
    print(i,N,norm_grad_square,epsilon)  # debug print
    # gradually insert loop-body
    i += 1

You can even add the statements of the loop-body gradually back to watch their evolution with each iteration and impact on the exit-condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM