Re: [Tutor] Encoding and Decoding
Kent, Will give this a try. Thanks for your help, Carlos Kent Johnson wrote: > OK, off the top of my head (not tested) here are some things to get > you started. > > You could write a function that would retrieve a coordinate value > given an index number, for example: > def getCoord(data, ix): > base = ix*4 > value = data[ix]*10 + data[ix+1] + data[ix+2]/10.0 > if data[ix+3] < 5: > value = -value > return value > > Now if data is your big list, you can write getCoord(data, 5) to get > the value stored at data[20] to data[23]. Similarly you could write a > setter and maybe a getXY() function that returns a pair (x, y). So > that is a place to start. > > If you want to avoid passing the list around it might make sense to > make a class to hold it. Then you would have something like > class Data(object): > def __init__(self, lst): > self.data = lst > > def getCoord(self, ix): > base = ix*4 > value = self.data[ix]*10 + self.data[ix+1] + self.data[ix+2]/10.0 > if self.data[ix+3] < 5: > value = -value > return value > > Now you can create a Data object from a list of values and ask it for > values: > d = Data() > d.getCoord(5) > > I'm not sure this is much improvement over passing around the list, > actually; you still have to pass around the Data object...it might > just be a matter of taste. > > HTH, > Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encoding and Decoding
Carlos wrote: > Hi Kent, > > I have yet to get into OO, and the GA that I'm using was done in this > way, so I can't mess with it that much. So for now yes, the list has to > be a flat element containing all this info. > > I have been reading about OO lately and a element class seems to be a > good idea, I'm working on it now, but I still don't get OO very well. My > initial idea is that a loop could iterate over the element list and > create objects with the needed parameters and hooks to the list that > link to the correct list locations. > > Could you elaborate on the converters and the class that wraps the list??? OK, off the top of my head (not tested) here are some things to get you started. You could write a function that would retrieve a coordinate value given an index number, for example: def getCoord(data, ix): base = ix*4 value = data[ix]*10 + data[ix+1] + data[ix+2]/10.0 if data[ix+3] < 5: value = -value return value Now if data is your big list, you can write getCoord(data, 5) to get the value stored at data[20] to data[23]. Similarly you could write a setter and maybe a getXY() function that returns a pair (x, y). So that is a place to start. If you want to avoid passing the list around it might make sense to make a class to hold it. Then you would have something like class Data(object): def __init__(self, lst): self.data = lst def getCoord(self, ix): base = ix*4 value = self.data[ix]*10 + self.data[ix+1] + self.data[ix+2]/10.0 if self.data[ix+3] < 5: value = -value return value Now you can create a Data object from a list of values and ask it for values: d = Data() d.getCoord(5) I'm not sure this is much improvement over passing around the list, actually; you still have to pass around the Data object...it might just be a matter of taste. HTH, Kent > > Thanks > > > > > Kent Johnson wrote: >> I will assume there is a good reason for storing the coordinates in >> this form... >> >> Do the numbers have to be all in a single list? I would start by >> breaking it up into lists of four, so if you have 10 elements you >> would have a list of 20 small lists. It might make sense to pair the x >> and y lists so you have a list of 10 lists of 2 lists of 4 numbers, e.g. >> [ [ [6, 1, 2, 3], [7, 2, 8, 4] ], ...] >> >> Another thing to consider is whether you might want to make a class to >> hold the coordinate values, then you could refer to x.tens, x.units, >> x.decimal, x.sign by name. >> >> If you need a single list for the GA to work, one alternative would be >> to make converters between the nested representation and the flat one. >> Alternately you could wrap the list in a class which provides helpful >> accessors. >> >> HTH >> Kent >> >> > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encoding and Decoding
Hi Kent, I have yet to get into OO, and the GA that I'm using was done in this way, so I can't mess with it that much. So for now yes, the list has to be a flat element containing all this info. I have been reading about OO lately and a element class seems to be a good idea, I'm working on it now, but I still don't get OO very well. My initial idea is that a loop could iterate over the element list and create objects with the needed parameters and hooks to the list that link to the correct list locations. Could you elaborate on the converters and the class that wraps the list??? Thanks Kent Johnson wrote: > I will assume there is a good reason for storing the coordinates in > this form... > > Do the numbers have to be all in a single list? I would start by > breaking it up into lists of four, so if you have 10 elements you > would have a list of 20 small lists. It might make sense to pair the x > and y lists so you have a list of 10 lists of 2 lists of 4 numbers, e.g. > [ [ [6, 1, 2, 3], [7, 2, 8, 4] ], ...] > > Another thing to consider is whether you might want to make a class to > hold the coordinate values, then you could refer to x.tens, x.units, > x.decimal, x.sign by name. > > If you need a single list for the GA to work, one alternative would be > to make converters between the nested representation and the flat one. > Alternately you could wrap the list in a class which provides helpful > accessors. > > HTH > Kent > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encoding and Decoding
Carlos wrote: > The genetic algorithm that Im using (GA) generates solutions for a given > problem, expressed in a list, this list is composed by integers. Every > element in the list takes 8 integers, is a little messy but this is because > > List [0] = Tens X position > List [1] = Units X position > List [2] = Decimals X position > List [3] = If < than 5 the number is negative, else is positive > > Then if the result is List = [6, 1, 2, 3] the X position equals -612.3. > This is the same for the Y position. If there are 10 elements the list > is going to be 80 integers long and if there are 100 elements, well you > get a very long list... > > With this in mind my question would be, how can I keep track of this > information? I mean how can I assign this List positions to each > element? This is needed because this is going to be a long list and the > GA needs to evaluate the position of each element with respect to the > position of the other elements. So it needs to know that certain numbers > are related to certain element and it needs to have access to the size, > level, name and parent information... I hope that this is clear enough. I will assume there is a good reason for storing the coordinates in this form... Do the numbers have to be all in a single list? I would start by breaking it up into lists of four, so if you have 10 elements you would have a list of 20 small lists. It might make sense to pair the x and y lists so you have a list of 10 lists of 2 lists of 4 numbers, e.g. [ [ [6, 1, 2, 3], [7, 2, 8, 4] ], ...] Another thing to consider is whether you might want to make a class to hold the coordinate values, then you could refer to x.tens, x.units, x.decimal, x.sign by name. If you need a single list for the GA to work, one alternative would be to make converters between the nested representation and the flat one. Alternately you could wrap the list in a class which provides helpful accessors. HTH Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Encoding and Decoding
Hello, I have been having problems to solve a situation in which I need to encode and decode information, hope that someone can give me a hand. The solution for this problem has gone thru many iterations, but I recently found that the long (for me) is the only one. As you might remember Im working with some elements, for example: ELEMENTS = [{'Name': 'Access', 'Parent': 'Plot', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Circulation_01', 'Parent': 'Access', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Circulation_02', 'Parent': 'Access', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Circulation_03', 'Parent': 'Access', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Int_Circ_01', 'Parent': 'Circulation_01', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Int_Circ_02', 'Parent': 'Circulation_01', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Int_Circ_03', 'Parent': 'Circulation_02', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Int_Circ_04', 'Parent': 'Circulation_02', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Int_Circ_05', 'Parent': 'Circulation_03', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}, {'Name': 'Int_Circ_06', 'Parent': 'Circulation_03', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0} ] The genetic algorithm that Im using (GA) generates solutions for a given problem, expressed in a list, this list is composed by integers. Every element in the list takes 8 integers, is a little messy but this is because List [0] = Tens X position List [1] = Units X position List [2] = Decimals X position List [3] = If < than 5 the number is negative, else is positive Then if the result is List = [6, 1, 2, 3] the X position equals -612.3. This is the same for the Y position. If there are 10 elements the list is going to be 80 integers long and if there are 100 elements, well you get a very long list... With this in mind my question would be, how can I keep track of this information? I mean how can I assign this List positions to each element? This is needed because this is going to be a long list and the GA needs to evaluate the position of each element with respect to the position of the other elements. So it needs to know that certain numbers are related to certain element and it needs to have access to the size, level, name and parent information... I hope that this is clear enough. Thanks in advance, Carlos ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor