Einige Teile fehlen in meinem Python First Fit Abnahme von StabschnittenalgorithmusPython

Python-Programme
Anonymous
 Einige Teile fehlen in meinem Python First Fit Abnahme von Stabschnittenalgorithmus

Post by Anonymous »

Ich implementiere eine Stabschneidoptimierung in Python mit dem FFD -Algorithmus (FIT DEMING MEHRSCHAFT). Ich habe eine Liste von Bestellungen mit jeweils eine Menge und Länge. Der Code sollte alle Teile in Balken mit fester Länge (6000 mm) zuweisen und einen Bericht erstellen. Loops laufen, aber bestimmte Teile werden nie hinzugefügt. < /li>
Gegenüberprüfung zeigt fehlende Mengen.

Code: Select all

# -*- coding: utf-8 -*-
import pandas as pd
from collections import Counter

def optimize_cuts(stock_length, required_pieces):
"""
Calculates the bar cutting plan using the First Fit Decreasing algorithm.
"""
# 1. Create a complete list of individual pieces
pieces_list = []
for item in required_pieces:
for _ in range(item['quantity']):
pieces_list.append({
'name': item['name'],
'length': item['length']
})

# 2. Sort pieces from largest to smallest
pieces_list.sort(key=lambda x: x['length'], reverse=True)

used_bars = []

# 3.  Allocate each piece
for piece in pieces_list:
allocated = False
# Try to fit the piece in an existing bar
for bar in used_bars:
used_length = sum(p['length'] for p in bar)
if stock_length - used_length >= piece['length']:
bar.append(piece)
allocated = True
break

# If it doesn't fit in any existing bar, create a new one
if not allocated:
used_bars.append([piece])

return used_bars

def generate_tabular_report(stock_length, cutting_plan):
"""
Formats the cutting plan result into a table (DataFrame) using pandas.
"""
print("=" * 80)
print("               BAR CUTTING OPTIMIZATION PLAN (TABULAR)")
print("=" * 80)

table_data = []

for i, bar in enumerate(cutting_plan, 1):
piece_counter = Counter(p['name'] for p in bar)
lengths = {p['name']: p['length'] for p in bar}

row_data = {'SEQUENCE': f'CUT {i}'}

piece_idx = 1
for name, qty in piece_counter.items():
row_data[f'NAME_{piece_idx}'] = name
row_data[f'QTY_{piece_idx}'] = qty
row_data[f'LENGTH_{piece_idx}'] = lengths[name]
piece_idx += 1

total_used = sum(p['length'] for p in bar)
leftover = stock_length - total_used
row_data['TOTAL USED'] = total_used
row_data['LEFTOVER'] = leftover

table_data.append(row_data)

if not table_data:
print("No pieces to process.")
return

df = pd.DataFrame(table_data)

max_pieces = (len(df.columns) - 3) // 3
ordered_columns = ['SEQUENCE']
for i in range(1, max_pieces + 1):
ordered_columns.extend([f'NAME_{i}', f'QTY_{i}', f'LENGTH_{i}'])
ordered_columns.extend(['TOTAL USED', 'LEFTOVER'])

for col in ordered_columns:
if col not in df.columns:
df[col] = ''

df = df[ordered_columns]
df.fillna('', inplace=True)

print(df.to_string(index=False))

total_bars = len(cutting_plan)
total_leftover = df['LEFTOVER'].astype(float).sum()
overall_utilization = ((total_bars * stock_length - total_leftover) / (total_bars * stock_length)) * 100

print("\n" + "=" * 80)
print("                             SUMMARY")
print("=" * 80)
print(f"Total {stock_length}mm bars needed: {total_bars}")
print(f"Overall material utilization: {overall_utilization:.2f}%")
print("=" * 80)

def verify_quantities(order_list, final_plan):
"""
Checks if the quantity of pieces in the final plan matches the initial order.
"""
requested = {item['name']: item['quantity'] for item in order_list}

allocated_pieces = []
for bar in final_plan:
for piece in bar:
allocated_pieces.append(piece['name'])
allocated = Counter(allocated_pieces)

