python
Dashboard
My Repos
Compilers
Python Online
Node JS Online
Golang Online
codepy
Login
My Repos
Sign Out
Online Python Interpreter
Stop
Run
from pulp import * import pandas as pd import datetime import random #Importações do Python import time item=[794, 793, 790, 786, 772, 764, 756, 743, 742, 740, 740, 736, 736, 732, 724, 724, 723, 713, 712, 709, 704, 703, 700, 699, 692, 691, 691, 678, 677, 673, 667, 662, 662, 659, 658, 646, 644, 640, 637, 635, 633, 629, 628, 625, 622, 617, 613, 612, 608, 608, 605, 602, 595, 595, 583, 580, 572, 568, 558, 541, 535, 531, 523, 521, 519, 518, 518, 518, 517, 517, 515, 512, 509, 506, 506, 504, 494, 487, 487, 485, 482, 476, 471, 469, 468, 466, 462, 459, 447, 446, 443, 436, 431, 423, 423, 419, 416, 415, 408, 407, 400, 399, 396, 395, 387, 371, 370, 370, 369, 365, 363, 360, 355, 354, 347, 346, 345, 337, 336, 334, 328, 321, 313, 312, 292, 278, 278, 276, 275, 266, 257, 254, 252, 250, 250, 249, 246, 242, 240, 233, 233, 232, 226, 223, 221, 220, 214, 213, 212, 210, 209, 208, 196, 196, 189, 185, 185, 184, 181, 179, 174, 171, 170, 168, 166, 164, 157, 147, 146, 141, 133, 123, 120, 119, 116, 116, 104, 103, 102, 99, 94, 93, 72, 66, 61, 61, 61, 59, 55, 51, 50, 45, 44, 41, 36, 21, 9, 7, 4, 1] items=[] for i in range(len(item)): x = tuple([i, item[i]]) items.append(x) itemCount = len(items) # Max number of bins allowed. maxBins = itemCount # Bin Size binCapacity = 1000 # Indicator variable assigned 1 when the bin is used. y = pulp.LpVariable.dicts('BinUsed', range(maxBins), lowBound = 0, upBound = 1, cat = LpInteger) # An indicator variable that is assigned 1 when item is placed into binNum possible_ItemInBin = [(itemTuple[0], binNum) for itemTuple in items for binNum in range(maxBins)] x = pulp.LpVariable.dicts('itemInBin', possible_ItemInBin, lowBound = 0, upBound = 1, cat = LpInteger) # Initialize the problem prob = LpProblem("Bin Packing Problem", LpMinimize) # Add the objective function. prob += lpSum([y[i] for i in range(maxBins)]), "Objective: Minimize Bins Used" # First constraint: For every item, the sum of bins in which it appears must be 1 for j in items: prob += lpSum([x[(j[0], i)] for i in range(maxBins)]) == 1, ("An item can be in only 1 bin -- " + str(j[0])) # Second constraint: For every bin, the number of items in the bin cannot exceed the bin capacity for i in range(maxBins): prob += lpSum([items[j][1] * x[(items[j][0], i)] for j in range(itemCount)]) <= binCapacity*y[i], ("The sum of item sizes must be smaller than the bin -- " + str(i)) # Write the model to disk prob.writeLP("BinPack.lp") # Solve the optimization. start_time = time.time() prob.solve(use_mps=False) print("Bins used: " + str(sum(([y[i].value() for i in range(maxBins)])))) print("Solved in %s seconds." % (time.time() - start_time))
Share this code with others
Public
Clear
My Repos
Repo
Lang
Login
Register
Login
Create a free account. No Credit card info required.
I agree with the Codepy
Term of Service
Sign Up