Skip to content

Instantly share code, notes, and snippets.

@ynyBonfennil
Last active November 15, 2018 05:13
Show Gist options
  • Save ynyBonfennil/94f951d982c01787f43fd72ca7706e35 to your computer and use it in GitHub Desktop.
Save ynyBonfennil/94f951d982c01787f43fd72ca7706e35 to your computer and use it in GitHub Desktop.
client = {
0: {"name": "Client 0", "demand": 120, "covered by": [10000, 10001, 10004]},
1: {"name": "Client 1", "demand": 100, "covered by": [10000]},
2: {"name": "Client 2", "demand": 80, "covered by": [10002, 10003, 10004]},
3: {"name": "Client 3", "demand": 140, "covered by": [10002, 10005]},
4: {"name": "Client 4", "demand": 170, "covered by": [10002, 10005]},
5: {"name": "Client 5", "demand": 100, "covered by": [10000, 10003]},
6: {"name": "Client 6", "demand": 120, "covered by": [10002, 10003, 10004]},
7: {"name": "Client 7", "demand": 90, "covered by": [10001, 10004]},
8: {"name": "Client 8", "demand": 50, "covered by": [10001, 10004, 10005]},
}
facility = {
10000: {"name": "Facility 0", "capacity": 200, "cover": [0, 1, 5]},
10001: {"name": "Facility 1", "capacity": 150, "cover": [0, 7, 8]},
10002: {"name": "Facility 2", "capacity": 240, "cover": [2, 3, 4, 6]},
10003: {"name": "Facility 3", "capacity": 210, "cover": [2, 5, 6]},
10004: {"name": "Facility 4", "capacity": 120, "cover": [0, 2, 6, 7, 8]},
10005: {"name": "Facility 5", "capacity": 200, "cover": [3, 4, 8]},
}
import matplotlib.pyplot as plt
import networkx as nx
fig = plt.figure(figsize=(6, 6))
ax = plt.subplot(111)
ax.set_title("CSCLP", fontsize=18)
# グラフの定義
G = nx.DiGraph()
# client ノードの登録
client_list = []
client_labels = {}
for i in client:
G.add_node(i)
client_list.append(i)
client_labels[i] = client[i]["demand"]
# facility ノードの登録
facility_list = []
facility_labels = {}
for j in facility:
G.add_node(j)
facility_list.append(j)
facility_labels[j] = facility[j]["capacity"]
# エッジの登録
for i in client:
for j in client[i]["covered by"]:
G.add_edge(i,j)
# 座標情報の生成
pos = nx.circular_layout(G)
# clientノードの描画
client_node_size = [ client[i]["demand"]*5 for i in client ]
nx.draw_networkx_nodes(G, pos, nodelist=client_list, node_color="r", alpha=0.6, node_size=client_node_size)
nx.draw_networkx_labels(G, pos, labels=client_labels)
# facilityノードの描画
facility_node_size = [ facility[j]["capacity"]* 5 for j in facility ]
nx.draw_networkx_nodes(G, pos, nodelist=facility_list, node_color="b", alpha=0.6, node_size=facility_node_size)
nx.draw_networkx_labels(G, pos, labels=facility_labels)
# エッジの描画
nx.draw_networkx_edges(G, pos, alpha=0.4, edge_color="C")
plt.axis("off")
plt.show()
from gurobipy import *
# モデルの構築
m = Model()
# 決定変数
x = {}
y = {}
# 決定変数の定義
for i in client:
for j in facility:
x[(i,j)] = m.addVar(lb=0, vtype=GRB.CONTINUOUS, name="x%d,%d" % (i,j))
y[j] = m.addVar(vtype=GRB.BINARY, name="x%d" % j)
m.update()
# 制約条件の定義
for i in client:
m.addConstr( quicksum(x[(i,j)] for j in client[i]["covered by"]) >= 1 )
for j in facility:
m.addConstr( quicksum(client[i]["demand"]*x[(i,j)] for i in client) - facility[j]["capacity"]*y[j] <= 0 )
# 目的関数の定義
m.setObjective( quicksum( y[j] for j in facility ), GRB.MINIMIZE )
# 計算の実行
m.optimize()
import matplotlib.pyplot as plt
import networkx as nx
fig = plt.figure(figsize=(6, 6))
ax = plt.subplot(111)
ax.set_title("CSCLP result", fontsize=18)
# グラフの定義
G = nx.DiGraph()
# client ノードの登録
client_list = []
client_labels = {}
for i in client:
G.add_node(i)
client_list.append(i)
client_labels[i] = client[i]["demand"]
# facility ノードの登録
facility_list = []
facility_labels = {}
for j in facility:
if y[j].x == 1:
G.add_node(j)
facility_list.append(j)
facility_labels[j] = facility[j]["capacity"]
# エッジを追加
for i in client:
for j in facility:
if x[(i,j)].x > 0:
G.add_edge(i, j, weight = x[(i,j)].x * client[i]["demand"] * 0.005)
# 座標情報の生成
pos = nx.circular_layout(G)
# clientノードの描画
client_node_size = [ client[i]["demand"]*5 for i in client ]
nx.draw_networkx_nodes(G, pos, nodelist=client_list, node_color="r", alpha=0.6, node_size=client_node_size)
nx.draw_networkx_labels(G, pos, labels=client_labels)
# facilityノードの描画
facility_node_size = [ facility[j]["capacity"]*5 for j in facility ]
nx.draw_networkx_nodes(G, pos, nodelist=facility_list, node_color="b", alpha=0.6, node_size=facility_node_size)
nx.draw_networkx_labels(G, pos, labels=facility_labels)
# エッジの描画
edge_width = [ d["weight"]*10 for (u,v,d) in G.edges(data=True) ]
nx.draw_networkx_edges(G, pos, alpha=0.4, edge_color="C", width=edge_width)
plt.axis("off")
plt.show()
client = {
0: {"name": "Client 0", "demand": 120, "covered by": [10000, 10001, 10004]},
1: {"name": "Client 1", "demand": 100, "covered by": [10000]},
2: {"name": "Client 2", "demand": 80, "covered by": [10002, 10003, 10004]},
3: {"name": "Client 3", "demand": 140, "covered by": [10002, 10005]},
4: {"name": "Client 4", "demand": 170, "covered by": [10002, 10005]},
5: {"name": "Client 5", "demand": 100, "covered by": [10000, 10003]},
6: {"name": "Client 6", "demand": 120, "covered by": [10002, 10003, 10004]},
7: {"name": "Client 7", "demand": 90, "covered by": [10001, 10004]},
8: {"name": "Client 8", "demand": 50, "covered by": [10001, 10004, 10005]},
}
facility = {
10000: {"name": "Facility 0", "capacity": 200, "cover": [0, 1, 5]},
10001: {"name": "Facility 1", "capacity": 150, "cover": [0, 7, 8]},
10002: {"name": "Facility 2", "capacity": 240, "cover": [2, 3, 4, 6]},
10003: {"name": "Facility 3", "capacity": 210, "cover": [2, 5, 6]},
10004: {"name": "Facility 4", "capacity": 120, "cover": [0, 2, 6, 7, 8]},
10005: {"name": "Facility 5", "capacity": 200, "cover": [3, 4, 8]},
}
p = 3
from gurobipy import *
# モデルの構築
m = Model()
# 決定変数
x = {}
y = {}
u = {}
# 決定変数の定義
for i in client:
for j in facility:
x[(i,j)] = m.addVar(lb=0, vtype=GRB.CONTINUOUS, name="x%d,%d" % (i,j))
y[j] = m.addVar(vtype=GRB.BINARY, name="x%d" % j)
u[i] = m.addVar(lb=0, vtype=GRB.CONTINUOUS, name="u%d" % i)
m.update()
# 制約条件の定義
for i in client:
m.addConstr( quicksum(x[(i,j)] for j in client[i]["covered by"]) + u[i] == 1 )
for j in facility:
m.addConstr( quicksum(client[i]["demand"]*x[(i,j)] for i in facility[j]["cover"]) - facility[j]["capacity"]*y[j] <= 0 )
m.addConstr( quicksum(y[j] for j in facility) == p )
# 目的関数の定義
m.setObjective( quicksum( client[i]["demand"]*u[i] for i in client ), GRB.MINIMIZE )
# 計算の実行
m.optimize()
import matplotlib.pyplot as plt
import networkx as nx
fig = plt.figure(figsize=(6, 6))
ax = plt.subplot(111)
ax.set_title("CMCLP result", fontsize=18)
# グラフの定義
G = nx.DiGraph()
# client ノードの登録
client_list = []
client_labels = {}
for i in client:
G.add_node(i)
client_list.append(i)
client_labels[i] = client[i]["demand"]
# facility ノードの登録
facility_list = []
facility_labels = {}
for j in facility:
if y[j].x == 1:
G.add_node(j)
facility_list.append(j)
facility_labels[j] = facility[j]["capacity"]
# エッジを追加
for i in client:
for j in facility:
if x[(i,j)].x > 0:
G.add_edge(i, j, weight = x[(i,j)].x * client[i]["demand"] * 0.005)
# 座標情報の生成
pos = nx.circular_layout(G)
# clientノードの描画
client_node_size = [ client[i]["demand"]*5 for i in client ]
nx.draw_networkx_nodes(G, pos, nodelist=client_list, node_color="r", alpha=0.6, node_size=client_node_size)
nx.draw_networkx_labels(G, pos, labels=client_labels)
# facilityノードの描画
facility_node_size = [ facility[j]["capacity"]*5 for j in facility ]
nx.draw_networkx_nodes(G, pos, nodelist=facility_list, node_color="b", alpha=0.6, node_size=facility_node_size)
nx.draw_networkx_labels(G, pos, labels=facility_labels)
# エッジの描画
edge_width = [ d["weight"]*10 for (u,v,d) in G.edges(data=True) ]
nx.draw_networkx_edges(G, pos, alpha=0.4, edge_color="C", width=edge_width)
plt.axis("off")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment