Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ortools中的特定约束非常慢_Python_Or Tools - Fatal编程技术网

Python ortools中的特定约束非常慢

Python ortools中的特定约束非常慢,python,or-tools,Python,Or Tools,我有一个作业问题要解决。我发现ortools是一个很好的工具。我设法解决了它,但它是非常缓慢的,我需要它是快速的 我遇到的问题基本上是一群商店以不同的价格出售相同的商品。我需要选择从哪里挑选这些物品,以达到最低的总价格,以及不超过4家商店 这是我的代码,但如果提供的成本矩阵有4个以上的存储,那么速度会很慢。问题在于“存储最大限制”约束。无论如何,是否可以用不同的编码来提高速度 import numpy as np from ortools.sat.python import cp_model f

我有一个作业问题要解决。我发现ortools是一个很好的工具。我设法解决了它,但它是非常缓慢的,我需要它是快速的

我遇到的问题基本上是一群商店以不同的价格出售相同的商品。我需要选择从哪里挑选这些物品,以达到最低的总价格,以及不超过4家商店

这是我的代码,但如果提供的成本矩阵有4个以上的存储,那么速度会很慢。问题在于“存储最大限制”约束。无论如何,是否可以用不同的编码来提高速度

import numpy as np
from ortools.sat.python import cp_model
from ortools.linear_solver import pywraplp

#cost matrix, where j are stores, i are items

C = np.array([[38, 13, 73, 10, 76,  6, 80, 65, 17,  2],
        [77, 72,  7, 26, 51, 21, 19, 85, 12, 29],
        [30, 15, 51, 69, 88, 88, 95, 97, 87, 14],
        [10,  8, 64, 62, 23, 58,  2,  1, 61, 82],
        [ 9, 89, 14, 48, 73, 31, 72,  4, 71, 22],
        [50, 58,  4, 69, 25, 44, 77, 27, 53, 81],
        [42, 83, 16, 65, 69, 26, 99, 88,  8, 27],
        [26, 23, 10, 68, 24, 28, 38, 58, 84, 39],
        [ 9, 33, 35, 11, 24, 16, 88, 26, 72, 93],
        [75, 63, 47, 33, 89, 24, 56, 66, 78,  4],
        [ 1, 78,  7, 53, 86, 71,  3, 77, 92, 22],
        [76,  8, 78, 73, 76, 77, 44, 21, 31, 37],
        [ 8, 46, 69, 58, 83, 97, 14, 11, 24, 82],
        [ 8, 25, 75, 93, 21, 33, 13, 66, 95, 61],
        [25, 83, 98,  3, 93, 99, 11, 55, 97, 83],
        [87, 71, 67, 72, 49, 55, 16,  6, 18, 43],
        [21, 49, 23, 14, 98, 54, 85, 11, 97, 56],
        [62, 57, 90, 22, 97, 84, 26, 15, 14, 85],
        [44,  7, 78, 57, 60, 16, 25, 10, 67, 72],
        [54, 70, 37, 22, 41, 78, 92, 50, 48, 78]])

# the solver func
def Solve_Cost_Matrix_2(cost):
    
    model = cp_model.CpModel()
    max_stops=4
    
    #generate ranges

    num_items = len(cost)
    num_shops = len(cost[0])

    all_items = range(num_items)
    all_shops = range(num_shops)


    

    # Create bool Variable matrix
    x=[]
    for i in all_items:
        t=[]
        for j in all_shops:
            t.append(model.NewBoolVar(f'i{i}_j{j}'))
        x.append(t)


    # Constraints
    # Each item only assigned once to any store .
    [model.Add(sum(x[i][j] for j in all_shops) == 1) for i in all_items]

    
    
    
    # Adding the intermediate variable to constrain the number of the stores. 
    s=[]
    for j in all_shops:
        s.append( model.NewBoolVar(f's_{j}') )
      
    for j in all_shops:
        model.Add(sum(x[i][j] for i in all_items) >= 1).OnlyEnforceIf(s[j])
        model.Add(sum(x[i][j] for i in all_items) == 0).OnlyEnforceIf(s[j].Not())

    model.Add(sum(s[j] for j in all_shops) <= max_stops)

    
    
    
    # Create the Objective function Variable
    total_cost = model.NewIntVar(0, 1000000, 'total_cost')

    # Create the Objective function, Minimize (Sum of cost) 
    model.Add(total_cost == (sum(x[i][j] * cost[i][j] for j in all_shops for i in all_items )))


    model.Minimize(total_cost)

    

    #Initialize the Solver ... 
    solver = cp_model.CpSolver()

    status = solver.Solve(model)

    
    print(solver.ResponseStats())
    Total_Cost,senario_cost = 0,0
    
    #printing the solution
    if status == cp_model.OPTIMAL:

        senario_cost={'Items':[],'Assigned_to':[],'Item_cost':[],'Num_stops':0,'cost':[]}
       
        Total_Cost = solver.ObjectiveValue()
        
        for i in range(num_items):
            for j in range(num_shops):
                if solver.Value(x[i][j]) == 1:
                    senario_cost['Items'].append(i)
                    senario_cost['Assigned_to'].append(j)
                    senario_cost['Item_cost'].append(cost[i][j])
        senario_cost['Num_stops'] = len(set(senario_cost['Assigned_to']))
        senario_cost['cost'] = cost
    
        return Total_Cost,senario_cost
    else:
        return None,None

当我在主分支上运行提供的代码时,没有并行性,我得到:

CpSolverResponse:
status: OPTIMAL
objective: 213
best_bound: 213
booleans: 210
conflicts: 31
branches: 617
propagations: 5226
integer_propagations: 8220
restarts: 428
lp_iterations: 130
walltime: 0.021303
usertime: 0.021303
deterministic_time: 0.0011162
primal_integral: 0.00536794

你得到了不同的结果吗?

你能试试solver.parameters.num\u search\u workers=8吗?@Stradivari哇,这让它变得如此之快。谢谢你的建议。如果你想把它作为一个答案,我会接受它。我在这个问题上添加了这个信息。我现在在jupyter上运行这个。这可能是原因吗?
CpSolverResponse:
status: OPTIMAL
objective: 213
best_bound: 213
booleans: 210
conflicts: 31
branches: 617
propagations: 5226
integer_propagations: 8220
restarts: 428
lp_iterations: 130
walltime: 0.021303
usertime: 0.021303
deterministic_time: 0.0011162
primal_integral: 0.00536794