简体   繁体   中英

PuLP print decision variable results that are Non-Zero only?

Currently I am using PuLP for a retail distribution problem. CZe represents a customer, where the indexed DC or S1,S2 etc. is a location that services the demand of that customer. I want to find a way to print only decision variables that have a non-zero allocation amount... if possible.

Service_1_('CZe_0004',_'DC_CA_FRESNO') = 0.0
Service_1_('CZe_0004',_'DC_IN_GREENWOOD') = 0.0
Service_1_('CZe_0004',_'DC_PA_CHAMBERSBURG') = 0.01
Service_1_('CZe_0004',_'DC_TX_DALLAS') = 0.0
Service_1_('CZe_0004',_'MFC_FL_JACKSONVILLE') = 0.0
Service_1_('CZe_0004',_'MFC_IL_ROMEOVILLE') = 0.0
Service_1_('CZe_0004',_'MFC_SC_GREER') = 0.0
Service_1_('CZe_0004',_'S1') = 0.0
Service_1_('CZe_0004',_'S10') = 0.0
Service_1_('CZe_0004',_'S11') = 0.0
Service_1_('CZe_0004',_'S12') = 0.0
Service_1_('CZe_0004',_'S13') = 0.0
Service_1_('CZe_0004',_'S14') = 0.0
Service_1_('CZe_0004',_'S15') = 0.0
Service_1_('CZe_0004',_'S16') = 0.0
Service_1_('CZe_0004',_'S17') = 0.0
Service_1_('CZe_0004',_'S18') = 0.0
Service_1_('CZe_0004',_'S19') = 0.0
Service_1_('CZe_0004',_'S2') = 0.0
Service_1_('CZe_0004',_'S20') = 0.0
Service_1_('CZe_0004',_'S21') = 0.0
Service_1_('CZe_0004',_'S22') = 0.0
Service_1_('CZe_0004',_'S23') = 0.0
Service_1_('CZe_0004',_'S24') = 0.0
Service_1_('CZe_0004',_'S25') = 0.0
Service_1_('CZe_0004',_'S26') = 0.0
Service_1_('CZe_0004',_'S27') = 0.0
Service_1_('CZe_0004',_'S3') = 0.0
Service_1_('CZe_0004',_'S4') = 0.0
Service_1_('CZe_0004',_'S5') = 0.0
Service_1_('CZe_0004',_'S6') = 0.0
Service_1_('CZe_0004',_'S7') = 0.0
Service_1_('CZe_0004',_'S8') = 0.0
Service_1_('CZe_0004',_'S9') = 0.0
Service_1_('CZe_0005',_'DC_CA_FRESNO') = 0.0
Service_1_('CZe_0005',_'DC_IN_GREENWOOD') = 0.0
Service_1_('CZe_0005',_'DC_PA_CHAMBERSBURG') = 0.01
Service_1_('CZe_0005',_'DC_TX_DALLAS') = 0.0
Service_1_('CZe_0005',_'MFC_FL_JACKSONVILLE') = 0.0
Service_1_('CZe_0005',_'MFC_IL_ROMEOVILLE') = 0.0
Service_1_('CZe_0005',_'MFC_SC_GREER') = 0.0
Service_1_('CZe_0005',_'S1') = 0.0
Service_1_('CZe_0005',_'S10') = 0.0
Service_1_('CZe_0005',_'S11') = 0.0
Service_1_('CZe_0005',_'S12') = 0.0
Service_1_('CZe_0005',_'S13') = 0.0
Service_1_('CZe_0005',_'S14') = 0.0
Service_1_('CZe_0005',_'S15') = 0.0
Service_1_('CZe_0005',_'S16') = 0.0
Service_1_('CZe_0005',_'S17') = 0.0
Service_1_('CZe_0005',_'S18') = 0.0
Service_1_('CZe_0005',_'S19') = 0.0
Service_1_('CZe_0005',_'S2') = 0.0
Service_1_('CZe_0005',_'S20') = 0.0
Service_1_('CZe_0005',_'S21') = 0.0
Service_1_('CZe_0005',_'S22') = 0.0
Service_1_('CZe_0005',_'S23') = 0.0
Service_1_('CZe_0005',_'S24') = 0.0
Service_1_('CZe_0005',_'S25') = 0.0
Service_1_('CZe_0005',_'S26') = 0.0
Service_1_('CZe_0005',_'S27') = 0.0
Service_1_('CZe_0005',_'S3') = 0.0
Service_1_('CZe_0005',_'S4') = 0.0
Service_1_('CZe_0005',_'S5') = 0.0
Service_1_('CZe_0005',_'S6') = 0.0
Service_1_('CZe_0005',_'S7') = 0.0
Service_1_('CZe_0005',_'S8') = 0.0
Service_1_('CZe_0005',_'S9') = 0.0

below is the line of code I am currently using to print the results of this optimization problem.

for v in problem.variables():
    print(v.name,"=",v.varValue)
    
#Print Optimal Solution
print("The cost of allocating demand equals",value(problem.objective))

You should be able to just insert a little logical test in your print loop.... I would suggest a small "epsilon" value for floating point values.

eps = 1e-4
for v in problem.variables():
    if v.varValue > eps:
        print(v.name,"=",v.varValue)

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