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
| import math import numpy as np import matplotlib.pyplot as plt import networkx as nx
R = 6371
def deg2rad(deg): return deg * (math.pi / 180)
def haversine_distance(lat1, lon1, lat2, lon2): dlat = deg2rad(lat2 - lat1) dlon = deg2rad(lon2 - lon1) a = math.sin(dlat / 2)**2 + math.cos(deg2rad(lat1)) * math.cos(deg2rad(lat2)) * math.sin(dlon / 2)**2 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) distance = R * c return distance
nodes = [ {'id': 1, 'lat': 36.121441, 'lon': 103.600377, 'price': 10.70}, {'id': 2, 'lat': 36.127271, 'lon': 103.707886, 'price': 11.00}, {'id': 3, 'lat': 36.081781, 'lon': 103.715648, 'price': 10.90}, {'id': 4, 'lat': 36.071980, 'lon': 103.791537, 'price': 11.20}, {'id': 5, 'lat': 36.050506, 'lon': 103.928079, 'price': 11.05}, ]
edges = [ {'from': 1, 'to': 2, 'rate': 0.10}, {'from': 2, 'to': 3, 'rate': 0.085}, {'from': 3, 'to': 4, 'rate': 0.09}, {'from': 4, 'to': 5, 'rate': 0.11}, ]
G = nx.Graph() for node in nodes: G.add_node(node['id'], lat=node['lat'], lon=node['lon'], price=node['price'])
for edge in edges: from_node = next(node for node in nodes if node['id'] == edge['from']) to_node = next(node for node in nodes if node['id'] == edge['to']) distance = haversine_distance( from_node['lat'], from_node['lon'], to_node['lat'], to_node['lon'] ) cost = distance * edge['rate'] G.add_edge(edge['from'], edge['to'], weight=cost)
lat_min, lat_max = 36.00, 36.15 lon_min, lon_max = 103.50, 104.00
lat_grid, lon_grid = np.meshgrid( np.arange(lat_min, lat_max, 0.005), np.arange(lon_min, lon_max, 0.005) )
terminal_prices = np.zeros(lat_grid.shape)
for i in range(lat_grid.shape[0]): for j in range(lat_grid.shape[1]): lat = lat_grid[i, j] lon = lon_grid[i, j] distance_to_node1 = haversine_distance(lat, lon, nodes[0]['lat'], nodes[0]['lon']) transport_rate = 0.10 transport_cost = distance_to_node1 * transport_rate terminal_price = nodes[0]['price'] + transport_cost terminal_prices[i, j] = terminal_price
plt.figure(figsize=(10, 8)) cs = plt.contour(lon_grid, lat_grid, terminal_prices, levels=15, cmap='viridis') plt.clabel(cs, inline=True, fontsize=8)
for node in nodes: plt.plot(node['lon'], node['lat'], 'ro') plt.text(node['lon'], node['lat'], f"节点{node['id']}", fontsize=9, ha='right') plt.title('Terminal price equipotential line') plt.xlabel('longitude') plt.ylabel('latitude') plt.colorbar(label='Terminal Price (RMB)') plt.show()
|