Class Expression
- All Implemented Interfaces:
Comparable<Expression>, Optimisation, Optimisation.Constraint, Optimisation.Objective
Expression represents a mathematical expression in the optimization model that can serve as either a
constraint or a component of the objective function.
An Expression can be:
- Linear: Contains only linear terms (variable coefficients)
- Quadratic: Contains quadratic terms (products of variables)
- Compound: Contains both linear and quadratic terms
Basic usage:
- Set coefficients for variables with
set(Variable, Comparable)oradd(Variable, Comparable)methods - Create quadratic terms with
set(Variable, Variable, Comparable)methods - Create constraints by setting lower/upper bounds using
ModelEntity.lower(Comparable),ModelEntity.upper(Comparable), orModelEntity.level(Comparable)for equality constraints - Contribute to the objective function by setting a weight using
ModelEntity.weight(Comparable)
Advanced features include:
- Integer expression handling with automatic detection and constraint tightening
- Support for binary variables with specialized methods for detecting binary constraints
- Expression evaluation at specific variable values using
evaluate(Access1D) - Compensation for fixed variables to simplify expressions during solving
- Numerical scaling for improved solver stability
- Convenient methods for creating common expression types (sums, distances, etc.)
- Conversion to various function types for integration with different solvers
The Expression class provides a fluent interface for building expressions with method chaining. It also supports automatic handling of numerical stability issues through scaling adjustments.
Note that while the Expression class supports quadratic terms in constraints, some solvers may only support linear constraints. Quadratic, linear, or compound expressions can typically be used in the objective function depending on the solver capabilities.
-
Nested Class Summary
Nested classes/interfaces inherited from interface Optimisation
Optimisation.Constraint, Optimisation.ConstraintType, Optimisation.Integration<M,S>, Optimisation.Model, Optimisation.Objective, Optimisation.Options, Optimisation.ProblemStructure, Optimisation.Result, Optimisation.Sense, Optimisation.Solver, Optimisation.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate BigDecimalprivate booleanprivate Booleanprivate final Map<Structure1D.IntIndex, BigDecimal> private final ExpressionsBasedModelprivate final Map<Structure2D.IntRowColumn, BigDecimal> private booleanprivate final booleanA shallow copy (typically created by presolver or integer solver) shares the Map:s holding the paramaters with other Expressions.Fields inherited from class ModelEntity
PRINT, RANGE -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)Expression(String name, ExpressionsBasedModel model) protectedExpression(Expression expressionToCopy, ExpressionsBasedModel destinationModel, boolean deep) -
Method Summary
Modifier and TypeMethodDescriptionadd(double scale, Expression values) add(int index, double value) add(int row, int column, double value) add(int row, int column, long value) add(int row, int column, Comparable<?> value) add(int index, long value) add(int index, Comparable<?> value) add(long scale, Expression values) add(Comparable<?> scale, Expression values) Adds the scaled values from another Expression to this Expression.add(Variable variable, Comparable<?> value) Will add the value to this variable's factor.add(Variable variable1, Variable variable2, Comparable<?> value) (package private) voidaddAll(Set<Structure1D.IntIndex> referenced) Add all indices of referenced variables to the suppliedSet.(package private) voidaddObjectiveConstant(BigDecimal value) voidaddTo(Expression target, BigDecimal scale) protected voidappendMiddlePart(StringBuilder builder, Access1D<BigDecimal> solution, NumberContext display) (package private) voidappendToString(StringBuilder builder, Access1D<BigDecimal> solution, NumberContext display) (package private) BigDecimalCalculates this expression's value - the subset variables' part of this expression.intcompareTo(Expression obj) compensate(Set<Structure1D.IntIndex> fixedVariables) Will return an Expression with factors corresponding to fixed variables removed, and lower/upper limits compensated for the fixed part of the expression.private BigDecimalconvert(BigDecimal value, boolean adjusted) (package private) Expressioncopy(ExpressionsBasedModel destinationModel, boolean deep) (package private) long(package private) int(package private) int(package private) doubledensity()(package private) intprotected voiddestroy()private ExpressiondoAdd(BigDecimal scale, Expression values) private ExpressiondoAdd(Structure1D.IntIndex key, BigDecimal value) private ExpressiondoAdd(Structure2D.IntRowColumn key, BigDecimal value) (package private) voidAssumes at least 1 variable, and all variables integer!(package private) voiddoIntegerRounding(Set<Structure1D.IntIndex> remaining, BigDecimal lower, BigDecimal upper) (package private) Expression(package private) ExpressiondoSet(Structure1D.IntIndex key, BigDecimal value) (package private) ExpressiondoSet(Structure2D.IntRowColumn key, BigDecimal value) doubledoubleValue(Structure1D.IntIndex key, boolean adjusted) doubledoubleValue(Structure2D.IntRowColumn key, boolean adjusted) voidenforce(NumberContext enforcer) booleanevaluate(Access1D<BigDecimal> point) get(Structure1D.IntIndex key) get(Structure1D.IntIndex key, boolean adjusted) get(Structure2D.IntRowColumn key, boolean adjusted) getAdjustedGradient(Access1D<?> point) getBinaryVariables(Set<Structure1D.IntIndex> subset) private BigDecimal(package private) Map<Structure1D.IntIndex, BigDecimal> (package private) ExpressionsBasedModelgetModel()(package private) Map<Structure2D.IntRowColumn, BigDecimal> inthashCode()(package private) booleanbooleanboolean(package private) booleanbooleanbooleanbooleanboolean(package private) booleanbooleanIs this entity (all involved variables) integer?booleanbooleanbooleanboolean(package private) booleanisNegativeOn(Set<Structure1D.IntIndex> subset) (package private) booleanisPositiveOn(Set<Structure1D.IntIndex> subset) (package private) booleanprivate AffineFunction<Double> private ConstantFunction<Double> private PureQuadraticFunction<Double> private QuadraticFunction<Double> (package private) Variableresolve(Structure1D.IntIndex index) set(int index, double value) set(int row, int column, double value) set(int row, int column, long value) set(int row, int column, Comparable<?> value) set(int index, long value) set(int index, Comparable<?> value) set(Variable variable, Comparable<?> value) Will set (replace) the variable's factor to this valueset(Variable variable1, Variable variable2, Comparable<?> value) voidsetCompoundFactorsOffset(List<Variable> variables, Access1D<?> point) Will set the quadratic and linear factors to an expression that measures (the square of) the distance from the given point.(package private) voidsetConstant(double value) (package private) voidsetConstant(long value) (package private) voidsetConstant(Comparable<?> value) (package private) void(package private) voidvoidsetLinearFactors(List<Variable> variables, Access1D<?> factors) voidsetLinearFactorsSimple(List<Variable> variables) Will set the linear factors to a simple sum expression - all factors equal 1.0.voidsetQuadraticFactors(List<Variable> variables, Access2D<?> factors) (package private) voidvoidtighten()Will attempt to exploit integer property to tighten the lower and/or upper limits (integer rounding).private Structure1D.IntIndextoIntIndex(int index) private Structure1D.IntIndextoIntIndex(Variable variable) private Structure2D.IntRowColumntoIntRowColumn(int row, int column) private Structure2D.IntRowColumntoIntRowColumn(Variable variable1, Variable variable2) private BigDecimaltoPositiveFraction(BigDecimal noninteger) Methods inherited from class ModelEntity
adjust, appendLeftPart, appendMiddlePart, appendRightPart, appendToString, deriveAdjustmentExponent, getAdjustmentExponent, getAdjustmentExponentValue, getAdjustmentFactor, getCompensatedLowerLimit, getCompensatedLowerLimit, getCompensatedUpperLimit, getCompensatedUpperLimit, getContributionWeight, getLowerLimit, getLowerLimit, getLowerLimit, getName, getUpperLimit, getUpperLimit, getUpperLimit, isClosedRange, isConstraint, isContributionWeightSet, isEqualityConstraint, isInfeasible, isLowerConstraint, isLowerLimitSet, isObjective, isUpperConstraint, isUpperLimitSet, level, level, level, lower, lower, lower, reverseAdjustment, shift, toAdjusted, toBigDecimal, toString, toUnadjusted, upper, upper, upper, validate, validate, weight, weight, weight
-
Field Details
-
myConstant
-
myInfeasible
private transient boolean myInfeasible -
myInteger
-
myLinear
-
myModel
-
myQuadratic
-
myRedundant
private transient boolean myRedundant -
myShallowCopy
private final boolean myShallowCopyA shallow copy (typically created by presolver or integer solver) shares the Map:s holding the paramaters with other Expressions. They will only differ on the lower/upper limits and on meta data like flags indicating redundancy or infeasibility.
-
-
Constructor Details
-
Expression
protected Expression(Expression expressionToCopy, ExpressionsBasedModel destinationModel, boolean deep) -
Expression
Expression(String name, ExpressionsBasedModel model)
-
-
Method Details
-
add
Adds the scaled values from another Expression to this Expression. The lower and upper limits/bounds, of either involved Expression, are not affected. Nor is the objective weight. Only the factors (linear and quadratic) are scaled and added.This allows for constructs like:
Expression expr1 = model.newExpression("Expr1"); Expression expr2 = model.newExpression("Expr2"); ... model.newExpression("Expr3").add(2.0, expr1).add(-3.0, expr2).lower(0.0); -
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
add
Will add the value to this variable's factor. -
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
add
- See Also:
-
addTo
Description copied from class:ModelEntity- Specified by:
addToin classModelEntity<Expression>- Parameters:
target- The targetExpressionscale- The scaling factor
-
compareTo
-
compensate
Will return an Expression with factors corresponding to fixed variables removed, and lower/upper limits compensated for the fixed part of the expression. Factors corresponding to bilinear variables, where one is fixed and the other is not, are linearized.- Parameters:
fixedVariables- A set of (by the presolver) fixed variable indices- Returns:
- The reduced/modified expression
-
doubleValue
-
doubleValue
-
enforce
-
equals
- Overrides:
equalsin classModelEntity<Expression>
-
evaluate
-
get
-
get
-
get
-
get
-
get
-
getAdjustedGradient
-
getAdjustedHessian
-
getLinearEntrySet
-
getLinearKeySet
-
getQuadraticEntrySet
-
getQuadraticKeySet
-
hashCode
public int hashCode()- Overrides:
hashCodein classModelEntity<Expression>
-
isAnyLinearFactorNonZero
public boolean isAnyLinearFactorNonZero() -
isAnyQuadraticFactorNonZero
public boolean isAnyQuadraticFactorNonZero() -
isFunctionConstant
public boolean isFunctionConstant() -
isFunctionLinear
public boolean isFunctionLinear() -
isFunctionPureQuadratic
public boolean isFunctionPureQuadratic() -
isFunctionQuadratic
public boolean isFunctionQuadratic() -
isInteger
public boolean isInteger()Description copied from class:ModelEntityIs this entity (all involved variables) integer?- Specified by:
isIntegerin classModelEntity<Expression>
-
isLinearAndAllBinary
public boolean isLinearAndAllBinary()- Returns:
- Are all the (linear) variables binary
-
isLinearAndAllInteger
public boolean isLinearAndAllInteger()- Returns:
- Are all the (linear) variables integer
-
isLinearAndAnyBinary
public boolean isLinearAndAnyBinary()- Returns:
- Are any of the (linear) variables binary
-
isLinearAndAnyInteger
public boolean isLinearAndAnyInteger()- Returns:
- Are any of the (linear) variables integer
-
set
- See Also:
-
set
- See Also:
-
set
- See Also:
-
set
- See Also:
-
set
- See Also:
-
set
- See Also:
-
set
Will set (replace) the variable's factor to this value -
set
- See Also:
-
set
- See Also:
-
set
- See Also:
-
set
- See Also:
-
set
- See Also:
-
setCompoundFactorsOffset
-
setLinearFactors
-
setLinearFactorsSimple
-
setQuadraticFactors
-
tighten
public void tighten()Will attempt to exploit integer property to tighten the lower and/or upper limits (integer rounding). -
toFunction
-
convert
-
doAdd
-
doAdd
-
doAdd
-
getConstant
-
makeAffineFunction
-
makeConstantFunction
-
makePureQuadraticFunction
-
makeQuadraticFunction
-
toIntIndex
-
toIntIndex
-
toIntRowColumn
-
toIntRowColumn
-
toPositiveFraction
-
appendMiddlePart
protected void appendMiddlePart(StringBuilder builder, Access1D<BigDecimal> solution, NumberContext display) -
destroy
protected void destroy()- Overrides:
destroyin classModelEntity<Expression>
-
addAll
Add all indices of referenced variables to the suppliedSet. -
addObjectiveConstant
-
appendToString
-
calculateSetValue
Calculates this expression's value - the subset variables' part of this expression. Will never return null. -
copy
-
countIntegerFactors
long countIntegerFactors() -
countLinearFactors
int countLinearFactors() -
countQuadraticFactors
int countQuadraticFactors() -
density
double density() -
deriveAdjustmentExponent
int deriveAdjustmentExponent()- Specified by:
deriveAdjustmentExponentin classModelEntity<Expression>
-
doIntegerRounding
void doIntegerRounding()Assumes at least 1 variable, and all variables integer!- Specified by:
doIntegerRoundingin classModelEntity<Expression>- See Also:
-
doIntegerRounding
-
doMixedIntegerRounding
Expression doMixedIntegerRounding() -
doSet
-
doSet
-
getBinaryVariables
-
getLinear
Map<Structure1D.IntIndex, BigDecimal> getLinear() -
getModel
ExpressionsBasedModel getModel() -
getQuadratic
Map<Structure2D.IntRowColumn, BigDecimal> getQuadratic() -
includes
-
isConstantSet
boolean isConstantSet() -
isInfeasible
boolean isInfeasible()- Overrides:
isInfeasiblein classModelEntity<Expression>
-
isNegativeOn
- Parameters:
subset- The indices of a variable subset- Returns:
- true if none of the variables in the subset can make a positve contribution to the expression value
-
isPositiveOn
- Parameters:
subset- The indices of a variable subset- Returns:
- true if none of the variables in the subset can make a negative contribution to the expression value
-
isRedundant
boolean isRedundant() -
resolve
-
setConstant
-
setConstant
void setConstant(double value) -
setConstant
void setConstant(long value) -
setInfeasible
void setInfeasible() -
setInteger
void setInteger() -
setRedundant
void setRedundant()
-