Oct-03-2016, 08:26 PM
I have some code for class inside which I have methods. I want to run the code which While True loop. The problem I face is with updating the variables.
The code is as following
which is not as I expect. I should have new values between (0,9). Because I take random value from this range and append it to xalg and yalg.
Quote:xlowrand and xhighrand.
The code is as following
class searchRandom(SearchBase):
def __init__(self,xlowrand,xhighrand,ygoal):
self.xlowrand=xlowrand
self.xhighrand=xhighrand
SearchBase.__init__(self, xlowrand, xhighrand, ygoal)
def calcrand(self,xlowrand,xhighrand):
xalg = self.xalg
yalg = self.yalg
xlowrand = self.xlowrand
xhighrand = self.xhighrand
msg=self.msg
if (len(xalg))>=2 and len(yalg)>=2:
last = len(yalg)-1
xPrevious = xalg[last-1]
xLast = xalg[last]
yPrevious = yalg[last-1]
yLast = yalg[last]
prevdist= abs(yalg[last-1]-ygoal)
currentdist= abs(yalg[last]-ygoal)
if currentdist<prevdist:
m=float ((yLast-yPrevious)/(xLast-xPrevious))
if m>0:
if yalg[last]< ygoal:
xlowrand=xalg[last]
msg.append(xlowrand)
xhighrand = float(xhighrand)
return msg[len(msg)-1]
else:
xhighrand=xalg[last]
np.append(msg,xhighrand)
xlowrand = float(xlowrand)
return msg[len(msg)-1]
elif yalg[last]< ygoal:
xhighrand=xalg[last]
msg.append(xhighrand)
xlowrand = float(xlowrand)
return msg[len(msg)-1]
else:
xlowrand=xalg[last]
np.append(msg,xlowrand)
xhighrand = float(xhighrand)
return msg[len(msg)-1]
elif (len(xalg))==1:
msg.append(xhighrand)
xlowrand = float(xlowrand)
return msg[len(msg)-1]
elif (len(xalg))<1:
msg.append(xlowrand)
xhighrand = float(xhighrand)
return msg[len(msg)-1]
else:
print('there are no data')
#return msg[len(msg)-1]then I define few variables and take an objectygoal = 55 yerror = ygoal*.001 searchClass= searchRandom(0,9,ygoal)
while True: searchClass.calcrand(searchClass.xlowrand,searchClass.xhighrand) print 'returnvalue',searchClass.xlowrand, searchClass.xhighrand if i<1: ylowrand = testfunction(searchClass.xlowrand) yhighrand = testfunction(searchClass.xhighrand) searchClass.append(searchClass.xlowrand,ylowrand) searchClass.append(searchClass.xhighrand,yhighrand) #print searchClass.xalg newrandomvalue=searchClass.randomx(searchClass.xlowrand, searchClass.xhighrand) #print newrandomvalue,'hello' ynew = testfunction(newrandomvalue) searchClass.append(newrandomvalue, ynew)The output is as follows
Quote:returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
returnvalue 0 9
which is not as I expect. I should have new values between (0,9). Because I take random value from this range and append it to xalg and yalg.
