-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.h
158 lines (144 loc) · 5.15 KB
/
Graph.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef _GRAPH_H
#define _GRAPH_H
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include "tools.h"
// 添加一条从 u 到 v 的边,长度为 w
void add_edge(std::unordered_map<int, std::vector<std::pair<int, int>>>& graph, int u, int v, int w) {
graph[u].push_back(std::make_pair(v, w));
}
// 从 u 到 v 移除一条边
void remove_edge(std::unordered_map<int, std::vector<std::pair<int, int>>>& graph, int u, int v) {
if (graph.find(u) != graph.end()) {
graph[u].erase(std::remove_if(graph[u].begin(), graph[u].end(), [v](const std::pair<int, int>& p) {
return p.first == v;
}), graph[u].end());
}
}
// 查找路径
std::vector<int> find_path(const std::vector<int> & pre, int u, int v) {
std::vector<int> path;
int current = v;
while (current != -1) {
path.push_back(current);
current = pre[current];
}
std::reverse(path.begin(), path.end());
return path;
}
// 获取距离
std::pair<int, std::vector<int>> get_distance(std::unordered_map<int, std::vector<std::pair<int, int>>>& graph, int s, int t, int JOBS, bool get_path = false) {
int N = JOBS * 5 + 1;
std::vector<int> ind(N+1, 0);
std::queue<int> q;
for (const auto& pair : graph) {
for (const auto& edge : pair.second) {
ind[edge.first]++;
}
}
for (const auto& pair : graph) {
if (ind[pair.first] == 0) {
q.push(pair.first);
}
}
std::vector<int> dp;
std::vector<int> pre;
for (int node = 0; node <= N; ++node) {
dp.push_back(INT_MIN);
pre.push_back(-1);
}
dp[s] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (const auto & edge : graph[x]) {
int v = edge.first;
int w = edge.second;
ind[v]--;
if (ind[v] == 0) {
q.push(v);
}
if (dp[x] + w > dp[v]) {
pre[v] = x;
dp[v] = dp[x] + w;
}
}
}
std::vector<int> path;
if(get_path) {
path = find_path(pre, s, t);
}
return std::make_pair(dp[t], path);
}
// 初始化图
Graph init_graph(const Individual & ind, int factory, const Dset_info & dset_data, const std::vector<cand_info> & cand_data) {
int MACHINES = dset_data.MACHINES;
int JOBS = dset_data.JOBS;
int N = JOBS * 5 + 1;
Graph graph(N, MACHINES);
for (int job = 0; job < JOBS; ++job) {
if (ind.factories[job] != factory) {
continue;
}
int first_node = get_node(job, 0);
int machine = ind.machines[first_node-1];
int first_time = get_time(first_node, factory, machine, cand_data);
add_edge(graph.graph, 0, first_node, first_time);
int last_node = get_node(job, 4);
add_edge(graph.graph, last_node, N, 0);
for (int operation = 0; operation < 5; ++operation) {
int node = get_node(job, operation);
int machine = ind.machines[node-1];
int time = get_time(node, factory, machine, cand_data);
if (operation == 0) {
graph.PJ[node] = 0;
graph.SJ[node] = node + 1;
} else if (operation == 4) {
graph.PJ[node] = node - 1;
graph.SJ[node] = N;
add_edge(graph.graph, node-1, node, time);
} else {
graph.PJ[node] = node - 1;
graph.SJ[node] = node + 1;
add_edge(graph.graph, node-1, node, time);
}
}
}
std::vector<int> endtime_job(JOBS, 0);
std::vector<int> endtime_machine(MACHINES, 0);
std::vector<int> last_node_machine(MACHINES, -1);
std::vector<int> operation(JOBS, -1);
for (int index = 0; index < ind.schedules.size(); index++) {
int job = ind.schedules[index];
if (ind.factories[job] != factory) {
continue;
}
operation[job]++;
int node = get_node(job, operation[job]);
int machine = ind.machines[node-1];
graph.all_operations_of_machine[machine].push_back(index);
int time = get_time(node, factory, machine, cand_data);
graph.PM[node] = last_node_machine[machine];
if(last_node_machine[machine] != -1) {
graph.SM[last_node_machine[machine]] = node;
}
last_node_machine[machine] = node;
int start_time = std::max(endtime_job[job], endtime_machine[machine]);
endtime_machine[machine] = endtime_job[job] = start_time + time;
graph.start_time_nodes[node] = start_time;
graph.end_time_nodes[node] = endtime_job[job];
}
for(int node=0; node<N; node++) {
if(graph.PM[node] == -1) continue;
int machine = ind.machines[node-1];
int time = get_time(node, factory, machine, cand_data);
add_edge(graph.graph, graph.PM[node], node, time);
}
// 添加N,结束点到图中,解决一些问题
graph.graph[N] = {};
return graph;
}
#endif