← 返回 Skills 市场
wu-uk

casadi-ipopt-nlp

作者 wu-uk · GitHub ↗ · v0.1.0 · MIT-0
cross-platform ✓ 安全检测通过
76
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install energy-ac-optimal-power-flow-casadi-ipopt-nlp
功能描述
Nonlinear optimization with CasADi and IPOPT solver. Use when building and solving NLP problems: defining symbolic variables, adding nonlinear constraints, s...
使用说明 (SKILL.md)

CasADi + IPOPT for Nonlinear Programming

CasADi is a symbolic framework for nonlinear optimization. IPOPT is an interior-point solver for large-scale NLP.

Quick start (Linux)

apt-get update -qq && apt-get install -y -qq libgfortran5
pip install numpy==1.26.4 casadi==3.6.7

Building an NLP

1. Decision variables

import casadi as ca

n_bus, n_gen = 100, 20
Vm = ca.MX.sym("Vm", n_bus)   # Voltage magnitudes
Va = ca.MX.sym("Va", n_bus)   # Voltage angles (radians)
Pg = ca.MX.sym("Pg", n_gen)   # Real power
Qg = ca.MX.sym("Qg", n_gen)   # Reactive power

# Stack into single vector for solver
x = ca.vertcat(Vm, Va, Pg, Qg)

2. Objective function

Build symbolic expression:

# Quadratic cost: sum of c2*P^2 + c1*P + c0
obj = ca.MX(0)
for k in range(n_gen):
    obj += c2[k] * Pg[k]**2 + c1[k] * Pg[k] + c0[k]

3. Constraints

Collect constraints in lists with bounds:

g_expr = []  # Constraint expressions
lbg = []     # Lower bounds
ubg = []     # Upper bounds

# Equality constraint: g(x) = 0
g_expr.append(some_expression)
lbg.append(0.0)
ubg.append(0.0)

# Inequality constraint: g(x) \x3C= limit
g_expr.append(another_expression)
lbg.append(-ca.inf)
ubg.append(limit)

# Two-sided: lo \x3C= g(x) \x3C= hi
g_expr.append(bounded_expression)
lbg.append(lo)
ubg.append(hi)

g = ca.vertcat(*g_expr)

4. Variable bounds

# Stack bounds matching variable order
lbx = np.concatenate([Vm_min, Va_min, Pg_min, Qg_min]).tolist()
ubx = np.concatenate([Vm_max, Va_max, Pg_max, Qg_max]).tolist()

5. Create and call solver

nlp = {"x": x, "f": obj, "g": g}
opts = {
    "ipopt.print_level": 0,
    "ipopt.max_iter": 2000,
    "ipopt.tol": 1e-7,
    "ipopt.acceptable_tol": 1e-5,
    "ipopt.mu_strategy": "adaptive",
    "print_time": False,
}
solver = ca.nlpsol("solver", "ipopt", nlp, opts)

sol = solver(x0=x0, lbx=lbx, ubx=ubx, lbg=lbg, ubg=ubg)
x_opt = np.array(sol["x"]).flatten()
obj_val = float(sol["f"])

IPOPT options (tuning guide)

Option Default Recommendation Notes
tol 1e-8 1e-7 Convergence tolerance
acceptable_tol 1e-6 1e-5 Fallback if tol not reached
max_iter 3000 2000 Increase for hard problems
mu_strategy monotone adaptive Better for nonconvex
print_level 5 0 Quiet output

Initialization matters

Nonlinear solvers are sensitive to starting points. Use multiple initializations:

initializations = [x0_from_data, x0_flat_start]
best_sol = None

for x0 in initializations:
    try:
        sol = solver(x0=x0, lbx=lbx, ubx=ubx, lbg=lbg, ubg=ubg)
        if best_sol is None or float(sol["f"]) \x3C float(best_sol["f"]):
            best_sol = sol
    except Exception:
        continue

if best_sol is None:
    raise RuntimeError("Solver failed from all initializations")

Good initialization strategies:

  • Data-derived: Use values from input data, clipped to bounds
  • Flat start: Nominal values (e.g., Vm=1.0, Va=0.0)
  • Always enforce known constraints in initial point (e.g., reference angle = 0)

Extracting solutions

x_opt = np.array(sol["x"]).flatten()

# Unpack by slicing (must match variable order)
Vm_sol = x_opt[:n_bus]
Va_sol = x_opt[n_bus:2*n_bus]
Pg_sol = x_opt[2*n_bus:2*n_bus+n_gen]
Qg_sol = x_opt[2*n_bus+n_gen:]

Power systems patterns

Per-unit scaling

Work in per-unit internally, convert for output:

baseMVA = 100.0
Pg_pu = Pg_MW / baseMVA      # Input conversion
Pg_MW = Pg_pu * baseMVA      # Output conversion

Cost functions often expect MW, not per-unit - check the formulation.

Bus ID mapping

Power system bus numbers may not be contiguous:

bus_id_to_idx = {int(bus[i, 0]): i for i in range(n_bus)}
gen_bus_idx = bus_id_to_idx[int(gen_row[0])]

Aggregating per-bus quantities

Pg_bus = [ca.MX(0) for _ in range(n_bus)]
for k in range(n_gen):
    bus_idx = gen_bus_idx[k]
    Pg_bus[bus_idx] += Pg[k]

Common failure modes

  • Infeasible: Check bound consistency, constraint signs, unit conversions
  • Slow convergence: Try different initialization, relax tolerances temporarily
  • Wrong tap handling: MATPOWER uses tap=0 to mean 1.0, not zero
  • Angle units: Data often in degrees, solver needs radians
  • Shunt signs: Check convention for Gs (conductance) vs Bs (susceptance)
  • Over-rounding outputs: Keep high precision (≥6 decimals) in results
安全使用建议
This is a coherent, instruction-only guide for building and solving NLPs with CasADi+IPOPT. Before following the guide: (1) run the apt-get/pip steps in a controlled environment (these commands require root or virtualenv and will modify system/python packages); (2) verify the pinned package versions (numpy==1.26.4, casadi==3.6.7) are appropriate for your platform; (3) if running on a managed/hosted agent, confirm you are allowed to install system packages; and (4) test the example code on a small problem first. No credentials or external endpoints are requested by the skill.
功能分析
Type: OpenClaw Skill Name: energy-ac-optimal-power-flow-casadi-ipopt-nlp Version: 0.1.0 The skill bundle provides documentation and code snippets for nonlinear optimization using CasADi and IPOPT, specifically for power systems applications. It includes standard environment setup commands (apt-get and pip install) and technical guidance on building NLP models, with no evidence of malicious intent, data exfiltration, or prompt injection.
能力评估
Purpose & Capability
The name and description (CasADi + IPOPT for NLPs and power-system patterns) match the SKILL.md content: examples, solver options, initialization strategies, and power-system-specific notes are all relevant and proportionate.
Instruction Scope
SKILL.md is a detailed how-to and stays on-topic. It includes shell commands to install system dependencies (apt-get libgfortran5) and pip packages (numpy, casadi). Those commands are expected for running CasADi/IPOPT but are operational actions the user/host must consciously execute — the instructions do not ask for unrelated files, credentials, or exfiltration.
Install Mechanism
There is no formal install spec; the document suggests running apt-get and pip. Because this is instruction-only, nothing will be written or executed automatically by the skill itself, but following the guide requires privileged package management (apt) and pip installs which the user should vet (version pins are provided for numpy and casadi).
Credentials
The skill requests no environment variables, credentials, or config paths. This is appropriate for a client-side how-to that only needs local Python packages and system libs.
Persistence & Privilege
The skill is not always-enabled and does not request persistent agent privileges, nor does it instruct modifying other skill configs or storing credentials. Autonomous invocation is allowed by platform default but the skill contains no automatic actions that would abuse that.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install energy-ac-optimal-power-flow-casadi-ipopt-nlp
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /energy-ac-optimal-power-flow-casadi-ipopt-nlp 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v0.1.0
Bulk publish from all-task-skills-dedup
元数据
Slug energy-ac-optimal-power-flow-casadi-ipopt-nlp
版本 0.1.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

casadi-ipopt-nlp 是什么?

Nonlinear optimization with CasADi and IPOPT solver. Use when building and solving NLP problems: defining symbolic variables, adding nonlinear constraints, s... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 76 次。

如何安装 casadi-ipopt-nlp?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install energy-ac-optimal-power-flow-casadi-ipopt-nlp」即可一键安装,无需额外配置。

casadi-ipopt-nlp 是免费的吗?

是的,casadi-ipopt-nlp 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

casadi-ipopt-nlp 支持哪些平台?

casadi-ipopt-nlp 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 casadi-ipopt-nlp?

由 wu-uk(@wu-uk)开发并维护,当前版本 v0.1.0。

💬 留言讨论