# Constraint Functions

Occasionally, you may be working with a decision problem that includes constraints (budget, time, health effects). There are two places you can tell DPL to apply a constraint: at the endpoints using the objective function and at each node using a constraint function. These constraints can be different, although in most cases, they will be the same.

For example, consider a decision problem in which the goal is to maximize the first attribute (call it Attribute1), but without letting the second attribute (call it Attribute2) exceed 150000. Assume also that Attribute1 can not be negative.

__Use the Objective Function to Impose a Constraint__

If you only want to check the constraints at the endpoints, define an objective function in the Objective & Utility dialog as follows:

Attribute2 <= 150000 ? Attribute1 : 0

This is an example of the conditional operator.

A constraint is an "if-then-else" expression that tests a condition and specifies different objective functions if the condition is true versus false. In this example, at each endpoint DPL will look at the total for Attribute2. If it is less than or equal to 150000, the endpoint value is the total for Attribute1. If Attribute2 is greater than 150000, the endpoint value is 0 (which guarantees that this path will not be chosen as the optimal path for the decision because all values of Attribute1 are positive). This makes sure that the optimal policy maximizes Attribute1 while staying within the constraints on Attribute2.

__Use the Constraint Function to Prune a Tree__

If the problem is large, you may wish to use a constraint to prune the tree. In this case, you should use the constraint function in the Objective & Utility dialog. The constraint will then be checked each time DPL starts to evaluate a node in the tree. In this example, the constraint is the same as the objective function and needs to be specified in both places. To prune the tree, the objective function and constraint function must include a halt( ) function. This function tells DPL to stop evaluating the subtree at the point it is encountered in the tree and also what value to use for the objective function value for pruned branches while rolling back the tree. Typically, if the objective is to maximize the value model, the halt function value should be very small; if to minimize, the value should be very large.

Attribute2 <= 150000 ? Attribute1 : halt(0)

Now, whenever DPL starts to evaluate a new node, it tests the constraint. If the constraint is violated, DPL stops evaluating the subtree and assigns the halt function value as the objective function value at that point in the tree.

__A Constraint Function Without an Objective Function__

What if you specify a constraint function but not a constraint in the objective function? In most cases you will get the wrong answer. Remember that the constraint is only checked when DPL is about to evaluate a new node. If the constraint would only be violated by the final get/pay expression in a path, the constraint function check would not catch it because there are no more nodes to evaluate. To catch these cases, the constraint expression must appear in the objective function.

However, if the constraint only applies to nodes, and not to the endpoints, it is appropriate to specify a constraint function without including a constraint in the objective function.

* Versions:* DPL Professional, DPL Enterprise, DPL Portfolio

*See Also*