-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcop.py
805 lines (532 loc) · 22.3 KB
/
dcop.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 9 20:34:12 2018
@author: yossi
"""
import pandas as pd
import numpy as np
import random
import time
from matplotlib import pyplot as plt
import datetime
class DCOP_Algorithm:
def __init__(self, num_iterations, problem):
self.num_iterations = num_iterations
self.problem = problem
self.costs = []
def solve(self):
pass
def get_problem(self):
return self.problem
def get_num_iterations(self):
return self.get_num_iterations
def calc_total_costs(self):
self.costs.append(self.problem.get_total_costs())
def random_assignment(self, nodes):
for node in nodes:
node.get_owner().assign_random()
def send_messages(self, nodes):
for node in nodes:
node.get_owner().send_message()
def find_best_alternative(self, nodes):
for node in nodes:
node.get_owner().find_best_alternative()
def do_alternative(self, nodes):
for node in nodes:
node.get_owner().do_alternative()
def show_solution_graph(self):
plt.title(self.__class__.__name__)
plt.plot(range(0, self.num_iterations), self.costs)
plt.show()
def show_problem_details(self):
self.problem.get_graph().show_graph_details()
def empty_mailboxs(self, nodes):
for node in nodes:
node.get_owner().empty_mailbox()
def total_costs_update(self, nodes):
for node in nodes:
node.get_owner().get_total_costs()
def get_costs(self):
return self.costs
class DSA(DCOP_Algorithm):
def __init__(self, num_iterations, problem, a_type):
self.a_type = a_type
super(DSA, self).__init__(num_iterations, problem)
def solve(self):
graph = self.problem.get_graph()
nodes = graph.get_nodes()
self.random_assignment(nodes)
self.total_costs_update(nodes)
for i in range(0, self.num_iterations):
self.empty_mailboxs(nodes)
self.send_messages(nodes)
self.find_best_alternative(nodes)
self.do_alternative(nodes)
self.calc_total_costs()
return self.problem.solution()
def do_alternative(self, nodes):
for node in nodes:
node.get_owner().do_alternative(0.7, "dsa")
def get_type(self):
return self.a_type
class MGM(DCOP_Algorithm):
def solve(self):
graph = self.problem.get_graph()
nodes = graph.get_nodes()
self.random_assignment(nodes)
self.total_costs_update(nodes)
for i in range(0, self.num_iterations):
self.empty_mailboxs(nodes)
self.send_messages(nodes)
self.find_best_alternative(nodes)
self.empty_improvments(nodes)
self.send_improvment(nodes)
self.do_alternative(nodes)
self.calc_total_costs()
return self.problem.solution()
def send_improvment(self, nodes):
for node in nodes:
node.get_owner().send_improvment()
def do_alternative(self, nodes):
for node in nodes:
node.get_owner().do_alternative(type_a="mgm")
def get_type(self):
return self.a_type
def empty_improvments(self, nodes):
for node in nodes:
node.get_owner().empty_improvments()
class MGM2(MGM):
def solve(self):
graph = self.problem.get_graph()
nodes = graph.get_nodes()
self.random_assignment(nodes)
self.total_costs_update(nodes)
for i in range(0, self.num_iterations):
self.am_i_a_proposer(nodes)
self.send_friend_request(nodes)
self.decide_friend_request(nodes)
self.empty_mailboxs(nodes)
self.send_messages(nodes)
self.find_best_alternative(nodes)
self.empty_improvments(nodes)
self.send_improvment(nodes)
self.do_alternative(nodes)
self.unfriend(nodes)
self.calc_total_costs()
return self.problem.solution()
def am_i_a_proposer(self, nodes):
for node in nodes:
node.get_owner().be_a_proposer()
def send_friend_request(self, nodes):
for node in nodes:
node.get_owner().send_friend_request()
def decide_friend_request(self, nodes):
for node in nodes:
node.get_owner().decide_friend_request(0)
def unfriend(self, nodes):
for node in nodes:
node.get_owner().unfriend()
def send_improvment(self, nodes):
for node in nodes:
node.get_owner().send_improvment("mgm2")
def do_alternative(self, nodes):
for node in nodes:
node.get_owner().do_alternative(type_a="mgm2")
class DBA(MGM):
def solve(self):
graph = self.problem.get_graph()
nodes = graph.get_nodes()
self.random_assignment(nodes)
self.total_costs_update(nodes)
for i in range(0, self.num_iterations):
self.empty_mailboxs(nodes)
self.send_messages(nodes)
self.find_best_alternative(nodes)
self.empty_improvments(nodes)
self.send_improvment(nodes)
self.do_alternative(nodes)
self.calc_total_costs()
self.check_for_QLO(nodes)
return self.problem.solution()
def check_for_QLO(self, nodes):
for node in nodes:
if node.get_owner().get_R_mine() <= 0:
QLO = True
for neighbor in node.get_owner().get_neighbors():
if neighbor["agent"].get_R_mine() > 0:
QLO = False
break
if QLO:
self.adjust_broken_constraints(node.get_owner())
def adjust_broken_constraints(self, agent):
mul_const = 2
for neighbor in agent.get_neighbors():
table = neighbor["costs_table"]
table = table * mul_const
class Agent:
def __init__(self, id_n, domain_size):
inf = 10000000000000
self.id = id_n
self.domain_size = domain_size
self.assignment = None
self.best_alternative = None
self.total_costs = inf
self.best_alternative_costs = inf
self.proposer = False
self.new_friend = None
self.been_asked_for_friendship = False
self.mailbox = []
self.neighbors = []
self.improvments = []
def assign_random(self):
self.assignment = random.randint(0, self.domain_size - 1)
def assign(self, assignment):
self.assignment = assignment
def get_improvment(self, improvment):
self.improvments.append(improvment)
def get_R_mine(self):
r = self.total_costs - self.best_alternative_costs
return r
def send_improvment(self, a_type=""):
if a_type == "mgm2" and self.new_friend != None:
diff_1 = self.total_costs - self.best_alternative_costs
diff = diff_1 + self.new_friend.total_costs - \
self.new_friend.get_best_alternative_costs()
else:
diff = self.total_costs - self.best_alternative_costs
for neighbor in self.neighbors:
if (a_type == "mgm2" and neighbor["agent"] != self.new_friend) \
or a_type == "":
neighbor["agent"].get_improvment(diff)
def get_total_costs(self):
total_costs = 0
for neighbor in self.neighbors:
assignment = self.assignment
other_assignment = neighbor["agent"].get_assignment()
costs_table = neighbor["costs_table"]
total_costs += costs_table[other_assignment][assignment]
self.total_costs = total_costs
return self.total_costs
def empty_mailbox(self):
self.mailbox = []
def empty_improvments(self):
self.improvments = []
def send_message(self):
for neighbor in self.neighbors:
message = Message(self, neighbor["costs_table"], self.assignment)
neighbor["agent"].get_message(message)
def get_message(self, message):
self.mailbox.append(message)
def find_best_alternative(self):
# total_costs = 0
sum_of_all = pd.Series(0, index=range(0, self.domain_size))
for message in self.mailbox:
costs_table = message.get_costs_table()
assignment = message.get_node_assignment()
series = costs_table.iloc[assignment]
sum_of_all += series
self.best_alternative = sum_of_all.idxmin()
self.best_alternative_costs = sum_of_all[self.best_alternative]
def do_alternative(self, p=None, type_a=None):
if type_a == "dsa":
if self.best_alternative_costs <= self.total_costs \
and random.random() <= p:
self.assignment = self.best_alternative
elif type_a == "mgm":
R_mine = self.total_costs - self.best_alternative_costs
if R_mine > 0 and (len(self.improvments) == 0 or R_mine > max(self.improvments)):
self.assignment = self.best_alternative
elif type_a == "mgm2":
if self.new_friend != None and self.proposer == False:
return
elif self.new_friend != None:
R_mine_1 = self.total_costs - self.best_alternative_costs
R_mine = R_mine_1 + self.new_friend.total_costs - \
self.new_friend.get_best_alternative_costs()
improvments = self.improvments + self.new_friend.get_improvments()
else:
R_mine = self.total_costs - self.best_alternative_costs
improvments = self.improvments
if R_mine > 0 and (len(improvments) == 0 or R_mine > max(improvments)):
self.assignment = self.best_alternative
if self.new_friend != None:
self.new_friend.set_assignment_as_alternative()
def get_improvments(self):
return self.improvments
def set_assignment_as_alternative(self):
self.assignment = self.best_alternative
def add_neighbor(self, agent, costs_table):
self.neighbors.append({"agent": agent, "costs_table": costs_table})
def get_costs_table(self, agent):
for neighbor in self.neighbors:
if neighbor == agent:
return neighbor["costs_table"]
def get_domain_size(self):
return self.domain_size
def get_neighbors(self):
return self.neighbors
def get_assignment(self):
return self.assignment
def get_id(self):
return self.id
def be_a_proposer(self):
lotery = random.random()
if lotery < 0.5:
self.proposer = True
else:
self.proposer = False
def get_friend_request(self, new_friend):
if self.been_asked_for_friendship == False and self.proposer == False:
self.been_asked_for_friendship = True
for neighbor in self.neighbors:
if new_friend == neighbor["agent"]:
self.new_friend = new_friend
return
raise Exception("there is no neighbor like this: " + str(new_friend.get_id()))
def send_friend_request(self):
if self.proposer == True:
if len(self.neighbors) > 1:
rand_index = random.randint(0, len(self.neighbors) - 1)
self.neighbors[rand_index]["agent"].get_friend_request(self)
elif len(self.neighbors) == 1:
self.neighbors[0]["agent"].get_friend_request(self)
def decide_friend_request(self, p):
if self.new_friend != None:
rand = random.random()
if rand <= p:
self.new_friend.new_friend = None
self.new_friend.been_asked_for_friendship = False
self.new_friend = None
self.been_asked_for_friendship = False
def unfriend(self):
self.proposer = False
self.new_friend = None
self.been_asked_for_friendship = False
def get_best_alternative_costs(self):
return self.best_alternative_costs
class Node:
def __init__(self, owner):
self.owner = owner
self.connections = []
def get_owner(self):
return self.owner
def add_connection(self, node, costs_table):
self.connections.append({"node": Node, "costs_table": costs_table})
def remove_connection(self, node):
self.nodes.remove(node)
def get_connections(self):
return self.connections
def get_domain_size(self):
return self.owner.get_domain_size()
class Problem():
def __init__(self, num_of_agents, p1, p2, domain_size):
self.num_of_agents = num_of_agents
self.p1 = p1
self.p2 = p2
self.graph = self.generate_graph(domain_size)
self.final_solution = None
def generate_graph(self, domain_size):
nodes = []
for i in range(0, self.num_of_agents):
agent = Agent(i + 1, domain_size)
node = Node(agent)
nodes.append(node)
for i in range(0, self.num_of_agents):
for j in range(i + 1, self.num_of_agents):
if random.random() <= self.p1:
node_i = nodes[i]
node_j = nodes[j]
agent_i = nodes[i].get_owner()
agent_j = nodes[j].get_owner()
i_domain_size = node_i.get_domain_size()
j_domain_size = node_j.get_domain_size()
data = [[random.randint(1, 10) \
if random.random() < self.p2 else 0 \
for i in range(0, j_domain_size)] \
for j in range(0, i_domain_size)]
if np.sum(data) > 0:
costs_table = pd.DataFrame(data, columns= \
range(0, j_domain_size))
node_i.add_connection(node_j, costs_table)
node_j.add_connection(node_i, costs_table.T)
agent_i.add_neighbor(agent_j, costs_table)
agent_j.add_neighbor(agent_i, costs_table.T)
return Graph(nodes)
def get_graph(self):
return self.graph
def get_total_costs(self):
total_costs = 0
for node in self.graph.get_nodes():
total_costs += node.get_owner().get_total_costs()
return total_costs
def solution(self):
total_costs = 0
assignments = []
for node in self.graph.get_nodes():
total_costs += node.get_owner().get_total_costs()
assignments.append({"node_index": node.get_owner().get_id(), \
"assignment": node.get_owner().get_assignment()})
self.final_solution = {"total_costs": total_costs, "assignments": assignments}
return self.final_solution
class Graph:
def __init__(self, nodes):
self.nodes = nodes
def get_nodes(self):
return self.nodes
def add_node(self, node):
self.nodes.append(node)
def show_graph_details(self):
for node in self.nodes:
agent = node.get_owner()
print("agent id: " + str(agent.get_id()))
print("neighbores: " + str(agent.get_neighbors()))
print("assignment: " + str(agent.assignment))
print("total costs: " + str(agent.total_costs))
print("total alterantive costs: " + str(agent.best_alternative_costs))
print("best alternative: " + str(agent.best_alternative))
class Message:
def __init__(self, agent, costs_table, assignment):
self.agent = agent
self.costs_table = costs_table
self.assignment = assignment
self.time = time.time()
def get_agent(self):
return self.agent
def get_costs_table(self):
return self.costs_table
def get_node_assignment(self):
return self.assignment
def get_time(self):
return self.time
def main():
num_of_problems = 10
num_of_agents = 30
domain_size = 10
iterations = 1000
step = 10
dsa_costs = []
mgm_costs = []
dba_costs = []
mgm2_costs = []
dsa_solution = []
mgm_solution = []
dba_solution = []
mgm2_solution = []
dsa_solution_mean = []
mgm_solution_mean = []
dba_solution_mean = []
mgm2_solution_mean = []
P1 = [0.5]
P2 = np.linspace(0.1, 0.9, num_of_problems - 1)
for p1 in P1:
print("p1 = " + str(p1) + " the hour is: {}".format(datetime.datetime.now().time()))
for i in range(0, num_of_problems):
print("iteration number: {} out of {}".format(i + 1, num_of_problems) + \
" the hour is: {}".format(datetime.datetime.now().time()))
for p2 in P2:
print("p2 = " + str(p2) + \
" the hour is: {}".format(datetime.datetime.now().time()))
print("before the problem was created" + \
" the hour is: {}".format(datetime.datetime.now().time()))
problem = Problem(num_of_agents, p1, p2, domain_size)
print("after the problem was created" + \
" the hour is: {}".format(datetime.datetime.now().time()))
print("Starting DSA" + \
" the hour is: {}".format(datetime.datetime.now().time()))
dsa = DSA(iterations, problem, "c")
dsa_solution.append(dsa.solve()["total_costs"])
print("Starting MGM" + \
" the hour is: {}".format(datetime.datetime.now().time()))
mgm = MGM(iterations, problem)
mgm_solution.append(mgm.solve()["total_costs"])
print("Starting DBA" + \
" the hour is: {}".format(datetime.datetime.now().time()))
dba = DBA(iterations, problem)
dba_solution.append(dba.solve()["total_costs"])
print("Starting MGM2" + \
" the hour is: {}".format(datetime.datetime.now().time()))
mgm2 = MGM2(iterations, problem)
mgm2_solution.append(mgm2.solve()["total_costs"])
dsa_solution_mean.append(dsa_solution)
mgm_solution_mean.append(mgm_solution)
dba_solution_mean.append(dba_solution)
mgm2_solution_mean.append(mgm2_solution)
dsa_solution = []
mgm_solution = []
dba_solution = []
mgm2_solution = []
("finished iterations first graphs the hour is: {}". \
format(datetime.datetime.now().time()))
dsa_solution_mean_df = pd.DataFrame(dsa_solution_mean).mean()
mgm_solution_mean_df = pd.DataFrame(mgm_solution_mean).mean()
dba_solution_mean_df = pd.DataFrame(dba_solution_mean).mean()
mgm2_solution_mean_df = pd.DataFrame(mgm2_solution_mean).mean()
plt.title("P1={} sum of costs".format(p1))
plt.plot(P2, dsa_solution_mean_df, label="DSA-C=0.7")
plt.plot(P2, mgm_solution_mean_df, label="MGM")
plt.plot(P2, dba_solution_mean_df, label="DBA")
plt.plot(P2, mgm2_solution_mean_df, label="MGM-2")
plt.legend(loc=2)
plt.show()
print("the other graphs starting P2=1" + \
" the hour is: {}".format(datetime.datetime.now().time()))
for i in range(0, num_of_problems):
print("the iteration number is: {} out of {}".format(i + 1, num_of_problems) + \
" the hour is: {}".format(datetime.datetime.now().time()))
print("the problem is starting to create" + \
" the hour is: {}".format(datetime.datetime.now().time()))
problem = Problem(num_of_agents, p1, 1, domain_size)
print("the problem is finished to create" + \
" the hour is: {}".format(datetime.datetime.now().time()))
print("DSA is starting" + \
" the hour is: {}".format(datetime.datetime.now().time()))
dsa = DSA(iterations, problem, "c")
dsa.solve()
dsa_costs.append(dsa.get_costs())
print("MGM is starting" + \
" the hour is: {}".format(datetime.datetime.now().time()))
mgm = MGM(iterations, problem)
mgm.solve()
mgm_costs.append(mgm.get_costs())
print("DBA is starting" + \
" the hour is: {}".format(datetime.datetime.now().time()))
dba = DBA(iterations, problem)
dba.solve()
dba_costs.append(dba.get_costs())
print("MGM2 is starting" + \
" the hour is: {}".format(datetime.datetime.now().time()))
mgm2 = MGM2(iterations, problem)
mgm2.solve()
mgm2_costs.append(mgm2.get_costs())
("finished iterations second graphs" + \
" the hour is: {}".format(datetime.datetime.now().time()))
iterations_g = np.arange(0, 1000, step)
dsa_mean = list(pd.DataFrame(dsa_costs).mean())[0::step]
mgm_mean = list(pd.DataFrame(mgm_costs).mean())[0::step]
dba_mean = list(pd.DataFrame(dba_costs).mean())[0::step]
mgm2_mean = list(pd.DataFrame(mgm2_costs).mean())[0::step]
plt.title("P1={}, P2=1 sum of costs over iterations (10 step)".format(p1))
plt.plot(iterations_g, dsa_mean, label="DSA-C=0.7")
plt.plot(iterations_g, mgm_mean, label="MGM")
plt.plot(iterations_g, dba_mean, label="DBA")
plt.plot(iterations_g, mgm2_mean, label="MGM-2")
plt.legend(loc=1)
plt.show()
iterations_g = np.arange(0, 1000, 1)
dsa_mean = list(pd.DataFrame(dsa_costs).mean())
mgm_mean = list(pd.DataFrame(mgm_costs).mean())
dba_mean = list(pd.DataFrame(dba_costs).mean())
mgm2_mean = list(pd.DataFrame(mgm2_costs).mean())
plt.title("P1={}, P2=1 sum of costs over iterations (1 step)".format(p1))
plt.plot(iterations_g, dsa_mean, label="DSA-C=0.7")
plt.plot(iterations_g, mgm_mean, label="MGM")
plt.plot(iterations_g, dba_mean, label="DBA")
plt.plot(iterations_g, mgm2_mean, label="MGM-2")
plt.legend(loc=1)
plt.show()
dsa_costs = []
mgm_costs = []
dba_costs = []
mgm2_costs = []
if __name__ == "__main__":
main()