Python Forum
Running Class methods in a loop and updating variables.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running Class methods in a loop and updating variables.
#1
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.
Reply
#2
Can you reproduce the problem in fewer lines of code? I suspect 10 would be enough, and 20 would almost certainly be enough.
Reply
#3
(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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 725 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 473 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Unchangeable variables in a class? Calab 12 1,531 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Structuring a large class: privite vs public methods 6hearts 3 1,051 May-05-2023, 10:06 AM
Last Post: Gribouillis
  "Name is not defined" when running a class lil_e 6 3,972 Jan-12-2023, 11:57 PM
Last Post: lil_e
  help RuntimeError: no running event loop marpaslight 5 3,718 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,039 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,484 Jul-27-2022, 08:50 PM
Last Post: rob101
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,480 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Strategy on updating edits back to data table and object variables hammer 0 1,196 Dec-11-2021, 02:58 PM
Last Post: hammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020