619 lines
No EOL
10 KiB
Python
619 lines
No EOL
10 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf-8
|
|
|
|
# In[26]:
|
|
|
|
|
|
pageHeight = 28.0
|
|
|
|
|
|
# In[4]:
|
|
|
|
|
|
textHeight = 0.8
|
|
textSpaceheight = 0.6
|
|
figHeight = 25.2
|
|
alpha = 1
|
|
beta = 0
|
|
|
|
|
|
# In[2]:
|
|
|
|
|
|
lineList = [('WL', True),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('Fig', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', False),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('Fig', True),
|
|
('Fig', True),
|
|
('WL', True),
|
|
('WL', True),
|
|
('WL', True)]
|
|
|
|
|
|
# In[8]:
|
|
|
|
|
|
lineAssignInfo = dict()
|
|
figToRefer = {0 : 0}
|
|
|
|
|
|
# In[7]:
|
|
|
|
|
|
n = len(lineList)
|
|
|
|
figAndRefer = dict()
|
|
|
|
# In[53]:
|
|
|
|
|
|
def cost(n):
|
|
global pageHeight
|
|
global lineAssignInfo
|
|
global textHeight
|
|
global textSpaceheight
|
|
global alpha
|
|
global beta
|
|
if n == 0:
|
|
lineAssignInfo[n] = 0
|
|
return 1
|
|
min_cost = float("inf")
|
|
min_p = -1
|
|
cost(n-1)
|
|
for p in range(n):
|
|
if previousSameItem(n) != -1 and p < lineAssignInfo[previousSameItem(n)]:
|
|
pass #cost = float("inf")
|
|
else:
|
|
# 對於所有的東西
|
|
|
|
input_texts_in_p = [k for k, v in lineAssignInfo.items() if v == p]
|
|
words_len_in_p = len(list(filter(
|
|
lambda i : lineList[i][0] == "WL" or lineList[i][0] == "EndOfPara", input_texts_in_p)))
|
|
|
|
figs_len_in_p = len(list(filter( lambda i : lineList[i][0] == "Fig", input_texts_in_p)))
|
|
|
|
input_texts_height_in_p = words_len_in_p * (textHeight + textSpaceheight) \
|
|
+ figs_len_in_p * (figHeight + textSpaceheight) - textSpaceheight
|
|
if (lineList[n][0] in ["WL", "EndOdPara"]) and input_texts_height_in_p + textSpaceheight + textHeight > pageHeight:
|
|
pass #cost = float("inf")
|
|
elif lineList[n][0] == "Fig" and input_texts_height_in_p + textSpaceheight + figHeight > pageHeight:
|
|
pass
|
|
else:
|
|
temp_max_page_no = p#max(p, max(lineAssignInfo.keys()))
|
|
if lineList[n][0] == "Fig":
|
|
print(f"fig n = {n}") #TMP
|
|
temp_total_diff = p - figRefererId(n) + sum([k - v for k, v in figToRefer.items()])
|
|
else:
|
|
temp_total_diff = sum([k - v for k, v in figToRefer.items()])
|
|
|
|
tmp_cost_fun_of_p = temp_max_page_no * beta + temp_total_diff * alpha
|
|
print(f"n=={n}, p={p}, tmpCost = {temp_max_page_no}, {temp_total_diff}, lineAssignInfo = {lineAssignInfo}")
|
|
if min_cost > tmp_cost_fun_of_p:
|
|
min_cost = tmp_cost_fun_of_p
|
|
min_p = p
|
|
lineAssignInfo[n] = min_p
|
|
if lineList[n][0] == "Fig":
|
|
figToRefer[min_p] = lineAssignInfo[figRefererId(n)]
|
|
return min
|
|
|
|
|
|
def figRefererId(fig_id):
|
|
global figAndRefer
|
|
print(fig_id) #TMP
|
|
referer_id = -1
|
|
for i in list(reversed(range(fig_id))):
|
|
if lineList[i][0] in ["WL", "EndOfPara"]:
|
|
referer_id = i
|
|
break
|
|
figAndRefer[fig_id] = referer_id
|
|
return referer_id
|
|
|
|
def previousSameItem(n):
|
|
global lineList
|
|
previous_id = -1
|
|
for i in list(reversed(range(n))):
|
|
if lineList[i][0] == lineList[n][0]:
|
|
previous_id = i
|
|
break
|
|
return previous_id
|
|
|
|
|
|
# In[54]:
|
|
|
|
|
|
cost(100)
|
|
print(lineAssignInfo)
|
|
print(figToRefer)
|
|
print(figAndRefer) |