# How To Use User Defined Variables

If you find that the standard constraints or costs in a model don’t quite capture your specific needs, you can create and define your own variables to use with costs and constraints.

#### Basic Input Example

To help in framing this discussion, let’s start with a simple example that fits into the standard input tables.

**Objectives:**

- Place a cost of $5 per unit that flows between my MFG and DC locations
- Enforce that a maximum of 1000 units of Product_1 can move between the MFG and DC locations.

We wouldn’t need to do anything special in this instance, just create policies as normal and attach a Unit Cost of 5 to the MFG > DC transportation policy. To apply the constraint, we would create a Flow Constraint that sets a Max flow of 1000 units. While the input requirements are straightforward in this instance, let’s define both objectives in terms of variables as the solver would see them.

Flow Variable: MFG_CZ_Product_1_Flow

- Place a cost of $5 per unit that flows between my MFG and DC locations
- 5 * MFG_CZ_Product_1_Flow

- Enforce that a maximum of 1000 units of Product_1 can move between the MFG and DC locations.
- MFG_CZ_Product_1_Flow <= 1000

This example is simple, but it is important to think about costs and constraints in terms of the variables that they are applied over. This becomes even more important when we want to craft our own variables.

#### Advanced Input Example

Let’s modify the constraint in the example above to now restrict the flow of Product_1 between MFG and DC to be no more than the flow of Product_2. Again, we will represent this in terms of variables as the solver will see them.

Flow Variables: MFG_CZ_Product_1_Flow, MFG_CZ_Product_2_Flow

- Enforce that the flow units of Product_1 between MFG and DC do not exceed the flow units of Product_2 between MFG and DC
- MFG_CZ_Product_1_Flow <= MFG_CZ_Product_2_Flow

We no longer have a constant on the right-hand side of our constraint – this is an issue as we have no way to input this type of a constraint requirement into the Flow Constraints table. Whenever we find ourselves expressing constraints or costs in terms of other variables that will be determined based on the model solve, we will need to make use of User Defined Variables.

#### Advanced Input – User Defined Variables

Continuing with the constraint above, let’s modify the inequality statement so that we do in fact have a constant on the right-hand side. We can do this by subtracting one of the variables from both sides of the statement – this will then leave the right-hand side as 0.

- MFG_CZ_Product_1_Flow <= MFG_CZ_Product_2_FlowSubtract MFG_CZ_Product_2_Flow from each side
- MFG_CZ_Product_1_Flow – MFG_CZ_Product_2_Flow <= 0

We now have a constraint that can be modelled but we need to be able to define the left-hand side through the User Defined Variables table. User Defined Variables are defined as a series of Terms which are all linked to the same Variable Name. Each Term can be thought of as a solver variable as we have defined them in the examples above. For each Term, we will also need to enter a Coefficient, the Type of behavior we want to be capturing, and all of the needed information in the columns that follow depending on the Type that was just selected. All of these columns are based off of the individual constraint tables, so it is helpful to think about data as if you were entering a row in the specific constraint table.

Here is how the inputs for our example would look set up as a User Defined Variable:

We can see that by using the coefficients of 1 and -1, we have now accurately built the left-hand side of our inequality statement. All that’s left is to link this to a User Defined Constraint.

#### Advanced Input – User Defined Constraints

User Defined Constraints can be used to add restrictions to the values captured by the User Defined Variables. All that is needed is to enter the corresponding Variable Name and then select the appropriate constraint type and value.

- For >= use MIN
- For <= use MAX
- For = use FIXED

Revisiting our inequality statement once more, we can see how the User Defined Constraint should be built

MFG_CZ_Product_1_Flow – MFG_CZ_Product_2_Flow <= 0