Allenfenqu/Code_bus_correlation.py

65 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
from test import test
#
# lenchrom = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
# print(type(lenchrom))
# 假设重复的行为 [1, 2, 3, 4]
# n = 40
# repeated_row = np.array([1, 8])
# bound = np.repeat([repeated_row], n, axis=0)
def Code(lenchrom, bound, subregion_v_bus_mean):
n = len(subregion_v_bus_mean)
repeated_row = np.array([1, 8])
bound = np.repeat([repeated_row], n, axis=0)
adr = np.load('adr.npy')
# 初始化标志变量 flag 为 0用于控制循环。
flag = 0
while flag == 0:
subregion_v_bus_mean_split = np.array_split(subregion_v_bus_mean, 8)
# 初始化一个空的列表,用于存储每个子数组的随机元素的对应值
random_elements_values = []
# 从每个子数组中随机选择一个元素
for sub_array in subregion_v_bus_mean_split:
random_element_index = np.random.choice(np.arange(sub_array.shape[0])) # 获取随机元素的索引
random_element_value = sub_array[random_element_index, 1] # 获取随机元素的对应值
random_elements_values.append(random_element_value)
# 将结果转换为numpy数组
a = np.array( random_elements_values)
a = a.astype(int)
# 创建一个长度为限制条件 bound 中的第一列元素数量的零数组 ret
ret = np.zeros(len(bound[:, 0]), dtype=int)
# 计算数组 ret 中 0 的数量,并将结果赋值给变量 x
x = np.sum(ret == 0)
# 将 i 赋值给 ret 中索引为 a(i) 的元素
for i in range(len(a)):
ret[a[i]-1] = i+1
while x > 0:
for i in range(len(a)):
try:
# 从数组 adr 中选择满足条件 ret==i 的行
add = adr[np.where(ret == i+1)[0], :]
# 找到 add 中非零元素的位置,将它们存储在 m 和 n
m, n = np.where(add != 0)
# 找到 n 中唯一的元素,并将它们存储在 add 中
add = np.unique(n)
# 找到数组 ret 中为 0 的位置,并将它们存储在 addc 中
addc = np.where(ret == 0)[0]
add = np.intersect1d(add, addc)
# 随机生成一个长度为 add 数组长度的排列,并将结果存储在变量 m 中
m = np.random.permutation(np.arange(len(add)))
add = add[m[0]]
ret[add] = i+1
except:
continue
x = np.sum(ret == 0)
flag = test(lenchrom, bound, ret)
return ret
# aaa=Code(lenchrom,bound)