# How can I assign an initial value to a variable in PuLP?

I am trying to solve a production problem and right now I need to assign an initial value to the inventory, and that value is given. This is my definition of the inventory variable:

H = pl.LpVariable.dicts("H[i,t]", [(i,t) for i in productos for t in periodos],cat='Integer')


where:

productos=["golilla","tornillo","tuerca"]
periodos= [1,2,3,4,5,6,7,8,9,10,11,12]


and H["golilla",0] need to be 2400, H["tuerca",0] need to be 140 and H["tornillo",0] need to be 320

• To assign a value to a variable, you can define it as a constraint. So, add a constraint that equals your decision variable to its initial value.
– EhsanK
Nov 2 '20 at 4:43

In Pyomo you can use the .fix() method to do that. In the following example based on a condition you can fix some variables to different values:

if pyo.value(instance.x[2]) == 0:
instance.x[2].fix(1)
else:
instance.x[2].fix(0)


In PuLP:

Assigning values to variables also permit fixing those variables to that value. In order to do that, you can use the fixValue method of the variable.(Please look at this link)

If you have not yet solved the model, you can use the setInitialValue method to assign a value to the variable.

For our example, if we know some variable needs to be 1, we can do:

_variable = x[('O', 'P', 'Q', 'R')]
_variable.setInitialValue(1)
_variable.fixValue()