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

电动汽车充电站集合覆盖问题:Pulp-LP目标函数构建问询

Correct Objective Function for Your EV Charging Station Set Cover Problem

It looks like the issue in your code is that you’re trying to multiply a generator expression directly by the variable X[j], which linear programming libraries like PuLP (assuming that’s what you’re using) can’t interpret correctly. Let’s fix that and break down the right way to build your objective.

The Mathematical Objective Recap

Your goal is to maximize the total number of demand points covered by enabled charging stations. Mathematically, that’s:
$$\text{Maximize } OF = \sum_{j=1}^{J} \left( X_j \times \sum_{i=1}^{I} Y_{ij} \right)$$
This is equivalent to rearranging the sums (since addition is commutative):
$$OF = \sum_{i=1}^{I} \sum_{j=1}^{J} Y_{ij} X_j$$
Both forms are valid, but the second is often simpler to code.

Correct Code Implementations

Approach 1: Match Your Original Structure

If you want to keep the structure of summing over each station first, compute the inner sum of Y[i,j] using lpSum before multiplying by X[j]:

OptModel += lpSum( (lpSum(Y[i,j] for i in range(I)) * X[j]) for j in range(J) )

This works because lpSum(Y[i,j] for i in range(I)) creates a valid linear expression (total demand points covered by station j), which we then multiply by X[j] (whether the station is enabled) and sum over all stations.

Approach 2: Simplified Double Sum

Since the two sums can be rearranged, you can write the objective more concisely as a double sum. This is often more efficient and easier to read:

OptModel += lpSum( Y[i,j] * X[j] for i in range(I) for j in range(J) )

This directly calculates total covered demand by summing Y[i,j] * X[j] for every demand-station pair—each term is 1 only if both the station is enabled and the demand point is covered by it.

Important Constraints to Add

Don’t forget to link Y[i,j] and X[j] with valid constraints to make your model logical:

  • For every demand point i and station j, if Y[i,j] = 1 (demand i is covered by station j), then X[j] must be 1 (station j is enabled):
    for i in range(I):
        for j in range(J):
            OptModel += Y[i,j] <= X[j]
    
  • Define Y[i,j] and X[j] as binary variables (0 or 1) since they represent yes/no decisions:
    X = LpVariable.dicts("Station", range(J), cat='Binary')
    Y = LpVariable.dicts("Covered", [(i,j) for i in range(I) for j in range(J)], cat='Binary')
    

Content of the question originates from Stack Exchange, question author Dustin Smith

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 18:05:19