def cost(Network, Tract_pop, Tractx, Tracty, Geox, Geoy, cnum): """Calculate the overall cost all a new solution: two type: demand-population, supply-transmission(transmission-demand) """ x = sf.FeatureScaling2(Network.demandx, np.min(Geox), np.max(Geox) - np.min(Geox)) y = sf.FeatureScaling2(Network.demandy, np.min(Geoy), np.max(Geoy) - np.min(Geoy)) Tract_pop1 = sf.FeatureScaling(Tract_pop) Tractx1 = sf.FeatureScaling(Tractx) Tracty1 = sf.FeatureScaling(Tracty) Sum_Cost = 0 Popu_assign2 = np.zeros(len(x)) for i in range(len(Tractx1)): Dist = np.empty(len(x), dtype=float) for k in range(len(x)): Dist[k] = math.sqrt((Tracty1[i] - y[k])**2 + (Tractx1[i] - x[k])**2) index = sf.minimumk(Dist, min(cnum, len(x))) ratio = sf.FeatureScaling3( np.sum(np.exp(Dist[index])) / np.exp(Dist[index])) for k in range(len(index)): Popu_assign2[index[k]] += Tract_pop1[i] * ratio[k] Sum_Cost += Popu_assign2[index[k]] * Dist[index[k]] Network.cost = Sum_Cost
def cost(sol, Geox, Geoy, PD, Type, Tractx, Tracty, PD_beforenormalize, cnum): """Calculate the overall cost of a new solution: two type: demand-population, supply-transmission(transmission-demand) Input: sol - new solution: location in the form of the index Geox - Geoy - PD - Type - Tractx - Tracty - Output: cost of the new solution and the population assignment for the demand nodes of the new solution """ Popu_assign, Popu_assign2 = np.zeros(len(sol)), np.zeros(len(sol)) Sum_Cost = 0 for i in range(len(Tractx)): Dist = np.empty(len(sol), dtype=float) for k in range(len(sol)): Dist[k] = math.sqrt((Tracty[i] - Geoy[sol[k][0]])**2 + (Tractx[i] - Geox[sol[k][1]])**2) index = sf.minimumk(Dist, min(cnum, len(sol))) ratio = sf.FeatureScaling3( np.sum(np.exp(Dist[index])) / np.exp(Dist[index])) for k in range(len(index)): Popu_assign[index[k]] += PD_beforenormalize[i] * ratio[k] Popu_assign2[index[k]] += PD[i] * ratio[k] if (Type == 'Population'): Sum_Cost += Popu_assign2[index[k]] * Dist[index[k]] else: Sum_Cost += Dist[index[k]] return Sum_Cost, Popu_assign
def Adjmatrix(self): """Define the adjacent matrix of this dependency Input: \ Output: 2D numpy array, the adjacent matrix of the dependency """ self.adjmatrix = np.zeros((self.nodenum1, self.nodenum2), dtype=int) for i in range(self.nodenum2): minindex = np.array( sf.minimumk(self.distmatrix[:, i], self.nearestnum)) self.adjmatrix[minindex, i] = 1
def Connect(self, m): """Connect the vertices generated in Function Nodeloc - generate the adjacent matrix Input: m - the most m cloest facilities to be connected Output: distmatrix - the distance matrix of the vertices adjmatrix - the adjacent matrix of the vertices """ self.distmatrix = np.zeros([len(self.loc), len(self.loc)]) for i in range(len(self.loc)): for j in range(len(self.loc)): self.distmatrix[i, j] = sf.dist(self.Geoloc[i, 0], self.Geoloc[i, 1], self.Geoloc[j, 0], self.Geoloc[j, 1]) #Calculate the adjacent matrix self.adjmatrix = np.zeros([len(self.loc), len(self.loc)]) for i in range(len(self.demandseries)): minindex = np.array(sf.minimumk(self.distmatrix[:self.demandseries[i], self.demandseries[i]], m)) self.adjmatrix[minindex, self.demandseries[i]] = 1
def connection(self, sampleseq, num): """Calculate the adjacent matrix between supply node and transmission node Input: the nodal neighborhood degree sequence, d: the number of adjacent nodes Output: the adjacent matrix between supply and transmission nodes """ self.Adjmatrix = np.zeros((self.nodenum, self.nodenum), dtype=int) for i in range(self.supplynum): minindex = np.array( sf.minimumk( self.Dismatrix[self.supplyseries[i], self.trandemandseries], sampleseq[self.supplyseries[i]])) self.Adjmatrix[self.supplyseries[i], self.trandemandseries[minindex]] = 1 # self.Adjmatrix[minindex, self.supplyseries[i]] = 1 for i in range(self.trannum): if (np.sum(self.Adjmatrix[self.supplyseries, self.transeries[i]]) == 0): minindex = np.array( sf.minimumk( self.Dismatrix[self.supplyseries, self.transeries[i]], num)) self.Adjmatrix[minindex, self.transeries[i]] = 1 # self.Adjmatrix[self.transeries[i], minindex] = 1 # for i in range(self.supplynum): # minindex = np.array(sf.minimumk(self.Dismatrix[self.supplyseries[i], self.supplyseries], num)) # self.Adjmatrix[self.supplyseries[i], minindex] = 1 # self.Adjmatrix[minindex, self.supplyseries[i]] = 1 # for i in range(self.trannum): # if(np.sum(self.Adjmatrix[self.supplyseries, self.transeries[i]]) != 0): # continue # minindex = np.array(sf.minimumk(self.Dismatrix[self.supplyseries, self.transeries[i]], num)) # self.Adjmatrix[minindex, self.transeries[i]] = 1 ## self.Adjmatrix[self.transeries[i], minindex] = 1 # for i in range(self.trannum): minindex = np.array( sf.minimumk( self.Dismatrix[self.transeries[i], self.demandseries], min(sampleseq[self.transeries[i]], self.demandnum))) + self.supplynum + self.trannum self.Adjmatrix[self.transeries[i], minindex] = 1 # self.Adjmatrix[minindex, self.transeries[i]] = 1 # for i in range(self.demandnum): # if(np.sum(self.Adjmatrix[self.transeries, self.demandseries[i]]) == 0): # minindex = np.array(sf.minimumk(self.Dismatrix[self.transeries, self.demandseries[i]], 1)) + self.supplynum # self.Adjmatrix[minindex, self.demandseries[i]] = 1 # for i in range(self.trannum): # minindex = np.array(sf.minimumk(self.Dismatrix[self.transeries[i], self.transeries], num)) + self.supplynum # self.Adjmatrix[self.transeries[i], minindex] = 1 for i in range(self.demandnum): if (np.sum(self.Adjmatrix[self.transeries, self.demandseries[i]]) == 0): minindex = np.array( sf.minimumk( self.Dismatrix[self.transeries, self.demandseries[i]], num)) + self.supplynum self.Adjmatrix[minindex, self.demandseries[i]] = 1 # self.Adjmatrix[self.demandseries[i], minindex] = 1 for i in range(self.demandnum): minindex = np.array( sf.minimumk( self.Dismatrix[self.demandseries[i], self.demandseries], min(sampleseq[self.demandseries[i]] + 1, self.demandnum))) + self.supplynum + self.trannum minindex = minindex[1:-1] for j in range(len(minindex)): if (self.Adjmatrix[self.demandseries[i], minindex[j]] == 1 or self.Adjmatrix[minindex[j], self.demandseries[i]] == 1): continue self.Adjmatrix[self.demandseries[i], minindex[j]] = 1