Python Forum

Full Version: Running Class methods in a loop and updating variables.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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 object
ygoal = 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.
Can you reproduce the problem in fewer lines of code? I suspect 10 would be enough, and 20 would almost certainly be enough.
(Oct-03-2016, 08:26 PM)ujjwalrathod007 Wrote: [ -> ]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.
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 object
ygoal = 55
yerror = ygoal*.001
searchClass= searchRandom(0,9,ygoal)
while True:
    searchClass.calcrand(searchClass.xlowrand,searchClass.xhighrand)
    print 'returnvalue',searchClass.xlowrand, searchClass.xhighrand
   
    newrandomvalue=searchClass.randomx(searchClass.xlowrand, searchClass.xhighrand)
    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

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.

(Oct-03-2016, 08:56 PM)ujjwalrathod007 Wrote: [ -> ]
(Oct-03-2016, 08:26 PM)ujjwalrathod007 Wrote: [ -> ]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.
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')
then I define few variables and take an object
ygoal = 55
searchClass= searchRandom(0,9,ygoal)
while True:
    searchClass.calcrand(searchClass.xlowrand,searchClass.xhighrand)
    print 'returnvalue',searchClass.xlowrand, searchClass.xhighrand
    newrandomvalue=searchClass.randomx(searchClass.xlowrand, searchClass.xhighrand)
    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

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.
You pass the same values to the method every time, "searchClass.xlowrand, searchClass.xhighrand", but you never change those values. If the method should be returning a predictable output for a given input, then it is working correctly.