我需要帮助找到无限循环或其他东西,因为我拥有的代码无法运行



我试图计算两个逻辑函数f(x)和g(x)的希尔系数,它们的组成,c(x),并将其输入到数据表中。此外,我正在输入函数本身,它们的组成以及f(x)和g(x)的希尔系数的乘积。为了计算山丘系数,我也使用数字平分法。

这是我的代码:

# Imports
import numpy as np
from numpy import log as ln
from matplotlib import pyplot as plt
from random import randint
import sympy as sym
import pandas as pd
import math

# initializing data
data = {'row': [],
'f(x)': [],
'g(x)': [],
'f(g(x))': [],
'H_f': [],
'H_g': [],
'H_fg': [],
'Product of H_f and H_g': [],
'Does this prove hypothesis?': []
}
df = pd.DataFrame(data)
#True Randomization
def logrand(b):
ra = randint(1,b)
ra = float(ra/100)
ra = 10**ra
ra = round(ra)
ra = int(ra)
return ra
# Two Hill functions
num1 = 10
number = 1
for _ in range(num1):
# Params
u = 10
c1 = logrand(300)
r1 = randint(1,u)
k1 = logrand(300)
c2 = logrand(300)
r2 = randint(1,u)
k2 = logrand(300)
# function layout
funcf = '{}/({}+e^(-{}x))'.format(c1, k1, r1)
funcg = '{}/({}+e^(-{}x))'.format(c2, k2, r2)
funcc = '{}/({}+e^(-{}({}/({}+e^(-{}x)))))'.format(c1, k1, r1, c2, k2, r2)
# figure layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Hill Function for f(x) and g(x) and f(g(x))
def f(x):
return c1 / (k1 + np.exp((-1*r1) * x))

def g(x):
return c2 / (k2 + np.exp((-1*r2) * x))

def comp(x):
return c1 / (k1 + np.exp((-1*r1) * (c2 / (k2 + np.exp((-1*r2) * x)))))

# EC finder
def BisectionEC10(fa, a, b):
c = 1
x = np.linspace(a, b, 1000)
ystar = 0.10 * (fa(x).max() - fa(x).min())
while abs(fa(c) - ystar) > 0.000000001:
c = (a + b) / 2
if fa(c) - ystar < 0:
a = c
elif fa(c) - ystar > 0:
b = c
# print('The EC10 of the function is: ',"{0:.15f}".format(c))
# print('Output of the function when evaluated at the EC10: ',fa(c))
return c

def BisectionEC90(fa, a, b):
c = 1
x = np.linspace(a, b, 1000)
ystar = 0.90 * (fa(x).max() - fa(x).min())
while abs(fa(c) - ystar) > 0.000000001:
c = (a + b) / 2
if fa(c) - ystar < 0:
a = c
elif fa(c) - ystar > 0:
b = c
# print('The EC90 of the function is: ',"{0:.15f}".format(c))
# print('Output of the function when evaluated at the EC90: ',fa(c))
return c

# EC90 and EC10 for f(x), g(x) and f(g(x))
up = 20
lo = 0
# x = np.linspace[lo,up,1000]
x = 1
# x = sym.symbols('x')
EC90_1 = BisectionEC90(f, lo, up)
EC10_1 = BisectionEC10(f, lo, up)
EC90_2 = BisectionEC90(g, lo, up)
EC10_2 = BisectionEC10(g, lo, up)
EC90_3 = BisectionEC90(comp, lo, up)
EC10_3 = BisectionEC10(comp, lo, up)
# Hill Coefficient for f(x) and g(x)
H_1 = ln(81) / (ln(EC90_1 / EC10_1))
H_1 = round(H_1,4)
H_2 = ln(81) / (ln(EC90_2 / EC10_2))
H_2 = round(H_2,4)
H_3 = ln(81) / (ln(EC90_3 / EC10_3))
H_3 = round(H_3,4)

prod = float(H_1.real) * float(H_2.real)
if prod >= float(H_3.real):
answer = 'yes'
else:
answer = 'no'
prod = round(prod,4)
# adding all data to dataframe 2
data2 = {'row': [number],
'f(x)': [funcf],
'g(x)': [funcg],
'f(g(x))': [funcc],
'H_f': [H_1],
'H_g': [H_2],
'H_fg': [H_3],
'Product of H_f and H_g': [prod],
'Does this prove hypothesis?': [answer]
}
number = number + 1
df = df.append(data2, ignore_index=True)
#final dataframe
print(df)
df.to_csv(r'/Users/*****/Desktop/Research/twoarctanfuncs.csv', index = False)

现在我卡住的问题是代码无法运行,或者它有一个无限循环,我无法弄清楚问题发生在哪里。

这是我强制停止它时的错误消息:

Traceback (most recent call last):
File "/Users/*****/Documents/Python/NumericBisectionMethod/venv/twologisticfuncs.py", line 112, in <module>
EC90_1 = BisectionEC90(f, lo, up)
File "/Users/*****/Documents/Python/NumericBisectionMethod/venv/twologisticfuncs.py", line 95, in BisectionEC90
if fa(c) - ystar < 0:
File "/Users/*****/Documents/Python/NumericBisectionMethod/venv/twologisticfuncs.py", line 60, in f
return c1 / (k1 + np.exp((-1*r1) * x))
KeyboardInterrupt

感谢您的任何帮助!

看起来您的二分搜索(平分)没有从足够大的跨度开始。

  1. 我快速尝试了您的代码,然后将语句print (a, b, c)添加到BisectionEC90while循环中。输出迅速收敛到0 0.0 0.0并保持在那里。

  2. 我用print (abs(fa(c)), ystar)替换了该打印语句,输出收敛到0.025157232704402517 0.00014330069262001276.

因此,代码永远不会退出循环是有道理的while abs(fa(c) - ystar) > 0.000000001:


EDIT:最好在Bisection函数的while循环中添加一些错误检查,以防止这种类型的无限循环。也许是这样的:

if b - a < 0.000000001:
raise Exception("Failed to find solution; a={}, b={}".format(a,b))

如果使用 print 语句或调试器,您将能够检测到问题。你陷入了一段时间的循环,这意味着你的条件永远不会得到满足。可能有多种原因,要找出确切的问题,您必须对其进行调试。尽管最合乎逻辑的结论是,虽然abs(fa(c) - ystar) > 0.000000001并没有像@Mr所说的那样得到满足。

在这种情况下,我还建议使用 if 条件来中断循环。

相关内容

最新更新