Constraints
Constraints are created either by a call to a specialized constraint function (see Section Predefined constraint functions) or by subsequently adding all the desired terms to a constraint. In the latter case, a new constraint is started with function XPRBnewctr by indicating its type and (optionally) its name, variable and constant terms are added with functions XPRBaddterm, XPRBsetterm and XPRBaddarrterm. Function XPRBaddterm adds the indicated coefficient value to the coefficient of the variable, whereas XPRBsetterm overrides any previously defined coefficient for the variable in the constraint. It is also possible to add an entire array of variables at once to a constraint, together with the corresponding coefficients (function XPRBaddarrterm). Figure Constraint definition gives some examples of constraint creation.
Currently defined coefficients of linear constraint terms can be retrieved with XPRBgetcoeff, for a single term, or the terms can be enumerated with XPRBgetnextterm.
- ∑i=03 si ≤ 20
- XPRBctr ctr
- XPRBnewsum(prob,"S1",s,XPRB_L,20);
-
ctr = XPRBnewctr(prob,"S1",XPRB_L);
for(i=0;i<=3;i++)
XPRBaddterm(ctr,s[i],1);
XPRBaddterm(ctr,NULL,20); - ∑i=03 Di · si = 9
- XPRBnewarrsum(prob,"S2",s,D, XPRB_E,9);
-
ctr = XPRBnewctr(prob,"S2",XPRB_E);
XPRBaddarrterm(ctr,s,D);
XPRBaddterm(ctr,NULL,9); - s0 + D0 ≤ y
- XPRBnewprec(prob,"Prc",s[0],D[0],y);
-
ctr=XPRBnewctr(prob,"Prc",XPRB_L);
XPRBaddterm(ctr,s[0],1);
XPRBaddterm(ctr,y,-1);
XPRBaddterm(ctr,NULL,-D[0]);
Figure 2.4: Constraint definition using the constraint functions provided by BCL (left column) or by adding coefficients (right column)
Since all functions for constraint definition identify the corresponding constraint via its model name, constraint definitions may be nested.
The length of constraint names is unlimited. If no name is specified the system generates default names ("CTR" followed by an index). A name may occur repeatedly and if so, BCL starts indexing the name, commencing with an index of 0. Variables and variable arrays used in the definition of a constraint must be defined previously. Any other variables not occurring in this constraint may be defined later in the model.
After a constraint has been defined, its type may be changed to a range constraint by indicating the lower and upper bounds in a call to function XPRBsetrange. Function XPRBgetbyname retrieves constraints via their name and function XPRBgetnextctr can be used to enumerate all constraints defined in a problem.
A coefficient can be deleted with XPRBdelterm, or an entire constraint definition by XPRBdelctr. It is possible to retrieve the constraint name (XPRBgetctrname), the matrix row index (XPRBgetrownum), the constraint size (XPRBgetctrsize), the constraint type (XPRBgetctrtype), the range values (XPRBgetrange, only applicable to ranged constraints) and right hand side value (XPRBgetrhs), as well as changing the constraint type (XPRBsetctrtype). A constraint can be transformed into a model cut (XPRBsetmodcut) and function XPRBgetmodcut indicates whether a constraint has been defined as a model cut. Similarly, a constraint can be transformed into a delayed row, include vars or indicator constraint with functions XPRBsetdelayed, XPRBsetincvars or XPRBsetindicator; and it can be checked if a constraint is of these types with functions XPRBgetdelayed, XPRBgetincvars and XPRBgetindicator.
In addition to the functions for handling linear constraints listed here, BCL also lets you define quadratic constraints for the formulation of QP and QCQP problems, see Section Quadratic Programming with BCL for further detail.
- Note:
- all terms that are added to a constraint must belong to the same problem as the constraint itself.
- Set objective function
-
XPRBctr c;
XPRBsetobj(prob,c);
- Set objective sense
-
XPRBsetsense(prob,XPRB_MAXIM);
- Access objective sense
-
int dir;
dir = XPRBgetsense(prob);
- Locate constraint
-
XPRBctr c;
c = XPRBgetbyname(prob,"Sum1",XPRB_CTR);
- Enumerate constraints
-
XPRBctr c = NULL;
c = XPRBgetnextctr(prob,c);
- Define range constraint
-
XPRBsetrange(c,1,5,15);
- Delete a constraint
-
XPRBdelctr(c);
- Delete a constraint term
-
XPRBvar y;
XPRBdelterm(c,y);
- Accessing constraints
-
double bdl, bdu;
XPRBgetctrname(c);
XPRBgetrange(c,&bdl,&bdu);
XPRBgetrownum(c);
XPRBgetctrsize(c);
XPRBgetctrtype(c);
XPRBsetctrtype(c,XPRB_L);
- Enumerate constraint terms
-
double coeff; const void *ref;
ref = XPRBgetnextterm(c,ref,&y,&coeff);
- Special constraints
-
XPRBgetmodcut(c);
XPRBsetmodcut(c,1);
XPRBgetdelayed(c);
XPRBsetdelayed(c,1);
XPRBgetincvars(c);
XPRBsetincvars(c,1);
XPRBgetindicator(c);
XPRBgetindvar(c);
XPRBsetindicator(c,1,y);
Figure 2.5: Defining the objective function and functions for modifying and accessing constraints
Predefined constraint functions
Besides the functions described above for defining constraints incrementally, BCL also provides some predefined constraint functions for formulating constraints `in one go'. The function XPRBnewarrsum creates a standard linear constraint with the indicated coefficients. The function XPRBnewsum creates a straight sum of the variables with each coefficient set to one. The function XPRBnewprec creates a so-called precedence constraint in which a variable plus a constant are less than or equal to a second variable (typically, this relation is established between start time variables in scheduling problems, hence the name).
Objective function
The objective function (Figure Defining the objective function and functions for modifying and accessing constraints) may be interpreted as a special type of constraint. It is defined like any other constraint, usually choosing the constraint type XPRB_N. But it is also possible to take a constraint of any other type. In the latter case, the variable terms of the constraint form the objective function but the equation or inequality expressed by the constraint also remains part of the problem. The objective function is declared via functions XPRBsetobj. If a different objective has been defined previously, it is replaced by the new choice.
The sense of the objective function can be set to be minimization (default) or maximization with function XPRBsetsense. Function XPRBgetsense returns the sense of the objective function.
All solution functions (XPRBlpoptimize, XPRBmipoptimize) and the problem output with XPRBexportprob require the objective to be defined. If the sense of the optimization has not been set, the problem is minimized by default.
© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.