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_cal(self, Type, Tract_pop, Tractx, Tracty): """Calculate the normalized demand-population cost """ Geox1 = sf.FeatureScaling(self.Geox) Geoy1 = sf.FeatureScaling(self.Geoy) Tract_pop1 = sf.FeatureScaling(Tract_pop) Tractx1 = sf.FeatureScaling(Tractx) Tracty1 = sf.FeatureScaling(Tracty) self.cost = ans.cost(self.loc[self.supplynum:, :], Geox1, Geoy1, Tract_pop1, Type, Tractx1, Tracty1)
def cost_cal(self, Type, Tract_pop, Tractx, Tracty, cnum): """Calculate the normalized demand-population cost """ Geox1 = sf.FeatureScaling(self.Geox) Geoy1 = sf.FeatureScaling(self.Geoy) Tract_pop1 = sf.FeatureScaling(Tract_pop) Tractx1 = sf.FeatureScaling(Tractx) Tracty1 = sf.FeatureScaling(Tracty) self.cost, temp = ans.cost(self.demandloc, Geox1, Geoy1, Tract_pop1, Type, Tractx1, Tracty1, Tract_pop, cnum)
def cost_cal(self, Type, Tract_pop, Tractx, Tracty): """Calculate the normalized demand-population cost """ Geox1 = sf.FeatureScaling(self.Geox) Geoy1 = sf.FeatureScaling(self.Geoy) Tract_pop1 = sf.FeatureScaling(Tract_pop) Tractx1 = sf.FeatureScaling(Tractx) Tracty1 = sf.FeatureScaling(Tracty) self.cost = ans.cost(np.array(self.nodelocindex), Geox1, Geoy1, Tract_pop1, Type, Tractx1, Tracty1, Tract_pop, dt.cnum)[0]
def distmatrix(self): """Set up the distance matrix of the network """ import numpy as np self.nodexnormal, self.nodeynormal = sf.FeatureScaling( self.nodex), sf.FeatureScaling(self.nodey) self.dmatrix = np.empty((self.nodenum, self.nodenum), dtype=float) self.dnormalmatrix = np.empty((self.nodenum, self.nodenum), dtype=float) for i in range(self.nodenum): for j in range(i, self.nodenum): self.dmatrix[i, j] = sf.dist(self.nodey[i], self.nodex[i], self.nodey[j], self.nodex[j]) self.dmatrix[j, i] = self.dmatrix[i, j] self.dnormalmatrix[i, j] = sf.dist(self.nodeynormal[i], self.nodexnormal[i], self.nodeynormal[j], self.nodexnormal[j]) self.dnormalmatrix[j, i] = self.dnormalmatrix[i, j]
def Nodelocation(self, Tract_pop, Tractx, Tracty, longitude, latitude, cnum): """Annealing simulation to decide the node location """ import annealsimulation self.latl, self.lonl = [], [] while (len(self.latl) != self.nodenum): lat = np.random.randint(len(self.Geoy) - 1) lon = np.random.randint(len(self.Geox) - 1) if (lat not in self.latl or lon not in self.lonl): self.latl.append(lat) self.lonl.append(lon) self.latl, self.lonl = np.array(self.latl), np.array(self.lonl) self.demandlat, self.demandlon = self.latl[ self.demandseries], self.lonl[self.demandseries] self.tranlat, self.tranlon = self.latl[self.transeries], self.lonl[ self.transeries] self.supplylat, self.supplylon = self.latl[ self.supplyseries], self.lonl[self.supplyseries] self.demandloc = np.stack((self.demandlat, self.demandlon)).transpose() self.tranloc = np.stack((self.tranlat, self.tranlon)).transpose() self.supplyloc = np.stack((self.supplylat, self.supplylon)).transpose() #Demand node Geox1 = sf.FeatureScaling(self.Geox) Geoy1 = sf.FeatureScaling(self.Geoy) Tract_pop1 = sf.FeatureScaling(Tract_pop) Tractx1 = sf.FeatureScaling(Tractx) Tracty1 = sf.FeatureScaling(Tracty) self.demandloc, self.demandc, self.popuassign = ans.anneal2( self.demandloc, 'Population', Geox1, Geoy1, Tract_pop1, Tractx1, Tracty1, Tract_pop, cnum) self.demandy1 = Geoy1[self.demandloc[:, 0]] self.demandx1 = Geox1[self.demandloc[:, 1]] self.demandy = self.Geoy[self.demandloc[:, 0]] self.demandx = self.Geox[self.demandloc[:, 1]] #Transmission node self.tranloc, self.tranc, temp = ans.anneal2(self.tranloc, 'Facility', Geox1, Geoy1, Tract_pop1, self.demandx1, self.demandy1, Tract_pop, cnum) self.trany1 = Geoy1[self.tranloc[:, 0]] self.tranx1 = Geox1[self.tranloc[:, 1]] self.trany = self.Geoy[self.tranloc[:, 0]] self.tranx = self.Geox[self.tranloc[:, 1]] #Supply node self.supplyloc, self.supplyc, temp = ans.anneal2( self.supplyloc, 'Facility', Geox1, Geoy1, Tract_pop1, self.tranx1, self.trany1, Tract_pop, cnum) self.supplyy1 = Geoy1[self.supplyloc[:, 0]] self.supplyx1 = Geox1[self.supplyloc[:, 1]] self.supplyy = self.Geoy[self.supplyloc[:, 0]] self.supplyx = self.Geox[self.supplyloc[:, 1]] ##Coordinates of nodes self.y = np.concatenate((self.supplyy, self.trany, self.demandy)) self.x = np.concatenate((self.supplyx, self.tranx, self.demandx)) ##Latitudes and longitudes of nodes self.demandlatitude, self.demandlongitude = latitude[ self.demandloc[:, 0]], longitude[self.demandloc[:, 1]] self.tranlatitude, self.tranlongitude = latitude[ self.tranloc[:, 0]], longitude[self.tranloc[:, 1]] self.supplylatitude, self.supplylongitude = latitude[ self.supplyloc[:, 0]], longitude[self.supplyloc[:, 1]] self.latitude = np.concatenate( (self.supplylatitude, self.tranlatitude, self.demandlatitude)) self.longitude = np.concatenate( (self.supplylongitude, self.tranlongitude, self.demandlongitude))