getAllNonlinearExpressionTreesMod();
/**
* Get all the nonlinear expression tree indexes, i.e. indexes of rows (objectives or constraints) that contain nonlinear expressions.
*
* @return a pointer to an integer array of nonlinear expression tree indexes.
*/
int* getNonlinearExpressionTreeIndexes();
/**
* Get the number of unique Nonlinear exrpession tree indexes.
*
* @return the number of unique nonlinear expression tree indexes.
*/
int getNumberOfNonlinearExpressionTreeIndexes();
/**
* Get all the nonlinear expression tree indexes, i.e. indexes of rows (objetives or constraints) that contain nonlinear expressions
* after modifying the expression tree to contain quadratic terms.
*
* @return a pointer to an integer array of nonlinear expression tree indexes (including quadratic terms).
*/
int* getNonlinearExpressionTreeModIndexes();
/**
* Get the number of unique Nonlinear exrpession tree indexes after
* modifying the expression tree to contain quadratic terms.
*
* @return the number of unique nonlinear expression tree indexes (including quadratic terms).
*/
int getNumberOfNonlinearExpressionTreeModIndexes();
// the set() methods
/**
* set the instance source.
*
* @param source holds the instance source.
* @return whether the instance source is set successfully.
*/
bool setInstanceSource(std::string source);
/**
* set the instance description.
*
* @param description holds the instance description.
* @return whether the instance description is set successfully.
*/
bool setInstanceDescription(std::string description);
/**
* set the instance name.
*
* @param name holds the instance name.
* @return whether the instance name is set successfully.
*/
bool setInstanceName(std::string name);
/**
* set the variable number.
*
* @param number holds the variable number.
* @return whether the variable number is set successfully.
*/
bool setVariableNumber(int number);
/**
* add a variable. In order to use the add method, the setVariableNumber must first be called
* so that the variable number is known ahead of time to assign appropriate memory.
* If a variable with the given variable index already exists, the old variable will be replaced.
*
*
*
* @param index holds the variable index. It is required.
* @param name holds the variable name; use null or empty std::string ("") if no variable name.
* @param lowerBound holds the variable lower bound; use Double.NEGATIVE_INFINITY if no lower bound.
* @param upperBound holds the variable upper bound; use Double.POSITIVE_INFINITY if no upper bound.
* @param type holds the variable type character, B for Binary, I for Integer, S for String, C or any other char for Continuous)
* @param init holds the double variable initial value; use Double.NaN if no initial value.
* @param initString holds the std::string variable initial value; use null or empty std::string ("") if no initial std::string value.
* @return whether the variable is added successfully.
*/
bool addVariable(int index, std::string name, double lowerBound, double upperBound, char type, double init, std::string initString);
/**
* set all the variable related elements. All the previous variable-related elements will be deleted.
*
*
*
* @param number holds the number of variables. It is required.
* @param names holds a std::string array of variable names; use null if no variable names.
* @param lowerBounds holds a double array of variable lower bounds; use null if all lower bounds are 0;
* use Double.NEGATIVE_INFINITY if no lower bound for a specific variable in the array.
* @param upperBounds holds a double array of variable upper bounds; use null if no upper bounds;
* use Double.POSITIVE_INFINITY if no upper bound for a specific variable in the array.
* @param types holds a char array of variable types; use null if all variables are continuous;
* for a specfic variable in the array use B for Binary, I for Integer, S for String, C or any other char for Continuous,)
* @param inits holds a double array of varible initial values; use null if no initial values.
* @param initsString holds a std::string array of varible initial values; use null if no initial std::string values.
* @return whether the variables are set successfully.
*/
bool setVariables(int number, std::string* names, double* lowerBounds,
double* upperBounds, char* types, double* inits, std::string* initsString);
/**
* set the objective number.
*
* @param number holds the objective number.
* @return whether the objective number is set successfully.
*/
bool setObjectiveNumber(int number);
/**
* add an objective. In order to use the add method, the setObjectiveNumber must first be called
* so that the objective number is known ahead of time to assign appropriate memory.
* If a objective with the given objective index already exists, the old objective will be replaced.
* Objective index will start from -1, -2, -3, ... down, with -1 corresponding to the first objective.
*
*
*
* @param index holds the objective index. Remember the first objective index is -1, second -2, ...
* @param name holds the objective name; use null or empty std::string ("") if no objective name.
* @param maxOrMin holds the objective sense or direction; it can only take two values: "max" or "min".
* @param constant holds the objective constant; use 0.0 if no objective constant.
* @param weight holds the objective weight; use 1.0 if no objective weight.
* @param objectiveCoefficients holds the objective coefficients (null if no objective coefficients) in a sparse
* representation that holds two arrays: index array and a value array.
* @return whether the objective is added successfully.
*/
bool addObjective(int index, std::string name, std::string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
/**
* set all the objectives related elements. All the previous objective-related elements will be deleted.
*
*
*
* @param number holds the number of objectives. It is required.
* @param names holds a std::string array of objective names; use null if no objective names.
* @param maxOrMins holds a std::string array of objective objective senses or directions: "max" or "min"; use null if all objectives are "min".
* @param constants holds a double array of objective constants; use null if all objective constants are 0.0.
* @param weights holds a double array of objective weights; use null if all objective weights are 1.0.
* @param objectitiveCoefficients holds an array of objective coefficients, (null if no objective have any coefficeints)
* For each objective, the coefficients are stored in a sparse representation that holds two arrays: index array and a value array.
* If for a specific objective, there are no objecitve coefficients, use null for the corresponding array member.
* @return whether the objectives are set successfully.
*/
bool setObjectives(int number, std::string *names, std::string *maxOrMins, double *constants, double *weights, SparseVector **objectitiveCoefficients);
/**
* set the constraint number.
*
* @param number holds the constraint number.
* @return whether the constraint number is set successfully.
*/
bool setConstraintNumber(int number);
/**
* add a constraint. In order to use the add method, the setConstraintNumber must first be called
* so that the constraint number is known ahead of time to assign appropriate memory.
* If a constraint with the given constraint index already exists, the old constraint will be replaced.
*
*
*
* @param index holds the constraint index. It is required.
* @param name holds the constraint name; use null or empty std::string ("") if no constraint name.
* @param lowerBound holds the constraint lower bound; use Double.NEGATIVE_INFINITY if no lower bound.
* @param upperBound holds the constraint upper bound; use Double.POSITIVE_INFINITY if no upper bound.
* @return whether the constraint is added successfully.
*/
bool addConstraint(int index, std::string name, double lowerBound, double upperBound, double constant);
/**
* set all the constraint related elements. All the previous constraint-related elements will be deleted.
*
*
*
* @param number holds the number of constraints. It is required.
* @param names holds a std::string array of constraint names; use null if no constraint names.
* @param lowerBounds holds a double array of constraint lower bounds; use null if no lower bounds; use Double.NEGATIVE_INFINITY if no lower bound for a specific constraint in the array.
* @param upperBounds holds a double array of constraint upper bounds; use null if no upper bounds; use Double.POSITIVE_INFINITY if no upper bound for a specific constraint in the array.
* @return whether the constraints are set successfully.
*/
bool setConstraints(int number, std::string* names, double* lowerBounds, double* upperBounds, double* constants);
/**
* set linear constraint coefficients
*
*
*
* @param numberOfValues holds the number of specified coefficient values (usually nonzero) in the coefficient matrix.
* @param isColumnMajor holds whether the coefficient matrix is stored in column major (true) or row major (false).
* @param values holds a double array coefficient values in the matrix.
* @param valuesBegin holds the begin index of the values array to copy from (usually 0).
* @param valuesEnd holds the end index of the values array to copy till (usually values.lenght - 1).
* @param indexes holds an integer array column/row indexes for each value in the values array.
* @param indexesBegin holds the begin index of the indexes array to copy from (usually 0).
* @param indexesEnd holds the end index of the indexes array to copy till (usually indexes.lenght - 1).
* @param starts holds an integer array start indexes in the matrix; the first value of starts should always be 0.
* @param startsBegin holds the begin index of the starts array to copy from (usually 0).
* @param startsEnd holds the end index of the starts array to copy till (usually starts.lenght - 1).
* @return whether the linear constraint coefficients are set successfully.
*/
bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor,
double* values, int valuesBegin, int valuesEnd,
int* indexes, int indexesBegin, int indexesEnd,
int* starts, int startsBegin, int startsEnd);
/**
* set quadratic terms
*
*
*
* @param number holds the number of quadratic terms.
* @param rowIndexes holds an integer array of row indexes of all the quadratic terms.
* A negative integer corresponds to an objective row, e.g. -1 for 1st objective and -2 for 2nd.
* @param varOneIndexes holds an integer array of the first varialbe indexes of all the quadratic terms.
* @param varTwoIndexes holds an integer array of the second varialbe indexes of all the quadratic terms.
* @param coefficients holds a double array all the quadratic term coefficients.
* @param begin holds the begin index of all the arrays to copy from (usually = 0).
* @param end holds the end index of all the arrays to copy till (usually = array length -1).
* @return whether the quadratic terms are set successfully.
*/
bool setQuadraticTerms(int number,
int* rowIndexes, int* varOneIndexes, int* varTwoIndexes, double* coefficients,
int begin, int end);
/**
* set quadratic terms in nonlinearExpressions
*
*
*
* @param number holds the number of quadratic terms.
* @param rowIndexes holds an integer array of row indexes of all the quadratic terms.
* A negative integer corresponds to an objective row, e.g. -1 for 1st objective and -2 for 2nd.
* @param varOneIndexes holds an integer array of the first varialbe indexes of all the quadratic terms.
* @param varTwoIndexes holds an integer array of the second varialbe indexes of all the quadratic terms.
* @param coefficients holds a double array all the quadratic term coefficients.
* @return whether the quadratic terms are set successfully.
*/
bool setQuadraticTermsInNonlinearExpressions(int number,
int* rowIndexes, int* varOneIndexes, int* varTwoIndexes, double* coefficients);
// nonlinear API methods
/**
* Initialize the data structures for the nonlinear API
*
* @return true if we have initialized the nonlinear data strucutres.
*/
bool initializeNonLinearStructures( );
/**
* Calculate the function value for function (constraint or objective)
* indexed by idx
*
*
*
* @param idx is the index on the constraint (0, 1, 2, 3, ...) or objective function (-1, -2, -3, ...).
* @param x is a pointer (double array) to the current variable values
* @param new_x is false if any evaluation method was previously called for the current x
* has been evaluated for the current iterate x
* use a value of false if not sure
* @return the function value as a double.
*/
double calculateFunctionValue(int idx, double* x, bool new_x);
/**
* Calculate all of the constraint function values
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param objLambda is the Lagrange multiplier on the objective function
* @param conLambda is pointer (double array) of Lagrange multipliers on
* the constratins
* @param new_x is false if any evaluation method was previously called for the current x
* for the current iterate
* @param highestOrder is the highest order of the derivative being calculated
* @return a double array of constraint function values -- the size of the array is equal to getConstraintNumber().
*/
double *calculateAllConstraintFunctionValues(double* x, double *objLambda, double *conLambda,
bool new_x, int highestOrder);
/**
* Calculate all of the constraint function values, we are overloading this function
* and this version of the method will not use any AD and will evaluate function
* values from the OS Expression Tree
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @return a double array of constraint function values --
* the size of the array is equal to getConstraintNumber().
*/
double *calculateAllConstraintFunctionValues(double* x, bool new_x);
/**
* Calculate all of the objective function values
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param objLambda is the Lagrange multiplier on the objective function
* @param conLambda is pointer (double array) of Lagrange multipliers on
* the constratins
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @param highestOrder is the highest order of the derivative being calculated
* @return a double array of objective function values -- the size of the array
* is equal to getObjectiveNumber().
*/
double *calculateAllObjectiveFunctionValues(double* x, double *objLambda, double *conLambda,
bool new_x, int highestOrder);
/**
* Calculate all of the objective function values, we are overloading this function
* and this version of the method will not use any AD and will evaluate function
* values from the OS Expression Tree
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @return a double array of objective function values --
* the size of the array is equal to getObjectiveNumber().
*/
double *calculateAllObjectiveFunctionValues(double* x, bool new_x);
/**
* Calculate the gradient of all constraint functions
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param objLambda is the Lagrange multiplier on the objective function
* @param conLambda is pointer (double array) of Lagrange multipliers on
* the constratins
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @param highestOrder is the highest order of the derivative being calculated
* @return a pointer a SparseJacobianMatrix.
*/
SparseJacobianMatrix *calculateAllConstraintFunctionGradients(double* x, double *objLambda,
double *conLambda, bool new_x, int highestOrder);
/**
* Calculate the gradient of the constraint function indexed by idx
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param objLambda is the Lagrange multiplier on the objective function
* @param conLambda is pointer (double array) of Lagrange multipliers on
* the constratins
* @parma idx is the index of the constraint function gradient
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @param highestOrder is the highest order of the derivative being calculated
* @return a pointer to a sparse vector of doubles.
*/
SparseVector *calculateConstraintFunctionGradient(double* x, double *objLambda, double *conLambda,
int idx, bool new_x, int highestOrder);
/**
* Calculate the gradient of the constraint function indexed by idx
* this function is overloaded
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param idx is the index of the constraint function gradient
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @return a pointer to a sparse vector of doubles.
*/
SparseVector *calculateConstraintFunctionGradient(double* x, int idx, bool new_x );
/**
* Calculate the gradient of all objective functions
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param objLambda is the Lagrange multiplier on the objective function
* @param conLambda is pointer (double array) of Lagrange multipliers on
* the constratins
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @param highestOrder is the highest order of the derivative being calculated
* @return an array of pointer to dense objective function gradients.
*/
double **calculateAllObjectiveFunctionGradients(double* x, double *objLambda, double *conLambda,
bool new_x, int highestOrder);
/**
* Calculate the gradient of the objective function indexed by objIdx
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param objLambda is the Lagrange multiplier on the objective function
* @param conLambda is pointer (double array) of Lagrange multipliers on
* the constratins
* @parma objIdx is the index of the objective function being optimized
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @param highestOrder is the highest order of the derivative being calculated
* @return a pointer to a dense vector of doubles.
*/
double *calculateObjectiveFunctionGradient(double* x, double *objLambda, double *conLambda,
int objIdx, bool new_x, int highestOrder);
/**
* Calculate the gradient of the objective function indexed by objIdx
* this function is overloaded
*
*
*
* @param x is a pointer (double array) to the current variable values
* @parma objIdx is the index of the objective function being optimized
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @param highestOrder is the highest order of the derivative being calculated
* @return a pointer to a dense vector of doubles.
*/
double *calculateObjectiveFunctionGradient(double* x, int objIdx, bool new_x );
/**
* Calculate the Hessian of the Lagrangian Expression Tree
* This method will build the CppAD expression tree for only the first iteration
* Use this method on if the value of x does not affect the operations sequence.
*
*
*
* @param x is a pointer (double array) to the current variable values
* @param objLambda is the Lagrange multiplier on the objective function
* @param conLambda is pointer (double array) of Lagrange multipliers on
* the constratins
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @param highestOrder is the highest order of the derivative being calculated
* @return a pointer a SparseHessianMatrix.
* Each array member corresponds to one constraint gradient.
*/
SparseHessianMatrix *calculateLagrangianHessian( double* x, double *objLambda, double *conLambda,
bool new_x, int highestOrder);
/**
* Calculate the Hessian of a constraint or objective function
*
*
* @param x is a pointer (double array) to the current variable values
* @param new_x is false if any evaluation method was previously called
* for the current iterate
* @parma idx is the index of the either a constraint or objective
* function Hessian
* @return a pointer a SparseVector.
* Each array member corresponds to one constraint gradient.
*/
SparseHessianMatrix *calculateHessian( double* x, int idx, bool new_x);
/**
*
* @return true if successful in generating the constraints gradient.
*/
bool getSparseJacobianFromColumnMajor();
/**
*
* @return true if successful in generating the constraints gradient.
*/
bool getSparseJacobianFromRowMajor();
/**
* @return a pointer to the ExpressionTree for the Lagrangian function of current instance
* we only take the Lagrangian of the rows with nonlinear terms
*/
OSExpressionTree* getLagrangianExpTree( );
/**
* @return a pointer to a map of the indices of all of the variables
* that appear in the Lagrangian function
*/
std::map getAllNonlinearVariablesIndexMap( );
/**
* @return a pointer to a SparseHessianMatrix with the nonzero structure
* of the Lagrangian Expression Tree
*/
SparseHessianMatrix* getLagrangianHessianSparsityPattern();
/**
*
* @return true if successful in adding the qTerms to the ExpressionTree.
*/
bool addQTermsToExressionTree();
/**
*
* @return pointer to a SparseJacobianMatrix.
*/
SparseJacobianMatrix *getJacobianSparsityPattern();
/**
*
* duplicate the map of expression trees.
*/
void duplicateExpressionTreesMap();
/**
* F is a CppAD function the range space is the objective +
* constraints functions, x is the domeain space
*/
CppAD::ADFun *Fad;
/**
* Create the a CppAD Function object: this is a function where the domain is
* the set of variables for the problem and the range is the objective function
* plus constraints
*
*
*
* @param vdX is a vector of doubles holding the current primal variable values
* the size of x should equal instanceData->variables->numberOfVariables
* @return if successfully created
*/
bool createCppADFun(std::vector vdX );
/**
* Perform an AD forward sweep
*
*
* @param p is the highest order Taylor coefficient
* @param vdX is a vector of doubles of the current primal variable values
* the size of vdX m_iNumberOfNonlinearVariables
* @return a double vector equal to the dimension of the range space
* the result of the forward p sweep
*/
std::vector forwardAD(int p, std::vector vdX);
/**
* Perform an AD reverse sweep
*
*
*
* @param p is the order of the sweep
* @param vdlambda is a vector of doubles of the current dual (lagrange) variable values
* the size of lambda should equal number of objective functions plus number of constraints
* @return a double vector equal to the n*p
*/
std::vector reverseAD(int p, std::vector vdlambda);
/**
* end revised AD code
*/
/**
* Get the information for each iteration. Get the functions values, Jacobian
* and Hessian of the Lagrangian
*
*
*
* @param x is a pointer of doubles of primal values for the current iteration
* @param objLambda is is a pointer of doubles of the current dual (Lagrange) multipliers
* on the objective functions
* @param conLambda is a pointer of doubles of the current dual (Lagrange) multipliers
* on the constraints
* @param new_x is false if any evaluation method was previously called
* @param highestOrder is the highest order derivative to be calculated
* @return true if successful
*/
bool getIterateResults(double *x, double *objLambda, double *conLambda,
bool new_x, int highestOrder);
/**
* Calculate function values
*
*
*
* @param x is a pointer of doubles of primal values for the current iteration
* @param objLambda is is a pointer of doubles of the current dual (Lagrange) multipliers
* on the objective functions
* @param conLambda is a pointer of doubles of the current dual (Lagrange) multipliers
* on the constraints
* @return true if successful
*/
bool getZeroOrderResults(double *x, double *objLambda, double *conLambda);
/**
* Calculate first derivatives
*
*
*
* @param x is a pointer of doubles of primal values for the current iteration
* @param objLambda is is a pointer of doubles of the current dual (Lagrange) multipliers
* on the objective functions
* @param conLambda is a pointer of doubles of the current dual (Lagrange) multipliers
* on the constraints
* @return true if successful
*/
bool getFirstOrderResults(double *x, double *objLambda, double *conLambda );
/**
* Calculate second derivatives
*
*
*
* @param x is a pointer of doubles of primal values for the current iteration
* @param objLambda is is a pointer of doubles of the current dual (Lagrange) multipliers
* on the objective functions
* @param conLambda is a pointer of doubles of the current dual (Lagrange) multipliers
* on the constraints
* @return true if successful
*/
bool getSecondOrderResults(double *x, double *objLambda, double *conLambda );
/**
* This should be called by nonlinear solvers using callback functions
*
*
*
* initForAlgDiff will initialize the correct nonlinear structures in preparation
* for using the algorithmic differentiation routines.
* @return true if successful
*/
bool initForAlgDiff();
/**
* This should be called by initForAlgDiff()
*
*
*
* initObjGradients will initialize the objective function gradients to be equal to the
* coefficients given in the section of the OSiL instance
* @return true if successful
*/
bool initObjGradients();
/**
* bUseExpTreeForFunEval is set to true if you wish to use the OS Expression Tree for
* function evaluations instead of AD -- false by default.
*/
bool bUseExpTreeForFunEval;
}; //class OSInstance
#endif