-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateConstraint.py
200 lines (172 loc) · 5.94 KB
/
GenerateConstraint.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
import numpy as np
import random
def generateMustlinkCannotlink(X, Y):
n = len(X)
ml = np.zeros((n, n))
cl = np.zeros((n, n))
for i in range(n):
for j in range(n):
if Y[i] == Y[j]:
fuzzy_degree = random.uniform(0, 1)
ml[i][j] = fuzzy_degree
ml[j][i] = fuzzy_degree
cl[i][j] = -1
cl[j][i] = -1
else:
fuzzy_degree = random.uniform(0, 1)
cl[i][j] = fuzzy_degree
cl[j][i] = fuzzy_degree
ml[i][j] = -1
ml[j][i] = -1
return ml, cl
def generateMustlinkCannotlink(X,Y):
n = len(X)
ml = np.zeros((n, n))
cl = np.zeros((n, n))
# generate two int random i and j
for i in range(n):
for j in range(n):
if Y[i] == Y[j]:
fuzzy_degree = random.uniform(0, 1)
ml[i][j] = fuzzy_degree
ml[j][i] = fuzzy_degree
cl[i][j] = -1
cl[j][i] = -1
else:
fuzzy_degree = random.uniform(0, 1)
cl[i][j] = fuzzy_degree
cl[j][i] = fuzzy_degree
ml[i][j] = -1
ml[j][i] = -1
return ml,cl
def generateRandomConstraints(x, y, max_constraint=30):
LINK_ARRAY_SIZE = max_constraint
samples = np.random.choice(len(x), LINK_ARRAY_SIZE)
must_links = []
must_links_index = []
ml_matrix = np.zeros((len(x), len(x)))
cl_matrix = np.zeros((len(x), len(x)))
ml_dic = {}
cl_dic = {}
for sample in samples:
target_sample = y[sample]
is_selected = [-1] * len(x)
while -1 in is_selected:
selected = np.random.choice(len(x), 1)[0] # check selected not selected before
if is_selected[selected] != -1:
continue
# new
flag = 0
for src, des in must_links_index:
# flag = 0
if src == sample and des == selected:
#print("must continue:", src, des)
flag = 1
break
if flag:
flag = 0
continue
# end new
is_selected[selected] = 1
if target_sample == y[selected]:
if sample == selected:
continue
ml = [np.asarray(x[sample]), np.asarray(x[selected])]
ml_index = (sample, selected)
must_links.append(ml)
must_links_index.append(ml_index)
degree = random.uniform(0, 1)
ml_matrix[sample][selected] = degree
ml_matrix[selected][sample] = degree
ml_dic[ml_index]=degree
break
else:
continue
samples = np.random.choice(len(x), LINK_ARRAY_SIZE) # DOIT
cannot_links = []
cannot_links_index = []
for sample in samples:
target_sample = y[sample]
is_selected = [-1] * len(x)
while -1 in is_selected:
selected = np.random.choice(len(x), 1)[0]
if is_selected[selected] != -1:
continue
# new
flag = 0
for src, des in cannot_links_index:
flag = 0
if src == sample and des == selected:
#print("cannot continue:", src, des)
flag = 1
break
if flag:
flag = 0
continue
# end new
is_selected[selected] = 1
if target_sample != y[selected]:
cl = [np.asarray(x[sample]), np.asarray(x[selected])]
cl_index = (sample, selected)
cannot_links.append(cl)
cannot_links_index.append(cl_index)
degree = random.uniform(0, 1)
cl_matrix[sample][selected] = degree
cl_matrix[selected][sample] = degree
cl_dic[cl_index] = degree
break
else:
continue
# TODO
links = {'must_links_index': must_links_index, 'cannot_links_index': cannot_links_index}
# np.save('best_'+dataset+str(LINK_ARRAY_SIZE), links)
# m_c_links = np.load('C:\\Users\Amin\Documents\PycharmProjects\constrained_clustering\\best_iris30.npy').item()
# ml_idx = m_c_links['must_links_index']
# cl_idx = m_c_links['cannot_links_index']
n = len(x)
for i in range(n):
for j in range(n):
if ml_matrix[i][j] == 0.0:
ml_matrix[i][j] = -1.0
if cl_matrix[i][j]:
cl_matrix[i][j] = -1.0
return must_links_index, cannot_links_index, ml_matrix, cl_matrix, ml_dic, cl_dic
def transitive_entailment_graph(ml, cl, dslen):
ml_graph = {}
cl_graph = {}
for i in range(dslen):
ml_graph[i] = set()
cl_graph[i] = set()
for (i, j) in ml:
ml_graph[i].add(j)
ml_graph[j].add(i)
def dfs(v, graph, visited, component):
visited[v] = True
for j in graph[v]:
if not visited[j]:
dfs(j, graph, visited, component)
component.append(v)
visited = [False] * dslen
neighborhoods = []
for i in range(dslen):
if not visited[i]:
component = []
dfs(i, ml_graph, visited, component)
for x1 in component:
for x2 in component:
if x1 != x2:
ml_graph[x1].add(x2)
neighborhoods.append(component)
for (i, j) in cl:
cl_graph[i].add(j)
cl_graph[j].add(i)
for y in ml_graph[j]:
cl_graph[i].add(y)
cl_graph[y].add(i)
for x in ml_graph[i]:
cl_graph[x].add(j)
cl_graph[j].add(x)
for y in ml_graph[j]:
cl_graph[x].add(y)
cl_graph[y].add(x)
return ml_graph, cl_graph, neighborhoods