Created
November 4, 2023 20:33
-
-
Save tbbooher/d7ae97e9d75efac3105ec162a788f6d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pulp, csv | |
# Conversion factors | |
in_to_mm = 25.4 # 1 inch is 25.4 mm | |
# Define the stock boards as a list of tuples (description, length in mm, width in mm, cost) | |
# All lengths are converted to mm (1 ft = 12 in) | |
# Conversion factors | |
in_to_mm = 25.4 # 1 inch = 25.4 millimeters | |
ft_to_mm = 304.8 # 1 foot = 304.8 millimeters | |
# Stock boards list | |
stock_boards = [ | |
("Board1", 8 * ft_to_mm, 4 * in_to_mm, 8.37), | |
("Board2", 4 * ft_to_mm, 12 * in_to_mm, 12.56), | |
("Board3", 8 * ft_to_mm, 8 * in_to_mm, 16.75), | |
("Board4", 8 * ft_to_mm, 5 * in_to_mm, 14.22), | |
("Board5", 8 * ft_to_mm, 4 * in_to_mm, 15.34), | |
("Board6", 16 * ft_to_mm, 5 * in_to_mm, 28.32), | |
("Board7", 8 * ft_to_mm, 2 * in_to_mm, 7.68), | |
("Board8", 8 * ft_to_mm, 12 * in_to_mm, 47.38), | |
("Board9", 8 * ft_to_mm, 8 * in_to_mm, 31.64), | |
("Board10", 8 * ft_to_mm, 4 * in_to_mm, 11.33) | |
# You can continue adding other boards in similar format if needed. | |
] | |
# Print out the stock boards to verify | |
for board in stock_boards: | |
print(f"{board[0]}: Length={board[1]} mm, Width={board[2]} mm, Cost=${board[3]}") | |
# Define the cuts required as a list of tuples (length in mm, width in mm, quantity) | |
cuts_required = [ | |
(2215.6, 60, 2), | |
(1066.8, 60, 2), | |
(2235.6, 60, 10), | |
] | |
# Set up the problem | |
prob = pulp.LpProblem("2D_Stock_Cutting", pulp.LpMinimize) | |
# Create a variable for each board type indicating how many of that board we use | |
x_vars = {i: pulp.LpVariable(f"board_use_{i}", lowBound=0, cat=pulp.LpInteger) for i, board in enumerate(stock_boards)} | |
# Objective function: minimize the total cost of the boards used | |
prob += pulp.lpSum([x_vars[i] * board[3] for i, board in enumerate(stock_boards)]) | |
# Add constraints for each type of cut required | |
for cut in cuts_required: | |
length_required, width_required, quantity_required = cut | |
prob += (pulp.lpSum([(board[1] // length_required) * (board[2] // width_required) * x_vars[i] for i, board in enumerate(stock_boards)]) >= quantity_required) | |
# Solve the problem | |
prob.solve() | |
# Prepare the data for CSV output | |
output_data = [] | |
if pulp.LpStatus[prob.status] == 'Optimal': | |
print("Optimal solution found:") | |
output_data.append(["Board Combination", "Quantity Used", "Total Cost (USD)"]) | |
for i, board in enumerate(stock_boards): | |
if x_vars[i].value() > 0: | |
output_line = [board[0], x_vars[i].value(), board[3] * x_vars[i].value()] | |
output_data.append(output_line) | |
print(f"Use {x_vars[i].value()} pieces of {board[0]} at a total cost of {board[3] * x_vars[i].value()} USD") | |
total_cost_line = ["Total", "", pulp.value(prob.objective)] | |
output_data.append(total_cost_line) | |
print(f"Total cost for this cutting plan: {pulp.value(prob.objective)} USD") | |
else: | |
print("No optimal solution found. Adjust the problem constraints and retry.") | |
# Write to CSV file | |
with open('output.csv', mode='w', newline='') as file: | |
writer = csv.writer(file) | |
writer.writerows(output_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment