Alexander February 2016

Why is the value not changing in the objects in the Lists?

In a code that I'm trying to implement a constraint such that the total is equal to demand by using the sum and then respective division of it as you can see in the code in

self.MaX[o].x=self.MaX[o].x*DEMAND/ToT

This is where I am implementing the constraint.

However when previously i was using just direct variables without using any lists the code was working fine, but after i introduced the List i have started having this problem where the value is not changing as i expected it to be, or they way it should logically change.

You can see the ToT value after the implementation of the code should be equal to the DEMAND, but that is not happening.

class chromosome:
    def __init__(self,identifier):
        self.x=D.PDF[identifier].rvs()
        self.identifier=identifier

class individual:
    def __init__(self):
        self.MaX=[]
        for o in range(3):
            self.MaX.append(chromosome(o))

        ToT=sum(chromosome.x for chromosome in self.MaX)
        print("before changing total is {}".format(ToT))

        for i in range(D.COEF.shape[1]):
            print("Individual values before {}".format(self.MaX[o].x))
            self.MaX[o].x=self.MaX[o].x*DEMAND/ToT
            print("Individual values after {}".format(self.MaX[o].x))


        ToT=sum(chromosome.x for chromosome in self.MaX)
        print("after changing total is {}".format(ToT))

Here D.PDF is a list of custom probability distribution created in the following manner.

from scipy import stats
import scipy.ndimage
for j in range(3):
        D.space[j]=np.ones(DEMAND, dtype=np.float32)
        D.space[j]=scipy.ndimage.filters.gaussian_filter1d(D.space[j],10)
        D.space[j]=D.space[j]/D.space[j].sum()
        D.PDF[j] = stats.rv_discrete(name='pdfX'.format(j), values=(np.arange(DEMAND), D.space[j]))

Please Help with what is going wrong. I don't see any logical mistakes,

Answers


keithb February 2016

In your second for loop, your variable is i, yet you're referencing o (from the previous for loop), so you're just overwriting the last item in self.MaX several times.

Post Status

Asked in February 2016
Viewed 3,730 times
Voted 10
Answered 1 times

Search




Leave an answer