Class ConstraintPenaltyCalculator
- All Implemented Interfaces:
Serializable
This utility class exposes the adaptive penalty logic used internally by
ProductionOptimizer so that external optimizers can apply the same penalty formulation
without reimplementing it.
Usage with an external optimizer (e.g., SciPy):
// Build constraints once ConstraintPenaltyCalculator calc = new ConstraintPenaltyCalculator(); calc.addEquipmentCapacityConstraints(processSystem); calc.addConstraint(myCustomConstraint); // Inside optimizer objective: double rawObjective = computeObjective(processSystem); double penalizedObjective = calc.penalize(rawObjective, processSystem); // penalizedObjective equals rawObjective if feasible, worse if infeasible
Penalty formulation:
- Adaptively scaled by the magnitude of the raw objective to be unit-independent
- Hard/critical constraint violations: linear-in-margin penalty scaled by objective
- Soft violations: quadratic penalty proportional to margin squared
- Result is always worse than any feasible objective value
- Version:
- 1.0
- Author:
- NeqSim Development Team
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classSnapshot of a single constraint evaluation. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<ProcessConstraint> All registered constraints.private static final longSerialization version UID. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddConstraint(ProcessConstraint constraint) Adds a single constraint.addConstraints(List<? extends ProcessConstraint> constraintList) Adds all constraints from a list.Auto-discovers and adds equipment capacity constraints from a process system.static doubleapplyPenaltyFormula(double rawObjective, double[] hardMargins, double[] softMargins, double[] softWeights) Applies the shared adaptive penalty formula to a raw objective value given pre-evaluated constraint margins and severity flags.voidclear()Clears all registered constraints.evaluate(ProcessSystem process) Returns a detailed evaluation report for all constraints.double[]evaluateMargins(ProcessSystem process) Evaluates all constraints and returns a constraint margin vector.intReturns the number of registered constraints.Returns all registered constraints.booleanisFeasible(ProcessSystem process) Checks if all hard constraints are satisfied.doublepenalize(double rawObjective, ProcessSystem process) Applies the adaptive penalty formulation to a raw objective value.doubletotalPenalty(ProcessSystem process) Computes total penalty for all constraint violations.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerialization version UID.- See Also:
-
constraints
All registered constraints.
-
-
Constructor Details
-
ConstraintPenaltyCalculator
public ConstraintPenaltyCalculator()Creates an empty penalty calculator.
-
-
Method Details
-
addConstraint
Adds a single constraint.- Parameters:
constraint- the constraint to add- Returns:
- this calculator for chaining
-
addConstraints
Adds all constraints from a list.- Parameters:
constraintList- list of constraints to add- Returns:
- this calculator for chaining
-
addEquipmentCapacityConstraints
Auto-discovers and adds equipment capacity constraints from a process system.Uses
EquipmentCapacityStrategyRegistryto find all equipment constraints across the process and wraps them asCapacityConstraintAdapterinstances. This ensures external optimizers get the same physical limits that internal optimizers discover automatically.- Parameters:
process- the process system to scan for equipment constraints- Returns:
- this calculator for chaining
-
getConstraints
Returns all registered constraints.- Returns:
- unmodifiable view of constraints
-
getConstraintCount
public int getConstraintCount()Returns the number of registered constraints.- Returns:
- constraint count
-
evaluateMargins
Evaluates all constraints and returns a constraint margin vector.The returned array is suitable for NLP solvers that expect a
g(x) >= 0constraint vector. Each element corresponds to a constraint in registration order. Positive values mean satisfied; negative means violated.- Parameters:
process- the process system (must have been run)- Returns:
- array of constraint margins (same order as
getConstraints())
-
isFeasible
Checks if all hard constraints are satisfied.- Parameters:
process- the process system- Returns:
- true if no hard/critical constraint is violated
-
totalPenalty
Computes total penalty for all constraint violations.Returns 0 when all constraints are satisfied. Each violated constraint contributes its individual penalty (typically quadratic in the margin).
- Parameters:
process- the process system- Returns:
- total penalty (0 if fully feasible)
-
penalize
Applies the adaptive penalty formulation to a raw objective value.This is the same formulation used by
ProductionOptimizer: the penalty is scaled by the magnitude of the raw objective to be unit-independent. For a maximization problem, the penalized objective is always less than any feasible value when constraints are violated.Penalty components:
- Hard/Critical violations:
-penaltyBase * (1 + |margin|)per constraint - Soft violations:
-penaltyBase * weight * margin^2per constraint
- Parameters:
rawObjective- the raw (unpenalized) objective valueprocess- the process system (must have been run)- Returns:
- penalized objective (equals rawObjective if feasible, worse if infeasible)
- Hard/Critical violations:
-
evaluate
Returns a detailed evaluation report for all constraints.- Parameters:
process- the process system- Returns:
- list of constraint evaluation snapshots
-
clear
public void clear()Clears all registered constraints. -
applyPenaltyFormula
public static double applyPenaltyFormula(double rawObjective, double[] hardMargins, double[] softMargins, double[] softWeights) Applies the shared adaptive penalty formula to a raw objective value given pre-evaluated constraint margins and severity flags.This is the single source of truth for the penalty computation used by both
penalize(double, ProcessSystem)andProductionOptimizer's internal feasibility scoring. Extracting it here prevents the two formulations from diverging.- Parameters:
rawObjective- the raw (unpenalized) objective valuehardMargins- array of margins for hard/critical constraints (negative = violated)softMargins- array of margins for soft/advisory constraints (negative = violated)softWeights- array of penalty weights for each soft constraint (same length as softMargins)- Returns:
- penalized objective (equals rawObjective if all margins >= 0)
-