在同一脚本中重新运行功能

时间:2019-03-11 07:43:38

标签: python python-3.x function

我有一个将物品分配给不同卡车的函数,但是当我尝试为不同的数据集重新运行同一函数时,它显示一个错误,指出'list'对象不可调用

第二个数据集(即dat)是原始数据集的副本,但排除了某些值,因此我提供的数据没有变化。

我正在粘贴整个代码以更好地理解,如果情况仍然不清楚,请告诉我我还需要提供什么

代码:

def allocator(item_mass,item_vol,truck_mass,truck_vol,truck_cost):
    n_items = len(item_vol)
    set_items = range(n_items)
    n_trucks = len(truck_cost)
    set_trucks = range(n_trucks)

    y = pulp.LpVariable.dicts('truckUsed', set_trucks,
        lowBound=0, upBound=1, cat=LpInteger)

    x = pulp.LpVariable.dicts('itemInTruck', (set_items, set_trucks), 
        lowBound=0, upBound=1, cat=LpInteger)

    # Model formulation
    prob = LpProblem("Truck allocatoin problem", LpMinimize)

    # Objective
    prob += lpSum([truck_cost[i] * y[i] for i in set_trucks])

    # Constraints
    for j in set_items:
        # Every item must be taken in one truck
        prob += lpSum([x[j][i] for i in set_trucks]) == 1

    for i in set_trucks:
        # Respect the mass constraint of trucks
        prob += lpSum([item_mass[j] * x[j][i] for j in set_items]) <= truck_mass[i]*y[i]

        # Respect the volume constraint of trucks
        prob += lpSum([item_vol[j] * x[j][i] for j in set_items]) <= truck_vol[i]*y[i]

    # Ensure y variables have to be set to make use of x variables:
    for j in set_items:
        for i in set_trucks:
            x[j][i] <= y[i]


    prob.solve()

    x_soln = np.array([[x[i][j].varValue for i in set_items] for j in set_trucks])
    y_soln = np.array([y[i].varValue for i in set_trucks])

    lpstatus = LpStatus[prob.status]
    value_obj = value(prob.objective)


    trucks_used = str(sum(([y_soln[i] for i in set_trucks])))        #output3

    a = []
    b = []

    for i in set_items:
        for j in set_trucks:
            if x[i][j].value() == 1:
                a.append(str(j))
                b.append(str(i))


    totalitemvol = sum(item_vol)

    totaltruckvol = sum([y[i].value() * truck_vol[i] for i in set_trucks])

    trucks_status = []                                                #output4  #sufficient or not

    if(totaltruckvol >= totalitemvol):
        trucks_status.append("Trucks are sufficient")
    else:
        trucks_status.append("Trucks are insufficient")


    zipped = list(zip(a, b))
    packed_items = {} 

    for tr,it in zipped:
        try:
            packed_items[tr].append(it)
        except KeyError:
            packed_items[tr] = [it]


    output_dict = {}                                                 #output5 #Dictionary with trucks and items 
    for k, v in packed_items.items():
        output_dict[int(k)] = [int(item) for item in v]


    return lpstatus,value_obj,trucks_used, trucks_status, output_dict

第二次调用同一函数:

dat = data[~data["Pid"].isin([8, 1, 2, 6])]
item_m = dat["Weight"].tolist()
item_v = dat["Volume"].tolist()

# Mass & volume capacities of trucks
truck_m = truck["Weight"].tolist()
truck_v = truck["Volume"].tolist()

# Cost of using each truck
truck_c = truck["Price"].tolist()
print(dat)
print(item_mass)


allocator(item_m,item_v,truck_m,truck_v,truck_c)

这是错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-179-134dc70f4c45> in <module>()
     14 
     15 
---> 16 allocator(item_m,item_v,truck_m,truck_v,truck_c)
     17 
     18 

<ipython-input-166-80638aac383c> in allocator(item_mass, item_vol, truck_mass, truck_vol, truck_cost)
     41 
     42     lpstatus = LpStatus[prob.status]
---> 43     value_obj = value(prob.objective)
     44 
     45 

TypeError: 'list' object is not callable

0 个答案:

没有答案