在同一个脚本中重新运行函数

2024-05-19 08:38:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个将项目分配给不同卡车的函数,但是当我尝试为不同的数据集重新运行相同的函数时,它显示了一个错误,即“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

Tags: ofinforvaluestatusitemsitemmass

热门问题