# Solving your first model

**After completing this module the student should:**

- Implement and solve a simple model in OPL Studio
- Understand comments.
- Understand decision variable definitions.
- Understand comparisons in constraints.

## Video

## Transcript

### Comments

A well written model will come with comments. These will come in two forms:

1) A multi line comment:

```
/ *
Everything between the symbols on the line above and below, will be ignored.
(Delete the two spaces between the / and the * .)
* /
```

2) A single line comment

```
dvar float x1; // The text after the // will be ignored.
```

You may also recognize comments by OPL Studio coloring them green.

### Decision variables

To actually make decisions, we need to represent them. As you may recall from linear/integer programs, they are modeled using decision variables.

A decision variable is declared using the `dvar`

keyword, its type, its name, and finally a semi colon (as most lines end with).

```
dvar float x1;
dvar float+ x2;
dvar int x3;
dvar int+ x4;
dvar boolean x5;
```

The decision variable `x1`

can take any value that the computer can represent as a floating point number such as 1.0, 1982.001928, -42.5 etc. While the variable `x2`

is restricted to the subset of these that are also positive. `x3`

can only attain integer values that the computer can represent such as 0, 83772166 and -42, while `x4`

is restricted to the positive of these. Finally `x5`

can assume the values 1 or 0 representing true and false respectively.

After learning about ranges, you can define the values the decision variables can take more precisely without using constraint explicitly.

### (Ordinary) variables / parameters

Sometimes it helps to have variables whose value can be computed before trying to optimize the LP, IP or MIP you are solving.

These can be declared by stating their type, their name and (optionally) their value:

```
int i = 10;
```

This will set a variable name i to the value 10. Note that as `i`

has type `int`

it can only be given integer values. If you need floating point numbers, use `float`

. `boolean`

can at this point only be used for decision variables.

### Objective function

We also need one objective function, and this can be declared using the keyword `maximize`

or `minimize`

.

```
maximize x1+x2;
```

You need not use all the decision variables you declared, and you may mix it in with ordinary variables and constants:

```
minimize i*i*2*5*x2-x1/100;
```

Note however that the objective must be a linear function of the **decision variables**. Sometimes OPL Studio will try to solve quadratic programs, so be *very* careful. The line above is ok, as `i`

is not a decision variable, but an ordinary variable.

### Constraints

No model is complete without constraints. These are specified right after the objective:

```
subject to {
//The lines between the { and the } will be interpreted as constraints.
}
```

Each constraint will involve at least one decision variable, a comparator from the set {`==`

, `<=`

, `>=`

} meaning {equals, less than or equal, greater than or equal} respectively. Be mindful of the fact that `==`

is a comparator, while `=`

is used for assigning variables as seen earlier.

An example of a constraint could be:

```
x1 >= 1;
```

Specifying that x1 must take a value greater than or equal to 1.

It can also involve more decision variables:

```
x1 >= x2 + x3;
```

Again, recall that the constraints must be **linear in the decision variables.** Replacing the + with a * is thus a no go.

### Example

Finally, here is an example of a simple mixed integer program:

```
int i = 10;
dvar float x1;
dvar int x2;
dvar float x3;
minimize i*x1 + 2*x2 - x3;
subject to {
x1+x2+x3 == 10;
x1 >= 0;
x2+x3 >= 2;
x1+x2 >= x3;
}
```

## Test

Solve the following model using OPL Studio.

$\textrm{minimize } 5 \cdot x+1.3 \cdot y - 5.4 \cdot z $

Subject to:

$z+10 \cdot y \leq 100$

$x+y \geq 0$

$x \in \mathbb{R}$

$y \in {0,1}$

$z \in \mathbb{N}$