进入实验室1

with open(csv_filepath) as f:
    reader = csv.reader(f)
    lst = list(reader)
lst.pop(0)

这段代码的作用是打开一个CSV文件(csv_filepath),

然后使用csv模块的reader函数创建一个CSV文件的读取器。

接着将读取器转换为列表,并将其存储在变量lst中。

最后,代码从列表lst中移除第一个元素(通常是表头),即将其从列表中删除。

.pop()函数 

n=512
load_profile = np.asarray(lst).astype('float64').flatten()
load_profile = np.array(load_profile[:n])

 这段代码首先将一个名为n的变量设置为512。

然后,将一个名为load_profile的变量初始化为将lst转换为NumPy数组(asarray()),然后将其数据类型转换(astype())为'float64'(64位浮点数),最后将其展平为一维数组(.flatten())

接着,代码将load_profile数组的前n个元素提取出来,形成一个新的数组,并将其存储回load_profile变量中。

new_profile = np.append(new_profile, [load_profile[i]])

 这行代码的作用是将load_profile数组中索引为i的元素添加到new_profile数组的末尾。这里使用了NumPy库中的append函数来实现。

def batCapCalc(load_profile,target_thresh,n):
    tempCap = np.array([], dtype='int32')
    m = int(n/24)  # 512/24
    for i in range(m):
        temp = 0
        for j in range(24*i,24*(i+1)):
            if load_profile[j] > target_thresh:
                temp += load_profile[j] - target_thresh
        buff_cap = 0.1*temp
        tempCap = np.append(tempCap, [buff_cap + temp])
    BattCap = max(tempCap)
    return BattCap

 这段代码定义了一个名为batCapCalc的函数,

该函数接受三个参数:load_profile(负载数据的数组)、target_thresh(目标阈值)和n(数组长度)。

函数首先创建一个空的NumPy数组tempCap来存储临时容量值,数据类型为'int32'。

然后计算m的值,即n除以24的整数部分。

接着,函数使用两个嵌套的for循环来遍历负载数据load_profile。在内部循环中,如果负载数据大于目标阈值target_thresh,则将超出部分加到temp变量中

然后计算缓冲容量buff_cap,其值为temp的10%。

最后,将buff_cap和temp的总和添加到tempCap数组中。

函数返回tempCap数组中的最大值作为BattCap(电池容量)。

def cyclesCalc(load_profile,new_profile,target_battery,n):
    counter = np.array([], dtype='int32')
    c = 0
    temp = 0
    for i in range(n):
        if temp < (2*target_battery):
            temp += abs(load_profile[i] - new_profile[i])
            counter = np.append(counter,[c])
        elif temp >= (2*target_battery): 
            temp = 0
            c += 1
            counter = np.append(counter,[c])
    return counter

 这段代码定义了一个名为cyclesCalc的函数,

该函数接受四个参数:load_profile(负载数据的数组)、new_profile(新负载数据的数组)、target_battery(目标电池容量)和n(数组长度)。

函数首先创建一个空的NumPy数组counter来存储循环次数,数据类型为'int32'。

然后初始化变量c为0,temp为0。

接着,函数使用一个for循环遍历负载数据的每个元素。

在循环中,如果temp小于目标电池容量的两倍,

则将负载数据和新负载数据之差的绝对值加到temp中,并将c的值添加到counter数组中。

                  如果temp大于等于目标电池容量的两倍,

则将temp重置为0,增加c的值,并将c的值添加到counter数组中。

最后,函数返回counter数组,其中包含了每个循环中的计数值。 

if __name__ == "__main__":
    target_peak_shave = 0.1 # 10% peak-shaving targeted
    target_thresh = max(load_profile) - target_peak_shave*max(load_profile)
    target_battery = batCapCalc(load_profile,target_thresh,n)

    new_profile, abs_charge = NewProf(target_thresh, load_profile, target_battery, n)
    charge_cycles = cyclesCalc(load_profile,new_profile,target_battery,n)
    numCycles = charge_cycles[-1]

    old_cost = CostCalc(load_profile,n)
    new_cost = CostCalc(new_profile,n)
    percent_saving = (abs(new_cost - old_cost)/old_cost)*100

这段代码首先检查当前脚本是否作为主程序运行,即判断是否为主模块。如果是主模块,则执行以下操作:

1. 将目标峰值削减量(target_peak_shave)设置为0.1,即目标为削减10%的峰值负载。
2. 计算目标阈值(target_thresh),其值为负载数据中的最大值减去目标削减量(target_peak_shave)乘以负载数据中的最大值。
3. 调用batCapCalc函数计算目标电池容量(target_battery),并将结果存储在target_battery变量中。
4. 调用NewProf函数计算新的负载数据(new_profile)和绝对充电量(abs_charge)。
5. 调用cyclesCalc函数计算充电循环次数(charge_cycles),并将结果存储在charge_cycles变量中。

将最后一个循环次数存储在numCycles变量中。
6. 调用CostCalc函数计算旧负载数据的成本(old_cost)和新负载数据的成本(new_cost)。
7. 计算成本节约百分比(percent_saving),其值为新成本与旧成本之差除以旧成本的绝对值,再乘以100。

x=np.arange(0,n,1)
    thresharr = np.ones(n)*target_thresh
    plt.figure(figsize=(15,7))

 这段代码首先使用NumPy的arange函数创建一个从0到n-1的数组x,步长stride为1

(stride = 1)。

接着,创建一个长度为n的由target_thresh值组成的数组thresharr,即将target_thresh重复n次。

最后,使用Matplotlib库创建一个大小为15x7的新图形(figure)对象,用于绘制后续的图形。

我在我的Jupyter notebook中实验,不行。试了洋红和black,不行 

两阶段鲁棒优化

c_T=[canshu(3)*ones(1,24) canshu(8)*canshu(9)*ones(1,24) canshu(8)/canshu(9)*ones(1,24) zeros(1,24) ...,
   canshu(10)*ones(1,24) canshu(10)*ones(1,24) fh(2,:) -fh(2,:) zeros(1,24) zeros(1,24)];


- ones(1,24) 创建了一个 1x24 的全为1的矩阵。
- zeros(1,24) 创建了一个 1x24 的全为0的矩阵。
- fh(2,:) 表示 fh 矩阵的第二行,其中冒号表示选取该行的所有列。

根据代码片段中的描述,c_T 的定义如下:
- 前 24 列为 canshu(3) 的值构成的行向量。
- 接下来的 24 列为 canshu(8) * canshu(9) 的值构成的行向量。
- 再接下来的 24 列为 canshu(8) / canshu(9) 的值构成的行向量。
- 然后是 24 列全为0的行向量。
- 紧接着是 24 列为 canshu(10) 的值构成的行向量。
- 再之后是另一个 24 列为 canshu(10) 的值构成的行向量。
- 最后是 fh 矩阵第二行的值减去 fh 矩阵第二行的值所得到的行向量。
- 最后再接上两个 24 列全为0的行向量。

希望这能帮助解释这段代码的功能。如果有任何疑问或需要进一步解释的地方,请随时告诉我。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值