简体   繁体   中英

How to initialize LpVariable in PuLP

I have declared list facility of LpVariable :

for fac in range (len(candidates)):
    facility.append(LpVariable("Facility_{0}".format(fac),lowBound=0, upBound=1, cat= pulp.LpInteger ))

When I do print(value(facility[i])) , it gives me output as None which is okay because all LpVariables have None value by default. But in my code I want to initialize with 0. But when I solve problem, then optimal solution can have any value between 0 to 1. If I am doing this :

for i in range (len(facility)):
        facility[i] = 0

It will set facility[i] = 0 (integer value and there is no more LpVariable).

It throws error if I do value(facility[i]) = 0. How should I initialize these variables ?

You can try facility[i].setInitialValue(0.) , as described here .

Note that PuLP will delegate this call to the corresponding method from the API of the solver you are calling. Therefore, it will work only if the solver supports it. Gurobi and CPLEX support it, I am not sure about other solvers.

Now there's some documentation on how to do this in pulp and which solver are available.

https://coin-or.github.io/pulp/guides/how_to_mip_start.html

as a summary you need to do setInitialValue on the values you need and then set the mip_start argument in the solver call:

import pulp
# facility is a dictionary with LpVariable()
# model is an LpProblem()
facility[i].setInitialValue(0)
solver = pulp.PULP_CBC_CMD(msg=1, mip_start=1)
model.solve(solver)

Some solvers (such as CBC) require a complete solution (at least in the current version that comes with pulp). Others (CPLEX and GUROBI), can repair an incomplete solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM