-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolving.py
292 lines (243 loc) · 9.64 KB
/
Solving.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 20 22:10:45 2019
@author: sev
"""
from Node import Node
from copy import copy
import numpy as np
from time import time
from Graph import create_graph_instance
from copy import deepcopy as dcopy
np.random.seed(0)
def copy_domains(D):
new_D = []
for d in D:
new_D.append(copy(d))
return new_D
def choose_node(nodes_list,search_strat,nchildren):
if search_strat == 0: #en largeur
node = nodes_list.pop(0)
return node
elif search_strat == 1: #en profondeur
node = nodes_list.pop()
return node
elif search_strat == 2: #aléatoire sur les derniers fils
if nchildren==0:
node = nodes_list.pop()
return node
else:
rdn=np.random.randint(0,nchildren)
node = nodes_list.pop(-rdn-1)
return node
else:
print("cette stratégie n'existe pas")
return []
def nb_col(solution,I):
col = 0
for x in range(I.N):
if solution.Domains[x][0] > col:
col = solution.Domains[x][0]
return col
def print_sol(solution,I):
tab=np.zeros((I.N,I.N))
for x in range(I.N):
print("Variable "+str(x)+" affectée à la valeur "+str(solution.Domains[x][0]))
tab[x][solution.Domains[x][0]-1]=1
print(tab)
def solve(I,branching_strat,var_strat,search_strat,look_ahead_strat,dynamic_search, time_limit=100000000000):
t_ini = time()
if dynamic_search:
branching_strat = 2
var_strat = 1
search_strat = 1
look_ahead_strat = 1
root_node = Node('',copy_domains(I.Domains))
nodes_list = [root_node]
found_feas = False
nbr_nodes = 0
nbr_fails = 0
count_FC = 0
nchildren=1
br_time=0
ac_time=0
fc_time=0
br3_time=0
#print(root_node.Domains)
while nodes_list != [] and not found_feas and time()-t_ini<time_limit:
#print([node.ID for node in nodes_list])
current_node = choose_node(nodes_list,search_strat,nchildren)
#print(current_node.ID)
# print("itération "+str(nbr_nodes)+"\n")
#print("My ID",current_node.get_ID())
#print("My domain before AC",current_node.Domains)
# *** stratégie look ahead ***
if nbr_nodes<0:
b1=time()
current_node.AC3(I)
b2=time()
ac_time+=b2-b1
else:
if look_ahead_strat == 0:
b1=time()
current_node.AC3(I)
b2=time()
ac_time+=b2-b1
elif look_ahead_strat == 1:
b1=time()
count_FC+=current_node.FC(I)
b2=time()
fc_time+=b2-b1
elif look_ahead_strat == 2:
b1=time()
count_FC+=current_node.FC(I)
b2=time()
fc_time+=b2-b1
b1=time()
current_node.AC3(I)
b2=time()
ac_time+=b2-b1
elif look_ahead_strat == 3:
b1=time()
count_FC+=current_node.FC(I)
b2=time()
fc_time+=b2-b1
if len(current_node.ID)%30==0:
b1=time()
current_node.AC3(I)
b2=time()
ac_time+=b2-b1
else:
print("Pas de stratégie valide")
#print("My domain after AC",current_node.Domains)
#input()
current_node.set_status()
status = current_node.get_status()
if status == 1: # solution found
found_feas = True
solution = current_node
elif status == -1: # unfeasible
nbr_fails += 1
del current_node #libère la mémoire
nchildren=0
if nbr_nodes == 0: # on est à la racine ==> infaisable
break
else:
b1=time()
var_ID, var_value = current_node.find_branch(var_strat)
b3=time()
#current_node.branch(var_ID,var_value,branching_strat)
#nchildren=len(current_node.branches)
#for k in range(nchildren):
# nodes_list.append(current_node.branches[k])
new_nodes=current_node.branch(var_ID,var_value,branching_strat)
nchildren=len(new_nodes)
nodes_list+=new_nodes
b2=time()
br_time+=b2-b1
br3_time+=b3-b1
del current_node #libère la mémoire
nbr_nodes += 1
if dynamic_search and nbr_nodes%10000==0:
branching_strat=1
search_strat=2
n=nodes_list.pop(0)
nodes_list.append(n)
if nbr_nodes%100000==0:
print("Progress report")
print(nbr_nodes)
print("Noeuds en memoire "+str(len(nodes_list)))
print("Il y a eu "+str(nbr_nodes)+" noeud(s) exploré(s)")
print("Il y a eu "+str(nbr_fails)+" échec(s)")
print("Le FC a enlevé "+str(count_FC)+" fois des variables")
print("Temps passé à brancher seulement : "+str(br_time))
print("Temps passé à brancher seulement (br3) : "+str(br3_time))
print("Temps passé sur l'AC : "+str(ac_time))
print("Temps passé sur le FC : "+str(fc_time)+"\n")
print("Temps total écoulé : "+str(time()-start))
if found_feas:
# print_sol(solution,I)
print("Il y a eu "+str(nbr_nodes)+" noeud(s) exploré(s)")
print("Il y a eu "+str(nbr_fails)+" échec(s)")
print("Le FC a enlevé "+str(count_FC)+" fois des variables")
print("Temps passé à brancher seulement : "+str(br_time))
print("Temps passé sur l'AC : "+str(ac_time))
print("Temps passé sur le FC : "+str(fc_time)+"\n")
return solution, nb_col(solution,I), nbr_nodes,nbr_fails,br_time,ac_time,fc_time
elif time()-t_ini >= time_limit:
# print("Temps limite atteint")
return [], 0, 0, 0, 0, 0, 0
else:
print("***** Le problème est infaisable *****")
print("Il y a eu "+str(nbr_nodes)+" noeud(s) exploré(s)")
print("Il y a eu "+str(nbr_fails)+" échec(s)")
print("Le FC a enlevé "+str(count_FC)+" fois des variables")
print("Temps passé à brancher seulement : "+str(br_time))
print("Temps passé sur l'AC : "+str(ac_time))
print("Temps passé sur le FC : "+str(fc_time)+"\n")
return [], 0, 0, 0, 0, 0, 0
def coloring_graph(filename,branching_strat,var_strat,search_strat,look_ahead_strat,dynamic_search,time_limit_int,time_limit_tot):
t_ini = time()
it = 1
time_it = []
nb_nodes_it=[]
print("\n"+"Résolution numéro : "+str(it))
I = create_graph_instance(filename)
print("Temps de création : "+str(time()-t_ini))
LB = I.lb
t2 = time()
sol, nb_col, nbr_nodes,nbr_fails,br_time,ac_time,fc_time = solve(I,branching_strat,var_strat,
search_strat,look_ahead_strat,dynamic_search,time_limit_int)
t3 = time()
time_it.append(t3-t2)
print("Nombre de couleurs utilisées : "+str(nb_col))
print("Nombre de noeuds : "+str(nbr_nodes))
bestcol = nb_col
col_it = []
col_it.append(nb_col)
nb_nodes_it.append(nbr_nodes)
while sol != [] and nb_col != LB and time()-t_ini < time_limit_tot:
bestsol = dcopy(sol.Domains)
t1 = time()
I = create_graph_instance(filename,nb_col-1)
it += 1
if I!=[]:
t2 = time()
print("\n"+"Résolution numéro : "+str(it))
print("Temps de création : "+str(t2-t1))
sol, nb_col, nbr_nodes,nbr_fails,br_time,ac_time,fc_time = solve(I,branching_strat,var_strat,
search_strat,look_ahead_strat,dynamic_search,time_limit_int)
t3 = time()
time_it.append(t3-t2)
print("Nombre de couleurs utilisées : "+str(nb_col))
print("Nombre de noeuds : "+str(nbr_nodes))
#print_sol(sol, I)
print("Temps de résolution : "+str(t3-t2))
col_it.append(nb_col)
nb_nodes_it.append(nbr_nodes)
if (nb_col > 0 and nb_col < bestcol):
bestcol = nb_col
else:
t2 = time()
print("Temps de création : "+str(t2-t1))
print("Problème infaisable par clique max")
print("\n"+"Nombre de couleurs optimal : "+ str(bestcol))
if time()-t_ini >= time_limit_tot or t3-t2 >= time_limit_int:
return 0, time()-t_ini, it, time_it, col_it, nb_nodes_it
else:
return bestcol, time()-t_ini, it, time_it, col_it, nb_nodes_it
def sat_graph(filename,nb_col_max,branching_strat,var_strat,search_strat,look_ahead_strat,dynamic_search,time_limit=10000000):
t1 = time()
I = create_graph_instance(filename, nb_col_max)
if I!=[]:
t2 = time()
print("Temps de création : "+str(t2-t1))
sol, nb_col, nbr_nodes,nbr_fails,br_time,ac_time,fc_time = solve(I, branching_strat, var_strat, search_strat, look_ahead_strat, dynamic_search, time_limit)
t3 = time()
print("Nombre de couleurs utilisées : "+str(nb_col))
print("Temps de résolution : "+str(t3-t2))
else:
t2 = time()
print("Temps de création : "+str(t2-t1))
print("Problème infaisable par clique max")
return 0