Class ProcessSimulationEvaluator
- All Implemented Interfaces:
Serializable
This class provides a unified interface for using NeqSim process simulations with external optimization libraries such as SciPy, NLopt, Pyomo, or any gradient-based/derivative-free optimizer.
Key Features
- Single-point evaluation:
evaluate(double[] x)runs simulation and returns all outputs - Parameter definition with bounds for decision variables
- Multiple objectives support for multi-objective optimization
- Constraint handling with automatic feasibility checking
- Gradient estimation via finite differences for gradient-based optimizers
- Thread-safe evaluation with process cloning option
Python Integration Example
# Using with SciPy
from neqsim import jneqsim
from scipy.optimize import minimize
# Setup evaluator
evaluator = jneqsim.process.util.optimizer.ProcessSimulationEvaluator(process)
evaluator.addParameter("feed", "flowRate", 1000.0, 100000.0, "kg/hr")
evaluator.addObjective("power", lambda p: p.getUnit("Compressor").getPower("kW"))
evaluator.addConstraint("surge", lambda p: p.getUnit("Compressor").getSurgeMargin(), 0.1, 1.0)
# Optimize
def objective(x):
result = evaluator.evaluate(x)
return result.getObjectives()[0]
def constraint(x):
result = evaluator.evaluate(x)
return result.getConstraintMargins()
result = minimize(objective, x0=[50000], bounds=evaluator.getBoundsAsList(),
constraints={'type': 'ineq', 'fun': constraint})
- Version:
- 1.0
- Author:
- NeqSim Development Team
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDefinition of a constraint for external optimizer integration.static classResult of a single evaluation.static classDefinition of an objective function.static classDefinition of a decision variable (parameter) for optimization. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanWhether to clone process for each evaluation (thread-safe but slower).List of constraints.private intCounter for number of evaluations.private doubleStep size for finite difference gradient estimation.private double[]Last parameter values for cache validation.Last evaluation result for caching.private static final org.apache.logging.log4j.LoggerLogger.List of objective functions.List of parameter definitions.private ProcessSystemThe process system to evaluate.private static final longSerialization version UID.private booleanWhether to use relative step size for finite differences. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.ProcessSimulationEvaluator(ProcessSystem processSystem) Constructor with process system. -
Method Summary
Modifier and TypeMethodDescriptionaddConstraintEquality(String name, ToDoubleFunction<ProcessSystem> evaluator, double target, double tolerance) Adds an equality constraint: g(x) == target (within tolerance).addConstraintLowerBound(String name, ToDoubleFunction<ProcessSystem> evaluator, double lowerBound) Adds a constraint: g(x) >= lowerBound.addConstraintRange(String name, ToDoubleFunction<ProcessSystem> evaluator, double lowerBound, double upperBound) Adds a range constraint: lowerBound <= g(x) <= upperBound.addConstraintUpperBound(String name, ToDoubleFunction<ProcessSystem> evaluator, double upperBound) Adds a constraint: g(x) <= upperBound.Auto-discovers equipment capacity constraints and adds them as constraint definitions.addObjective(String name, ToDoubleFunction<ProcessSystem> evaluator) Adds an objective function to minimize.addObjective(String name, ToDoubleFunction<ProcessSystem> evaluator, ProcessSimulationEvaluator.ObjectiveDefinition.Direction direction) Adds an objective function with direction.addObjective(String name, ToDoubleFunction<ProcessSystem> evaluator, ProcessSimulationEvaluator.ObjectiveDefinition.Direction direction, double weight) Adds a weighted objective function.addParameter(String equipmentName, String propertyName, double lowerBound, double upperBound, String unit) Adds a parameter (decision variable) for optimization.addParameterWithSetter(String name, BiConsumer<ProcessSystem, Double> setter, double lowerBound, double upperBound, String unit) Adds a parameter with a custom setter function.double[][]estimateConstraintJacobian(double[] x) Estimates the Jacobian of all constraints using finite differences.double[]estimateGradient(double[] x) Estimates the gradient of the primary objective using finite differences.double[]estimateGradient(double[] x, int objectiveIndex) Estimates the gradient of a specific objective using finite differences.evaluate(double[] x) Evaluates the process for given parameter values.doubleevaluateObjective(double[] x) Evaluates just the primary objective (for simple scalar optimizers).doubleevaluatePenalizedObjective(double[] x) Evaluates the penalized objective (objective + constraint penalties).Returns all constraints as a unifiedProcessConstraintlist.double[][]Gets parameter bounds as 2D array [[lb1,ub1], [lb2,ub2], ...].intGets number of constraints.double[]getConstraintMargins(double[] x) Gets constraint margins (positive = satisfied).double[]getConstraintMarginVector(ProcessSystem process) Evaluates all constraints and returns a margin vector suitable for NLP solvers.Gets all constraint definitions.intdoubledouble[]Gets initial values array.double[]Gets lower bounds array.intGets number of objectives.Gets all objective definitions.intGets number of parameters.Gets all parameter definitions.Gets problem definition as a map (for JSON export).double[]Gets upper bounds array.booleanbooleanisFeasible(double[] x) Checks if a point is feasible.booleanvoidvoidsetCloneForEvaluation(boolean cloneForEvaluation) private voidsetEquipmentProperty(ProcessSystem process, String equipmentName, String propertyName, double value, String unit) Sets a property on equipment by name.voidsetFiniteDifferenceStep(double finiteDifferenceStep) private voidsetParameterValues(ProcessSystem process, double[] x) Sets parameter values on the process.voidsetProcessSystem(ProcessSystem processSystem) voidsetUseRelativeStep(boolean useRelativeStep) toJson()Gets problem definition as JSON string.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerialization version UID.- See Also:
-
logger
private static final org.apache.logging.log4j.Logger loggerLogger. -
processSystem
The process system to evaluate. -
parameters
List of parameter definitions. -
objectives
List of objective functions. -
constraints
List of constraints. -
finiteDifferenceStep
private double finiteDifferenceStepStep size for finite difference gradient estimation. -
useRelativeStep
private boolean useRelativeStepWhether to use relative step size for finite differences. -
cloneForEvaluation
private boolean cloneForEvaluationWhether to clone process for each evaluation (thread-safe but slower). -
evaluationCount
private int evaluationCountCounter for number of evaluations. -
lastResult
Last evaluation result for caching. -
lastParameters
private double[] lastParametersLast parameter values for cache validation.
-
-
Constructor Details
-
ProcessSimulationEvaluator
public ProcessSimulationEvaluator()Default constructor. -
ProcessSimulationEvaluator
Constructor with process system.- Parameters:
processSystem- the process system to evaluate
-
-
Method Details
-
addParameter
public ProcessSimulationEvaluator addParameter(String equipmentName, String propertyName, double lowerBound, double upperBound, String unit) Adds a parameter (decision variable) for optimization.- Parameters:
equipmentName- name of the equipmentpropertyName- property to vary (flowRate, pressure, temperature, etc.)lowerBound- lower boundupperBound- upper boundunit- unit of measurement- Returns:
- this evaluator for chaining
-
addParameterWithSetter
public ProcessSimulationEvaluator addParameterWithSetter(String name, BiConsumer<ProcessSystem, Double> setter, double lowerBound, double upperBound, String unit) Adds a parameter with a custom setter function.- Parameters:
name- parameter namesetter- function to set the parameter valuelowerBound- lower boundupperBound- upper boundunit- unit of measurement- Returns:
- this evaluator for chaining
-
getParameters
Gets all parameter definitions.- Returns:
- list of parameters
-
getParameterCount
public int getParameterCount()Gets number of parameters.- Returns:
- parameter count
-
getBounds
public double[][] getBounds()Gets parameter bounds as 2D array [[lb1,ub1], [lb2,ub2], ...].- Returns:
- bounds array
-
getLowerBounds
public double[] getLowerBounds()Gets lower bounds array.- Returns:
- lower bounds
-
getUpperBounds
public double[] getUpperBounds()Gets upper bounds array.- Returns:
- upper bounds
-
getInitialValues
public double[] getInitialValues()Gets initial values array.- Returns:
- initial values
-
addObjective
public ProcessSimulationEvaluator addObjective(String name, ToDoubleFunction<ProcessSystem> evaluator) Adds an objective function to minimize.- Parameters:
name- objective nameevaluator- function that evaluates the objective- Returns:
- this evaluator for chaining
-
addObjective
public ProcessSimulationEvaluator addObjective(String name, ToDoubleFunction<ProcessSystem> evaluator, ProcessSimulationEvaluator.ObjectiveDefinition.Direction direction) Adds an objective function with direction.- Parameters:
name- objective nameevaluator- function that evaluates the objectivedirection- MINIMIZE or MAXIMIZE- Returns:
- this evaluator for chaining
-
addObjective
public ProcessSimulationEvaluator addObjective(String name, ToDoubleFunction<ProcessSystem> evaluator, ProcessSimulationEvaluator.ObjectiveDefinition.Direction direction, double weight) Adds a weighted objective function.- Parameters:
name- objective nameevaluator- function that evaluates the objectivedirection- MINIMIZE or MAXIMIZEweight- weight for multi-objective optimization- Returns:
- this evaluator for chaining
-
getObjectives
Gets all objective definitions.- Returns:
- list of objectives
-
getObjectiveCount
public int getObjectiveCount()Gets number of objectives.- Returns:
- objective count
-
addConstraintLowerBound
public ProcessSimulationEvaluator addConstraintLowerBound(String name, ToDoubleFunction<ProcessSystem> evaluator, double lowerBound) Adds a constraint: g(x) >= lowerBound.- Parameters:
name- constraint nameevaluator- function that evaluates g(x)lowerBound- minimum allowed value- Returns:
- this evaluator for chaining
-
addConstraintUpperBound
public ProcessSimulationEvaluator addConstraintUpperBound(String name, ToDoubleFunction<ProcessSystem> evaluator, double upperBound) Adds a constraint: g(x) <= upperBound.- Parameters:
name- constraint nameevaluator- function that evaluates g(x)upperBound- maximum allowed value- Returns:
- this evaluator for chaining
-
addConstraintRange
public ProcessSimulationEvaluator addConstraintRange(String name, ToDoubleFunction<ProcessSystem> evaluator, double lowerBound, double upperBound) Adds a range constraint: lowerBound <= g(x) <= upperBound.- Parameters:
name- constraint nameevaluator- function that evaluates g(x)lowerBound- minimum valueupperBound- maximum value- Returns:
- this evaluator for chaining
-
addConstraintEquality
public ProcessSimulationEvaluator addConstraintEquality(String name, ToDoubleFunction<ProcessSystem> evaluator, double target, double tolerance) Adds an equality constraint: g(x) == target (within tolerance).- Parameters:
name- constraint nameevaluator- function that evaluates g(x)target- target valuetolerance- allowed deviation- Returns:
- this evaluator for chaining
-
getConstraints
Gets all constraint definitions.- Returns:
- list of constraints
-
getConstraintCount
public int getConstraintCount()Gets number of constraints.- Returns:
- constraint count
-
addEquipmentCapacityConstraints
Auto-discovers equipment capacity constraints and adds them as constraint definitions.Uses the
EquipmentCapacityStrategyRegistryto find all physical limits across every equipment item in the process (compressor surge, separator flooding, pipe velocity, etc.) and converts each to an upper-boundProcessSimulationEvaluator.ConstraintDefinitionwhere g(x) = utilization <= 1.0.This ensures external optimizers get exactly the same physical constraints that internal NeqSim optimizers discover automatically, without manual re-creation.
- Returns:
- this evaluator for chaining
-
getAllProcessConstraints
Returns all constraints as a unifiedProcessConstraintlist.Since
ProcessSimulationEvaluator.ConstraintDefinitionimplementsProcessConstraint, this returns the same constraints in their unified form — usable by both internal and external optimizers.- Returns:
- list of all constraints as ProcessConstraint instances
-
getConstraintMarginVector
Evaluates all constraints and returns a margin vector suitable for NLP solvers.Each element is a constraint margin: positive means satisfied, negative means violated. This is the
g(x) >= 0vector expected by standard NLP libraries.- Parameters:
process- the process system (must have been run)- Returns:
- array of constraint margins in registration order
-
evaluate
Evaluates the process for given parameter values.This is the main entry point for external optimizers. It:
- Sets all parameter values on the process
- Runs the simulation
- Evaluates all objectives and constraints
- Returns a complete result object
- Parameters:
x- array of parameter values (length must match parameter count)- Returns:
- evaluation result with objectives, constraints, and feasibility
-
setParameterValues
Sets parameter values on the process.- Parameters:
process- the process systemx- parameter values
-
setEquipmentProperty
private void setEquipmentProperty(ProcessSystem process, String equipmentName, String propertyName, double value, String unit) Sets a property on equipment by name.- Parameters:
process- the process systemequipmentName- equipment namepropertyName- property namevalue- value to setunit- unit of measurement
-
estimateGradient
public double[] estimateGradient(double[] x) Estimates the gradient of the primary objective using finite differences.- Parameters:
x- parameter values- Returns:
- gradient array
-
estimateGradient
public double[] estimateGradient(double[] x, int objectiveIndex) Estimates the gradient of a specific objective using finite differences.- Parameters:
x- parameter valuesobjectiveIndex- which objective (0-based)- Returns:
- gradient array
-
estimateConstraintJacobian
public double[][] estimateConstraintJacobian(double[] x) Estimates the Jacobian of all constraints using finite differences.- Parameters:
x- parameter values- Returns:
- Jacobian matrix [constraints x parameters]
-
evaluateObjective
public double evaluateObjective(double[] x) Evaluates just the primary objective (for simple scalar optimizers).- Parameters:
x- parameter values- Returns:
- objective value (for minimization)
-
evaluatePenalizedObjective
public double evaluatePenalizedObjective(double[] x) Evaluates the penalized objective (objective + constraint penalties).- Parameters:
x- parameter values- Returns:
- penalized objective
-
isFeasible
public boolean isFeasible(double[] x) Checks if a point is feasible.- Parameters:
x- parameter values- Returns:
- true if all constraints satisfied
-
getConstraintMargins
public double[] getConstraintMargins(double[] x) Gets constraint margins (positive = satisfied).- Parameters:
x- parameter values- Returns:
- array of constraint margins
-
getProcessSystem
-
setProcessSystem
-
getFiniteDifferenceStep
public double getFiniteDifferenceStep() -
setFiniteDifferenceStep
public void setFiniteDifferenceStep(double finiteDifferenceStep) -
isUseRelativeStep
public boolean isUseRelativeStep() -
setUseRelativeStep
public void setUseRelativeStep(boolean useRelativeStep) -
isCloneForEvaluation
public boolean isCloneForEvaluation() -
setCloneForEvaluation
public void setCloneForEvaluation(boolean cloneForEvaluation) -
getEvaluationCount
public int getEvaluationCount() -
resetEvaluationCount
public void resetEvaluationCount() -
getLastResult
-
getProblemDefinition
-
toJson
-