Commit 47ef5212 authored by Jirka Fink's avatar Jirka Fink
Browse files

Add 2nd assignment

parent 6aa5b328
49
100
70 1
68 1
67 2
66 3
65 1
64 2
63 1
62 1
61 1
60 3
59 1
58 3
57 1
56 2
55 1
54 2
53 2
52 4
51 3
50 1
49 4
48 1
47 1
46 1
44 2
42 1
40 3
39 2
38 5
37 2
36 3
34 3
33 3
32 2
31 3
30 2
29 2
27 1
25 3
23 1
22 2
20 2
19 1
17 3
16 3
14 1
13 3
12 3
11 1
74
200
159 1
157 1
156 2
153 1
151 1
150 1
149 2
148 1
147 1
144 1
143 1
139 2
136 1
133 2
122 1
120 2
119 2
117 2
116 1
115 1
114 1
112 1
108 4
107 1
105 1
104 1
103 1
102 1
101 2
100 1
99 2
98 1
96 1
94 1
90 2
89 2
88 2
87 3
86 1
83 1
82 1
81 1
80 1
79 2
78 1
77 1
75 1
74 1
71 2
70 2
68 2
63 1
62 2
57 1
55 1
53 2
52 1
50 1
49 2
48 1
47 1
43 1
42 1
41 1
39 1
38 1
37 1
35 1
34 1
32 2
30 1
28 2
25 1
20 1
83
150
120 3
118 3
117 3
116 2
114 3
113 1
112 3
111 2
110 1
109 2
108 5
107 2
106 5
105 1
104 3
103 1
102 1
101 3
100 3
99 1
98 5
97 2
96 2
95 2
94 5
93 1
92 3
91 1
90 2
89 4
88 3
87 2
86 1
85 3
84 5
83 2
82 1
80 1
79 1
78 1
77 1
76 5
75 4
74 1
73 3
72 3
71 2
68 3
67 1
66 2
65 2
64 2
62 5
61 3
60 4
59 3
58 1
57 4
56 3
54 1
53 1
52 2
51 4
50 1
49 1
48 1
47 1
46 1
45 3
44 3
43 1
41 3
40 2
39 3
38 3
37 2
36 4
35 1
34 2
33 2
32 8
31 1
30 2
98
150
120 4
119 5
118 2
117 1
116 4
115 3
114 2
113 2
112 7
111 3
110 2
109 3
108 3
107 2
106 1
104 1
103 1
102 2
100 4
99 1
98 1
97 4
95 8
94 2
93 2
92 2
90 2
89 5
88 3
87 3
86 4
85 6
84 3
83 5
82 1
81 2
80 2
79 2
78 4
77 5
76 1
75 2
74 4
73 2
72 1
71 4
70 2
69 6
68 1
65 3
64 5
63 2
62 3
61 3
60 3
59 3
58 4
57 3
56 2
55 7
54 6
53 3
52 1
51 2
50 2
49 2
48 1
47 7
46 2
45 4
44 4
43 5
41 2
40 2
38 3
37 3
36 4
35 2
34 5
33 5
32 1
31 2
30 3
29 3
28 6
27 2
26 4
25 5
24 2
23 4
22 3
21 4
20 3
19 5
18 2
17 2
16 2
15 2
21
50
35 4
34 3
33 1
32 3
31 1
29 1
28 1
27 2
26 3
25 5
24 4
23 3
21 2
20 2
18 2
17 2
16 1
15 2
14 3
13 2
11 3
import math
import gurobipy as gp
from gurobipy import GRB
def solve_cutting_stock_problem(instance):
"""
Find an optimal solution for the cutting stock problem
instance - Instance of the problem
return - A list of bins, each contains a list of weights of items
TODO: This function returs a feasible solution which is (mostly) very far from the optimal solution
"""
return [ [w] for (w,d) in zip(instance.weight, instance.demand) for _ in range(d) ]
import sys
import copy
from time import time
from prettytable import PrettyTable
from cutting_stock import solve_cutting_stock_problem
# Instance of the Cutting Stock Problem
class Instance:
def __init__(self, items, capacity):
self.items = items # The number of items
self.capacity = capacity # The capacity of identical bins
self.weight = [0] * items # Weight of each item (initialized in the function load)
self.demand = [0] * items # Demand of each item (initialized in the function load)
# Returns the instance of CSP stored in a given file
def load(filename):
file = open(filename, 'r')
items = int(file.readline())
capacity = int(file.readline())
instance = Instance(items, capacity)
for i in range(items):
instance.weight[i], instance.demand[i] = (int(x) for x in file.readline().split())
file.close()
return instance
# Returns True if the solution is feasible for the given instance; otherwise, an error string.
# If the optimal number of bins is provided, the optimality of the solution is tested.
def verify_solution(instance, solution, optimal_bins = None):
satisfied = { w: 0 for w in instance.weight }
for bin in solution:
if sum(bin) > instance.capacity:
return (False, "Bin {} exceeds the capacity {}".format(bin, instance.capacity))
for w in bin:
if not w in satisfied:
return (False, "There is no demand for rolls of width {}".format(w))
satisfied[w] += 1
if not bin:
print("Your solution contains an empty bin which is feasible but clearly suboptimal")
for (w,d) in zip(instance.weight, instance.demand):
if not d == satisfied[w]:
return (False, "The demand for rolls of width {} is {} but your solution produces is {} rolls".format(w, d, satisfied[w]))
if optimal_bins:
if len(solution) > optimal_bins:
return (False, "Solution is not optimal")
elif len(solution) < optimal_bins:
return (False, "Solution is better than the optimal one which should be impossible")
return (True, "Correct solution")
# Run one test with a given instance parameters
def run_test(filename, optimal_bins):
instance = load(filename)
solution = solve_cutting_stock_problem(copy.deepcopy(instance))
return verify_solution(instance, solution, optimal_bins) + (instance,)
def main():
tests = {
"first": ("BPP_50_50_0.2_0.7_4.txt", 25, 0.2),
"second": ("BPP_100_100_0.1_0.7_8.txt", 41, 0.2),
"third": ("BPP_100_200_0.1_0.8_0.txt", 46, 1.5),
"fourth": ("BPP_200_150_0.2_0.8_7.txt", 104, 5),
"fifth": ("BPP_300_150_0.1_0.8_2.txt", 132, 20),
}
if len(sys.argv) == 1:
results = PrettyTable(["Instance name", "Points", "Filename", "Items", "Time limit [s]", "Your time [s]", "Evaluation"])
for name in tests:
print("Running test", name)
filename, optimal_bins, time_limit = tests[name]
start_time = time()
status,msg,instance = run_test(filename, optimal_bins)
running_time = time() - start_time
print(msg)
print()
results.add_row([name, 2, filename, instance.items, time_limit, running_time, msg])
print(results)
else:
name = sys.argv[1]
if name in tests:
filename, optimal_bins, time_limit = tests[name]
status,msg,instance = run_test(filename, optimal_bins)
print(msg)
else:
print("Unknown test", name)
"""
To run all tests, run the command
$ python3 cutting_stock_tests.py
To run a test NAME, run the command
$ python3 multiple_divisor_tests.py NAME
"""
if __name__ == "__main__":
main()
In the cutting stock problem (CSP), we are given m item types, each having an integer weight w_j and an integer demand d_j (j = 1, ..., m), and an unlimited number of identical bins (frequently called rolls in the literature) of integer capacity c. The objective is to produce d_j copies of each item type j using the minimum number of bins so that the total weight packed in any bin does not exceed its capacity.
The task is to implement the function *solve_cutting_stock_problem* in the file *cutting_stock.py* which is expected to find an optimal solution to a given instance of CSP. Note that your program has to find an optimal solution for any CSP instance assuming sufficient amount of memory and time is provided. In you program, explain your algorithm.
http://or.dei.unibo.it/library/bpplib
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment