蟒蛇。在循环中重新分配列表的问题

时间:2019-05-12 16:27:05

标签: python list loops arraylist indexing

我的MAIN函数中的代码仅在运行一次时才起作用,但是当我尝试在for循环中运行时它不起作用。我认为在每次迭代结束时重新分配mypop列表都存在一些问题,我正在尝试先清理mypop列表,然后再使用新形成的列表重新分配mypop列表,但这无济于事。我对在Python中建立索引感到非常困惑,并且我假设我每次迭代都使用相同的引用。

def function(x, y):
    return x*x + y*y

def initial_pop():
    pop1 = np.random.uniform(p1, p2, 20)
    pop2 = np.random.uniform(q1, q2, 20)
    pop = np.c_[pop1, pop2]
    pop = pop.tolist()
    return pop




def get_random_pairs(numbers):
    pairs = []
    taken = []
    for i in range(0, 10):
        first_item = numbers[random.randrange(len(numbers))]
        while first_item in taken:
            first_item = numbers[random.randrange(len(numbers))]
        taken.append(first_item)
        second_item = numbers[random.randrange(len(numbers))]
        while second_item in taken:
            second_item = numbers[random.randrange(len(numbers))]
        taken.append(second_item)
        pairs.append([first_item, second_item])
    return pairs


def calculate_mean(population):
    func_array = []
    for x in population:
        func_array.append(function(x[0], x[1]))
    sum = 0
    for x in func_array:
        sum += x
    mean = sum/len(population)
    return mean


def shrinking(pop_, pairs_):
    shrank_pop = pop_.copy()
    pairs = pairs_.copy()
    pop = pop_.copy()
    new = []
    for pair in pairs:
        if function(pair[0][0], pair[0][1]) <= calculate_mean(pop):
            for x in shrank_pop:
                if x == pair[1]:
                    new += [[x[0] - 0.1, x[1] - 0.1]]
                if x == pair[0]:
                    new += [[x[0], x[1]]]
        if function(pair[0][0], pair[0][1]) >= calculate_mean(pop):
            for x in shrank_pop:
                if x == pair[1]:
                    new += [[x[0] + 0.1, x[1] + 0.1]]
                if x == pair[0]:
                    new += [[x[0], x[1]]]
    return new


def moving(pop_, pairs_):
    moved_pop = pop_.copy()
    pop = pop_.copy()
    pairs = pairs_.copy()
    new = []
    for pair in pairs:
        x1 = pair[0][0]
        y1 = pair[0][1]
        x2 = pair[1][0]
        y2 = pair[1][1]
        x21 = x2 + l*math.cos(angle)
        y21 = y2 + l*math.sin(angle)
        x11 = x1 + l*math.cos(angle)
        y11 = y1 + l*math.sin(angle)
        if (y21 > q2) & (x11 > p1) & (x11 <= p2):
            y11 = q2 + y11 - q2
            x11 = x21
        for x in moved_pop:
            if x == pair[0]:
                new += [[x11, y11]]
            if x == pair[1]:
                new += [[x21, y21]]
    return new


def FormNewPop(mypop):
    pairs = get_random_pairs(mypop)
    children = []
    shrinks = shrinking(mypop, pairs)
    moves = moving(mypop, pairs)

    children = shrinks + moves
    functions = []
    for x in children:
        functions.append([x[0], x[1], function(x[0], x[1])])
    functions_sorted = sorted(functions, key=operator.itemgetter(2))
    results = []
    for item in functions_sorted:
        results.append([item[0], item[1]])
    results = results[20:40]
    return results


def main():
    print("Initial Pop")
    mypop = initial_pop()
    pairs = get_random_pairs(mypop)
    shrinks = shrinking(mypop, pairs)
    moves = moving(mypop, pairs)
    print(shrinks)
    print(moves)

    for i in range(1,10):
        new = FormNewPop(mypop)
        mypop = [new[x] for x in range(20)]
        new.clear()

main()

0 个答案:

没有答案