-
Notifications
You must be signed in to change notification settings - Fork 0
/
jonas.py
117 lines (111 loc) · 6.85 KB
/
jonas.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
import utils
import numpy as np
from algorithm import QuantumAlgorithm
# the methods in this class is mostly based on code by Jonas Nüßlein
class Jonas(QuantumAlgorithm):
def __init__(self, seed, num_graph_sizes, solver="qbsolv", num_coalitions=None, timeout=600):
super().__init__(seed=seed, num_graph_sizes=num_graph_sizes, solver=solver, num_coalitions=num_coalitions, timeout=timeout)
self.name = f"ours_n_half_{self.solver}"
# based on code by Jonas Nüßlein
def solve(self, num_agents, edges):
if not self.num_coalitions: # self.num_coalitions is still None
self.num_coalitions = num_agents // 2
# create the QUBO
Q = {}
# sum of all the edges absolute values to use as a penalty value later
penalty = np.sum(np.abs(list(edges.values())))
# iterate over agents vertically (rows)
for i in range(num_agents):
# iterate over coalition groups vertically (rows)
for c_i in range(self.num_coalitions):
# get number of logical qubit vertically (rows)
p_i = c_i * num_agents + i
# iterate over agents horizontally (columns)
for j in range(num_agents):
# iterate over coalition groups horizontally (columns)
for c_j in range(self.num_coalitions):
# get number of logical qubit horizontally (columns)
p_j = c_j * num_agents + j
# if we are in the upper triangular matrix looking at
# two different agents and the same coalition
if p_i < p_j and i != j and c_i == c_j:
# put the negative of the edge weight in the graph as an incentive
# to put the agents in the same coalition if the edge weight is > 0
utils.add(Q, p_i, p_j, -edges[(i,j)])
# if we are in the upper triangular matrix looking at
# the same agent and two different coalitions
elif p_i < p_j and i == j and c_i != c_j:
# add sum of all the edges absolute values as a penalty value to ensure that one agent cannot be in two coalitions at once
utils.add(Q, p_i, p_j, penalty)
# solve the QUBO
solution = self.solve_qubo(Q, self.num_coalitions * num_agents)
# make a list of coalitions, with each coalition being a list with the numbers of the agents in these coalitions
# TODO: Add singletons to coalitions list (currently not added if their value is 00000...)
# Is no big problem, cause singletons are irrelevant for CS value and can be easily obtained by check what nodes are "missing" from coalitions list, but not pretty
coalitions = []
for c in range(self.num_coalitions):
coalition = [k for k in range(num_agents) if solution[c * num_agents:(c + 1) * num_agents][k] == 1]
coalitions.append(coalition)
return coalitions
def measure_embedding_run(self, num_agents, edges):
if not self.num_coalitions: # self.num_coalitions is still None
self.num_coalitions = num_agents // 2
# create the QUBO
Q = {}
# sum of all the edges absolute values to use as a penalty value later
penalty = np.sum(np.abs(list(edges.values())))
# iterate over agents vertically (rows)
for i in range(num_agents):
# iterate over coalition groups vertically (rows)
for c_i in range(self.num_coalitions):
# get number of logical qubit vertically (rows)
p_i = c_i * num_agents + i
# iterate over agents horizontally (columns)
for j in range(num_agents):
# iterate over coalition groups horizontally (columns)
for c_j in range(self.num_coalitions):
# get number of logical qubit horizontally (columns)
p_j = c_j * num_agents + j
# if we are in the upper triangular matrix looking at
# two different agents and the same coalition
if p_i < p_j and i != j and c_i == c_j:
# put the negative of the edge weight in the graph as an incentive
# to put the agents in the same coalition if the edge weight is > 0
utils.add(Q, p_i, p_j, -edges[(i,j)])
# if we are in the upper triangular matrix looking at
# the same agent and two different coalitions
elif p_i < p_j and i == j and c_i != c_j:
# add sum of all the edges absolute values as a penalty value to ensure that one agent cannot be in two coalitions at once
utils.add(Q, p_i, p_j, penalty)
self.measure_embedding(Q)
def get_qubo(self, num_agents, edges):
if not self.num_coalitions: # self.num_coalitions is still None
self.num_coalitions = num_agents // 2
# create the QUBO
Q = {}
# sum of all the edges absolute values to use as a penalty value later
penalty = np.sum(np.abs(list(edges.values())))
# iterate over agents vertically (rows)
for i in range(num_agents):
# iterate over coalition groups vertically (rows)
for c_i in range(self.num_coalitions):
# get number of logical qubit vertically (rows)
p_i = c_i * num_agents + i
# iterate over agents horizontally (columns)
for j in range(num_agents):
# iterate over coalition groups horizontally (columns)
for c_j in range(self.num_coalitions):
# get number of logical qubit horizontally (columns)
p_j = c_j * num_agents + j
# if we are in the upper triangular matrix looking at
# two different agents and the same coalition
if p_i < p_j and i != j and c_i == c_j:
# put the negative of the edge weight in the graph as an incentive
# to put the agents in the same coalition if the edge weight is > 0
utils.add(Q, p_i, p_j, -edges[(i,j)])
# if we are in the upper triangular matrix looking at
# the same agent and two different coalitions
elif p_i < p_j and i == j and c_i != c_j:
# add sum of all the edges absolute values as a penalty value to ensure that one agent cannot be in two coalitions at once
utils.add(Q, p_i, p_j, penalty)
return Q