Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Add `scaling=` to `add_piecewise_formulation`

未关闭
#1,080 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
python

调研方向

Start with Model.add_piecewise_formulation and the scaling handling in add_variables and add_constraints; inspect how generated constraints map to each expression pair. Define scaling behavior for the link rows and applicable domain bounds, while keeping unitless rows at factor 1. Done when callers can provide per-pair scaling without relying on generated constraint names and the behavior is covered for the supported formulations.

由索引模型根据 Issue 内容生成。

描述

enhancement

This issue was drafted with AI assistance.

Describe the feature you'd like to see

Since v0.10.0, add_variables and add_constraints accept scaling=, but Model.add_piecewise_formulation does not. Callers that build scaled models must set factors after the fact on the generated constraints, found by name via PiecewiseFormulation.constraint_names. That couples downstream code to linopy's internal naming (*_link, *_delta_bound, *_fill_order, ...), which may change with the method (sos2, incremental, lp, ...).

Context: PyPSA sets scaling factors where each variable/constraint is created (per-bus unit factors, objective factor for cost curves). Piecewise formulations are the only place where this is not possible.

import linopy
from linopy import Model

m = Model()
x = m.add_variables(lower=0, upper=1000, name="x", scaling=1e-3)
y = m.add_variables(lower=0, name="y", scaling=1e-3)
pwl = m.add_piecewise_formulation((x, [0, 500, 1000]), (y, [0, 400, 1000]), name="pwl")

print(linopy.__version__)        # 0.10.1
print(pwl.constraint_names)      # ['pwl_delta_bound', 'pwl_fill_order', 'pwl_binary_order', 'pwl_link']
print(m.constraints["pwl_link"].scaling.values)  # [1. 1.] over _pwl_var = ['x', 'y']

m.add_piecewise_formulation((x, [0, 500, 1000]), (y, [0, 400, 1000]), name="pwl2", scaling=1e-3)
# TypeError: add_piecewise_formulation() got an unexpected keyword argument 'scaling'

Note that pwl_link stacks all pairs along _pwl_var, so the rows of different expressions (e.g. MW and currency) live in one constraint and need different factors along that dimension.

Implementation idea

Accept an optional factor per pair, e.g. a third/fourth tuple element or scaling={"x": 1e-3, "y": 1e-6} keyed like _pwl_var, and apply it to the rows that carry the pair's units (the link rows and, where applicable, domain bounds). Rows that only involve unitless weights/binaries (delta_bound, fill_order, binary_order, convexity) keep factor 1. Alternatively, a default of "inherit the scaling of the expression's variables" would cover the common case without new arguments.

主要语言
Python
星标
263
派生
92
平均合并
1 天 13 小时
30 天内合并 PR
32

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

PyPSA/linopy 的其他 Issue

查看 PyPSA/linopy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。