第六章

6.1

import pandas as pd
import cvxpy as cp
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False   
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
nodes_labels = ['v' + str(i) for i in range(1, 7)]
nodes_labels = dict(zip(range(1,7), nodes_labels))

L1 = [(1,2), (1,3), (1,4), (2,3), (2,6), (3,4), (4,5), (5,6)]
G1 = nx.Graph()
G1.add_edges_from(L1)

L2 = [(1,2,7), (1,3,3), (1,4,12), (2,3,1), (2,6,1), (3,4,8), (4,5,9), (5,6,3)]
G2 = nx.Graph()
G2.add_weighted_edges_from(L2)

L3 = [(2,1,7), (1,3,3), (4,1,12), (2,3,1), (6,2,1), (3,4,8), (5,4,9), (5,6,3)]
G3 = nx.DiGraph()
G3.add_weighted_edges_from(L3)
fig = plt.figure(dpi=400, figsize=(9,3))

ax = fig.add_subplot(131)
pos1 = nx.planar_layout(G1)
nx.draw(G1, pos1, labels=nodes_labels, with_labels='True', font_color='w', node_size=100, font_size=8)

ax1 = fig.add_subplot(132)
pos2 = nx.planar_layout(G2)
nx.draw(G2, pos2, labels=nodes_labels, with_labels='True', font_color='w', node_size=100, font_size=8)
edgelabels = nx.get_edge_attributes(G2, 'weight')
nx.draw_networkx_edge_labels(G2, pos2, edge_labels=edgelabels, font_size=6)

ax2 = fig.add_subplot(133)
pos3 = nx.planar_layout(G3)
nx.draw(G3, pos3, labels=nodes_labels, with_labels='True', font_color='w', node_size=100, font_size=8)
edgelabels = nx.get_edge_attributes(G3, 'weight')
nx.draw_networkx_edge_labels(G3, pos3, edge_labels=edgelabels, font_size=6)

fig.show()
print(“2022310143024”)

6.3

import pandas as pd
import cvxpy as cp
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False   
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
L=[(1,2,20),(1,5,15),(2,3,20),(2,4,60),(2,5,25),
   (3,4,30),(3,5,18),(4,5,35),(4,6,10),(5,6,15)]
G = nx.Graph()
G.add_weighted_edges_from(L)
T = nx.minimum_spanning_tree(G)
w = nx.get_edge_attributes(T, 'weight')
print("最小生成树的长度为:", sum(w.values()))
nodes_labels = ['v' + str(i) for i in range(1, 7)]
nodes_labels = dict(zip(range(1,7), nodes_labels))
pos = nx.spring_layout(T)
nx.draw(T, pos, with_labels=True, labels=nodes_labels, font_color='w')
nx.draw_networkx_edge_labels(T, pos, edge_labels=w)
plt.show()
print(“2022310143024”)

6.4

import pandas as pd
import cvxpy as cp
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False   
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
price = np.array([2.5, 2.6, 2.8, 3.1])
sell = np.array([2.0, 1.6, 1.3, 1.1])
cost = np.array([0.3, 0.8, 1.5, 2.0])
cumsum_cost = np.cumsum(cost)
year_num = 4
W = np.zeros((year_num+1, year_num+1))
for i in range(year_num+1):
    W[i,i] = 0
for i in range(year_num+1):
    for j in range(i+1, year_num+1):
        W[i, j] = price[i] + cumsum_cost[j-i-1] - sell[j-i-1]
W[np.isinf(W)] = 0
G = nx.DiGraph(W)
path = nx.shortest_path(G, 0, year_num, weight='weight') 
dis = nx.shortest_path_length(G, 0, year_num, weight='weight')
print("应当在以下年份购置新设备", np.array(path[:-1]) + 1)
print("最少费用", round(dis,1))
#2022310143024

 

posted @ 2024-11-18 16:52  殷昊  阅读(8)  评论(0)    收藏  举报