print("\n" + "=" * 80)
print("                      QUANTITY VERIFICATION")
print("=" * 80)
if dict(allocated) == requested:
print("✅ SUCCESS: All requested quantities were correctly allocated in the plan!")
else:
print("❌ WARNING: There is a mismatch between the order and the final plan.")
for name, qty_requested in requested.items():
qty_allocated = allocated.get(name, 0)
if qty_requested != qty_allocated:
print(f"  - Piece '{name}': Requested={qty_requested}, Allocated={qty_allocated}")
print("="  * 80)

# --- INPUT DATA ---
if __name__ == "__main__":
STOCK_BAR_LENGTH = 6000

order_list = [
{'name': '396', 'quantity': 290, 'length': 6000},
{'name': 'XC21', 'quantity': 1, 'length': 4478},
{'name': 'XC52', 'quantity': 1, 'length': 4308},
{'name': 'XC19', 'quantity': 2, 'length': 4240},
{'name': 'XC22', 'quantity': 1, 'length': 3759},
{'name': 'XC48', 'quantity': 1, 'length': 3698},
{'name': 'XC20', 'quantity': 20, 'length': 3514},
{'name': 'XC10', 'quantity': 1, 'length': 3452},
{'name': 'XC51', 'quantity': 4, 'length': 3382},
{'name': 'XC41', 'quantity': 1, 'length': 3256},
{'name': 'XC15', 'quantity': 1, 'length': 3239},
{'name': 'XC8', 'quantity': 16, 'length': 3076},
{'name': 'XC9', 'quantity': 2, 'length': 2732},
{'name': 'XC1', 'quantity': 1, 'length': 2693},
{'name': 'XC13', 'quantity': 2, 'length': 2631},
{'name': 'XC42', 'quantity': 1, 'length': 2567},
{'name': 'XC40', 'quantity': 1, 'length': 2556},
{'name': 'XC18', 'quantity': 1, 'length': 2531},
{'name': 'XC30', 'quantity': 12, 'length': 2398},
{'name': 'XC49', 'quantity': 1, 'length': 2378},
{'name': 'XC47', 'quantity': 1, 'length': 2310},
{'name': 'XC7', 'quantity': 76, 'length': 2251},
{'name': 'XC29', 'quantity': 12, 'length': 2248},
{'name': 'XC5', 'quantity': 2, 'length': 2142},
{'name': 'XC43', 'quantity': 4, 'length': 2104},
{'name': 'XC14', 'quantity': 1, 'length': 2064},
{'name': 'XC11', 'quantity': 1, 'length': 2063},
{'name': 'XC38', 'quantity': 2, 'length': 2014},
{'name': 'XC16', 'quantity': 2, 'length': 1989},
{'name': 'XC50', 'quantity': 1, 'length': 1875},
{'name': 'XC2', 'quantity': 16, 'length': 1830},
{'name': 'XC46', 'quantity': 1, 'length': 1826},
{'name': 'XC44', 'quantity': 1, 'length': 1825},
{'name': 'XC6', 'quantity': 1, 'length': 1710},
{'name': 'XC35', 'quantity': 2, 'length': 1669},
{'name': 'XC12', 'quantity': 1, 'length': 1659},
{'name': 'XC39', 'quantity': 1, 'length': 1628},
{'name': 'XC17', 'quantity': 1, 'length': 1578},
{'name': 'XC53', 'quantity': 1, 'length': 1546},
{'name': 'XC45', 'quantity': 1, 'length': 1519},
{'name': 'XC3', 'quantity': 1, 'length': 1259},
{'name': 'XC36', 'quantity': 2, 'length': 1170},
{'name': 'XC4', 'quantity': 1, 'length': 1006},
{'name': 'XC37', 'quantity': 1, 'length': 969}
]

final_plan = optimize_cuts(STOCK_BAR_LENGTH, order_list)
generate_tabular_report(STOCK_BAR_LENGTH, final_plan)
verify_quantities(order_list, final_plan)
Warum überspringt FFD einige Teile wie 'xc9'?
Wie kann ich den Code beheben, um sicherzustellen>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post