Utilities¶
Solver Discovery¶
File Loading¶
- src.Miscellaneous.load_file(file_path)[source]¶
Read a data file and convert it to a NumPy array.
Supports CSV, TXT, XLSX, and JSON formats. A
.txtfile may be separated by whitespace or by commas; a.jsonfile holds a list of rows (one per scenario) or a table in pandas’ default JSON format.- Parameters:
file_path (str) – Path to the input file. Must have a supported extension (
.csv,.txt,.xlsx,.json).- Returns:
Array containing the file data, or
Noneif the file cannot be read or has an unsupported extension (the error is printed).- Return type:
numpy.ndarray or None
Active Constraint Detection¶
These functions identify the support list — the constraints whose removal changes the optimal value — a key step for computing the scenario approach risk bounds.
- src.Miscellaneous.get_support(constraints, non_risk_constraints, prob, objective, solver=None, threshold=1e-08, sol_tol=1e-06)[source]¶
Identify the support list of a solved scenario problem.
The support list comprises every scenario constraint whose removal changes the optimal value: all genuinely violated constraints together with an irreducible subset of the active ones (cf. the support-list definition). Candidates are screened by dual value – under relaxation the dual variables of a violated constraint sum to rho, so both violated and active constraints are captured (these are the support scenarios) – then greedily pruned by re-solving and comparing the optimal value. Hard constraints are always enforced in re-solves and never counted.
Non-degeneracy is equivalent to the support scenarios forming a support list, so the prune doubles as a degeneracy test. Interior-point solvers may leave a small nonzero dual on constraints that are neither active nor violated, so each screened constraint’s activity margin is recorded at the optimum (see
_is_slack) and only the removal of a genuinely active or violated constraint raisesdegeneracy– the support scenarios are then reducible, more than one support list exists, and the lower risk bound is not certified. Removals of spuriously screened (strictly slack) constraints are discounted as solver dual noise.If the screened set fails to reproduce the optimum (inaccurate solver duals, or a degenerate instance), the support list is instead recovered by pruning the full scenario-constraint list. That recovery yields a valid support list but cannot certify it is of minimum cardinality, so
degeneracyis raised conservatively and the two-sided lower risk bound should not be trusted in that case.- Parameters:
constraints (list) – Scenario constraints only (one constraint object per scenario).
non_risk_constraints (list) – Hard constraints; always enforced in re-solves, never counted.
prob (cvxpy.Problem) – The solved CVXPY problem instance.
objective (cvxpy.Minimize) – The CVXPY objective function.
solver (str or None, optional) – CVXPY solver name.
threshold (float, optional) – Dual-value threshold for screening candidates. Default
1e-8.sol_tol (float, optional) – Relative tolerance for judging whether a re-solve reproduces the reference optimal value. Set above solver value-reproducibility (~1e-8 relative) and below the smallest meaningful support contribution. Default
1e-6.
- Returns:
complexity (int) – Cardinality of the support list (k in the scenario approach).
support (list) – The support list of CVXPY scenario constraint objects.
degeneracy (bool) –
Truewhen the support list is not certified minimal – either pruning removed a genuinely active or violated constraint (the support scenarios were reducible) or the dual screen failed and the list was recovered from the full constraint set. In both cases the lower risk bound is not certified.
- Raises:
ValueError – If no valid support list can be determined.
- src.Miscellaneous.test_support(objective, support, non_risk, ref, solver=None)[source]¶
Check whether the scenario constraints in support, together with the hard constraints in non_risk (always enforced), reproduce the reference optimal value.
Support membership is decided on the optimal value, which is well defined even when the optimizer is not unique. A scenario is of support exactly when its removal changes the optimum: a genuinely violated scenario changes it by its penalty contribution, an active support constraint by the relaxation its removal permits, while a redundant or only-spuriously-slack scenario leaves it unchanged. Comparing solution vectors instead would misfire on problems with a non-unique optimum (a flat optimal face), which is why only the value is used.
- Parameters:
objective (cvxpy.Minimize) – The original objective function.
support (list) – Candidate support list of scenario constraint objects.
non_risk (list) – Hard (non-scenario) constraints, always enforced.
ref (dict) – Reference optimum with keys
costandtol.solver (str or None, optional) – CVXPY solver name.
- Returns:
Trueif the reduced problem reproduces the reference value.- Return type:
bool