Class Expression
- java.lang.Object
-
- org.ojalgo.optimisation.ModelEntity<Expression>
-
- org.ojalgo.optimisation.Expression
-
- All Implemented Interfaces:
java.lang.Comparable<Expression>
,Optimisation
,Optimisation.Constraint
,Optimisation.Objective
public final class Expression extends ModelEntity<Expression>
Think of an Expression as one constraint or a component to the objective function. An expression becomes a linear expression as soon as you set a linear factor. Setting a quadratic factor turns it into a quadratic expression. If you set both linear and quadratic factors it is a compound expression, and if you set neither it is an empty expression. Currently the solvers supplied by ojAlgo can only handle linear constraint expressions. The objective function can be linear, quadratic or compound. Empty expressions makes no sense...
An expression is turned into a constraint by setting a lower and/or upper limit. Use ModelEntity.lower(Comparable), ModelEntity.upper(Comparable) or ModelEntity.level(Comparable). An expression is made part of (contributing to) the objective function by setting a contribution weight. Use ModelEntity.weight(Comparable). The contribution weight can be set to anything except zero (0.0). Often you may just want to set it to one (1.0). Other values can be used to balance multiple expressions contributing to the objective function.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.ojalgo.optimisation.Optimisation
Optimisation.Constraint, Optimisation.ConstraintType, Optimisation.Integration<M extends Optimisation.Model,S extends Optimisation.Solver>, Optimisation.Model, Optimisation.Objective, Optimisation.Options, Optimisation.ProblemStructure, Optimisation.Result, Optimisation.Sense, Optimisation.Solver, Optimisation.State
-
-
Field Summary
Fields Modifier and Type Field Description private java.math.BigDecimal
myConstant
private boolean
myInfeasible
private java.lang.Boolean
myInteger
private java.util.Map<Structure1D.IntIndex,java.math.BigDecimal>
myLinear
private ExpressionsBasedModel
myModel
private java.util.Map<Structure2D.IntRowColumn,java.math.BigDecimal>
myQuadratic
private boolean
myRedundant
private boolean
myShallowCopy
A shallow copy (typically created by presolver or integer solver) shares the Map:s holding the paramaters with other Expressions.-
Fields inherited from class org.ojalgo.optimisation.ModelEntity
PRINT, RANGE
-
-
Constructor Summary
Constructors Modifier Constructor Description (package private)
Expression(java.lang.String name, ExpressionsBasedModel model)
private
Expression(Expression entityToCopy)
protected
Expression(Expression expressionToCopy, ExpressionsBasedModel destinationModel, boolean deep)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Expression
add(int index, double value)
Expression
add(int row, int column, double value)
Expression
add(int row, int column, long value)
Expression
add(int row, int column, java.lang.Comparable<?> value)
Expression
add(int index, long value)
Expression
add(int index, java.lang.Comparable<?> value)
Expression
add(Variable variable, double value)
Expression
add(Variable variable, long value)
Expression
add(Variable variable, java.lang.Comparable<?> value)
Will add the value to this variable's factor.Expression
add(Variable variable1, Variable variable2, double value)
Expression
add(Variable variable1, Variable variable2, long value)
Expression
add(Variable variable1, Variable variable2, java.lang.Comparable<?> value)
(package private) void
addObjectiveConstant(java.math.BigDecimal value)
void
addTo(Expression target, java.math.BigDecimal scale)
protected void
appendMiddlePart(java.lang.StringBuilder builder, Access1D<java.math.BigDecimal> solution, NumberContext display)
(package private) void
appendToString(java.lang.StringBuilder builder, Access1D<java.math.BigDecimal> solution, NumberContext display)
(package private) java.math.BigDecimal
calculateSetValue(java.util.Collection<Structure1D.IntIndex> subset)
Calculates this expression's value - the subset variables' part of this expression.int
compareTo(Expression obj)
Expression
compensate(java.util.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 java.math.BigDecimal
convert(java.math.BigDecimal value, boolean adjusted)
(package private) Expression
copy(ExpressionsBasedModel destinationModel, boolean deep)
(package private) long
countIntegerFactors()
(package private) int
countLinearFactors()
(package private) int
countQuadraticFactors()
(package private) int
deriveAdjustmentExponent()
protected void
destroy()
private Expression
doAdd(Structure1D.IntIndex key, java.math.BigDecimal value)
private Expression
doAdd(Structure2D.IntRowColumn key, java.math.BigDecimal value)
(package private) void
doIntegerRounding()
Assumes at least 1 variable, and all variables integer!(package private) void
doIntegerRounding(java.util.Set<Structure1D.IntIndex> remaining, java.math.BigDecimal lower, java.math.BigDecimal upper)
(package private) Expression
doMixedIntegerRounding()
(package private) Expression
doSet(Structure1D.IntIndex key, java.math.BigDecimal value)
(package private) Expression
doSet(Structure2D.IntRowColumn key, java.math.BigDecimal value)
double
doubleValue(Structure1D.IntIndex key, boolean adjusted)
double
doubleValue(Structure2D.IntRowColumn key, boolean adjusted)
void
enforce(NumberContext enforcer)
java.math.BigDecimal
evaluate(Access1D<java.math.BigDecimal> point)
java.math.BigDecimal
get(Variable variable)
java.math.BigDecimal
get(Structure1D.IntIndex key)
java.math.BigDecimal
get(Structure1D.IntIndex key, boolean adjusted)
java.math.BigDecimal
get(Structure2D.IntRowColumn key)
java.math.BigDecimal
get(Structure2D.IntRowColumn key, boolean adjusted)
MatrixStore<java.lang.Double>
getAdjustedGradient(Access1D<?> point)
MatrixStore<java.lang.Double>
getAdjustedHessian()
(package private) java.util.Set<Variable>
getBinaryVariables(java.util.Set<Structure1D.IntIndex> subset)
private java.math.BigDecimal
getConstant()
(package private) java.util.Map<Structure1D.IntIndex,java.math.BigDecimal>
getLinear()
java.util.Set<java.util.Map.Entry<Structure1D.IntIndex,java.math.BigDecimal>>
getLinearEntrySet()
java.util.Set<Structure1D.IntIndex>
getLinearKeySet()
(package private) ExpressionsBasedModel
getModel()
(package private) java.util.Map<Structure2D.IntRowColumn,java.math.BigDecimal>
getQuadratic()
java.util.Set<java.util.Map.Entry<Structure2D.IntRowColumn,java.math.BigDecimal>>
getQuadraticEntrySet()
java.util.Set<Structure2D.IntRowColumn>
getQuadraticKeySet()
(package private) boolean
includes(Variable variable)
boolean
isAnyLinearFactorNonZero()
boolean
isAnyQuadraticFactorNonZero()
(package private) boolean
isConstantSet()
boolean
isFunctionConstant()
boolean
isFunctionLinear()
boolean
isFunctionPureQuadratic()
boolean
isFunctionQuadratic()
(package private) boolean
isInfeasible()
boolean
isInteger()
Is this entity (all involved variables) integer?boolean
isLinearAndAllBinary()
boolean
isLinearAndAllInteger()
boolean
isLinearAndAnyBinary()
boolean
isLinearAndAnyInteger()
(package private) boolean
isNegativeOn(java.util.Set<Structure1D.IntIndex> subset)
(package private) boolean
isPositiveOn(java.util.Set<Structure1D.IntIndex> subset)
(package private) boolean
isRedundant()
private AffineFunction<java.lang.Double>
makeAffineFunction()
private ConstantFunction<java.lang.Double>
makeConstantFunction()
private PureQuadraticFunction<java.lang.Double>
makePureQuadraticFunction()
private QuadraticFunction<java.lang.Double>
makeQuadraticFunction()
(package private) Variable
resolve(Structure1D.IntIndex index)
Expression
set(int index, double value)
Expression
set(int row, int column, double value)
Expression
set(int row, int column, long value)
Expression
set(int row, int column, java.lang.Comparable<?> value)
Expression
set(int index, long value)
Expression
set(int index, java.lang.Comparable<?> value)
Expression
set(Variable variable, double value)
Expression
set(Variable variable, long value)
Expression
set(Variable variable, java.lang.Comparable<?> value)
Will set (replace) the variable's factor to this valueExpression
set(Variable variable1, Variable variable2, double value)
Expression
set(Variable variable1, Variable variable2, long value)
Expression
set(Variable variable1, Variable variable2, java.lang.Comparable<?> value)
void
setCompoundFactorsOffset(java.util.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) void
setConstant(double value)
(package private) void
setConstant(long value)
(package private) void
setConstant(java.lang.Comparable<?> value)
(package private) void
setInfeasible()
(package private) void
setInteger()
void
setLinearFactors(java.util.List<Variable> variables, Access1D<?> factors)
void
setLinearFactorsSimple(java.util.List<Variable> variables)
Will set the linear factors to a simple sum expression - all factors equal 1.0.void
setQuadraticFactors(java.util.List<Variable> variables, Access2D<?> factors)
(package private) void
setRedundant()
void
tighten()
Will attempt to exploit integer property to tighten the lower and/or upper limits (integer rounding).MultiaryFunction.TwiceDifferentiable<java.lang.Double>
toFunction()
private Structure1D.IntIndex
toIntIndex(int index)
private Structure1D.IntIndex
toIntIndex(Variable variable)
private Structure2D.IntRowColumn
toIntRowColumn(int row, int column)
private Structure2D.IntRowColumn
toIntRowColumn(Variable variable1, Variable variable2)
private java.math.BigDecimal
toPositiveFraction(java.math.BigDecimal noninteger)
-
Methods inherited from class org.ojalgo.optimisation.ModelEntity
adjust, appendLeftPart, appendMiddlePart, appendRightPart, appendToString, deriveAdjustmentExponent, equals, getAdjustmentExponent, getAdjustmentExponentValue, getAdjustmentFactor, getCompensatedLowerLimit, getCompensatedLowerLimit, getCompensatedUpperLimit, getCompensatedUpperLimit, getContributionWeight, getLowerLimit, getLowerLimit, getLowerLimit, getName, getUpperLimit, getUpperLimit, getUpperLimit, hashCode, 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 Detail
-
myConstant
private java.math.BigDecimal myConstant
-
myInfeasible
private transient boolean myInfeasible
-
myInteger
private transient java.lang.Boolean myInteger
-
myLinear
private final java.util.Map<Structure1D.IntIndex,java.math.BigDecimal> myLinear
-
myModel
private final ExpressionsBasedModel myModel
-
myQuadratic
private final java.util.Map<Structure2D.IntRowColumn,java.math.BigDecimal> myQuadratic
-
myRedundant
private transient boolean myRedundant
-
myShallowCopy
private final boolean myShallowCopy
A 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 Detail
-
Expression
private Expression(Expression entityToCopy)
-
Expression
protected Expression(Expression expressionToCopy, ExpressionsBasedModel destinationModel, boolean deep)
-
Expression
Expression(java.lang.String name, ExpressionsBasedModel model)
-
-
Method Detail
-
add
public Expression add(int index, java.lang.Comparable<?> value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(int index, double value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(int row, int column, java.lang.Comparable<?> value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(int row, int column, double value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(int row, int column, long value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(int index, long value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(Variable variable, java.lang.Comparable<?> value)
Will add the value to this variable's factor.
-
add
public Expression add(Variable variable, double value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(Variable variable, long value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(Variable variable1, Variable variable2, java.lang.Comparable<?> value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(Variable variable1, Variable variable2, double value)
- See Also:
add(Variable, Comparable)
-
add
public Expression add(Variable variable1, Variable variable2, long value)
- See Also:
add(Variable, Comparable)
-
addTo
public void addTo(Expression target, java.math.BigDecimal scale)
Description copied from class:ModelEntity
- Specified by:
addTo
in classModelEntity<Expression>
- Parameters:
target
- The targetExpression
scale
- The scaling factor
-
compareTo
public int compareTo(Expression obj)
-
compensate
public Expression compensate(java.util.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. 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
public double doubleValue(Structure1D.IntIndex key, boolean adjusted)
-
doubleValue
public double doubleValue(Structure2D.IntRowColumn key, boolean adjusted)
-
enforce
public void enforce(NumberContext enforcer)
-
evaluate
public java.math.BigDecimal evaluate(Access1D<java.math.BigDecimal> point)
-
get
public java.math.BigDecimal get(Structure1D.IntIndex key)
-
get
public java.math.BigDecimal get(Structure1D.IntIndex key, boolean adjusted)
-
get
public java.math.BigDecimal get(Structure2D.IntRowColumn key)
-
get
public java.math.BigDecimal get(Structure2D.IntRowColumn key, boolean adjusted)
-
get
public java.math.BigDecimal get(Variable variable)
-
getAdjustedGradient
public MatrixStore<java.lang.Double> getAdjustedGradient(Access1D<?> point)
-
getAdjustedHessian
public MatrixStore<java.lang.Double> getAdjustedHessian()
-
getLinearEntrySet
public java.util.Set<java.util.Map.Entry<Structure1D.IntIndex,java.math.BigDecimal>> getLinearEntrySet()
-
getLinearKeySet
public java.util.Set<Structure1D.IntIndex> getLinearKeySet()
-
getQuadraticEntrySet
public java.util.Set<java.util.Map.Entry<Structure2D.IntRowColumn,java.math.BigDecimal>> getQuadraticEntrySet()
-
getQuadraticKeySet
public java.util.Set<Structure2D.IntRowColumn> getQuadraticKeySet()
-
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:ModelEntity
Is this entity (all involved variables) integer?- Specified by:
isInteger
in 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
public Expression set(int index, java.lang.Comparable<?> value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(int index, double value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(int row, int column, java.lang.Comparable<?> value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(int row, int column, double value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(int row, int column, long value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(int index, long value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(Variable variable, java.lang.Comparable<?> value)
Will set (replace) the variable's factor to this value
-
set
public Expression set(Variable variable, double value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(Variable variable, long value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(Variable variable1, Variable variable2, java.lang.Comparable<?> value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(Variable variable1, Variable variable2, double value)
- See Also:
set(Variable, Comparable)
-
set
public Expression set(Variable variable1, Variable variable2, long value)
- See Also:
set(Variable, Comparable)
-
setCompoundFactorsOffset
public void setCompoundFactorsOffset(java.util.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.- Parameters:
variables
- The relevant variablespoint
- The point to measure from
-
setLinearFactors
public void setLinearFactors(java.util.List<Variable> variables, Access1D<?> factors)
-
setLinearFactorsSimple
public void setLinearFactorsSimple(java.util.List<Variable> variables)
Will set the linear factors to a simple sum expression - all factors equal 1.0.- Parameters:
variables
- The relevant variables
-
setQuadraticFactors
public void setQuadraticFactors(java.util.List<Variable> variables, Access2D<?> factors)
-
tighten
public void tighten()
Will attempt to exploit integer property to tighten the lower and/or upper limits (integer rounding).
-
toFunction
public MultiaryFunction.TwiceDifferentiable<java.lang.Double> toFunction()
-
convert
private java.math.BigDecimal convert(java.math.BigDecimal value, boolean adjusted)
-
doAdd
private Expression doAdd(Structure1D.IntIndex key, java.math.BigDecimal value)
-
doAdd
private Expression doAdd(Structure2D.IntRowColumn key, java.math.BigDecimal value)
-
getConstant
private java.math.BigDecimal getConstant()
-
makeAffineFunction
private AffineFunction<java.lang.Double> makeAffineFunction()
-
makeConstantFunction
private ConstantFunction<java.lang.Double> makeConstantFunction()
-
makePureQuadraticFunction
private PureQuadraticFunction<java.lang.Double> makePureQuadraticFunction()
-
makeQuadraticFunction
private QuadraticFunction<java.lang.Double> makeQuadraticFunction()
-
toIntIndex
private Structure1D.IntIndex toIntIndex(int index)
-
toIntIndex
private Structure1D.IntIndex toIntIndex(Variable variable)
-
toIntRowColumn
private Structure2D.IntRowColumn toIntRowColumn(int row, int column)
-
toIntRowColumn
private Structure2D.IntRowColumn toIntRowColumn(Variable variable1, Variable variable2)
-
toPositiveFraction
private java.math.BigDecimal toPositiveFraction(java.math.BigDecimal noninteger)
-
appendMiddlePart
protected void appendMiddlePart(java.lang.StringBuilder builder, Access1D<java.math.BigDecimal> solution, NumberContext display)
-
destroy
protected void destroy()
- Overrides:
destroy
in classModelEntity<Expression>
-
addObjectiveConstant
void addObjectiveConstant(java.math.BigDecimal value)
-
appendToString
void appendToString(java.lang.StringBuilder builder, Access1D<java.math.BigDecimal> solution, NumberContext display)
-
calculateSetValue
java.math.BigDecimal calculateSetValue(java.util.Collection<Structure1D.IntIndex> subset)
Calculates this expression's value - the subset variables' part of this expression. Will never return null.
-
copy
Expression copy(ExpressionsBasedModel destinationModel, boolean deep)
-
countIntegerFactors
long countIntegerFactors()
-
countLinearFactors
int countLinearFactors()
-
countQuadraticFactors
int countQuadraticFactors()
-
deriveAdjustmentExponent
int deriveAdjustmentExponent()
- Specified by:
deriveAdjustmentExponent
in classModelEntity<Expression>
-
doIntegerRounding
void doIntegerRounding()
Assumes at least 1 variable, and all variables integer!- Specified by:
doIntegerRounding
in classModelEntity<Expression>
- See Also:
ModelEntity.doIntegerRounding()
-
doIntegerRounding
void doIntegerRounding(java.util.Set<Structure1D.IntIndex> remaining, java.math.BigDecimal lower, java.math.BigDecimal upper)
-
doMixedIntegerRounding
Expression doMixedIntegerRounding()
-
doSet
Expression doSet(Structure1D.IntIndex key, java.math.BigDecimal value)
-
doSet
Expression doSet(Structure2D.IntRowColumn key, java.math.BigDecimal value)
-
getBinaryVariables
java.util.Set<Variable> getBinaryVariables(java.util.Set<Structure1D.IntIndex> subset)
-
getLinear
java.util.Map<Structure1D.IntIndex,java.math.BigDecimal> getLinear()
-
getModel
ExpressionsBasedModel getModel()
-
getQuadratic
java.util.Map<Structure2D.IntRowColumn,java.math.BigDecimal> getQuadratic()
-
includes
boolean includes(Variable variable)
-
isConstantSet
boolean isConstantSet()
-
isInfeasible
boolean isInfeasible()
- Overrides:
isInfeasible
in classModelEntity<Expression>
-
isNegativeOn
boolean isNegativeOn(java.util.Set<Structure1D.IntIndex> subset)
- 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
boolean isPositiveOn(java.util.Set<Structure1D.IntIndex> subset)
- 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
Variable resolve(Structure1D.IntIndex index)
-
setConstant
void setConstant(java.lang.Comparable<?> value)
-
setConstant
void setConstant(double value)
-
setConstant
void setConstant(long value)
-
setInfeasible
void setInfeasible()
-
setInteger
void setInteger()
-
setRedundant
void setRedundant()
-
-