# Documentation

Filter Content By
Version
Languages
Modify a model

## Modify a model

Examples: diet, feasopt, fixanddive, lpmod, sensitivity, workforce3, workforce4, workforce5

This section considers model modification. Modification can take many forms, including adding constraints or variables, deleting constraints or variables, modifying constraint and variable attributes, changing constraint coefficients, etc. The Gurobi examples don't cover all possible modifications, but they cover the most common types.

diet

This example builds a linear model that solves the classic diet problem: to find the minimum cost diet that satisfies a set of daily nutritional requirements. Once the model has been formulated and solved, it adds an additional constraint to limit the number of servings of dairy products and solves the model again. Let's focus on the model modification.

Adding constraints to a model that has already been solved is no different from adding constraints when constructing an initial model. In Python, we can introduce a limit of 6 dairy servings through the following constraint:

```  m.addConstr(buy['milk'] + buy['ice cream'] <= 6, "limit_dairy")
```
For linear models, the previously computed solution can be used as an efficient warm start for the modified model. The Gurobi solver retains the previous solution, so the next `optimize` call automatically starts from the previous solution.

lpmod

Changing a variable bound is also straightforward. The `lpmod` example changes a single variable bound, then re-solves the model in two different ways. A variable bound can be changed by modifying the `UB` or `LB` attribute of the variable. In C:

```  error = GRBsetdblattrelement(model, GRB_DBL_ATTR_UB, var, 0.0);
```
In Python:
```  minVar.ub = 0
```
The model is re-solved simply by calling the `optimize` method again. For a continuous model, this starts the optimization from the previous solution. To illustrate the difference when solving the model from an initial, unsolved state, the `lpmod` example calls the `reset` function. In C:
```  error = GRBreset(model, 0);
```
In C++, Java, and Python:
```  m.reset(0)
```
In C#:
```  m.Reset(0)
```
When we call the optimize method after resetting the model, optimization starts from scratch. Although the difference in computation time is insignificant for this tiny example, a warm start can make a big difference for larger models.

fixanddive

The `fixanddive` example provides another example of bound modification. In this case, we repeatedly modify a set of variable bounds, utilizing warm starts each time. In C, variables are fixed as follows:

```  for (j = 0; j < nfix; ++j)
{
fixval = floor(fractional[j].X + 0.5);
error = GRBsetdblattrelement(model, "LB", fractional[j].index, fixval);
if (error) goto QUIT;
error = GRBsetdblattrelement(model, "UB", fractional[j].index, fixval);
if (error) goto QUIT;
}
```
In Python, they are fixed as follows:
```  for i in range(nfix):
v = fractional[i]
fixval = int(v.x + 0.5)
v.lb = fixval
v.ub = fixval
```
Again, the subsequent call to `optimize` starts from the previous solution.

sensitivity

The `sensitivity` example computes the amount by which the optimal objective changes if each binary variable is fixed at either 0 or 1. For each binary variable, the example creates and solves a copy of the model with new upper and lower bounds. This example is a MIP, so Gurobi can not make use of advanced start information. As a result, the model is solved from scratch after each bound modification.

feasopt

The last modification example we consider is `feasopt`, which adds variables to existing constraints and also changes the optimization objective. Setting the objective to zero is straightforward: simply call `setObjective` with a zero argument:

```  m.setObjective(0)
```
Adding new variables is somewhat more complex. In the example, we want to add artificial variable(s) to each constraint in order to allow the constraint to be relaxed. We use two artificial variables for equality constraints and one for inequality constraints. The Python code for adding a single artificial variable to constraint `c` is:
```  feasModel.addVar(obj=1.0, name="ArtP_" + c.Constrname, column=Column(, [c]))
```
We use the `column` argument of the `addVar` method to specify the set of constraints in which the new variable participates, as well as the associated coefficients. In this example, the new variable only participates in the constraint to be relaxed. Default values are used here for all variables attributes except the objective and the variable name.