Class ConstraintPenaltyCalculator

java.lang.Object
neqsim.process.util.optimizer.ConstraintPenaltyCalculator
All Implemented Interfaces:
Serializable

public class ConstraintPenaltyCalculator extends Object implements Serializable
Reusable penalty calculator for constrained process optimization.

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:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serialization version UID.
      See Also:
    • constraints

      private final List<ProcessConstraint> constraints
      All registered constraints.
  • Constructor Details

    • ConstraintPenaltyCalculator

      public ConstraintPenaltyCalculator()
      Creates an empty penalty calculator.
  • Method Details

    • addConstraint

      public ConstraintPenaltyCalculator addConstraint(ProcessConstraint constraint)
      Adds a single constraint.
      Parameters:
      constraint - the constraint to add
      Returns:
      this calculator for chaining
    • addConstraints

      public ConstraintPenaltyCalculator addConstraints(List<? extends ProcessConstraint> constraintList)
      Adds all constraints from a list.
      Parameters:
      constraintList - list of constraints to add
      Returns:
      this calculator for chaining
    • addEquipmentCapacityConstraints

      public ConstraintPenaltyCalculator addEquipmentCapacityConstraints(ProcessSystem process)
      Auto-discovers and adds equipment capacity constraints from a process system.

      Uses EquipmentCapacityStrategyRegistry to find all equipment constraints across the process and wraps them as CapacityConstraintAdapter instances. 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

      public List<ProcessConstraint> getConstraints()
      Returns all registered constraints.
      Returns:
      unmodifiable view of constraints
    • getConstraintCount

      public int getConstraintCount()
      Returns the number of registered constraints.
      Returns:
      constraint count
    • evaluateMargins

      public double[] evaluateMargins(ProcessSystem process)
      Evaluates all constraints and returns a constraint margin vector.

      The returned array is suitable for NLP solvers that expect a g(x) >= 0 constraint 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

      public boolean isFeasible(ProcessSystem process)
      Checks if all hard constraints are satisfied.
      Parameters:
      process - the process system
      Returns:
      true if no hard/critical constraint is violated
    • totalPenalty

      public double totalPenalty(ProcessSystem process)
      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

      public double penalize(double rawObjective, ProcessSystem process)
      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^2 per constraint
      Parameters:
      rawObjective - the raw (unpenalized) objective value
      process - the process system (must have been run)
      Returns:
      penalized objective (equals rawObjective if feasible, worse if infeasible)
    • 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) and ProductionOptimizer's internal feasibility scoring. Extracting it here prevents the two formulations from diverging.

      Parameters:
      rawObjective - the raw (unpenalized) objective value
      hardMargins - 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)