Package com.opttek.optquest
Class COptQuestStringConstraint
java.lang.Object
com.opttek.optquest.COptQuestStringConstraint
The COptQuestStringConstraint class is used to define linear and non-linear
constraints using a mathematical expression. For example,
"2*Var1 + 3*Var2 + 1.5*Var3 ≤ 100" could be used to define a budget constraint.
"Var1", "Var2" and "Var3" must be names that were assigned to COptQuestVariable
objects. If a variable name contains non-alphanumeric characters such as blanks,
the names should be enclosed in square brackets in the equation string.
For example, "2*[Var 1] + 3*[Var 2] + 1.5*[Var 3] ≤ 100"
The constraint expression can define an efficient frontier by specifying a comma separated list of right hand side values. For example, "2*Var1 + 3*Var2 + 1.5*Var3 ≤ 100,200,300". If solving an efficient frontier you must specify a stopping criterion based on number of iterations or time, or by using COptQuestOptimization.SetUserControlledVariableConstraint(true).
The following mathematical functions are supported and can be used in the mathematical expression:
The constraint expression can define an efficient frontier by specifying a comma separated list of right hand side values. For example, "2*Var1 + 3*Var2 + 1.5*Var3 ≤ 100,200,300". If solving an efficient frontier you must specify a stopping criterion based on number of iterations or time, or by using COptQuestOptimization.SetUserControlledVariableConstraint(true).
The following mathematical functions are supported and can be used in the mathematical expression:
- min(x,y) Returns the smaller value of x and y
- max(x,y) Returns the larger value of x and y
- sqrt(x) Returns the square root of x
- log(x) Returns the logarithm of x
- log10(x) Returns the base 10 logarithm of x
- pow(x,y) Returns x raise to the power y
- exp(x) Returns e raised to the power x
- abs(x) Returns the absolute value of x
- pi The mathematical constant pi (3.141159)
- e The mathematical constant e (2.718)
- rand() Returns a random number between 0 and 1
- fmod(x,y) Returns the remainder of x/y
- floor(x) Returns the largest whole number less than or equal to x
- ceil(x) Return the smallest whole number greater than or equal to x
- sin(x) Returns the sine of x where x is an angle in radians
- cos(x) Returns the cosine of x where x is an angle in radians
- tan(x) Returns the tangent of x where x is an angle in radians
- sinh(x) Returns the hyperbolic sine of x where x is an angle in radians
- cosh(x) Returns the hyperbolic cosine of x where x is an angle in radians
- tanh(x) Returns the hyperbolic tangent of x where x is an angle in radians
- asin(x) Returns the arcsine of x in the range -n/2 to n/2 radians. x is between -1 and 1.
- asin(x) Returns the arcsine of x in the range -n/2 to n/2 radians. x is between -1 and 1.
- acos(x) Returns the arccosine of x in the range 0 to n radians. x is between -1 and 1.
- atan(x) Returns the arcstangent of x in the range -pi/2 to pi/2 radians. if x is 0, atan returns 0.
- atan2(x,y) Returns the arctangent of y/x in the range -pi to pi radians. If both x and y are 0, the function returns 0.
- DtoR(x) Converts degrees to radians.
- RtoD(x) Converts radians to degrees.
-
Constructor Summary
ConstructorDescriptionDefault constructorCOptQuestStringConstraint
(String equation) Constructor that accepts the string expression that defines the constraint.COptQuestStringConstraint
(String name, String equation) Constructor that assigns a name to the constraint and the string expression that defines the constraint.COptQuestStringConstraint
(String name, String equation, boolean isGoal) Constructor that assigns a name to the constraint and the string expression that defines the constraint. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Checks the constraint expression for syntax errors.boolean
Returns true if the constraint equation should be checked for linearity.Returns the mathematical expression of the constraint.GetLHSString
(int clause) Returns the left hand side string expression associated with the clause indicated by the input parameter.GetName()
Returns the constraint name.int
Returns the number of clauses in the string equation.int
Returns the number of steps defined for an Efficient Frontier.int
If the expression is a linear expression, this method returns the number of decision variables in the constraint expression after the expression has been parsed and reduced.GetOperatorString
(int clause) Returns the operator as a string expression for the clause indicated by the input parameter.GetRHSString
(int clause) Returns the right hand side string expression associated with the clause indicated by the input parameter.boolean
Returns true if the constraint expression contains a COptQuestUserControlledVariable.boolean
Returns true if the constraint expression has an implied "And" condition.boolean
IsGoal()
Returns true if the constraint has been set as a goal rather than a hard requirement.boolean
IsLinear()
Returns true if the expression is linear.boolean
Returns true if the constraint expression contains the keyword "Or".boolean
Returns true if the constraint equation defines variable bounds.void
SetCheckLinear
(boolean checkLinear) If true, the constraint should be checked for linearity.void
SetEquation
(String equation) Sets the mathematical expression that defines the constraint.void
SetGoal
(boolean goalValue) Makes the constraint a goal where the solution is not marked as infeasible but a solution that satisfies the bounds of the constraint is considered better than a solution that violates the bounds.void
Assigns an alphanumeric name to the constraint.void
SetTolerance
(double tolerance) Set the tolerance
-
Constructor Details
-
COptQuestStringConstraint
Constructor that assigns a name to the constraint and the string expression that defines the constraint.- Parameters:
name
- - alphanumeric nameequation
- - linear or non-linear expression defining the constraintisGoal
- - if true calls SetGoal(true) on new constraint
-
COptQuestStringConstraint
Constructor that assigns a name to the constraint and the string expression that defines the constraint.- Parameters:
name
- - alphanumeric nameequation
- - linear or non-linear expression defining the constraint
-
COptQuestStringConstraint
Constructor that accepts the string expression that defines the constraint.- Parameters:
equation
- - linear or non-linear expression defining the constraint
-
COptQuestStringConstraint
public COptQuestStringConstraint()Default constructor
-
-
Method Details
-
SetName
Assigns an alphanumeric name to the constraint.- Parameters:
name
- - alphanumeric string name
-
GetName
Returns the constraint name.- Returns:
- alphanumeric constraint name
-
SetEquation
Sets the mathematical expression that defines the constraint.- Parameters:
equation
- - mathematical expression
-
GetEquation
Returns the mathematical expression of the constraint.- Returns:
- - the mathematical expression of the constraint.
-
SetGoal
public void SetGoal(boolean goalValue) Makes the constraint a goal where the solution is not marked as infeasible but a solution that satisfies the bounds of the constraint is considered better than a solution that violates the bounds.- Parameters:
goalValue
- desired goal value
-
IsGoal
public boolean IsGoal()Returns true if the constraint has been set as a goal rather than a hard requirement.- Returns:
- true if the constraint bound is to treated as a goal.
-
SetCheckLinear
public void SetCheckLinear(boolean checkLinear) If true, the constraint should be checked for linearity. Linear constraints can be evaluated before the Evaluate() method is called and solutions that violate the linear constraint can be rejected. The default value is true.- Parameters:
checkLinear
- - true if the mathematical expression should be checked for linearity. False if it should not be checked.
-
GetCheckLinear
public boolean GetCheckLinear()Returns true if the constraint equation should be checked for linearity. Returns false if the linearity check has been turned off by calling SetCheckLinear(false)- Returns:
- boolean indicating the whether linearity checking is turned on or off.
-
IsLinear
public boolean IsLinear()Returns true if the expression is linear. Returns false if it is not. This method should be called after the optimization has started or after callingCheckExpression()
. IsLinear() will return false if it is called before one of these methods.- Returns:
- true if the constraints equation is linear. Returns false if it is not linear or if it's state has not been checked.
-
IsVariable
public boolean IsVariable()Returns true if the constraint equation defines variable bounds. For example, the expression "2*Var1 + 3*Var2 + 1.5*Var3 ≤ 100, 200, 300" defines variable bounds for the right hand side.- Returns:
- true if the constraint equation defines variable bounds. return false if it does not.
-
GetNumberOfClauses
public int GetNumberOfClauses()Returns the number of clauses in the string equation. Constraints that use the logical operator "or" and constraints that use the implied "and" operator have multiple clauses. For constraints of the form 'var1 < var2 or var1 < 4', this method returns the number of constraints in the 'or' relationship. In this example, it returns 2. For constraints for the form '10 < var2+var3 < 20', it returns 2 indicating that there are 2 constraints in an implied 'and' relationship. Constraints without any logical operators, return 1.- Returns:
- the number of clauses in the constraint equation.
-
IsAndClauses
public boolean IsAndClauses()Returns true if the constraint expression has an implied "And" condition. The "and" keyword isn't supported but the expression 5 < x + y < 10 creates an "And" clause.- Returns:
- true if the constraint has an implied "And" condition.
-
IsOrClauses
public boolean IsOrClauses()Returns true if the constraint expression contains the keyword "Or". For example 5 < x Or x > 8- Returns:
- true if the constraint has "or" clauses.
-
GetRHSString
Returns the right hand side string expression associated with the clause indicated by the input parameter. For the constraint "var1 < var2 or var3 < 4", GetRHSString(1) would return "var2". The clause parameter is a 1 based value.- Parameters:
clause
- 1 based index of the clause.- Returns:
- the string representing the right hand side.
- Throws:
COptQuestException
- if a problem is encountered
-
GetLHSString
Returns the left hand side string expression associated with the clause indicated by the input parameter. For the constraint "var1 < var2 or var3 < 4", GetLHSString(1) would return "var1". The clause parameter is a 1 based value.- Parameters:
clause
- 1 based index of the clause.- Returns:
- the string representing the left hand side.
- Throws:
COptQuestException
- if a problem is encountered
-
GetOperatorString
Returns the operator as a string expression for the clause indicated by the input parameter. The clause parameter is a 1 based value.- Parameters:
clause
- 1 based index of the clause.- Returns:
- the string representing the operator.
-
GetNumberOfSteps
public int GetNumberOfSteps()Returns the number of steps defined for an Efficient Frontier.- Returns:
- 1 if no Efficient Frontier is defined. Returns the number of steps defined in an Efficient Frontier
-
CheckExpression
Checks the constraint expression for syntax errors. If the expression contains errors, a COptQuestException is thrown.- Throws:
COptQuestException
- if a problem is encountered
-
GetNumberOfVariables
public int GetNumberOfVariables()If the expression is a linear expression, this method returns the number of decision variables in the constraint expression after the expression has been parsed and reduced. For example, constraint expression is "DecVar1 + 100 = DecVar1" reduces to "100 = 0" and the number of decision variables is 0. Always returns 0 for non-linear expressions.- Returns:
- the number of unique decision variables in the liner constraint expression. return 0 for non-linear expressions.
-
HasUserVariables
public boolean HasUserVariables()Returns true if the constraint expression contains a COptQuestUserControlledVariable.- Returns:
- true if the constraint expression contains a COptQuestUserControlledVariable
-
SetTolerance
public void SetTolerance(double tolerance) Set the tolerance
-