From ea4ab70921626425afe8f32a27cb29d58e1fce54 Mon Sep 17 00:00:00 2001 From: David Legg Date: Thu, 1 Feb 2024 09:52:56 -0800 Subject: [PATCH] Use dynamics in LBCS, not resources The LinearBoundaryConsistencySolver, a linear solver for polynomial resources driven by a boundary-consistency algorithm, currently computes its results using resources. This involves some more indirection than is necessary. By changing LBCS to use dynamics instead of resources, we avoid that extra overhead, and improve performance somewhat. --- .../polynomial/LinearBoundaryConsistencySolver.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/polynomial/LinearBoundaryConsistencySolver.java b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/polynomial/LinearBoundaryConsistencySolver.java index 11515d3be9..8eea6498ef 100644 --- a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/polynomial/LinearBoundaryConsistencySolver.java +++ b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/polynomial/LinearBoundaryConsistencySolver.java @@ -121,7 +121,7 @@ private void solve() { while ((constraint = remainingConstraints.poll()) != null) { var V = constraint.constrainedVariable; var D = domains.get(V); - var newBound = constraint.bound.apply(domains).getDynamics().getOrThrow(); + var newBound = constraint.bound.apply(domains); boolean domainChanged = switch (constraint.comparison) { case LessThanOrEquals -> D.restrictUpper(newBound); case GreaterThanOrEquals -> D.restrictLower(newBound); @@ -313,23 +313,23 @@ private Stream directionalConstraints(Variable constraine // Expiry for driven terms is captured by re-solving rather than expiring the solution. // If solver has a feedback loop from last iteration (which is common) // feeding that expiry in here can loop the solver forever. - var result = drivenTerm; + var result = drivenTerm.getDynamics().getOrThrow(); for (var drivingVariable : drivingVariables) { var scale = controlledTerm.get(drivingVariable); var domain = domains.get(drivingVariable); var useLowerBound = (scale > 0) == (c == LessThanOrEquals); var domainBound = ExpiringMonad.map( useLowerBound ? domain.lowerBound() : domain.upperBound(), - b -> b.multiply(polynomial(-scale))); - result = add(result, () -> success(domainBound)); + polynomial(-scale)::multiply); + result = ExpiringMonad.map(result, domainBound, Polynomial::add); } - return multiply(result, constant(inverseScale)); + return ExpiringMonad.map(result, polynomial(inverseScale)::multiply); }, drivingVariables)); } } // Directional constraints are useful for arc consistency, since they have input (driving) and output (constrained) variables. // However, many directional constraints are required in general to express one General constraint. - private record DirectionalConstraint(Variable constrainedVariable, InequalityComparison comparison, Function, Resource> bound, Set drivingVariables) {} + private record DirectionalConstraint(Variable constrainedVariable, InequalityComparison comparison, Function, Expiring> bound, Set drivingVariables) {} public static final class Domain { public final Variable variable;