You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pymoo遗传算法求解孤岛微电网最优潮流:(T,n_var)型个体实现问询

实现方案

你的需求完全可以实现,将全周期所有时刻的决策变量平铺为单个一维向量作为种群个体即可,pymoo原生支持这类高维混合变量优化,具体实现步骤如下:


1. 变量编码设计

  • 总变量维度计算:你设定的周期长度T=7,单时刻有5个决策变量,因此单个个体总长度为7*5=35,按「t0的5个变量→t1的5个变量→…→t6的5个变量」顺序平铺,每个时刻内部变量顺序保持原有逻辑:[U_DG1, U_DG2, P_DG1, P_DG2, P_ESS]
  • 变量类型掩码构造:每个时刻前2个为0/1整数变量,后3个为连续实数变量,因此mask直接将["int", "int", "real", "real", "real"]重复7次即可
  • 上下界构造:同理将单时刻上下界数组重复7次拼接,单时刻下界xl_t = [0, 0, DG1_pmin*P_dg1, DG2_pmin*P_dg2, -P_ESS_inv],单时刻上界xu_t = [1, 1, P_dg1, P_dg2, P_ESS_inv]

2. 核心逻辑实现

直接在Problem类内部实现目标函数、约束的全周期批量计算,不需要逐时刻调用minimize,避免时序割裂问题:

  • 目标函数:遍历所有时刻累加运行成本即可
  • 约束条件:
    • 功率平衡约束:每个时刻单独计算残差,共7个等式约束
    • SOC约束:从初始SOC开始逐时刻更新得到全周期SOC序列,再对每个时刻SOC加上下界约束,共14个不等式约束
    • DG出力联动约束:补充启停状态和出力的联动约束(U=0时出力必须为0,避免出现非法解),共14个不等式约束

3. 完整实现代码

from pymoo.core.problem import Problem
from pymoo.factory import get_sampling, get_crossover, get_mutation, get_termination
from pymoo.operators.mixed_variable_operator import MixedVariableSampling, MixedVariableMutation, MixedVariableCrossover
from pymoo.algorithms.nsga2 import NSGA2
from pymoo.optimize import minimize
import numpy as np

# 固定参数
PV = np.array([10, 19.8, 16, 25, 7.8, 42.8, 10]) # 光伏出力,kW
Load = np.array([100, 108, 150, 150, 90, 16, 170]) # 负荷,kW
DG1_pmin = 0.3 # DG1最小负载率
DG2_pmin = 0.3 # DG2最小负载率
P_dg1 = 75 # DG1额定功率,kW
P_dg2 = 75 # DG2额定功率,kW
P_ESS_inv = 30 # 储能变流器额定功率,kW
ESS_c = 100 # 储能容量,kWh
SOC_min = 30 # SOC下限
SOC_max = 100 # SOC上限
SOC_init = 100 # 初始SOC
T = 7 # 周期长度
n_var_per_t = 5 # 单时刻变量数
total_var = T * n_var_per_t # 总变量数

# 自定义问题类
class MicrogridOPF(Problem):
    def __init__(self):
        # 构造上下界
        xl_t = [0, 0, DG1_pmin*P_dg1, DG2_pmin*P_dg2, -P_ESS_inv]
        xu_t = [1, 1, P_dg1, P_dg2, P_ESS_inv]
        xl = np.array(xl_t * T)
        xu = np.array(xu_t * T)
        super().__init__(n_var=total_var, n_obj=1, n_constr=7+14+14, xl=xl, xu=xu, vtype=float)
    
    def _evaluate(self, x, out, *args, **kwargs):
        n_pop = x.shape[0]
        objs = np.zeros(n_pop)
        constr = np.zeros((n_pop, 7+14+14))
        
        for i in range(n_pop):
            ind = x[i]
            total_cost = 0
            soc = SOC_init
            for t in range(T):
                # 提取当前时刻变量
                base = t * n_var_per_t
                u1 = int(ind[base])
                u2 = int(ind[base+1])
                p1 = ind[base+2]
                p2 = ind[base+3]
                pess = ind[base+4]
                
                # 累加目标函数
                total_cost += u1*p1*200 + u2*p2*200 + abs(pess)*0.002 # 储能充放电都计成本可保留abs,根据你的需求调整
                
                # 功率平衡约束(等式约束转化为<=0形式)
                constr[i, t] = abs(Load[t] - u1*p1 - u2*p2 - pess - PV[t]) - 1e-3
                
                # DG出力联动约束:U=0时P必须为0
                constr[i, 7 + 2*t] = p1 * (1-u1) # <=0
                constr[i, 7 + 2*t +1] = p2 * (1-u2) # <=0
                
                # 更新SOC
                soc = soc - 100 * pess / ESS_c
                # SOC上下界约束
                constr[i, 7+14 + 2*t] = SOC_min - soc # <=0
                constr[i, 7+14 + 2*t +1] = soc - SOC_max # <=0
            
            objs[i] = total_cost
        
        out["F"] = objs
        out["G"] = constr

# 构造混合变量算子
mask = ["int", "int", "real", "real", "real"] * T
sampling = MixedVariableSampling(mask, {
    "real": get_sampling("real_random"),
    "int": get_sampling("int_random")
})
crossover = MixedVariableCrossover(mask, {
    "real": get_crossover("real_sbx", prob=1.0, eta=3.0),
    "int": get_crossover("int_sbx", prob=1.0, eta=3.0)
})
mutation = MixedVariableMutation(mask, {
    "real": get_mutation("real_pm", eta=3.0),
    "int": get_mutation("int_pm", eta=3.0)
})

# 配置算法
algorithm = NSGA2(
    pop_size=300,
    sampling=sampling,
    crossover=crossover,
    mutation=mutation,
    eliminate_duplicates=True
)

# 求解
problem = MicrogridOPF()
res = minimize(
    problem,
    algorithm,
    termination=get_termination("n_gen", 800),
    seed=1,
    save_history=True,
    verbose=True
)

# 结果解析
opt_x = res.X
opt_x = opt_x.reshape(T, n_var_per_t) # 还原为(T,5)的时序结构
print("各时刻决策变量:")
print(opt_x)

4. 调优建议

  • 维度升高后可适当调大种群规模和迭代代数,保证解的收敛性
  • 如果需要考虑DG启停成本、爬坡约束等时序相关约束,直接在_evaluate方法内添加对应逻辑即可,不需要修改编码结构

内容的提问来源于stack exchange,提问作者furious_bilbo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 08:06:02