-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciach.py
More file actions
executable file
·129 lines (105 loc) · 3.28 KB
/
ciach.py
File metadata and controls
executable file
·129 lines (105 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
##!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Daniel Pęcak <daniel.pecak@gmail.com>
import sys
from operator import itemgetter
if len(sys.argv)>1:
inputfile=sys.argv[1]
else:
sys.exit("Nie podano pliku wejsciowego.")
if len(sys.argv)>2:
outputfile=sys.argv[2]
else:
outputfile='output.txt'
# File format
# === beginning
# default_length_of_bar how_many cut_length
# length1 how_many1
# length2 how_many2
# length3 how_many3
# ...
# === ending
with open(inputfile,'r') as fp:
input=fp.readlines()
cpinput = []
count3=0
for i in input:
line = i[:-1].split()
if len(line)==2:
cpinput.append((map(int,line)))
if len(line)==3:
count3 += 1
L=int(line[0])
N=int(line[1])
dl=int(line[2])
if len(line)>3: sys.exit("Zly format pliku wejsciowego: za duzo argumentow w linii.")
if(count3!=1): sys.exit("Pierwsza linia pliku jest nieprawidlowa. \n Format: dlugosc profilu [mm] liczba profili | szerokosc ciecia [mm] ")
sumA=L*N
sumN=0.0
items=0
for i in cpinput:
sumN += i[0]*i[1]
items += i[1]
if(sumA<sumN): sys.exit("SPAWN MORE pretow! Wincyj pretow!")
cpaval=[L]*N*2 # TODO maybe change items to items?
f = open(outputfile,'w')
f.write("###### Informacje ###### \n")
f.write("### Dlugosc profilu: {} mm \n### liczba profili: {} \n### Szerokosc ciecia: {} mm\n\n".format(L,N,dl))
f.write("### Sortowanie od najwiekszego kawalka [dlugosc, #sztuk]:\n")
cpinput=sorted(cpinput,key=itemgetter(0),reverse=True)
for i in xrange(len(cpinput)):
f.write("Item{}: {}\n".format(str(i+1), cpinput[i]))
f.write("\n")
out=[[] for _ in xrange(items)]
# Greedy algorithm:
# loop through the working list subtracting numbers
scraps = 0.0
for it in xrange(items):
iwanttobreakfree=False
for i in xrange(len(cpinput)):
if iwanttobreakfree: continue
if(cpinput[i][1]>0):
for y in xrange(items):
if iwanttobreakfree: continue
if(cpaval[y]>=cpinput[i][0]):
cpinput[i][1] = cpinput[i][1] - 1
cpaval[y] = cpaval[y] - cpinput[i][0]
out[y].append(cpinput[i][0])
iwanttobreakfree=True
if(cpaval[y]>=dl):
cpaval[y] = cpaval[y] - dl
scraps += dl
# print " "
# for ii in xrange(len(cpinput)):
# print "Item"+str(ii+1)+":", cpinput[ii]
# print " "
# for i in xrange(len(cpaval)):
# print cpaval[i]
# print out
# for i in xrange(items):
# print out[i]
f.write("\nScinki (lacznie): {} mm\n".format(scraps))
scraps = -scraps
rods=0
f.write("### Ciachaj kolejne profile w ten sposób: \n")
i = 0
for o in out:
if len(o)>0:
i+=1
txt = "Profil nr {} ({}/{}): ".format(i,sum(o),L)
txt += " | ".join(list(map(str,o)))
# print txt
f.write(txt+'\n')
scraps += L-sum(o)
rods+=1
f.write("\nResztki: {} mm".format(scraps))
f.write("\nPotrzebnych profili: {}".format(rods))
f.write("\nWydajnosc: {0:.2f} %\n".format(100.*(rods*L-scraps)/(rods*L)))
f.close()
# output format:
# how many bars needed, how many scaps, efficiency
# bar1 length1 | length2 | .... | lengthN1
# bar2 length1 | length2 | .... | lengthN2
# .......
# barM length1 | length2 | .... | lengthNM
# TODO test examples