-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_programming.py
128 lines (100 loc) · 3.29 KB
/
dynamic_programming.py
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
#!coding: utf-8
import pickle
import psyco
psyco.full()
#Chargement des scores sauvegardés
f=open('scores.pck')
liste_scores = pickle.load(f)
f.close()
liste_scores.sort()
#Max scores at ending point
posmax = 0
sizes = set([])
d = {}
for (pos, size, score, prediction) in liste_scores:
d[pos] = {0 : [[], [], 0],
1 : [[], [], 0],
2 : [[], [], 0],
3 : [[], [], 0],
4 : [[], [], 0],
5 : [[], [], 0],
6 : [[], [], 0]}
sizes.add(size)
if pos>posmax:
posmax=pos
d[0] = {0 : [[], [], 0],
1 : [[], [], 0],
2 : [[], [], 0],
3 : [[], [], 0],
4 : [[], [], 0],
5 : [[], [], 0],
6 : [[], [], 0]}
sizes = list(sizes)
sizes.sort()
sizemax = sizes[-1]
for (pos, size, score, prediction) in liste_scores:
#Pour mettre à jour le plus haut score, il faut que:
#- le score de l'intervalle considéré soit plus grand que le score courant (LE PLUS HAUT SCORE)
#- il y ait une entrée dans le dico correspondant au début de l'intervalle considéré (LES INTERVALLES SE TOUCHENT)
#- l'intervalle considéré
#print "position considérée", pos
if d.has_key(pos-size):
#Trajectoires précédentes
precedent = d[pos-size]
#Rajout de la trajectoire considéré à la précédente
for [sommets, predicts, old_score] in precedent.values():
path_length_old = len(predicts)
#print 'path_length_old: ', path_length_old
if path_length_old < 6:
if d[pos][path_length_old+1][2] < old_score + score:
d[pos][path_length_old+1] = [sommets+[pos], predicts+[prediction], old_score+score]
## print "append at ", pos,
## raw_input()
del precedent
segs, preds, score = d[posmax][6]
print
print "##########################"
print " Programmation dynamique: "
print "##########################"
print "Segmentation: ", segs
print "Prediction: ", "".join(preds)
print "Score total: ", score
####ESSAI CHRONOLOGIQUE
##POSITION = 0
##pred = ""
##while POSITION != posmax:
## l=[(score, size, pos, prediction) for (pos, size, score, prediction) in liste_scores if pos-size==POSITION]
## #print l
## l.sort(reverse=True)
## print l[:2]
## (score, size, pos, prediction) = l[0]
## pred += prediction
## POSITION += size
##
##print
##print "######################"
##print " Essai chronologique: "
##print "######################"
##print "Prediction: ", pred
##VRAIE COUPE
print
print "##############"
print " Vraie coupe: "
print "##############"
aretes_good = [0, 11, 28, 38, 51, 79, posmax]
#aretes_good = [0, 16, 30, 45, 70, 83, posmax]
l=[]
for index, elem in enumerate(aretes_good[1:]):
l.append((aretes_good[index], aretes_good[index+1]))
aretes_good = l
SCORE = 0
pred = ""
for beg, fin in aretes_good:
print beg, fin
[(pos, size, score, prediction)] = [(pos, size, score, prediction) for (pos, size, score, prediction) in liste_scores if pos == fin and pos-size==beg]
print prediction, score
SCORE += score
pred += prediction
print
print "Prediction: ", pred
print "Score total: ", SCORE