def generate_flow_matrix(self): print("generating flow") shape = (self.n * self.k, self.n * self.k) array_memory_gb = BigArray.get_size_GB(shape, 16) print(array_memory_gb) if array_memory_gb > 500: ret = BigArray(shape, dtype=np.int16) ret = np.zeros(shape=shape, dtype=np.int16) for k in range(1, self.k + 1): for i, j in itertools.product(range(1, self.n + 1), range(1, self.n + 1)): # NOTE: define interaction between identical items to be 0 because popularity is not relevant here. #if i==j: # x_ik_idx_linear = idx.index_1_to_0(idx.index_1_q_to_l_1(i,k,self.k)) # ret[x_ik_idx_linear][x_ik_idx_linear] = -self.F[i-1][i-1] if i < j: x_ik_idx_linear = idx.index_1_to_0( idx.index_1_q_to_l_1(i, k, self.k)) x_jk_idx_linear = idx.index_1_to_0( idx.index_1_q_to_l_1(j, k, self.k)) ret[x_ik_idx_linear][x_jk_idx_linear] = -self.F[i - 1][j - 1] else: pass # print("%d nonzeros out of %d" % (np.count_nonzero(ret), ret.shape[0]*ret.shape[1])) print("done") return ret
def generate_dwavecsp(self): ''' returns: a weighted constraint matrix generated by dwave's algorithm ''' csp = dwavebinarycsp.ConstraintSatisfactionProblem('BINARY') def Aix_1(*args): return sum(list(args)) == 1 for i in range(1, self.n + 1): args = [] for k in range(1, self.k + 1): var_index = idx.index_1_q_to_l_1(i, k, self.k) - 1 args.append(var_index) csp.add_constraint(Aix_1, args) def Aix_le_s(*args): return sum(list(args)) <= self.bunch_size for k in range(1, self.k + 1): args = [] for i in range(1, self.n + 1): var_index = idx.index_1_q_to_l_1(i, k, self.k) - 1 args.append(var_index) print("adding %d inequality" % k) csp.add_constraint(Aix_le_s, args) print("stitching...") bqm = dwavebinarycsp.stitch(csp, max_graph_size=24) mtx = bqm.to_numpy_matrix() print(mtx) return 0
def initialise_flow_matrix(self): ret = np.zeros((self.m * self.n, self.m * self.n)) for i in range(1, self.n + 1): for j in range(1, self.n + 1): for k in range(1, self.m + 1): for l in range(1, self.m + 1): # X is n by m x_ik = idx.index_1_q_to_l_1(i, k, self.m) x_jl = idx.index_1_q_to_l_1(j, l, self.m) if x_ik == x_jl: ret[x_ik - 1][x_jl - 1] = self.gamma * self.F[ i - 1][j - 1] * self.D[k - 1][k - 1] elif x_ik < x_jl: ret[x_ik - 1][x_jl - 1] = self.F[i - 1][j - 1] * self.D[k - 1][l - 1] ''' np.set_printoptions(threshold=np.inf) print("flow matrix: ", ret) np.set_printoptions(threshold=6) ''' if not (self.linear is None): ret = ret + np.diag(self.linear) np.savetxt("flow.txt", ret, fmt='%d') return ret
def initialise_flow_matrix(F, D): n = F.shape[0] m = D.shape[0] ret = np.zeros((m * n, m * n), dtype=np.int32) for i in range(1, n + 1): for j in range(1, n + 1): for k in range(1, m + 1): for l in range(1, m + 1): # X is n by m x_ik = idx.index_1_q_to_l_1(i, k, m) x_jl = idx.index_1_q_to_l_1(j, l, m) if x_ik == x_jl: ret[x_ik - 1][x_jl - 1] = F[i - 1][j - 1] * D[k - 1][k - 1] elif x_ik < x_jl: ret[x_ik - 1][x_jl - 1] = F[i - 1][j - 1] * D[k - 1][l - 1] return ret # F = np.array([[1,2,3],[0,1,5],[0,0,2]]) # D = np.array([[2,1,4],[0,3,2],[0,0,6]]) # computeQ(F,D) # initialise_flow_matrix(F,D)
def make_c(self): c = np.zeros(self.m) columns = int(math.ceil(self.m / (self.num_rows * 2))) for x in range(columns): for y in range(1,self.num_rows+1): c[idx.index_1_q_to_l_1(2*x+1,y,self.num_rows) - 1] = x c[idx.index_1_q_to_l_1(2*x+2,y,self.num_rows) - 1] = x return c
def generate_matrix_ct2(self): print("generating inequality constraint") coeff = np.zeros(shape=(self.k, self.n * self.k + self.num_ancillaries)) num_constraints = self.k twos = np.zeros(self.ancillary_bit_length) for i in range(self.ancillary_bit_length): twos[i] = math.pow(2, i) ancillary_startpos = self.n * self.k for k in range(1, num_constraints + 1): # fill out the coefficients # forall 1<=k<=num_groups, sum(xik) <= s for i in range(1, self.n + 1): xik_idx_linear = idx.index_1_q_to_l_1(i, k, self.k) coeff[idx.index_1_to_0(k)][idx.index_1_to_0( xik_idx_linear)] = 1 # fill out the twos complement coeff[idx.index_1_to_0(k), ancillary_startpos:ancillary_startpos + self.ancillary_bit_length] = twos ancillary_startpos += self.ancillary_bit_length s = math.floor(self.n / self.k) b = np.full(shape=num_constraints, fill_value=s) if self.initial_weight_estimate: weights = np.full(shape=num_constraints, fill_value=self.initial_weight) else: weights = np.full(shape=num_constraints, fill_value=self.inequality_weight) return coeff, b, weights
def solution_matrix(solution, n, m): solution_mtx = np.zeros((n, m), dtype=np.int8) for i in range(1, n + 1): for j in range(1, m + 1): index = idx.index_1_q_to_l_1(i, j, m) - 1 solution_mtx[i - 1][j - 1] = solution[index] return solution_mtx
def specialise_bunch( self, initial_solution, bunch_i_idx_map, locations_i_idx_map, ): bunch_size = len(bunch_i_idx_map.keys()) linear = np.zeros(bunch_size * bunch_size) members = bunch_i_idx_map.keys() locations = locations_i_idx_map.keys() for item_global_idx, item_local_idx in bunch_i_idx_map.items(): #i=item_global_idx for loc_global_idx, loc_local_idx in locations_i_idx_map.items(): k = loc_global_idx x_ik_local = idx.index_1_q_to_l_1( item_local_idx + 1, loc_local_idx + 1, bunch_size) - 1 #print(x_ik_local) #j=i #l=j for i in range(self.n): for j in range(self.m): if i in members or j in locations: continue if initial_solution[i][j]: linear[x_ik_local] += self.F[item_global_idx][ i] * self.D[k][j] return linear
def initialise_constraint_matrix(self, flow_matrix): # prepare A A = np.zeros((self.m*self.n,self.m*self.n),dtype=np.int32) for i in range(1,self.n+1): #ct1: each item in exactly one location # forall i from 1 to n, sum(xik) = 1 for k in range(1,self.m+1): x_ik = idx.index_1_q_to_l_1(i,k,self.m) A[i-1][x_ik-1] = 1 for k in range(1, self.m+1): #ct2: each location has exactly one item # forall k from 1 to m, sum(xik) = 1 for i in range(1,self.n+1): x_ik = idx.index_1_q_to_l_1(i,k,self.m) A[k+self.n - 1][x_ik-1] = 1 ''' np.set_printoptions(threshold=np.inf) print(A) np.set_printoptions(threshold=6) ''' # prepare b b = np.zeros(self.m*self.n) for i in range(self.num_constraints): b[i] = 1 # prepare weights if self.initial_weight_estimate: initial_weight = self.estimate_initial_weight(flow_matrix) weights = np.full(shape=self.num_constraints, fill_value=initial_weight) alphas = np.full(shape=self.num_constraints, fill_value=1.2) else: weights = np.full(shape=self.num_constraints, fill_value=self.weight0) alphas = np.full(shape=self.num_constraints,fill_value=self.alpha0) self.canonical_A = A.copy() self.canonical_b = b.copy() self.ms = weights self.alphas = alphas ret = super().A_to_Q(A,b,weights) # np.savetxt("constraint.txt",ret,fmt='%d') return ret
def initial(self): ret = {} for i in range(1, self.n + 1): loc = random.randint(1, self.m) for j in range(1, self.m + 1): index = idx.index_1_q_to_l_1(i, j, self.m) - 1 if j == loc: ret[index] = 1 else: ret[index] = 0 return (ret, 0)
def solution_mtx(self, solution): ''' param: solution: dict of (var,val) returns: np array of solution matrix ''' solution_mtx = np.zeros((self.n, self.k), dtype=np.int8) for i in range(1, self.n + 1): for k in range(1, self.k + 1): index = idx.index_1_q_to_l_1(i, k, self.k) - 1 solution_mtx[i - 1][k - 1] = solution[index] return solution_mtx
def initial(self): ''' returns a (dict,energy) tuple of initial state that assigns each item to a random bunch. ''' ret_dict = {} #randomly assign each item to some bunch for i in range(1, self.n + 1): grp = random.randint(1, self.k) for k in range(1, self.k + 1): index = idx.index_1_q_to_l_1(i, k, self.k) - 1 if grp == k: ret_dict[index] = 1 else: ret_dict[index] = 0 for i in range(self.n * self.k, self.n * self.k + self.num_ancillaries): ret_dict[i] = random.randint(0, 1) print("initial state dict has %d vars" % len(ret_dict.keys())) return (ret_dict, 0)
def generate_matrix_ct1(self): ''' ct1: forall 1<=i<=n, sum(x_ik) = 1 forall 1<=k<=num_groups Remarks: A (n by nk) linear constraint coefficients ''' print("generating equality constraint") A = np.zeros(shape=(self.n, self.n * self.k), dtype=np.float32) for i in range(1, self.n + 1): for k in range(1, self.k + 1): x_ik_index = idx.index_1_q_to_l_1(i, k, self.k) # forall 1<=i<=n, (a)i,xik = 1 forall k, where 1<=k<=num_groups A[idx.index_1_to_0(i)][idx.index_1_to_0(x_ik_index)] = 1 b = np.ones(shape=self.n) if self.initial_weight_estimate: weights = np.full(shape=self.n, fill_value=self.initial_weight) else: weights = np.full(shape=self.n, fill_value=self.euqality_weight) #print(A) return A, b, weights